Actual source code: test12.c
slepc-3.18.1 2022-11-02
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 function evaluation via diagonalization.\n\n";
13: #include <slepcfn.h>
15: int main(int argc,char **argv)
16: {
17: FN fn;
18: Mat A,F,G;
19: PetscInt i,j,n=10;
20: PetscReal nrm;
21: PetscScalar *As,alpha,beta;
22: PetscViewer viewer;
23: PetscBool verbose;
26: SlepcInitialize(&argc,&argv,(char*)0,help);
27: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
28: PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
29: PetscPrintf(PETSC_COMM_WORLD,"Matrix function of symmetric/Hermitian matrix, n=%" PetscInt_FMT ".\n",n);
31: /* Create function object */
32: FNCreate(PETSC_COMM_WORLD,&fn);
33: FNSetType(fn,FNEXP); /* default to exponential */
34: #if defined(PETSC_USE_COMPLEX)
35: alpha = PetscCMPLX(0.3,0.8);
36: beta = PetscCMPLX(1.1,-0.1);
37: #else
38: alpha = 0.3;
39: beta = 1.1;
40: #endif
41: FNSetScale(fn,alpha,beta);
42: FNSetFromOptions(fn);
44: /* Set up viewer */
45: PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
46: if (verbose) PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
48: /* Create a symmetric/Hermitian Toeplitz matrix */
49: MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&A);
50: PetscObjectSetName((PetscObject)A,"A");
51: MatDenseGetArray(A,&As);
52: for (i=0;i<n;i++) As[i+i*n]=2.0;
53: for (j=1;j<3;j++) {
54: for (i=0;i<n-j;i++) {
55: #if defined(PETSC_USE_COMPLEX)
56: As[i+(i+j)*n]=PetscCMPLX(1.0,0.1); As[(i+j)+i*n]=PetscCMPLX(1.0,-0.1);
57: #else
58: As[i+(i+j)*n]=0.5; As[(i+j)+i*n]=0.5;
59: #endif
60: }
61: }
62: MatDenseRestoreArray(A,&As);
63: if (verbose) {
64: PetscPrintf(PETSC_COMM_WORLD,"Matrix A - - - - - - - -\n");
65: MatView(A,viewer);
66: }
68: /* compute matrix function */
69: MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&F);
70: PetscObjectSetName((PetscObject)F,"F");
71: FNEvaluateFunctionMat(fn,A,F);
72: if (verbose) {
73: PetscPrintf(PETSC_COMM_WORLD,"Computed f(A) - - - - - - -\n");
74: MatView(F,viewer);
75: }
77: /* Repeat with MAT_HERMITIAN flag set */
78: MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE);
79: MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&G);
80: PetscObjectSetName((PetscObject)G,"G");
81: FNEvaluateFunctionMat(fn,A,G);
82: if (verbose) {
83: PetscPrintf(PETSC_COMM_WORLD,"Computed f(A) symm - - - - - - -\n");
84: MatView(G,viewer);
85: }
87: /* compare the two results */
88: MatAXPY(F,-1.0,G,SAME_NONZERO_PATTERN);
89: MatNorm(F,NORM_FROBENIUS,&nrm);
90: if (nrm>100*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"Warning: the norm of F-G is %g\n",(double)nrm);
91: else PetscPrintf(PETSC_COMM_WORLD,"Computed results match.\n");
93: MatDestroy(&A);
94: MatDestroy(&F);
95: MatDestroy(&G);
96: FNDestroy(&fn);
97: SlepcFinalize();
98: return 0;
99: }
101: /*TEST
103: test:
104: suffix: 1
105: nsize: 1
106: args: -fn_type {{exp sqrt}shared output}
107: output_file: output/test12_1.out
109: test:
110: suffix: 1_rational
111: nsize: 1
112: args: -fn_type rational -fn_rational_numerator 2,-1.5 -fn_rational_denominator 1,0.8
113: output_file: output/test12_1.out
115: TEST*/