Line data Source code
1 : /*
2 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 : SLEPc - Scalable Library for Eigenvalue Problem Computations
4 : Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
5 :
6 : This file is part of SLEPc.
7 : SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 : */
10 :
11 : static char help[] = "Test matrix logarithm.\n\n";
12 :
13 : #include <slepcfn.h>
14 :
15 : /*
16 : Compute matrix logarithm B = logm(A)
17 : */
18 3 : PetscErrorCode TestMatLog(FN fn,Mat A,PetscViewer viewer,PetscBool verbose,PetscBool inplace)
19 : {
20 3 : PetscBool set,flg;
21 3 : PetscScalar tau,eta;
22 3 : PetscInt n;
23 3 : Mat F,R;
24 3 : Vec v,f0;
25 3 : FN fnexp;
26 3 : PetscReal nrm;
27 :
28 3 : PetscFunctionBeginUser;
29 3 : PetscCall(MatGetSize(A,&n,NULL));
30 3 : PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&F));
31 3 : PetscCall(PetscObjectSetName((PetscObject)F,"F"));
32 3 : PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&R));
33 3 : PetscCall(PetscObjectSetName((PetscObject)R,"R"));
34 3 : PetscCall(FNGetScale(fn,&tau,&eta));
35 : /* compute matrix logarithm */
36 3 : if (inplace) {
37 0 : PetscCall(MatCopy(A,F,SAME_NONZERO_PATTERN));
38 0 : PetscCall(MatIsHermitianKnown(A,&set,&flg));
39 0 : if (set && flg) PetscCall(MatSetOption(F,MAT_HERMITIAN,PETSC_TRUE));
40 0 : PetscCall(FNEvaluateFunctionMat(fn,F,NULL));
41 3 : } else PetscCall(FNEvaluateFunctionMat(fn,A,F));
42 3 : if (verbose) {
43 0 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Matrix A - - - - - - - -\n"));
44 0 : PetscCall(MatView(A,viewer));
45 0 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Computed logm(A) - - - - - - -\n"));
46 0 : PetscCall(MatView(F,viewer));
47 : }
48 : /* check error ||expm(F)-A||_F */
49 3 : PetscCall(FNCreate(PETSC_COMM_WORLD,&fnexp));
50 3 : PetscCall(FNSetType(fnexp,FNEXP));
51 3 : PetscCall(MatCopy(F,R,SAME_NONZERO_PATTERN));
52 3 : if (eta!=1.0) PetscCall(MatScale(R,1.0/eta));
53 3 : PetscCall(FNEvaluateFunctionMat(fnexp,R,NULL));
54 3 : PetscCall(FNDestroy(&fnexp));
55 3 : PetscCall(MatAXPY(R,-tau,A,SAME_NONZERO_PATTERN));
56 3 : PetscCall(MatNorm(R,NORM_FROBENIUS,&nrm));
57 3 : if (nrm<100*PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"||expm(F)-A||_F < 100*eps\n"));
58 0 : else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"||expm(F)-A||_F = %g\n",(double)nrm));
59 : /* check FNEvaluateFunctionMatVec() */
60 3 : PetscCall(MatCreateVecs(A,&v,&f0));
61 3 : PetscCall(MatGetColumnVector(F,f0,0));
62 3 : PetscCall(FNEvaluateFunctionMatVec(fn,A,v));
63 3 : PetscCall(VecAXPY(v,-1.0,f0));
64 3 : PetscCall(VecNorm(v,NORM_2,&nrm));
65 3 : if (nrm>100*PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Warning: the norm of f(A)*e_1-v is %g\n",(double)nrm));
66 3 : PetscCall(MatDestroy(&F));
67 3 : PetscCall(MatDestroy(&R));
68 3 : PetscCall(VecDestroy(&v));
69 3 : PetscCall(VecDestroy(&f0));
70 3 : PetscFunctionReturn(PETSC_SUCCESS);
71 : }
72 :
73 3 : int main(int argc,char **argv)
74 : {
75 3 : FN fn;
76 3 : Mat A;
77 3 : PetscInt i,j,n=10;
78 3 : PetscScalar *As;
79 3 : PetscViewer viewer;
80 3 : PetscBool verbose,inplace,random,triang;
81 :
82 3 : PetscFunctionBeginUser;
83 3 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
84 3 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
85 3 : PetscCall(PetscOptionsHasName(NULL,NULL,"-verbose",&verbose));
86 3 : PetscCall(PetscOptionsHasName(NULL,NULL,"-inplace",&inplace));
87 3 : PetscCall(PetscOptionsHasName(NULL,NULL,"-random",&random));
88 3 : PetscCall(PetscOptionsHasName(NULL,NULL,"-triang",&triang));
89 3 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Matrix logarithm, n=%" PetscInt_FMT ".\n",n));
90 :
91 : /* Create logarithm function object */
92 3 : PetscCall(FNCreate(PETSC_COMM_WORLD,&fn));
93 3 : PetscCall(FNSetType(fn,FNLOG));
94 3 : PetscCall(FNSetFromOptions(fn));
95 :
96 : /* Set up viewer */
97 3 : PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer));
98 3 : PetscCall(FNView(fn,viewer));
99 3 : if (verbose) PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB));
100 :
101 : /* Create matrices */
102 3 : PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&A));
103 3 : PetscCall(PetscObjectSetName((PetscObject)A,"A"));
104 :
105 3 : if (random) PetscCall(MatSetRandom(A,NULL));
106 : else {
107 : /* Fill A with a non-symmetric Toeplitz matrix */
108 2 : PetscCall(MatDenseGetArray(A,&As));
109 152 : for (i=0;i<n;i++) As[i+i*n]=2.0;
110 6 : for (j=1;j<3;j++) {
111 298 : for (i=0;i<n-j;i++) {
112 294 : As[i+(i+j)*n]=1.0;
113 294 : if (!triang) As[(i+j)+i*n]=-1.0;
114 : }
115 : }
116 2 : As[(n-1)*n] = -5.0;
117 2 : As[0] = 2.01;
118 2 : PetscCall(MatDenseRestoreArray(A,&As));
119 : }
120 3 : PetscCall(TestMatLog(fn,A,viewer,verbose,inplace));
121 :
122 3 : PetscCall(MatDestroy(&A));
123 3 : PetscCall(FNDestroy(&fn));
124 3 : PetscCall(SlepcFinalize());
125 : return 0;
126 : }
127 :
128 : /*TEST
129 :
130 : testset:
131 : filter: grep -v "computing matrix functions"
132 : output_file: output/test13_1.out
133 : test:
134 : suffix: 1
135 : args: -fn_scale .04,2 -n 75
136 : requires: c99_complex !__float128
137 : test:
138 : suffix: 1_triang
139 : args: -fn_scale .04,2 -n 75 -triang
140 : requires: c99_complex !__float128
141 : test:
142 : suffix: 1_random
143 : args: -fn_scale .02,2 -n 75 -random
144 : requires: complex !__float128
145 : filter_output: sed -e 's/04/02/'
146 :
147 : TEST*/
|