xref: /petsc/src/mat/impls/transpose/htransm.c (revision 3fbf2391c9dde277e962e42f21bedf0074869095)
1 #include <../src/mat/impls/shell/shell.h> /*I "petscmat.h" I*/
2 
3 static PetscErrorCode MatProductSetFromOptions_HT(Mat D)
4 {
5   Mat            A, B, C, Ain, Bin, Cin;
6   PetscBool      Aistrans, Bistrans, Cistrans;
7   PetscInt       Atrans, Btrans, Ctrans;
8   MatProductType ptype;
9 
10   PetscFunctionBegin;
11   MatCheckProduct(D, 1);
12   A = D->product->A;
13   B = D->product->B;
14   C = D->product->C;
15   PetscCall(PetscObjectTypeCompare((PetscObject)A, MATHERMITIANTRANSPOSEVIRTUAL, &Aistrans));
16   PetscCall(PetscObjectTypeCompare((PetscObject)B, MATHERMITIANTRANSPOSEVIRTUAL, &Bistrans));
17   PetscCall(PetscObjectTypeCompare((PetscObject)C, MATHERMITIANTRANSPOSEVIRTUAL, &Cistrans));
18   PetscCheck(Aistrans || Bistrans || Cistrans, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "This should not happen");
19   Atrans = 0;
20   Ain    = A;
21   while (Aistrans) {
22     Atrans++;
23     PetscCall(MatShellGetScalingShifts(Ain, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
24     PetscCall(MatHermitianTransposeGetMat(Ain, &Ain));
25     PetscCall(PetscObjectTypeCompare((PetscObject)Ain, MATHERMITIANTRANSPOSEVIRTUAL, &Aistrans));
26   }
27   Btrans = 0;
28   Bin    = B;
29   while (Bistrans) {
30     Btrans++;
31     PetscCall(MatShellGetScalingShifts(Bin, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
32     PetscCall(MatHermitianTransposeGetMat(Bin, &Bin));
33     PetscCall(PetscObjectTypeCompare((PetscObject)Bin, MATHERMITIANTRANSPOSEVIRTUAL, &Bistrans));
34   }
35   Ctrans = 0;
36   Cin    = C;
37   while (Cistrans) {
38     Ctrans++;
39     PetscCall(MatShellGetScalingShifts(Cin, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
40     PetscCall(MatHermitianTransposeGetMat(Cin, &Cin));
41     PetscCall(PetscObjectTypeCompare((PetscObject)Cin, MATHERMITIANTRANSPOSEVIRTUAL, &Cistrans));
42   }
43   Atrans = Atrans % 2;
44   Btrans = Btrans % 2;
45   Ctrans = Ctrans % 2;
46   ptype  = D->product->type; /* same product type by default */
47   if (Ain->symmetric == PETSC_BOOL3_TRUE) Atrans = 0;
48   if (Bin->symmetric == PETSC_BOOL3_TRUE) Btrans = 0;
49   if (Cin && Cin->symmetric == PETSC_BOOL3_TRUE) Ctrans = 0;
50 
51   if (Atrans || Btrans || Ctrans) {
52     PetscCheck(!PetscDefined(USE_COMPLEX), PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "No support for complex Hermitian transpose matrices");
53     ptype = MATPRODUCT_UNSPECIFIED;
54     switch (D->product->type) {
55     case MATPRODUCT_AB:
56       if (Atrans && Btrans) { /* At * Bt we do not have support for this */
57         /* TODO custom implementation ? */
58       } else if (Atrans) { /* At * B */
59         ptype = MATPRODUCT_AtB;
60       } else { /* A * Bt */
61         ptype = MATPRODUCT_ABt;
62       }
63       break;
64     case MATPRODUCT_AtB:
65       if (Atrans && Btrans) { /* A * Bt */
66         ptype = MATPRODUCT_ABt;
67       } else if (Atrans) { /* A * B */
68         ptype = MATPRODUCT_AB;
69       } else { /* At * Bt we do not have support for this */
70         /* TODO custom implementation ? */
71       }
72       break;
73     case MATPRODUCT_ABt:
74       if (Atrans && Btrans) { /* At * B */
75         ptype = MATPRODUCT_AtB;
76       } else if (Atrans) { /* At * Bt we do not have support for this */
77         /* TODO custom implementation ? */
78       } else { /* A * B */
79         ptype = MATPRODUCT_AB;
80       }
81       break;
82     case MATPRODUCT_PtAP:
83       if (Atrans) { /* PtAtP */
84         /* TODO custom implementation ? */
85       } else { /* RARt */
86         ptype = MATPRODUCT_RARt;
87       }
88       break;
89     case MATPRODUCT_RARt:
90       if (Atrans) { /* RAtRt */
91         /* TODO custom implementation ? */
92       } else { /* PtAP */
93         ptype = MATPRODUCT_PtAP;
94       }
95       break;
96     case MATPRODUCT_ABC:
97       /* TODO custom implementation ? */
98       break;
99     default:
100       SETERRQ(PetscObjectComm((PetscObject)D), PETSC_ERR_SUP, "ProductType %s is not supported", MatProductTypes[D->product->type]);
101     }
102   }
103   PetscCall(MatProductReplaceMats(Ain, Bin, Cin, D));
104   PetscCall(MatProductSetType(D, ptype));
105   PetscCall(MatProductSetFromOptions(D));
106   PetscFunctionReturn(PETSC_SUCCESS);
107 }
108 
109 static PetscErrorCode MatMult_HT(Mat N, Vec x, Vec y)
110 {
111   Mat A;
112 
113   PetscFunctionBegin;
114   PetscCall(MatShellGetContext(N, &A));
115   PetscCall(MatMultHermitianTranspose(A, x, y));
116   PetscFunctionReturn(PETSC_SUCCESS);
117 }
118 
119 static PetscErrorCode MatMultHermitianTranspose_HT(Mat N, Vec x, Vec y)
120 {
121   Mat A;
122 
123   PetscFunctionBegin;
124   PetscCall(MatShellGetContext(N, &A));
125   PetscCall(MatMult(A, x, y));
126   PetscFunctionReturn(PETSC_SUCCESS);
127 }
128 
129 static PetscErrorCode MatSolve_HT_LU(Mat N, Vec b, Vec x)
130 {
131   Mat A;
132   Vec w;
133 
134   PetscFunctionBegin;
135   PetscCall(MatShellGetContext(N, &A));
136   PetscCall(VecDuplicate(b, &w));
137   PetscCall(VecCopy(b, w));
138   PetscCall(VecConjugate(w));
139   PetscCall(MatSolveTranspose(A, w, x));
140   PetscCall(VecConjugate(x));
141   PetscCall(VecDestroy(&w));
142   PetscFunctionReturn(PETSC_SUCCESS);
143 }
144 
145 static PetscErrorCode MatSolveAdd_HT_LU(Mat N, Vec b, Vec y, Vec x)
146 {
147   Mat A;
148   Vec v, w;
149 
150   PetscFunctionBegin;
151   PetscCall(MatShellGetContext(N, &A));
152   PetscCall(VecDuplicate(b, &v));
153   PetscCall(VecDuplicate(b, &w));
154   PetscCall(VecCopy(y, v));
155   PetscCall(VecCopy(b, w));
156   PetscCall(VecConjugate(v));
157   PetscCall(VecConjugate(w));
158   PetscCall(MatSolveTransposeAdd(A, w, v, x));
159   PetscCall(VecConjugate(x));
160   PetscCall(VecDestroy(&v));
161   PetscCall(VecDestroy(&w));
162   PetscFunctionReturn(PETSC_SUCCESS);
163 }
164 
165 static PetscErrorCode MatMatSolve_HT_LU(Mat N, Mat B, Mat X)
166 {
167   Mat A, W;
168 
169   PetscFunctionBegin;
170   PetscCall(MatShellGetContext(N, &A));
171   PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &W));
172   PetscCall(MatConjugate(W));
173   PetscCall(MatMatSolveTranspose(A, W, X));
174   PetscCall(MatConjugate(X));
175   PetscCall(MatDestroy(&W));
176   PetscFunctionReturn(PETSC_SUCCESS);
177 }
178 
179 static PetscErrorCode MatLUFactor_HT(Mat N, IS row, IS col, const MatFactorInfo *minfo)
180 {
181   Mat A;
182 
183   PetscFunctionBegin;
184   PetscCall(MatShellGetContext(N, &A));
185   PetscCall(MatLUFactor(A, col, row, minfo));
186   PetscCall(MatShellSetOperation(N, MATOP_SOLVE, (void (*)(void))MatSolve_HT_LU));
187   PetscCall(MatShellSetOperation(N, MATOP_SOLVE_ADD, (void (*)(void))MatSolveAdd_HT_LU));
188   PetscCall(MatShellSetOperation(N, MATOP_MAT_SOLVE, (void (*)(void))MatMatSolve_HT_LU));
189   PetscFunctionReturn(PETSC_SUCCESS);
190 }
191 
192 static PetscErrorCode MatSolve_HT_Cholesky(Mat N, Vec b, Vec x)
193 {
194   Mat A;
195 
196   PetscFunctionBegin;
197   PetscCall(MatShellGetContext(N, &A));
198   PetscCall(MatSolve(A, b, x));
199   PetscFunctionReturn(PETSC_SUCCESS);
200 }
201 
202 static PetscErrorCode MatSolveAdd_HT_Cholesky(Mat N, Vec b, Vec y, Vec x)
203 {
204   Mat A;
205   Vec v, w;
206 
207   PetscFunctionBegin;
208   PetscCall(MatShellGetContext(N, &A));
209   PetscCall(VecDuplicate(b, &v));
210   PetscCall(VecDuplicate(b, &w));
211   PetscCall(VecCopy(y, v));
212   PetscCall(VecCopy(b, w));
213   PetscCall(VecConjugate(v));
214   PetscCall(VecConjugate(w));
215   PetscCall(MatSolveTransposeAdd(A, w, v, x));
216   PetscCall(VecConjugate(x));
217   PetscCall(VecDestroy(&v));
218   PetscCall(VecDestroy(&w));
219   PetscFunctionReturn(PETSC_SUCCESS);
220 }
221 
222 static PetscErrorCode MatMatSolve_HT_Cholesky(Mat N, Mat B, Mat X)
223 {
224   Mat A, W;
225 
226   PetscFunctionBegin;
227   PetscCall(MatShellGetContext(N, &A));
228   PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &W));
229   PetscCall(MatConjugate(W));
230   PetscCall(MatMatSolveTranspose(A, W, X));
231   PetscCall(MatConjugate(X));
232   PetscCall(MatDestroy(&W));
233   PetscFunctionReturn(PETSC_SUCCESS);
234 }
235 
236 static PetscErrorCode MatCholeskyFactor_HT(Mat N, IS perm, const MatFactorInfo *minfo)
237 {
238   Mat A;
239 
240   PetscFunctionBegin;
241   PetscCall(MatShellGetContext(N, &A));
242   PetscCheck(!PetscDefined(USE_COMPLEX) || A->hermitian == PETSC_BOOL3_TRUE, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Cholesky supported only if original matrix is Hermitian");
243   PetscCall(MatCholeskyFactor(A, perm, minfo));
244   PetscCall(MatShellSetOperation(N, MATOP_SOLVE, (void (*)(void))MatSolve_HT_Cholesky));
245   PetscCall(MatShellSetOperation(N, MATOP_SOLVE_ADD, (void (*)(void))MatSolveAdd_HT_Cholesky));
246   PetscCall(MatShellSetOperation(N, MATOP_MAT_SOLVE, (void (*)(void))MatMatSolve_HT_Cholesky));
247   PetscFunctionReturn(PETSC_SUCCESS);
248 }
249 
250 static PetscErrorCode MatLUFactorNumeric_HT(Mat F, Mat N, const MatFactorInfo *info)
251 {
252   Mat A, FA;
253 
254   PetscFunctionBegin;
255   PetscCall(MatShellGetContext(N, &A));
256   PetscCall(MatShellGetContext(F, &FA));
257   PetscCall(MatLUFactorNumeric(FA, A, info));
258   PetscCall(MatShellSetOperation(F, MATOP_SOLVE, (void (*)(void))MatSolve_HT_LU));
259   PetscCall(MatShellSetOperation(F, MATOP_SOLVE_ADD, (void (*)(void))MatSolveAdd_HT_LU));
260   PetscCall(MatShellSetOperation(F, MATOP_MAT_SOLVE, (void (*)(void))MatMatSolve_HT_LU));
261   PetscFunctionReturn(PETSC_SUCCESS);
262 }
263 
264 static PetscErrorCode MatLUFactorSymbolic_HT(Mat F, Mat N, IS row, IS col, const MatFactorInfo *info)
265 {
266   Mat A, FA;
267 
268   PetscFunctionBegin;
269   PetscCall(MatShellGetContext(N, &A));
270   PetscCall(MatShellGetContext(F, &FA));
271   PetscCall(MatLUFactorSymbolic(FA, A, row, col, info));
272   PetscCall(MatShellSetOperation(F, MATOP_LUFACTOR_NUMERIC, (void (*)(void))MatLUFactorNumeric_HT));
273   PetscFunctionReturn(PETSC_SUCCESS);
274 }
275 
276 static PetscErrorCode MatCholeskyFactorNumeric_HT(Mat F, Mat N, const MatFactorInfo *info)
277 {
278   Mat A, FA;
279 
280   PetscFunctionBegin;
281   PetscCall(MatShellGetContext(N, &A));
282   PetscCall(MatShellGetContext(F, &FA));
283   PetscCall(MatCholeskyFactorNumeric(FA, A, info));
284   PetscCall(MatShellSetOperation(F, MATOP_SOLVE, (void (*)(void))MatSolve_HT_Cholesky));
285   PetscCall(MatShellSetOperation(F, MATOP_SOLVE_ADD, (void (*)(void))MatSolveAdd_HT_Cholesky));
286   PetscCall(MatShellSetOperation(F, MATOP_MAT_SOLVE, (void (*)(void))MatMatSolve_HT_Cholesky));
287   PetscFunctionReturn(PETSC_SUCCESS);
288 }
289 
290 static PetscErrorCode MatCholeskyFactorSymbolic_HT(Mat F, Mat N, IS perm, const MatFactorInfo *info)
291 {
292   Mat A, FA;
293 
294   PetscFunctionBegin;
295   PetscCall(MatShellGetContext(N, &A));
296   PetscCall(MatShellGetContext(F, &FA));
297   PetscCall(MatCholeskyFactorSymbolic(FA, A, perm, info));
298   PetscCall(MatShellSetOperation(F, MATOP_CHOLESKY_FACTOR_NUMERIC, (void (*)(void))MatCholeskyFactorNumeric_HT));
299   PetscFunctionReturn(PETSC_SUCCESS);
300 }
301 
302 static PetscErrorCode MatGetFactor_HT(Mat N, MatSolverType type, MatFactorType ftype, Mat *F)
303 {
304   Mat A, FA;
305 
306   PetscFunctionBegin;
307   PetscCall(MatShellGetContext(N, &A));
308   PetscCall(MatGetFactor(A, type, ftype, &FA));
309   PetscCall(MatCreateTranspose(FA, F));
310   if (ftype == MAT_FACTOR_LU) PetscCall(MatShellSetOperation(*F, MATOP_LUFACTOR_SYMBOLIC, (void (*)(void))MatLUFactorSymbolic_HT));
311   else if (ftype == MAT_FACTOR_CHOLESKY) {
312     PetscCheck(!PetscDefined(USE_COMPLEX) || A->hermitian == PETSC_BOOL3_TRUE, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Cholesky supported only if original matrix is Hermitian");
313     PetscCall(MatPropagateSymmetryOptions(A, FA));
314     PetscCall(MatShellSetOperation(*F, MATOP_CHOLESKY_FACTOR_SYMBOLIC, (void (*)(void))MatCholeskyFactorSymbolic_HT));
315   } else SETERRQ(PetscObjectComm((PetscObject)N), PETSC_ERR_SUP, "Support for factor type %s not implemented in MATTRANSPOSEVIRTUAL", MatFactorTypes[ftype]);
316   (*F)->factortype = ftype;
317   PetscCall(MatDestroy(&FA));
318   PetscFunctionReturn(PETSC_SUCCESS);
319 }
320 
321 static PetscErrorCode MatDestroy_HT(Mat N)
322 {
323   Mat A;
324 
325   PetscFunctionBegin;
326   PetscCall(MatShellGetContext(N, &A));
327   PetscCall(MatDestroy(&A));
328   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatHermitianTransposeGetMat_C", NULL));
329 #if !defined(PETSC_USE_COMPLEX)
330   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatTransposeGetMat_C", NULL));
331 #endif
332   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatProductSetFromOptions_anytype_C", NULL));
333   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatShellSetContext_C", NULL));
334   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatFactorGetSolverType_C", NULL));
335   PetscFunctionReturn(PETSC_SUCCESS);
336 }
337 
338 static PetscErrorCode MatGetInfo_HT(Mat N, MatInfoType flag, MatInfo *info)
339 {
340   Mat A;
341 
342   PetscFunctionBegin;
343   PetscCall(MatShellGetContext(N, &A));
344   PetscCall(MatGetInfo(A, flag, info));
345   PetscFunctionReturn(PETSC_SUCCESS);
346 }
347 
348 static PetscErrorCode MatFactorGetSolverType_HT(Mat N, MatSolverType *type)
349 {
350   Mat A;
351 
352   PetscFunctionBegin;
353   PetscCall(MatShellGetContext(N, &A));
354   PetscCall(MatFactorGetSolverType(A, type));
355   PetscFunctionReturn(PETSC_SUCCESS);
356 }
357 
358 static PetscErrorCode MatDuplicate_HT(Mat N, MatDuplicateOption op, Mat *m)
359 {
360   Mat A, C;
361 
362   PetscFunctionBegin;
363   PetscCall(MatShellGetContext(N, &A));
364   PetscCall(MatDuplicate(A, op, &C));
365   PetscCall(MatCreateHermitianTranspose(C, m));
366   if (op == MAT_COPY_VALUES) PetscCall(MatCopy(N, *m, SAME_NONZERO_PATTERN));
367   PetscCall(MatDestroy(&C));
368   PetscFunctionReturn(PETSC_SUCCESS);
369 }
370 
371 static PetscErrorCode MatHasOperation_HT(Mat mat, MatOperation op, PetscBool *has)
372 {
373   Mat A;
374 
375   PetscFunctionBegin;
376   PetscCall(MatShellGetContext(mat, &A));
377   *has = PETSC_FALSE;
378   if (op == MATOP_MULT || op == MATOP_MULT_ADD) {
379     PetscCall(MatHasOperation(A, MATOP_MULT_HERMITIAN_TRANSPOSE, has));
380     if (!*has) PetscCall(MatHasOperation(A, MATOP_MULT_TRANSPOSE, has));
381   } else if (op == MATOP_MULT_HERMITIAN_TRANSPOSE || op == MATOP_MULT_HERMITIAN_TRANS_ADD || op == MATOP_MULT_TRANSPOSE || op == MATOP_MULT_TRANSPOSE_ADD) {
382     PetscCall(MatHasOperation(A, MATOP_MULT, has));
383   } else if (((void **)mat->ops)[op]) *has = PETSC_TRUE;
384   PetscFunctionReturn(PETSC_SUCCESS);
385 }
386 
387 static PetscErrorCode MatHermitianTransposeGetMat_HT(Mat N, Mat *M)
388 {
389   PetscFunctionBegin;
390   PetscCall(MatShellGetContext(N, M));
391   PetscFunctionReturn(PETSC_SUCCESS);
392 }
393 
394 /*@
395   MatHermitianTransposeGetMat - Gets the `Mat` object stored inside a `MATHERMITIANTRANSPOSEVIRTUAL`
396 
397   Logically Collective
398 
399   Input Parameter:
400 . A - the `MATHERMITIANTRANSPOSEVIRTUAL` matrix
401 
402   Output Parameter:
403 . M - the matrix object stored inside A
404 
405   Level: intermediate
406 
407 .seealso: [](ch_matrices), `Mat`, `MATHERMITIANTRANSPOSEVIRTUAL`, `MatCreateHermitianTranspose()`
408 @*/
409 PetscErrorCode MatHermitianTransposeGetMat(Mat A, Mat *M)
410 {
411   PetscFunctionBegin;
412   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
413   PetscValidType(A, 1);
414   PetscAssertPointer(M, 2);
415   PetscUseMethod(A, "MatHermitianTransposeGetMat_C", (Mat, Mat *), (A, M));
416   PetscFunctionReturn(PETSC_SUCCESS);
417 }
418 
419 static PetscErrorCode MatGetDiagonal_HT(Mat N, Vec v)
420 {
421   Mat A;
422 
423   PetscFunctionBegin;
424   PetscCall(MatShellGetContext(N, &A));
425   PetscCall(MatGetDiagonal(A, v));
426   PetscCall(VecConjugate(v));
427   PetscFunctionReturn(PETSC_SUCCESS);
428 }
429 
430 static PetscErrorCode MatCopy_HT(Mat A, Mat B, MatStructure str)
431 {
432   Mat a, b;
433 
434   PetscFunctionBegin;
435   PetscCall(MatShellGetContext(A, &a));
436   PetscCall(MatShellGetContext(B, &b));
437   PetscCall(MatCopy(a, b, str));
438   PetscFunctionReturn(PETSC_SUCCESS);
439 }
440 
441 static PetscErrorCode MatConvert_HT(Mat N, MatType newtype, MatReuse reuse, Mat *newmat)
442 {
443   Mat         A;
444   PetscScalar vscale = 1.0, vshift = 0.0;
445   PetscBool   flg;
446 
447   PetscFunctionBegin;
448   PetscCall(MatShellGetContext(N, &A));
449   PetscCall(MatHasOperation(A, MATOP_HERMITIAN_TRANSPOSE, &flg));
450   if (flg || N->ops->getrow) { /* if this condition is false, MatConvert_Shell() will be called in MatConvert_Basic(), so the following checks are not needed */
451     PetscCall(MatShellGetScalingShifts(N, &vshift, &vscale, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
452   }
453   if (flg) {
454     Mat B;
455 
456     PetscCall(MatHermitianTranspose(A, MAT_INITIAL_MATRIX, &B));
457     if (reuse != MAT_INPLACE_MATRIX) {
458       PetscCall(MatConvert(B, newtype, reuse, newmat));
459       PetscCall(MatDestroy(&B));
460     } else {
461       PetscCall(MatConvert(B, newtype, MAT_INPLACE_MATRIX, &B));
462       PetscCall(MatHeaderReplace(N, &B));
463     }
464   } else { /* use basic converter as fallback */
465     flg = (PetscBool)(N->ops->getrow != NULL);
466     PetscCall(MatConvert_Basic(N, newtype, reuse, newmat));
467   }
468   if (flg) {
469     PetscCall(MatScale(*newmat, vscale));
470     PetscCall(MatShift(*newmat, vshift));
471   }
472   PetscFunctionReturn(PETSC_SUCCESS);
473 }
474 
475 /*MC
476    MATHERMITIANTRANSPOSEVIRTUAL - "hermitiantranspose" - A matrix type that represents a virtual transpose of a matrix
477 
478   Level: advanced
479 
480   Developer Notes:
481   This is implemented on top of `MATSHELL` to get support for scaling and shifting without requiring duplicate code
482 
483   Users can not call `MatShellSetOperation()` operations on this class, there is some error checking for that incorrect usage
484 
485 .seealso: [](ch_matrices), `Mat`, `MATTRANSPOSEVIRTUAL`, `Mat`, `MatCreateHermitianTranspose()`, `MatCreateTranspose()`
486 M*/
487 
488 /*@
489   MatCreateHermitianTranspose - Creates a new matrix object of `MatType` `MATHERMITIANTRANSPOSEVIRTUAL` that behaves like A'*
490 
491   Collective
492 
493   Input Parameter:
494 . A - the (possibly rectangular) matrix
495 
496   Output Parameter:
497 . N - the matrix that represents A'*
498 
499   Level: intermediate
500 
501   Note:
502   The Hermitian transpose A' is NOT actually formed! Rather the new matrix
503   object performs the matrix-vector product, `MatMult()`, by using the `MatMultHermitianTranspose()` on
504   the original matrix
505 
506 .seealso: [](ch_matrices), `Mat`, `MatCreateNormal()`, `MatMult()`, `MatMultHermitianTranspose()`, `MatCreate()`,
507           `MATTRANSPOSEVIRTUAL`, `MatCreateTranspose()`, `MatHermitianTransposeGetMat()`, `MATNORMAL`, `MATNORMALHERMITIAN`
508 @*/
509 PetscErrorCode MatCreateHermitianTranspose(Mat A, Mat *N)
510 {
511   VecType vtype;
512 
513   PetscFunctionBegin;
514   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), N));
515   PetscCall(PetscLayoutReference(A->rmap, &((*N)->cmap)));
516   PetscCall(PetscLayoutReference(A->cmap, &((*N)->rmap)));
517   PetscCall(MatSetType(*N, MATSHELL));
518   PetscCall(MatShellSetContext(*N, A));
519   PetscCall(PetscObjectReference((PetscObject)A));
520 
521   PetscCall(MatSetBlockSizes(*N, PetscAbs(A->cmap->bs), PetscAbs(A->rmap->bs)));
522   PetscCall(MatGetVecType(A, &vtype));
523   PetscCall(MatSetVecType(*N, vtype));
524 #if defined(PETSC_HAVE_DEVICE)
525   PetscCall(MatBindToCPU(*N, A->boundtocpu));
526 #endif
527   PetscCall(MatSetUp(*N));
528 
529   PetscCall(MatShellSetOperation(*N, MATOP_DESTROY, (void (*)(void))MatDestroy_HT));
530   PetscCall(MatShellSetOperation(*N, MATOP_MULT, (void (*)(void))MatMult_HT));
531   PetscCall(MatShellSetOperation(*N, MATOP_MULT_HERMITIAN_TRANSPOSE, (void (*)(void))MatMultHermitianTranspose_HT));
532 #if !defined(PETSC_USE_COMPLEX)
533   PetscCall(MatShellSetOperation(*N, MATOP_MULT_TRANSPOSE, (void (*)(void))MatMultHermitianTranspose_HT));
534 #endif
535   PetscCall(MatShellSetOperation(*N, MATOP_LUFACTOR, (void (*)(void))MatLUFactor_HT));
536   PetscCall(MatShellSetOperation(*N, MATOP_CHOLESKYFACTOR, (void (*)(void))MatCholeskyFactor_HT));
537   PetscCall(MatShellSetOperation(*N, MATOP_GET_FACTOR, (void (*)(void))MatGetFactor_HT));
538   PetscCall(MatShellSetOperation(*N, MATOP_GETINFO, (void (*)(void))MatGetInfo_HT));
539   PetscCall(MatShellSetOperation(*N, MATOP_DUPLICATE, (void (*)(void))MatDuplicate_HT));
540   PetscCall(MatShellSetOperation(*N, MATOP_HAS_OPERATION, (void (*)(void))MatHasOperation_HT));
541   PetscCall(MatShellSetOperation(*N, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiagonal_HT));
542   PetscCall(MatShellSetOperation(*N, MATOP_COPY, (void (*)(void))MatCopy_HT));
543   PetscCall(MatShellSetOperation(*N, MATOP_CONVERT, (void (*)(void))MatConvert_HT));
544 
545   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatHermitianTransposeGetMat_C", MatHermitianTransposeGetMat_HT));
546 #if !defined(PETSC_USE_COMPLEX)
547   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatTransposeGetMat_C", MatHermitianTransposeGetMat_HT));
548 #endif
549   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatProductSetFromOptions_anytype_C", MatProductSetFromOptions_HT));
550   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatFactorGetSolverType_C", MatFactorGetSolverType_HT));
551   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContext_C", MatShellSetContext_Immutable));
552   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContextDestroy_C", MatShellSetContextDestroy_Immutable));
553   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetManageScalingShifts_C", MatShellSetManageScalingShifts_Immutable));
554   PetscCall(PetscObjectChangeTypeName((PetscObject)*N, MATHERMITIANTRANSPOSEVIRTUAL));
555   PetscFunctionReturn(PETSC_SUCCESS);
556 }
557