Date: 2022-10-01T06:39:10+00:00

Actual source code: test5.c
slepc-3.18.0 2022-10-01
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-, 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 rational function.\n\n";

     13: #include <slepcfn.h>

     15: /*
     16:    Compute matrix rational function B = q(A)\p(A)
     17:  */
     18: PetscErrorCode TestMatRational(FN fn,Mat A,PetscViewer viewer,PetscBool verbose,PetscBool inplace)
     19: {
     20:   PetscBool      set,flg;
     21:   PetscInt       n;
     22:   Mat            F,Acopy;
     23:   Vec            v,f0;
     24:   PetscReal      nrm;

     27:   MatGetSize(A,&n,NULL);
     28:   MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&F);
     29:   PetscObjectSetName((PetscObject)F,"F");
     30:   /* compute matrix function */
     31:   if (inplace) {
     32:     MatCopy(A,F,SAME_NONZERO_PATTERN);
     33:     MatIsHermitianKnown(A,&set,&flg);
     34:     if (set && flg) MatSetOption(F,MAT_HERMITIAN,PETSC_TRUE);
     35:     FNEvaluateFunctionMat(fn,F,NULL);
     36:   } else {
     37:     MatDuplicate(A,MAT_COPY_VALUES,&Acopy);
     38:     FNEvaluateFunctionMat(fn,A,F);
     39:     /* check that A has not been modified */
     40:     MatAXPY(Acopy,-1.0,A,SAME_NONZERO_PATTERN);
     41:     MatNorm(Acopy,NORM_1,&nrm);
     42:     if (nrm>100*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"Warning: the input matrix has changed by %g\n",(double)nrm);
     43:     MatDestroy(&Acopy);
     44:   }
     45:   if (verbose) {
     46:     PetscPrintf(PETSC_COMM_WORLD,"Matrix A - - - - - - - -\n");
     47:     MatView(A,viewer);
     48:     PetscPrintf(PETSC_COMM_WORLD,"Computed f(A) - - - - - - -\n");
     49:     MatView(F,viewer);
     50:   }
     51:   /* print matrix norm for checking */
     52:   MatNorm(F,NORM_1,&nrm);
     53:   PetscPrintf(PETSC_COMM_WORLD,"The 1-norm of f(A) is %g\n",(double)nrm);
     54:   /* check FNEvaluateFunctionMatVec() */
     55:   MatCreateVecs(A,&v,&f0);
     56:   MatGetColumnVector(F,f0,0);
     57:   FNEvaluateFunctionMatVec(fn,A,v);
     58:   VecAXPY(v,-1.0,f0);
     59:   VecNorm(v,NORM_2,&nrm);
     60:   if (nrm>100*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"Warning: the norm of f(A)*e_1-v is %g\n",(double)nrm);
     61:   MatDestroy(&F);
     62:   VecDestroy(&v);
     63:   VecDestroy(&f0);
     64:   return 0;
     65: }

     67: int main(int argc,char **argv)
     68: {
     69:   FN             fn;
     70:   Mat            A=NULL;
     71:   PetscInt       i,j,n=10,np,nq;
     72:   PetscScalar    *As,p[10],q[10];
     73:   PetscViewer    viewer;
     74:   PetscBool      verbose,inplace,matcuda;

     77:   SlepcInitialize(&argc,&argv,(char*)0,help);
     78:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     79:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
     80:   PetscOptionsHasName(NULL,NULL,"-inplace",&inplace);
     81:   PetscOptionsHasName(NULL,NULL,"-matcuda",&matcuda);
     82:   PetscPrintf(PETSC_COMM_WORLD,"Matrix rational function, n=%" PetscInt_FMT ".\n",n);

     84:   /* Create rational function r(x)=p(x)/q(x) */
     85:   FNCreate(PETSC_COMM_WORLD,&fn);
     86:   FNSetType(fn,FNRATIONAL);
     87:   np = 2; nq = 3;
     88:   p[0] = -3.1; p[1] = 1.1;
     89:   q[0] = 1.0; q[1] = -2.0; q[2] = 3.5;
     90:   FNRationalSetNumerator(fn,np,p);
     91:   FNRationalSetDenominator(fn,nq,q);
     92:   FNSetFromOptions(fn);

     94:   /* Set up viewer */
     95:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
     96:   FNView(fn,viewer);
     97:   if (verbose) PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);

     99:   /* Create matrices */
    100:   if (matcuda) {
    101: #if defined(PETSC_HAVE_CUDA)
    102:     MatCreateSeqDenseCUDA(PETSC_COMM_SELF,n,n,NULL,&A);
    103: #endif
    104:   } else MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&A);
    105:   PetscObjectSetName((PetscObject)A,"A");

    107:   /* Fill A with a symmetric Toeplitz matrix */
    108:   MatDenseGetArray(A,&As);
    109:   for (i=0;i<n;i++) As[i+i*n]=2.0;
    110:   for (j=1;j<3;j++) {
    111:     for (i=0;i<n-j;i++) { As[i+(i+j)*n]=1.0; As[(i+j)+i*n]=1.0; }
    112:   }
    113:   MatDenseRestoreArray(A,&As);
    114:   MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE);
    115:   TestMatRational(fn,A,viewer,verbose,inplace);

    117:   /* Repeat with same matrix as non-symmetric */
    118:   MatSetOption(A,MAT_HERMITIAN,PETSC_FALSE);
    119:   TestMatRational(fn,A,viewer,verbose,inplace);

    121:   MatDestroy(&A);
    122:   FNDestroy(&fn);
    123:   SlepcFinalize();
    124:   return 0;
    125: }

    127: /*TEST

    129:    testset:
    130:       output_file: output/test5_1.out
    131:       requires: !single
    132:       test:
    133:          suffix: 1
    134:       test:
    135:          suffix: 1_cuda
    136:          args: -matcuda
    137:          requires: cuda
    138:       test:
    139:          suffix: 2
    140:          args: -inplace
    141:       test:
    142:          suffix: 2_cuda
    143:          args: -inplace -matcuda
    144:          requires: cuda

    146: TEST*/