Date: 2021-09-20T09:43:38+00:00

Actual source code: test13.c
slepc-3.15.2 2021-09-20
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2021, Universitat Politecnica de Valencia, Spain

      6:    This file is part of SLEPc.
      7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
      8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      9: */

     11: static char help[] = "Test matrix logarithm.\n\n";

     13: #include <slepcfn.h>

     15: /*
     16:    Compute matrix logarithm B = logm(A)
     17:  */
     18: PetscErrorCode TestMatLog(FN fn,Mat A,PetscViewer viewer,PetscBool verbose,PetscBool inplace)
     19: {
     21:   PetscBool      set,flg;
     22:   PetscScalar    tau,eta;
     23:   PetscInt       n;
     24:   Mat            F,R;
     25:   Vec            v,f0;
     26:   FN             fnexp;
     27:   PetscReal      nrm;

     30:   MatGetSize(A,&n,NULL);
     31:   MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&F);
     32:   PetscObjectSetName((PetscObject)F,"F");
     33:   MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&R);
     34:   PetscObjectSetName((PetscObject)R,"R");
     35:   FNGetScale(fn,&tau,&eta);
     36:   /* compute matrix logarithm */
     37:   if (inplace) {
     38:     MatCopy(A,F,SAME_NONZERO_PATTERN);
     39:     MatIsHermitianKnown(A,&set,&flg);
     40:     if (set && flg) { MatSetOption(F,MAT_HERMITIAN,PETSC_TRUE); }
     41:     FNEvaluateFunctionMat(fn,F,NULL);
     42:   } else {
     43:     FNEvaluateFunctionMat(fn,A,F);
     44:   }
     45:   if (verbose) {
     46:     PetscPrintf(PETSC_COMM_WORLD,"Matrix A - - - - - - - -\n");
     47:     MatView(A,viewer);
     48:     PetscPrintf(PETSC_COMM_WORLD,"Computed logm(A) - - - - - - -\n");
     49:     MatView(F,viewer);
     50:   }
     51:   /* check error ||expm(F)-A||_F */
     52:   FNCreate(PETSC_COMM_WORLD,&fnexp);
     53:   FNSetType(fnexp,FNEXP);
     54:   MatCopy(F,R,SAME_NONZERO_PATTERN);
     55:   if (eta!=1.0) {
     56:     MatScale(R,1.0/eta);
     57:   }
     58:   FNEvaluateFunctionMat(fnexp,R,NULL);
     59:   FNDestroy(&fnexp);
     60:   MatAXPY(R,-tau,A,SAME_NONZERO_PATTERN);
     61:   MatNorm(R,NORM_FROBENIUS,&nrm);
     62:   if (nrm<100*PETSC_MACHINE_EPSILON) {
     63:     PetscPrintf(PETSC_COMM_WORLD,"||expm(F)-A||_F < 100*eps\n");
     64:   } else {
     65:     PetscPrintf(PETSC_COMM_WORLD,"||expm(F)-A||_F = %g\n",(double)nrm);
     66:   }
     67:   /* check FNEvaluateFunctionMatVec() */
     68:   MatCreateVecs(A,&v,&f0);
     69:   MatGetColumnVector(F,f0,0);
     70:   FNEvaluateFunctionMatVec(fn,A,v);
     71:   VecAXPY(v,-1.0,f0);
     72:   VecNorm(v,NORM_2,&nrm);
     73:   if (nrm>100*PETSC_MACHINE_EPSILON) {
     74:     PetscPrintf(PETSC_COMM_WORLD,"Warning: the norm of f(A)*e_1-v is %g\n",(double)nrm);
     75:   }
     76:   MatDestroy(&F);
     77:   MatDestroy(&R);
     78:   VecDestroy(&v);
     79:   VecDestroy(&f0);
     80:   return(0);
     81: }

     83: int main(int argc,char **argv)
     84: {
     86:   FN             fn;
     87:   Mat            A;
     88:   PetscInt       i,j,n=10;
     89:   PetscScalar    *As;
     90:   PetscViewer    viewer;
     91:   PetscBool      verbose,inplace,random,triang;

     93:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     94:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     95:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
     96:   PetscOptionsHasName(NULL,NULL,"-inplace",&inplace);
     97:   PetscOptionsHasName(NULL,NULL,"-random",&random);
     98:   PetscOptionsHasName(NULL,NULL,"-triang",&triang);
     99:   PetscPrintf(PETSC_COMM_WORLD,"Matrix logarithm, n=%D.\n",n);

    101:   /* Create logarithm function object */
    102:   FNCreate(PETSC_COMM_WORLD,&fn);
    103:   FNSetType(fn,FNLOG);
    104:   FNSetFromOptions(fn);

    106:   /* Set up viewer */
    107:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
    108:   FNView(fn,viewer);
    109:   if (verbose) {
    110:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
    111:   }

    113:   /* Create matrices */
    114:   MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&A);
    115:   PetscObjectSetName((PetscObject)A,"A");

    117:   if (random) {
    118:     MatSetRandom(A,NULL);
    119:   } else {
    120:     /* Fill A with a non-symmetric Toeplitz matrix */
    121:     MatDenseGetArray(A,&As);
    122:     for (i=0;i<n;i++) As[i+i*n]=2.0;
    123:     for (j=1;j<3;j++) {
    124:       for (i=0;i<n-j;i++) {
    125:         As[i+(i+j)*n]=1.0;
    126:         if (!triang) As[(i+j)+i*n]=-1.0;
    127:       }
    128:     }
    129:     As[(n-1)*n] = -5.0;
    130:     As[0] = 2.01;
    131:     MatDenseRestoreArray(A,&As);
    132:   }
    133:   TestMatLog(fn,A,viewer,verbose,inplace);

    135:   MatDestroy(&A);
    136:   FNDestroy(&fn);
    137:   SlepcFinalize();
    138:   return ierr;
    139: }

    141: /*TEST

    143:    testset:
    144:       filter: grep -v "computing matrix functions"
    145:       output_file: output/test13_1.out
    146:       test:
    147:          suffix: 1
    148:          args: -fn_scale .04,2 -n 75
    149:          requires: c99_complex !__float128
    150:       test:
    151:          suffix: 1_triang
    152:          args: -fn_scale .04,2 -n 75 -triang
    153:          requires: c99_complex !__float128
    154:       test:
    155:          suffix: 1_random
    156:          args: -fn_scale .02,2 -n 75 -random
    157:          requires: complex
    158:          filter_output: sed -e 's/04/02/'

    160: TEST*/