GCC Code Coverage Report


Directory: ./
File: src/sys/slepcsc.c
Date: 2026-09-11 04:26:36
Exec Total Coverage
Lines: 170 180 94.4%
Functions: 15 15 100.0%
Branches: 235 428 54.9%

Line Branch Exec Source
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 #include <slepc/private/slepcimpl.h> /*I "slepcsys.h" I*/
12 #include <slepcrg.h>
13 #include <slepcst.h>
14
15 /*@
16 SlepcSCCompare - Compares two (possibly complex) values according
17 to a certain criterion.
18
19 Not Collective
20
21 Input Parameters:
22 + sc - the sorting criterion context
23 . ar - real part of the 1st value
24 . ai - imaginary part of the 1st value
25 . br - real part of the 2nd value
26 - bi - imaginary part of the 2nd value
27
28 Output Parameter:
29 . res - result of comparison
30
31 Notes:
32 Returns an integer less than, equal to, or greater than zero if the first
33 value is considered to be respectively less than, equal to, or greater
34 than the second one.
35
36 Level: developer
37
38 .seealso: `SlepcSortEigenvalues()`, `SlepcSC`
39 @*/
40 23689531 PetscErrorCode SlepcSCCompare(SlepcSC sc,PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *res)
41 {
42 23689531 PetscScalar re[2],im[2];
43 23689531 PetscInt cin[2];
44 23689531 PetscBool inside[2];
45
46
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
23689531 PetscFunctionBegin;
47
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
23689531 PetscAssertPointer(res,6);
48 #if PetscDefined(USE_DEBUG)
49
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4477637 PetscCheck(sc->comparison,PETSC_COMM_SELF,PETSC_ERR_USER,"Undefined comparison function");
50 #endif
51 23689531 re[0] = ar; re[1] = br;
52 23689531 im[0] = ai; im[1] = bi;
53
6/8
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
23689531 if (sc->map) PetscCall((*sc->map)(sc->mapobj,2,re,im));
54
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
23689531 if (sc->rg) {
55
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
899960 PetscCall(RGCheckInside(sc->rg,2,re,im,cin));
56 899960 inside[0] = PetscNot(cin[0]<0);
57 899960 inside[1] = PetscNot(cin[1]<0);
58
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 10 times.
899960 if (inside[0] && !inside[1]) *res = -1;
59
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 10 times.
806447 else if (!inside[0] && inside[1]) *res = 1;
60
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
794838 else PetscCall((*sc->comparison)(re[0],im[0],re[1],im[1],res,sc->comparisonctx));
61
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
22789571 } else PetscCall((*sc->comparison)(re[0],im[0],re[1],im[1],res,sc->comparisonctx));
62
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
4477637 PetscFunctionReturn(PETSC_SUCCESS);
63 }
64
65 14502 static PetscErrorCode SlepcSortEigenvalues_Private(SlepcSC sc,PetscInt n,PetscScalar *eigr,PetscScalar *eigi,PetscInt *perm,PetscBool flg)
66 {
67 14502 PetscScalar re,im;
68 14502 PetscInt i,j,result,tmp;
69
70
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
14502 PetscFunctionBegin;
71 /* insertion sort */
72
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
147441 for (i=n-1;i>=0;i--) {
73 132939 re = eigr[perm[i]];
74 132939 im = eigi[perm[i]];
75 132939 j = i+1;
76 #if !PetscDefined(USE_COMPLEX)
77
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 5 times.
66139 if (im!=0 && (re!=0 || !flg)) {
78 /* complex eigenvalue */
79 1640 i--;
80 1640 im = eigi[perm[i]];
81 }
82 #endif
83
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
1886832 while (j<n) {
84
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
1859193 PetscCall(SlepcSCCompare(sc,re,im,eigr[perm[j]],eigi[perm[j]],&result));
85
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
1859193 if (result<=0) break;
86 #if !PetscDefined(USE_COMPLEX)
87 /* keep together every complex conjugated eigenpair */
88
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
1161300 if (!im || (!re && flg)) {
89
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1161265 if (eigi[perm[j]] == 0.0 || (flg && eigr[perm[j]] == 0.0)) {
90 #endif
91 1753858 tmp = perm[j-1]; perm[j-1] = perm[j]; perm[j] = tmp;
92 1753858 j++;
93 #if !PetscDefined(USE_COMPLEX)
94 } else {
95 tmp = perm[j-1]; perm[j-1] = perm[j]; perm[j] = perm[j+1]; perm[j+1] = tmp;
96 j+=2;
97 }
98 } else {
99
2/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
35 if (eigi[perm[j]] == 0.0 || (flg && eigr[perm[j]] == 0.0)) {
100 tmp = perm[j-2]; perm[j-2] = perm[j]; perm[j] = perm[j-1]; perm[j-1] = tmp;
101 j++;
102 } else {
103 35 tmp = perm[j-2]; perm[j-2] = perm[j]; perm[j] = tmp;
104 35 tmp = perm[j-1]; perm[j-1] = perm[j+1]; perm[j+1] = tmp;
105 35 j+=2;
106 }
107 }
108 #endif
109 }
110 }
111
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
2801 PetscFunctionReturn(PETSC_SUCCESS);
112 }
113 /*@
114 SlepcSortEigenvalues - Sorts a list of eigenvalues according to the
115 sorting criterion specified in a `SlepcSC` context.
116
117 Not Collective
118
119 Input Parameters:
120 + sc - the sorting criterion context
121 . n - number of eigenvalues in the list
122 . eigr - pointer to the array containing the eigenvalues
123 - eigi - imaginary part of the eigenvalues (only when using real scalars)
124
125 Output Parameter:
126 . perm - permutation array, must be initialized to `0:n-1` on input
127
128 Notes:
129 The result is a list of indices in the original eigenvalue array
130 corresponding to the first `n` eigenvalues sorted in the specified
131 criterion.
132
133 In real scalars, this functions assumes that complex values come in
134 conjugate pairs that are consecutive (including purely imaginary ones).
135
136 Level: developer
137
138 .seealso: `SlepcSCCompare()`, `SlepcSC`
139 @*/
140 14467 PetscErrorCode SlepcSortEigenvalues(SlepcSC sc,PetscInt n,PetscScalar eigr[],PetscScalar eigi[],PetscInt perm[])
141 {
142
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
14467 PetscFunctionBegin;
143
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
14467 PetscAssertPointer(sc,1);
144
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
14467 PetscAssertPointer(eigr,3);
145
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
14467 PetscAssertPointer(eigi,4);
146
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
14467 PetscAssertPointer(perm,5);
147
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
14467 PetscCall(SlepcSortEigenvalues_Private(sc,n,eigr,eigi,perm,PETSC_FALSE));
148
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
2794 PetscFunctionReturn(PETSC_SUCCESS);
149 }
150
151 /*@
152 SlepcSortEigenvaluesSpecial - Sorts a list of eigenvalues according to the
153 sorting criterion specified in a `SlepcSC` context, with a special assumption
154 on the input values.
155
156 Not Collective
157
158 Input Parameters:
159 + sc - the sorting criterion context
160 . n - number of eigenvalues in the list
161 . eigr - pointer to the array containing the eigenvalues
162 - eigi - imaginary part of the eigenvalues (only when using real scalars)
163
164 Output Parameter:
165 . perm - permutation array, must be initialized to `0:n-1` on input
166
167 Notes:
168 The result is a list of indices in the original eigenvalue array
169 corresponding to the first `n` eigenvalues sorted in the specified
170 criterion.
171
172 In real scalars, this functions assumes that complex values come in
173 conjugate pairs that are consecutive, but not purely imaginary ones in which
174 case only the one with positive imaginary part appears.
175
176 Level: developer
177
178 .seealso: `SlepcSCCompare()`, `SlepcSC`
179 @*/
180 35 PetscErrorCode SlepcSortEigenvaluesSpecial(SlepcSC sc,PetscInt n,PetscScalar eigr[],PetscScalar eigi[],PetscInt perm[])
181 {
182
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
35 PetscFunctionBegin;
183
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
35 PetscAssertPointer(sc,1);
184
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
35 PetscAssertPointer(eigr,3);
185
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
35 PetscAssertPointer(eigi,4);
186
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
35 PetscAssertPointer(perm,5);
187
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
35 PetscCall(SlepcSortEigenvalues_Private(sc,n,eigr,eigi,perm,PETSC_TRUE));
188
6/12
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
7 PetscFunctionReturn(PETSC_SUCCESS);
189 }
190
191 /*
192 SlepcMap_ST - Gateway function to call STBackTransform from outside ST.
193 */
194 18946933 PetscErrorCode SlepcMap_ST(PetscObject obj,PetscInt n,PetscScalar *eigr,PetscScalar *eigi)
195 {
196
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
18946933 PetscFunctionBegin;
197
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
18946933 PetscCall(STBackTransform((ST)obj,n,eigr,eigi));
198
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
3838789 PetscFunctionReturn(PETSC_SUCCESS);
199 }
200
201 /*@
202 SlepcCompareLargestMagnitude - An eigenvalue comparison function used to sort with respect
203 to largest magnitude.
204
205 Logically Collective
206
207 Input Parameters:
208 + ar - real part of the 1st eigenvalue
209 . ai - imaginary part of the 1st eigenvalue
210 . br - real part of the 2nd eigenvalue
211 . bi - imaginary part of the 2nd eigenvalue
212 - ctx - user-defined context, not used here
213
214 Output Parameter:
215 . result - result of comparison
216
217 Note:
218 The result is 1 if $|\lambda_1|<|\lambda_2|$.
219
220 Level: developer
221
222 .seealso: `SlepcEigenvalueComparisonFn`, `SlepcSCCompare()`, `SlepcSC`
223 @*/
224 10415187 PetscErrorCode SlepcCompareLargestMagnitude(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *result,PetscCtx ctx)
225 {
226 10415187 PetscReal a,b;
227
228
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
10415187 PetscFunctionBegin;
229 10415187 a = SlepcAbsEigenvalue(ar,ai);
230 10415187 b = SlepcAbsEigenvalue(br,bi);
231
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
10415187 if (a<b) *result = 1;
232
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
7317272 else if (a>b) *result = -1;
233 14986 else *result = 0;
234
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
10415187 PetscFunctionReturn(PETSC_SUCCESS);
235 }
236
237 /*@
238 SlepcCompareSmallestMagnitude - An eigenvalue comparison function used to sort with respect
239 to smallest magnitude.
240
241 Logically Collective
242
243 Input Parameters:
244 + ar - real part of the 1st eigenvalue
245 . ai - imaginary part of the 1st eigenvalue
246 . br - real part of the 2nd eigenvalue
247 . bi - imaginary part of the 2nd eigenvalue
248 - ctx - user-defined context, not used here
249
250 Output Parameter:
251 . result - result of comparison
252
253 Note:
254 The result is 1 if $|\lambda_1|>|\lambda_2|$.
255
256 Level: developer
257
258 .seealso: `SlepcEigenvalueComparisonFn`, `SlepcSCCompare()`, `SlepcSC`
259 @*/
260 632476 PetscErrorCode SlepcCompareSmallestMagnitude(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *result,PetscCtx ctx)
261 {
262 632476 PetscReal a,b;
263
264
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
632476 PetscFunctionBegin;
265 632476 a = SlepcAbsEigenvalue(ar,ai);
266 632476 b = SlepcAbsEigenvalue(br,bi);
267
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
632476 if (a>b) *result = 1;
268
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
400530 else if (a<b) *result = -1;
269 301 else *result = 0;
270
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
632476 PetscFunctionReturn(PETSC_SUCCESS);
271 }
272
273 /*@
274 SlepcCompareLargestReal - An eigenvalue comparison function used to sort with respect
275 to largest real part.
276
277 Logically Collective
278
279 Input Parameters:
280 + ar - real part of the 1st eigenvalue
281 . ai - imaginary part of the 1st eigenvalue
282 . br - real part of the 2nd eigenvalue
283 . bi - imaginary part of the 2nd eigenvalue
284 - ctx - user-defined context, not used here
285
286 Output Parameter:
287 . result - result of comparison
288
289 Note:
290 The result is 1 if $\mathrm{Re}(\lambda_1)<\mathrm{Re}(\lambda_2)$.
291
292 Level: developer
293
294 .seealso: `SlepcEigenvalueComparisonFn`, `SlepcSCCompare()`, `SlepcSC`
295 @*/
296 2800248 PetscErrorCode SlepcCompareLargestReal(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *result,PetscCtx ctx)
297 {
298 2800248 PetscReal a,b;
299
300
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2800248 PetscFunctionBegin;
301 2800248 a = PetscRealPart(ar);
302 2800248 b = PetscRealPart(br);
303
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
2800248 if (a<b) *result = 1;
304
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
2137448 else if (a>b) *result = -1;
305 5041 else *result = 0;
306
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
2800248 PetscFunctionReturn(PETSC_SUCCESS);
307 }
308
309 /*@
310 SlepcCompareSmallestReal - An eigenvalue comparison function used to sort with respect
311 to smallest real part.
312
313 Logically Collective
314
315 Input Parameters:
316 + ar - real part of the 1st eigenvalue
317 . ai - imaginary part of the 1st eigenvalue
318 . br - real part of the 2nd eigenvalue
319 . bi - imaginary part of the 2nd eigenvalue
320 - ctx - user-defined context, not used here
321
322 Output Parameter:
323 . result - result of comparison
324
325 Note:
326 The result is 1 if $\mathrm{Re}(\lambda_1)>\mathrm{Re}(\lambda_2)$.
327
328 Level: developer
329
330 .seealso: `SlepcEigenvalueComparisonFn`, `SlepcSCCompare()`, `SlepcSC`
331 @*/
332 2132470 PetscErrorCode SlepcCompareSmallestReal(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *result,PetscCtx ctx)
333 {
334 2132470 PetscReal a,b;
335
336
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2132470 PetscFunctionBegin;
337 2132470 a = PetscRealPart(ar);
338 2132470 b = PetscRealPart(br);
339
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
2132470 if (a>b) *result = 1;
340
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
1276521 else if (a<b) *result = -1;
341 571 else *result = 0;
342
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
2132470 PetscFunctionReturn(PETSC_SUCCESS);
343 }
344
345 /*@
346 SlepcCompareLargestImaginary - An eigenvalue comparison function used to sort with respect
347 to largest imaginary real part.
348
349 Logically Collective
350
351 Input Parameters:
352 + ar - real part of the 1st eigenvalue
353 . ai - imaginary part of the 1st eigenvalue
354 . br - real part of the 2nd eigenvalue
355 . bi - imaginary part of the 2nd eigenvalue
356 - ctx - user-defined context, not used here
357
358 Output Parameter:
359 . result - result of comparison
360
361 Note:
362 In complex scalars, the result is 1 if $\mathrm{Im}(\lambda_1)<\mathrm{Im}(\lambda_2)$.
363 In real scalars, the result is 1 if $|\mathrm{Im}(\lambda_1)|<|\mathrm{Im}(\lambda_2)|$.
364 If the two values are equal, then $|\lambda_1|<|\lambda_2|$ is used to break the tie.
365
366 Level: developer
367
368 .seealso: `SlepcEigenvalueComparisonFn`, `SlepcSCCompare()`, `SlepcSC`
369 @*/
370 32871 PetscErrorCode SlepcCompareLargestImaginary(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *result,PetscCtx ctx)
371 {
372 32871 PetscReal a,b;
373
374
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
32871 PetscFunctionBegin;
375 #if PetscDefined(USE_COMPLEX)
376 20870 a = PetscImaginaryPart(ar);
377 20870 b = PetscImaginaryPart(br);
378 #else
379 12001 a = PetscAbsReal(ai);
380 12001 b = PetscAbsReal(bi);
381 #endif
382
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
32871 if (a<b) *result = 1;
383
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
25687 else if (a>b) *result = -1;
384 else { /* break the tie by checking the magnitude */
385 7343 a = SlepcAbsEigenvalue(ar,ai);
386 7343 b = SlepcAbsEigenvalue(br,bi);
387
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
7343 if (a<b) *result = 1;
388
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
6977 else if (a>b) *result = -1;
389 else *result = 0;
390 }
391
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
32871 PetscFunctionReturn(PETSC_SUCCESS);
392 }
393
394 /*@
395 SlepcCompareSmallestImaginary - An eigenvalue comparison function used to sort with respect
396 to smallest imaginary real part.
397
398 Logically Collective
399
400 Input Parameters:
401 + ar - real part of the 1st eigenvalue
402 . ai - imaginary part of the 1st eigenvalue
403 . br - real part of the 2nd eigenvalue
404 . bi - imaginary part of the 2nd eigenvalue
405 - ctx - user-defined context, not used here
406
407 Output Parameter:
408 . result - result of comparison
409
410 Notes:
411 In complex scalars, the result is 1 if $\mathrm{Im}(\lambda_1)>\mathrm{Im}(\lambda_2)$.
412 In real scalars, the result is 1 if $|\mathrm{Im}(\lambda_1)|>|\mathrm{Im}(\lambda_2)|$.
413 If the two values are equal, then $|\lambda_1|<|\lambda_2|$ is used to break the tie.
414
415 Level: developer
416
417 .seealso: `SlepcEigenvalueComparisonFn`, `SlepcSCCompare()`, `SlepcSC`
418 @*/
419 25610 PetscErrorCode SlepcCompareSmallestImaginary(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *result,PetscCtx ctx)
420 {
421 25610 PetscReal a,b;
422
423
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
25610 PetscFunctionBegin;
424 #if PetscDefined(USE_COMPLEX)
425 22085 a = PetscImaginaryPart(ar);
426 22085 b = PetscImaginaryPart(br);
427 #else
428 3525 a = PetscAbsReal(ai);
429 3525 b = PetscAbsReal(bi);
430 #endif
431
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
25610 if (a>b) *result = 1;
432
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
20695 else if (a<b) *result = -1;
433 else { /* break the tie by checking the magnitude */
434 2955 a = SlepcAbsEigenvalue(ar,ai);
435 2955 b = SlepcAbsEigenvalue(br,bi);
436
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
2955 if (a<b) *result = 1;
437
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
1220 else if (a>b) *result = -1;
438 else *result = 0;
439 }
440
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
25610 PetscFunctionReturn(PETSC_SUCCESS);
441 }
442
443 /*@
444 SlepcCompareTargetMagnitude - An eigenvalue comparison function used to sort with respect
445 to a target (in magnitude).
446
447 Logically Collective
448
449 Input Parameters:
450 + ar - real part of the 1st eigenvalue
451 . ai - imaginary part of the 1st eigenvalue
452 . br - real part of the 2nd eigenvalue
453 . bi - imaginary part of the 2nd eigenvalue
454 - ctx - user-defined context, contains the target value (a `PetscScalar`)
455
456 Output Parameter:
457 . result - result of comparison
458
459 Note:
460 The result is 1 if $|\lambda_1-\tau|<|\lambda_2-\tau|$ where $\tau$ is the target.
461
462 Level: developer
463
464 .seealso: `SlepcEigenvalueComparisonFn`, `SlepcSCCompare()`, `SlepcSC`
465 @*/
466 6245224 PetscErrorCode SlepcCompareTargetMagnitude(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *result,PetscCtx ctx)
467 {
468 6245224 PetscReal a,b;
469 6245224 PetscScalar *target = (PetscScalar*)ctx;
470
471
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
6245224 PetscFunctionBegin;
472 /* complex target only allowed if scalartype=complex */
473 6245224 a = SlepcAbsEigenvalue(ar-(*target),ai);
474 6245224 b = SlepcAbsEigenvalue(br-(*target),bi);
475
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
6245224 if (a>b) *result = 1;
476
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
4838355 else if (a<b) *result = -1;
477 79 else *result = 0;
478
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
6245224 PetscFunctionReturn(PETSC_SUCCESS);
479 }
480
481 /*@
482 SlepcCompareTargetReal - An eigenvalue comparison function used to sort with respect
483 to a target (along the real axis).
484
485 Logically Collective
486
487 Input Parameters:
488 + ar - real part of the 1st eigenvalue
489 . ai - imaginary part of the 1st eigenvalue
490 . br - real part of the 2nd eigenvalue
491 . bi - imaginary part of the 2nd eigenvalue
492 - ctx - user-defined context, contains the target value (a `PetscScalar`)
493
494 Output Parameter:
495 . result - result of comparison
496
497 Note:
498 The result is 1 if $\mathrm{Re}(\lambda_1-\tau)<\mathrm{Re}(\lambda_2-\tau)$ where
499 $\tau$ is the target.
500
501 Level: developer
502
503 .seealso: `SlepcEigenvalueComparisonFn`, `SlepcSCCompare()`, `SlepcSC`
504 @*/
505 25021 PetscErrorCode SlepcCompareTargetReal(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *result,PetscCtx ctx)
506 {
507 25021 PetscReal a,b;
508 25021 PetscScalar *target = (PetscScalar*)ctx;
509
510
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
25021 PetscFunctionBegin;
511 25021 a = PetscAbsReal(PetscRealPart(ar-(*target)));
512 25021 b = PetscAbsReal(PetscRealPart(br-(*target)));
513
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
25021 if (a>b) *result = 1;
514
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
7784 else if (a<b) *result = -1;
515 else *result = 0;
516
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
25021 PetscFunctionReturn(PETSC_SUCCESS);
517 }
518
519 /*@
520 SlepcCompareTargetImaginary - An eigenvalue comparison function used to sort with respect
521 to a target (along the imaginary axis).
522
523 Logically Collective
524
525 Input Parameters:
526 + ar - real part of the 1st eigenvalue
527 . ai - imaginary part of the 1st eigenvalue
528 . br - real part of the 2nd eigenvalue
529 . bi - imaginary part of the 2nd eigenvalue
530 - ctx - user-defined context, contains the target value (a `PetscScalar`)
531
532 Output Parameter:
533 . result - result of comparison
534
535 Notes:
536 In complex scalars, the result is 1 if $\mathrm{Im}(\lambda_1-\tau)<\mathrm{Im}(\lambda_2-\tau)$
537 where $\tau$ is the target.
538 In real scalars, the result is always zero because sorting with respect to target is
539 not supported (the target is always real in this case).
540
541 Level: developer
542
543 .seealso: `SlepcEigenvalueComparisonFn`, `SlepcSCCompare()`, `SlepcSC`
544 @*/
545 14205 PetscErrorCode SlepcCompareTargetImaginary(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *result,PetscCtx ctx)
546 {
547 #if PetscDefined(USE_COMPLEX)
548 14205 PetscReal a,b;
549 14205 PetscScalar *target = (PetscScalar*)ctx;
550 #endif
551
552
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
14205 PetscFunctionBegin;
553 #if PetscDefined(USE_COMPLEX)
554 14205 a = PetscAbsReal(PetscImaginaryPart(ar-(*target)));
555 14205 b = PetscAbsReal(PetscImaginaryPart(br-(*target)));
556
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
14205 if (a>b) *result = 1;
557
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10590 else if (a<b) *result = -1;
558 else *result = 0;
559 #else
560 *result = 0;
561 #endif
562
6/12
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
14205 PetscFunctionReturn(PETSC_SUCCESS);
563 }
564
565 /*@
566 SlepcCompareSmallestPosReal - An eigenvalue comparison function used to sort according
567 to the smallest positive real part.
568
569 Logically Collective
570
571 Input Parameters:
572 + ar - real part of the 1st eigenvalue
573 . ai - imaginary part of the 1st eigenvalue
574 . br - real part of the 2nd eigenvalue
575 . bi - imaginary part of the 2nd eigenvalue
576 - ctx - user-defined context, not used here
577
578 Output Parameter:
579 . result - result of comparison
580
581 Note:
582 This sorting criterion is used in the SVD for computing smallest singular values
583 from the cyclic matrix. In that case, the computed values come in pairs $\pm\lambda$.
584 If the two values to compare have the same sign, the preferred one is the one with
585 smallest magnitude. Otherwise, the prefered one is the rightmost (positive sign).
586
587 Level: developer
588
589 .seealso: `SlepcEigenvalueComparisonFn`, `SlepcSCCompare()`, `SlepcSC`
590 @*/
591 90365 PetscErrorCode SlepcCompareSmallestPosReal(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *result,PetscCtx ctx)
592 {
593 90365 PetscReal a,b;
594 90365 PetscBool aisright,bisright;
595
596
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
90365 PetscFunctionBegin;
597
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
90365 if (PetscRealPart(ar)>0.0) aisright = PETSC_TRUE;
598 14790 else aisright = PETSC_FALSE;
599
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
90365 if (PetscRealPart(br)>0.0) bisright = PETSC_TRUE;
600 65185 else bisright = PETSC_FALSE;
601
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
90365 if (aisright == bisright) { /* same sign */
602 37130 a = SlepcAbsEigenvalue(ar,ai);
603 37130 b = SlepcAbsEigenvalue(br,bi);
604
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
37130 if (a>b) *result = 1;
605
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
9885 else if (a<b) *result = -1;
606 else *result = 0;
607
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
53235 } else if (aisright && !bisright) *result = -1; /* 'a' is on the right */
608 1420 else *result = 1; /* 'b' is on the right */
609
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
90365 PetscFunctionReturn(PETSC_SUCCESS);
610 }
611