xref: /petsc/src/mat/interface/matrix.c (revision 557cca28306cbc0719cc09ad6b30cd4481f57bf1)
1 #define PETSCMAT_DLL
2 
3 /*
4    This is where the abstract matrix operations are defined
5 */
6 
7 #include "include/private/matimpl.h"        /*I "petscmat.h" I*/
8 #include "private/vecimpl.h"
9 
10 /* Logging support */
11 PetscCookie PETSCMAT_DLLEXPORT MAT_COOKIE;
12 PetscCookie PETSCMAT_DLLEXPORT MAT_FDCOLORING_COOKIE;
13 
14 PetscLogEvent  MAT_Mult, MAT_Mults, MAT_MultConstrained, MAT_MultAdd, MAT_MultTranspose;
15 PetscLogEvent  MAT_MultTransposeConstrained, MAT_MultTransposeAdd, MAT_Solve, MAT_Solves, MAT_SolveAdd, MAT_SolveTranspose, MAT_MatSolve;
16 PetscLogEvent  MAT_SolveTransposeAdd, MAT_Relax, MAT_ForwardSolve, MAT_BackwardSolve, MAT_LUFactor, MAT_LUFactorSymbolic;
17 PetscLogEvent  MAT_LUFactorNumeric, MAT_CholeskyFactor, MAT_CholeskyFactorSymbolic, MAT_CholeskyFactorNumeric, MAT_ILUFactor;
18 PetscLogEvent  MAT_ILUFactorSymbolic, MAT_ICCFactorSymbolic, MAT_Copy, MAT_Convert, MAT_Scale, MAT_AssemblyBegin;
19 PetscLogEvent  MAT_AssemblyEnd, MAT_SetValues, MAT_GetValues, MAT_GetRow, MAT_GetRowIJ, MAT_GetSubMatrices, MAT_GetColoring, MAT_GetOrdering, MAT_GetRedundantMatrix, MAT_GetSeqNonzeroStructure;
20 PetscLogEvent  MAT_IncreaseOverlap, MAT_Partitioning, MAT_ZeroEntries, MAT_Load, MAT_View, MAT_AXPY, MAT_FDColoringCreate;
21 PetscLogEvent  MAT_FDColoringApply,MAT_Transpose,MAT_FDColoringFunction;
22 PetscLogEvent  MAT_MatMult, MAT_MatMultSymbolic, MAT_MatMultNumeric;
23 PetscLogEvent  MAT_PtAP, MAT_PtAPSymbolic, MAT_PtAPNumeric;
24 PetscLogEvent  MAT_MatMultTranspose, MAT_MatMultTransposeSymbolic, MAT_MatMultTransposeNumeric;
25 PetscLogEvent  MAT_Getsymtranspose, MAT_Getsymtransreduced, MAT_Transpose_SeqAIJ, MAT_GetBrowsOfAcols;
26 PetscLogEvent  MAT_GetBrowsOfAocols, MAT_Getlocalmat, MAT_Getlocalmatcondensed, MAT_Seqstompi, MAT_Seqstompinum, MAT_Seqstompisym;
27 PetscLogEvent  MAT_Applypapt, MAT_Applypapt_numeric, MAT_Applypapt_symbolic, MAT_GetSequentialNonzeroStructure;
28 
29 /* nasty global values for MatSetValue() */
30 PetscInt    PETSCMAT_DLLEXPORT MatSetValue_Row = 0;
31 PetscInt    PETSCMAT_DLLEXPORT MatSetValue_Column = 0;
32 PetscScalar PETSCMAT_DLLEXPORT MatSetValue_Value = 0.0;
33 
34 #undef __FUNCT__
35 #define __FUNCT__ "MatRealPart"
36 /*@
37    MatRealPart - Zeros out the imaginary part of the matrix
38 
39    Collective on Mat
40 
41    Input Parameters:
42 .  mat - the matrix
43 
44    Level: advanced
45 
46 
47 .seealso: MatImaginaryPart()
48 @*/
49 
50 PetscErrorCode PETSCMAT_DLLEXPORT MatRealPart(Mat mat)
51 {
52   PetscErrorCode ierr;
53 
54   PetscFunctionBegin;
55   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
56   PetscValidType(mat,1);
57   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
58   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
59   if (!mat->ops->realpart) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
60   ierr = MatPreallocated(mat);CHKERRQ(ierr);
61   ierr = (*mat->ops->realpart)(mat);CHKERRQ(ierr);
62   PetscFunctionReturn(0);
63 }
64 
65 
66 #undef __FUNCT__
67 #define __FUNCT__ "MatImaginaryPart"
68 /*@
69    MatImaginaryPart - Moves the imaginary part of the matrix to the real part and zeros the imaginary part
70 
71    Collective on Mat
72 
73    Input Parameters:
74 .  mat - the matrix
75 
76    Level: advanced
77 
78 
79 .seealso: MatRealPart()
80 @*/
81 
82 PetscErrorCode PETSCMAT_DLLEXPORT MatImaginaryPart(Mat mat)
83 {
84   PetscErrorCode ierr;
85 
86   PetscFunctionBegin;
87   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
88   PetscValidType(mat,1);
89   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
90   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
91   if (!mat->ops->imaginarypart) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
92   ierr = MatPreallocated(mat);CHKERRQ(ierr);
93   ierr = (*mat->ops->imaginarypart)(mat);CHKERRQ(ierr);
94   PetscFunctionReturn(0);
95 }
96 
97 #undef __FUNCT__
98 #define __FUNCT__ "MatMissingDiagonal"
99 /*@
100    MatMissingDiagonal - Determine if sparse matrix is missing a diagonal entry (or block entry for BAIJ matrices)
101 
102    Collective on Mat
103 
104    Input Parameter:
105 .  mat - the matrix
106 
107    Output Parameters:
108 +  missing - is any diagonal missing
109 -  dd - first diagonal entry that is missing (optional)
110 
111    Level: advanced
112 
113 
114 .seealso: MatRealPart()
115 @*/
116 
117 PetscErrorCode PETSCMAT_DLLEXPORT MatMissingDiagonal(Mat mat,PetscTruth *missing,PetscInt *dd)
118 {
119   PetscErrorCode ierr;
120 
121   PetscFunctionBegin;
122   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
123   PetscValidType(mat,1);
124   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
125   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
126   if (!mat->ops->missingdiagonal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
127   ierr = (*mat->ops->missingdiagonal)(mat,missing,dd);CHKERRQ(ierr);
128   PetscFunctionReturn(0);
129 }
130 
131 #undef __FUNCT__
132 #define __FUNCT__ "MatGetRow"
133 /*@C
134    MatGetRow - Gets a row of a matrix.  You MUST call MatRestoreRow()
135    for each row that you get to ensure that your application does
136    not bleed memory.
137 
138    Not Collective
139 
140    Input Parameters:
141 +  mat - the matrix
142 -  row - the row to get
143 
144    Output Parameters:
145 +  ncols -  if not NULL, the number of nonzeros in the row
146 .  cols - if not NULL, the column numbers
147 -  vals - if not NULL, the values
148 
149    Notes:
150    This routine is provided for people who need to have direct access
151    to the structure of a matrix.  We hope that we provide enough
152    high-level matrix routines that few users will need it.
153 
154    MatGetRow() always returns 0-based column indices, regardless of
155    whether the internal representation is 0-based (default) or 1-based.
156 
157    For better efficiency, set cols and/or vals to PETSC_NULL if you do
158    not wish to extract these quantities.
159 
160    The user can only examine the values extracted with MatGetRow();
161    the values cannot be altered.  To change the matrix entries, one
162    must use MatSetValues().
163 
164    You can only have one call to MatGetRow() outstanding for a particular
165    matrix at a time, per processor. MatGetRow() can only obtain rows
166    associated with the given processor, it cannot get rows from the
167    other processors; for that we suggest using MatGetSubMatrices(), then
168    MatGetRow() on the submatrix. The row indix passed to MatGetRows()
169    is in the global number of rows.
170 
171    Fortran Notes:
172    The calling sequence from Fortran is
173 .vb
174    MatGetRow(matrix,row,ncols,cols,values,ierr)
175          Mat     matrix (input)
176          integer row    (input)
177          integer ncols  (output)
178          integer cols(maxcols) (output)
179          double precision (or double complex) values(maxcols) output
180 .ve
181    where maxcols >= maximum nonzeros in any row of the matrix.
182 
183 
184    Caution:
185    Do not try to change the contents of the output arrays (cols and vals).
186    In some cases, this may corrupt the matrix.
187 
188    Level: advanced
189 
190    Concepts: matrices^row access
191 
192 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatGetSubMatrices(), MatGetDiagonal()
193 @*/
194 
195 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[])
196 {
197   PetscErrorCode ierr;
198   PetscInt       incols;
199 
200   PetscFunctionBegin;
201   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
202   PetscValidType(mat,1);
203   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
204   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
205   if (!mat->ops->getrow) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
206   ierr = MatPreallocated(mat);CHKERRQ(ierr);
207   ierr = PetscLogEventBegin(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr);
208   ierr = (*mat->ops->getrow)(mat,row,&incols,(PetscInt **)cols,(PetscScalar **)vals);CHKERRQ(ierr);
209   if (ncols) *ncols = incols;
210   ierr = PetscLogEventEnd(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr);
211   PetscFunctionReturn(0);
212 }
213 
214 #undef __FUNCT__
215 #define __FUNCT__ "MatConjugate"
216 /*@
217    MatConjugate - replaces the matrix values with their complex conjugates
218 
219    Collective on Mat
220 
221    Input Parameters:
222 .  mat - the matrix
223 
224    Level: advanced
225 
226 .seealso:  VecConjugate()
227 @*/
228 PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate(Mat mat)
229 {
230   PetscErrorCode ierr;
231 
232   PetscFunctionBegin;
233   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
234   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
235   if (!mat->ops->conjugate) SETERRQ(PETSC_ERR_SUP,"Not provided for this matrix format, send email to petsc-maint@mcs.anl.gov");
236   ierr = (*mat->ops->conjugate)(mat);CHKERRQ(ierr);
237   PetscFunctionReturn(0);
238 }
239 
240 #undef __FUNCT__
241 #define __FUNCT__ "MatRestoreRow"
242 /*@C
243    MatRestoreRow - Frees any temporary space allocated by MatGetRow().
244 
245    Not Collective
246 
247    Input Parameters:
248 +  mat - the matrix
249 .  row - the row to get
250 .  ncols, cols - the number of nonzeros and their columns
251 -  vals - if nonzero the column values
252 
253    Notes:
254    This routine should be called after you have finished examining the entries.
255 
256    Fortran Notes:
257    The calling sequence from Fortran is
258 .vb
259    MatRestoreRow(matrix,row,ncols,cols,values,ierr)
260       Mat     matrix (input)
261       integer row    (input)
262       integer ncols  (output)
263       integer cols(maxcols) (output)
264       double precision (or double complex) values(maxcols) output
265 .ve
266    Where maxcols >= maximum nonzeros in any row of the matrix.
267 
268    In Fortran MatRestoreRow() MUST be called after MatGetRow()
269    before another call to MatGetRow() can be made.
270 
271    Level: advanced
272 
273 .seealso:  MatGetRow()
274 @*/
275 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[])
276 {
277   PetscErrorCode ierr;
278 
279   PetscFunctionBegin;
280   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
281   PetscValidIntPointer(ncols,3);
282   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
283   if (!mat->ops->restorerow) PetscFunctionReturn(0);
284   ierr = (*mat->ops->restorerow)(mat,row,ncols,(PetscInt **)cols,(PetscScalar **)vals);CHKERRQ(ierr);
285   PetscFunctionReturn(0);
286 }
287 
288 #undef __FUNCT__
289 #define __FUNCT__ "MatGetRowUpperTriangular"
290 /*@
291    MatGetRowUpperTriangular - Sets a flag to enable calls to MatGetRow() for matrix in MATSBAIJ format.
292    You should call MatRestoreRowUpperTriangular() after calling MatGetRow/MatRestoreRow() to disable the flag.
293 
294    Not Collective
295 
296    Input Parameters:
297 +  mat - the matrix
298 
299    Notes:
300    The flag is to ensure that users are aware of MatGetRow() only provides the upper trianglular part of the row for the matrices in MATSBAIJ format.
301 
302    Level: advanced
303 
304    Concepts: matrices^row access
305 
306 .seealso: MatRestoreRowRowUpperTriangular()
307 @*/
308 
309 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowUpperTriangular(Mat mat)
310 {
311   PetscErrorCode ierr;
312 
313   PetscFunctionBegin;
314   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
315   PetscValidType(mat,1);
316   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
317   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
318   if (!mat->ops->getrowuppertriangular) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
319   ierr = MatPreallocated(mat);CHKERRQ(ierr);
320   ierr = (*mat->ops->getrowuppertriangular)(mat);CHKERRQ(ierr);
321   PetscFunctionReturn(0);
322 }
323 
324 #undef __FUNCT__
325 #define __FUNCT__ "MatRestoreRowUpperTriangular"
326 /*@
327    MatRestoreRowUpperTriangular - Disable calls to MatGetRow() for matrix in MATSBAIJ format.
328 
329    Not Collective
330 
331    Input Parameters:
332 +  mat - the matrix
333 
334    Notes:
335    This routine should be called after you have finished MatGetRow/MatRestoreRow().
336 
337 
338    Level: advanced
339 
340 .seealso:  MatGetRowUpperTriangular()
341 @*/
342 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreRowUpperTriangular(Mat mat)
343 {
344   PetscErrorCode ierr;
345 
346   PetscFunctionBegin;
347   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
348   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
349   if (!mat->ops->restorerowuppertriangular) PetscFunctionReturn(0);
350   ierr = (*mat->ops->restorerowuppertriangular)(mat);CHKERRQ(ierr);
351   PetscFunctionReturn(0);
352 }
353 
354 #undef __FUNCT__
355 #define __FUNCT__ "MatSetOptionsPrefix"
356 /*@C
357    MatSetOptionsPrefix - Sets the prefix used for searching for all
358    Mat options in the database.
359 
360    Collective on Mat
361 
362    Input Parameter:
363 +  A - the Mat context
364 -  prefix - the prefix to prepend to all option names
365 
366    Notes:
367    A hyphen (-) must NOT be given at the beginning of the prefix name.
368    The first character of all runtime options is AUTOMATICALLY the hyphen.
369 
370    Level: advanced
371 
372 .keywords: Mat, set, options, prefix, database
373 
374 .seealso: MatSetFromOptions()
375 @*/
376 PetscErrorCode PETSCMAT_DLLEXPORT MatSetOptionsPrefix(Mat A,const char prefix[])
377 {
378   PetscErrorCode ierr;
379 
380   PetscFunctionBegin;
381   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
382   ierr = PetscObjectSetOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr);
383   PetscFunctionReturn(0);
384 }
385 
386 #undef __FUNCT__
387 #define __FUNCT__ "MatAppendOptionsPrefix"
388 /*@C
389    MatAppendOptionsPrefix - Appends to the prefix used for searching for all
390    Mat options in the database.
391 
392    Collective on Mat
393 
394    Input Parameters:
395 +  A - the Mat context
396 -  prefix - the prefix to prepend to all option names
397 
398    Notes:
399    A hyphen (-) must NOT be given at the beginning of the prefix name.
400    The first character of all runtime options is AUTOMATICALLY the hyphen.
401 
402    Level: advanced
403 
404 .keywords: Mat, append, options, prefix, database
405 
406 .seealso: MatGetOptionsPrefix()
407 @*/
408 PetscErrorCode PETSCMAT_DLLEXPORT MatAppendOptionsPrefix(Mat A,const char prefix[])
409 {
410   PetscErrorCode ierr;
411 
412   PetscFunctionBegin;
413   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
414   ierr = PetscObjectAppendOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr);
415   PetscFunctionReturn(0);
416 }
417 
418 #undef __FUNCT__
419 #define __FUNCT__ "MatGetOptionsPrefix"
420 /*@C
421    MatGetOptionsPrefix - Sets the prefix used for searching for all
422    Mat options in the database.
423 
424    Not Collective
425 
426    Input Parameter:
427 .  A - the Mat context
428 
429    Output Parameter:
430 .  prefix - pointer to the prefix string used
431 
432    Notes: On the fortran side, the user should pass in a string 'prefix' of
433    sufficient length to hold the prefix.
434 
435    Level: advanced
436 
437 .keywords: Mat, get, options, prefix, database
438 
439 .seealso: MatAppendOptionsPrefix()
440 @*/
441 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOptionsPrefix(Mat A,const char *prefix[])
442 {
443   PetscErrorCode ierr;
444 
445   PetscFunctionBegin;
446   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
447   ierr = PetscObjectGetOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr);
448   PetscFunctionReturn(0);
449 }
450 
451 #undef __FUNCT__
452 #define __FUNCT__ "MatSetUp"
453 /*@
454    MatSetUp - Sets up the internal matrix data structures for the later use.
455 
456    Collective on Mat
457 
458    Input Parameters:
459 .  A - the Mat context
460 
461    Notes:
462    For basic use of the Mat classes the user need not explicitly call
463    MatSetUp(), since these actions will happen automatically.
464 
465    Level: advanced
466 
467 .keywords: Mat, setup
468 
469 .seealso: MatCreate(), MatDestroy()
470 @*/
471 PetscErrorCode PETSCMAT_DLLEXPORT MatSetUp(Mat A)
472 {
473   PetscMPIInt    size;
474   PetscErrorCode ierr;
475 
476   PetscFunctionBegin;
477   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
478   if (!((PetscObject)A)->type_name) {
479     ierr = MPI_Comm_size(((PetscObject)A)->comm, &size);CHKERRQ(ierr);
480     if (size == 1) {
481       ierr = MatSetType(A, MATSEQAIJ);CHKERRQ(ierr);
482     } else {
483       ierr = MatSetType(A, MATMPIAIJ);CHKERRQ(ierr);
484     }
485   }
486   ierr = MatSetUpPreallocation(A);CHKERRQ(ierr);
487   PetscFunctionReturn(0);
488 }
489 
490 #undef __FUNCT__
491 #define __FUNCT__ "MatView"
492 /*@C
493    MatView - Visualizes a matrix object.
494 
495    Collective on Mat
496 
497    Input Parameters:
498 +  mat - the matrix
499 -  viewer - visualization context
500 
501   Notes:
502   The available visualization contexts include
503 +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
504 .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
505         output where only the first processor opens
506         the file.  All other processors send their
507         data to the first processor to print.
508 -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
509 
510    The user can open alternative visualization contexts with
511 +    PetscViewerASCIIOpen() - Outputs matrix to a specified file
512 .    PetscViewerBinaryOpen() - Outputs matrix in binary to a
513          specified file; corresponding input uses MatLoad()
514 .    PetscViewerDrawOpen() - Outputs nonzero matrix structure to
515          an X window display
516 -    PetscViewerSocketOpen() - Outputs matrix to Socket viewer.
517          Currently only the sequential dense and AIJ
518          matrix types support the Socket viewer.
519 
520    The user can call PetscViewerSetFormat() to specify the output
521    format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF,
522    PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen).  Available formats include
523 +    PETSC_VIEWER_ASCII_DEFAULT - default, prints matrix contents
524 .    PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format
525 .    PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros
526 .    PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse
527          format common among all matrix types
528 .    PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific
529          format (which is in many cases the same as the default)
530 .    PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix
531          size and structure (not the matrix entries)
532 .    PETSC_VIEWER_ASCII_INFO_DETAIL - prints more detailed information about
533          the matrix structure
534 
535    Options Database Keys:
536 +  -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly()
537 .  -mat_view_info_detailed - Prints more detailed info
538 .  -mat_view - Prints matrix in ASCII format
539 .  -mat_view_matlab - Prints matrix in Matlab format
540 .  -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
541 .  -display <name> - Sets display name (default is host)
542 .  -draw_pause <sec> - Sets number of seconds to pause after display
543 .  -mat_view_socket - Sends matrix to socket, can be accessed from Matlab (see users manual)
544 .  -viewer_socket_machine <machine>
545 .  -viewer_socket_port <port>
546 .  -mat_view_binary - save matrix to file in binary format
547 -  -viewer_binary_filename <name>
548    Level: beginner
549 
550    Notes: see the manual page for MatLoad() for the exact format of the binary file when the binary
551       viewer is used.
552 
553       See bin/matlab/PetscBinaryRead.m for a Matlab code that can read in the binary file when the binary
554       viewer is used.
555 
556    Concepts: matrices^viewing
557    Concepts: matrices^plotting
558    Concepts: matrices^printing
559 
560 .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
561           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad()
562 @*/
563 PetscErrorCode PETSCMAT_DLLEXPORT MatView(Mat mat,PetscViewer viewer)
564 {
565   PetscErrorCode    ierr;
566   PetscInt          rows,cols;
567   PetscTruth        iascii;
568   const MatType     cstr;
569   PetscViewerFormat format;
570 
571   PetscFunctionBegin;
572   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
573   PetscValidType(mat,1);
574   if (!viewer) {
575     ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr);
576   }
577   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
578   PetscCheckSameComm(mat,1,viewer,2);
579   if (!mat->assembled) SETERRQ(PETSC_ERR_ORDER,"Must call MatAssemblyBegin/End() before viewing matrix");
580   ierr = MatPreallocated(mat);CHKERRQ(ierr);
581 
582   ierr = PetscLogEventBegin(MAT_View,mat,viewer,0,0);CHKERRQ(ierr);
583   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
584   if (iascii) {
585     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
586     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
587       if (((PetscObject)mat)->prefix) {
588         ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:(%s)\n",((PetscObject)mat)->prefix);CHKERRQ(ierr);
589       } else {
590         ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:\n");CHKERRQ(ierr);
591       }
592       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
593       ierr = MatGetType(mat,&cstr);CHKERRQ(ierr);
594       ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr);
595       ierr = PetscViewerASCIIPrintf(viewer,"type=%s, rows=%D, cols=%D\n",cstr,rows,cols);CHKERRQ(ierr);
596       if (mat->ops->getinfo) {
597         MatInfo info;
598         ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr);
599         ierr = PetscViewerASCIIPrintf(viewer,"total: nonzeros=%D, allocated nonzeros=%D\n",
600                           (PetscInt)info.nz_used,(PetscInt)info.nz_allocated);CHKERRQ(ierr);
601       }
602     }
603   }
604   if (mat->ops->view) {
605     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
606     ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr);
607     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
608   } else if (!iascii) {
609     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported",((PetscObject)viewer)->type_name);
610   }
611   if (iascii) {
612     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
613     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
614       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
615     }
616   }
617   ierr = PetscLogEventEnd(MAT_View,mat,viewer,0,0);CHKERRQ(ierr);
618   PetscFunctionReturn(0);
619 }
620 
621 #undef __FUNCT__
622 #define __FUNCT__ "MatScaleSystem"
623 /*@
624    MatScaleSystem - Scale a vector solution and right hand side to
625    match the scaling of a scaled matrix.
626 
627    Collective on Mat
628 
629    Input Parameter:
630 +  mat - the matrix
631 .  b - right hand side vector (or PETSC_NULL)
632 -  x - solution vector (or PETSC_NULL)
633 
634 
635    Notes:
636    For AIJ, and BAIJ matrix formats, the matrices are not
637    internally scaled, so this does nothing. For MPIROWBS it
638    permutes and diagonally scales.
639 
640    The KSP methods automatically call this routine when required
641    (via PCPreSolve()) so it is rarely used directly.
642 
643    Level: Developer
644 
645    Concepts: matrices^scaling
646 
647 .seealso: MatUseScaledForm(), MatUnScaleSystem()
648 @*/
649 PetscErrorCode PETSCMAT_DLLEXPORT MatScaleSystem(Mat mat,Vec b,Vec x)
650 {
651   PetscErrorCode ierr;
652 
653   PetscFunctionBegin;
654   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
655   PetscValidType(mat,1);
656   ierr = MatPreallocated(mat);CHKERRQ(ierr);
657   if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE,2);PetscCheckSameComm(mat,1,x,2);}
658   if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE,3);PetscCheckSameComm(mat,1,b,3);}
659 
660   if (mat->ops->scalesystem) {
661     ierr = (*mat->ops->scalesystem)(mat,b,x);CHKERRQ(ierr);
662   }
663   PetscFunctionReturn(0);
664 }
665 
666 #undef __FUNCT__
667 #define __FUNCT__ "MatUnScaleSystem"
668 /*@
669    MatUnScaleSystem - Unscales a vector solution and right hand side to
670    match the original scaling of a scaled matrix.
671 
672    Collective on Mat
673 
674    Input Parameter:
675 +  mat - the matrix
676 .  b - right hand side vector (or PETSC_NULL)
677 -  x - solution vector (or PETSC_NULL)
678 
679 
680    Notes:
681    For AIJ and BAIJ matrix formats, the matrices are not
682    internally scaled, so this does nothing. For MPIROWBS it
683    permutes and diagonally scales.
684 
685    The KSP methods automatically call this routine when required
686    (via PCPreSolve()) so it is rarely used directly.
687 
688    Level: Developer
689 
690 .seealso: MatUseScaledForm(), MatScaleSystem()
691 @*/
692 PetscErrorCode PETSCMAT_DLLEXPORT MatUnScaleSystem(Mat mat,Vec b,Vec x)
693 {
694   PetscErrorCode ierr;
695 
696   PetscFunctionBegin;
697   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
698   PetscValidType(mat,1);
699   ierr = MatPreallocated(mat);CHKERRQ(ierr);
700   if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE,2);PetscCheckSameComm(mat,1,x,2);}
701   if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE,3);PetscCheckSameComm(mat,1,b,3);}
702   if (mat->ops->unscalesystem) {
703     ierr = (*mat->ops->unscalesystem)(mat,b,x);CHKERRQ(ierr);
704   }
705   PetscFunctionReturn(0);
706 }
707 
708 #undef __FUNCT__
709 #define __FUNCT__ "MatUseScaledForm"
710 /*@
711    MatUseScaledForm - For matrix storage formats that scale the
712    matrix (for example MPIRowBS matrices are diagonally scaled on
713    assembly) indicates matrix operations (MatMult() etc) are
714    applied using the scaled matrix.
715 
716    Collective on Mat
717 
718    Input Parameter:
719 +  mat - the matrix
720 -  scaled - PETSC_TRUE for applying the scaled, PETSC_FALSE for
721             applying the original matrix
722 
723    Notes:
724    For scaled matrix formats, applying the original, unscaled matrix
725    will be slightly more expensive
726 
727    Level: Developer
728 
729 .seealso: MatScaleSystem(), MatUnScaleSystem()
730 @*/
731 PetscErrorCode PETSCMAT_DLLEXPORT MatUseScaledForm(Mat mat,PetscTruth scaled)
732 {
733   PetscErrorCode ierr;
734 
735   PetscFunctionBegin;
736   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
737   PetscValidType(mat,1);
738   ierr = MatPreallocated(mat);CHKERRQ(ierr);
739   if (mat->ops->usescaledform) {
740     ierr = (*mat->ops->usescaledform)(mat,scaled);CHKERRQ(ierr);
741   }
742   PetscFunctionReturn(0);
743 }
744 
745 #undef __FUNCT__
746 #define __FUNCT__ "MatDestroy"
747 /*@
748    MatDestroy - Frees space taken by a matrix.
749 
750    Collective on Mat
751 
752    Input Parameter:
753 .  A - the matrix
754 
755    Level: beginner
756 
757 @*/
758 PetscErrorCode PETSCMAT_DLLEXPORT MatDestroy(Mat A)
759 {
760   PetscErrorCode ierr;
761   PetscFunctionBegin;
762   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
763   if (--((PetscObject)A)->refct > 0) PetscFunctionReturn(0);
764   ierr = MatPreallocated(A);CHKERRQ(ierr);
765   /* if memory was published with AMS then destroy it */
766   ierr = PetscObjectDepublish(A);CHKERRQ(ierr);
767   if (A->ops->destroy) {
768     ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
769   }
770   if (A->mapping) {
771     ierr = ISLocalToGlobalMappingDestroy(A->mapping);CHKERRQ(ierr);
772   }
773   if (A->bmapping) {
774     ierr = ISLocalToGlobalMappingDestroy(A->bmapping);CHKERRQ(ierr);
775   }
776 
777   if (A->spptr){ierr = PetscFree(A->spptr);CHKERRQ(ierr);}
778   ierr = PetscMapDestroy(A->rmap);CHKERRQ(ierr);
779   ierr = PetscMapDestroy(A->cmap);CHKERRQ(ierr);
780   ierr = PetscHeaderDestroy(A);CHKERRQ(ierr);
781   PetscFunctionReturn(0);
782 }
783 
784 #undef __FUNCT__
785 #define __FUNCT__ "MatValid"
786 /*@
787    MatValid - Checks whether a matrix object is valid.
788 
789    Collective on Mat
790 
791    Input Parameter:
792 .  m - the matrix to check
793 
794    Output Parameter:
795    flg - flag indicating matrix status, either
796    PETSC_TRUE if matrix is valid, or PETSC_FALSE otherwise.
797 
798    Level: developer
799 
800    Concepts: matrices^validity
801 @*/
802 PetscErrorCode PETSCMAT_DLLEXPORT MatValid(Mat m,PetscTruth *flg)
803 {
804   PetscFunctionBegin;
805   PetscValidIntPointer(flg,1);
806   if (!m)                                          *flg = PETSC_FALSE;
807   else if (((PetscObject)m)->cookie != MAT_COOKIE) *flg = PETSC_FALSE;
808   else                                             *flg = PETSC_TRUE;
809   PetscFunctionReturn(0);
810 }
811 
812 #undef __FUNCT__
813 #define __FUNCT__ "MatSetValues"
814 /*@
815    MatSetValues - Inserts or adds a block of values into a matrix.
816    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
817    MUST be called after all calls to MatSetValues() have been completed.
818 
819    Not Collective
820 
821    Input Parameters:
822 +  mat - the matrix
823 .  v - a logically two-dimensional array of values
824 .  m, idxm - the number of rows and their global indices
825 .  n, idxn - the number of columns and their global indices
826 -  addv - either ADD_VALUES or INSERT_VALUES, where
827    ADD_VALUES adds values to any existing entries, and
828    INSERT_VALUES replaces existing entries with new values
829 
830    Notes:
831    By default the values, v, are row-oriented and unsorted.
832    See MatSetOption() for other options.
833 
834    Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES
835    options cannot be mixed without intervening calls to the assembly
836    routines.
837 
838    MatSetValues() uses 0-based row and column numbers in Fortran
839    as well as in C.
840 
841    Negative indices may be passed in idxm and idxn, these rows and columns are
842    simply ignored. This allows easily inserting element stiffness matrices
843    with homogeneous Dirchlet boundary conditions that you don't want represented
844    in the matrix.
845 
846    Efficiency Alert:
847    The routine MatSetValuesBlocked() may offer much better efficiency
848    for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ).
849 
850    Level: beginner
851 
852    Concepts: matrices^putting entries in
853 
854 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
855           InsertMode, INSERT_VALUES, ADD_VALUES
856 @*/
857 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv)
858 {
859   PetscErrorCode ierr;
860 
861   PetscFunctionBegin;
862   if (!m || !n) PetscFunctionReturn(0); /* no values to insert */
863   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
864   PetscValidType(mat,1);
865   PetscValidIntPointer(idxm,3);
866   PetscValidIntPointer(idxn,5);
867   ierr = MatPreallocated(mat);CHKERRQ(ierr);
868   if (mat->insertmode == NOT_SET_VALUES) {
869     mat->insertmode = addv;
870   }
871 #if defined(PETSC_USE_DEBUG)
872   else if (mat->insertmode != addv) {
873     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
874   }
875   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
876 #endif
877 
878   if (mat->assembled) {
879     mat->was_assembled = PETSC_TRUE;
880     mat->assembled     = PETSC_FALSE;
881   }
882   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
883   if (!mat->ops->setvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
884   ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr);
885   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
886   PetscFunctionReturn(0);
887 }
888 
889 
890 #undef __FUNCT__
891 #define __FUNCT__ "MatSetValuesRowLocal"
892 /*@
893    MatSetValuesRowLocal - Inserts a row (block row for BAIJ matrices) of nonzero
894         values into a matrix
895 
896    Not Collective
897 
898    Input Parameters:
899 +  mat - the matrix
900 .  row - the (block) row to set
901 -  v - a logically two-dimensional array of values
902 
903    Notes:
904    By the values, v, are column-oriented (for the block version) and sorted
905 
906    All the nonzeros in the row must be provided
907 
908    The matrix must have previously had its column indices set
909 
910    The row must belong to this process
911 
912    Level: intermediate
913 
914    Concepts: matrices^putting entries in
915 
916 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
917           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues(), MatSetValuesRow(), MatSetLocalToGlobalMapping()
918 @*/
919 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesRowLocal(Mat mat,PetscInt row,const PetscScalar v[])
920 {
921   PetscErrorCode ierr;
922 
923   PetscFunctionBegin;
924   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
925   PetscValidType(mat,1);
926   PetscValidScalarPointer(v,2);
927   ierr = MatSetValuesRow(mat, mat->mapping->indices[row],v);CHKERRQ(ierr);
928   PetscFunctionReturn(0);
929 }
930 
931 #undef __FUNCT__
932 #define __FUNCT__ "MatSetValuesRow"
933 /*@
934    MatSetValuesRow - Inserts a row (block row for BAIJ matrices) of nonzero
935         values into a matrix
936 
937    Not Collective
938 
939    Input Parameters:
940 +  mat - the matrix
941 .  row - the (block) row to set
942 -  v - a logically two-dimensional array of values
943 
944    Notes:
945    By the values, v, are column-oriented (for the block version) and sorted
946 
947    All the nonzeros in the row must be provided
948 
949    The matrix must have previously had its column indices set
950 
951    The row must belong to this process
952 
953    Level: intermediate
954 
955    Concepts: matrices^putting entries in
956 
957 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
958           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues()
959 @*/
960 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesRow(Mat mat,PetscInt row,const PetscScalar v[])
961 {
962   PetscErrorCode ierr;
963 
964   PetscFunctionBegin;
965   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
966   PetscValidType(mat,1);
967   PetscValidScalarPointer(v,2);
968 #if defined(PETSC_USE_DEBUG)
969   if (mat->insertmode == ADD_VALUES) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add and insert values");
970   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
971 #endif
972   mat->insertmode = INSERT_VALUES;
973 
974   if (mat->assembled) {
975     mat->was_assembled = PETSC_TRUE;
976     mat->assembled     = PETSC_FALSE;
977   }
978   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
979   if (!mat->ops->setvaluesrow) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
980   ierr = (*mat->ops->setvaluesrow)(mat,row,v);CHKERRQ(ierr);
981   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
982   PetscFunctionReturn(0);
983 }
984 
985 #undef __FUNCT__
986 #define __FUNCT__ "MatSetValuesStencil"
987 /*@
988    MatSetValuesStencil - Inserts or adds a block of values into a matrix.
989      Using structured grid indexing
990 
991    Not Collective
992 
993    Input Parameters:
994 +  mat - the matrix
995 .  v - a logically two-dimensional array of values
996 .  m - number of rows being entered
997 .  idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered
998 .  n - number of columns being entered
999 .  idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered
1000 -  addv - either ADD_VALUES or INSERT_VALUES, where
1001    ADD_VALUES adds values to any existing entries, and
1002    INSERT_VALUES replaces existing entries with new values
1003 
1004    Notes:
1005    By default the values, v, are row-oriented and unsorted.
1006    See MatSetOption() for other options.
1007 
1008    Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES
1009    options cannot be mixed without intervening calls to the assembly
1010    routines.
1011 
1012    The grid coordinates are across the entire grid, not just the local portion
1013 
1014    MatSetValuesStencil() uses 0-based row and column numbers in Fortran
1015    as well as in C.
1016 
1017    For setting/accessing vector values via array coordinates you can use the DAVecGetArray() routine
1018 
1019    In order to use this routine you must either obtain the matrix with DAGetMatrix()
1020    or call MatSetLocalToGlobalMapping() and MatSetStencil() first.
1021 
1022    The columns and rows in the stencil passed in MUST be contained within the
1023    ghost region of the given process as set with DACreateXXX() or MatSetStencil(). For example,
1024    if you create a DA with an overlap of one grid level and on a particular process its first
1025    local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the
1026    first i index you can use in your column and row indices in MatSetStencil() is 5.
1027 
1028    In Fortran idxm and idxn should be declared as
1029 $     MatStencil idxm(4,m),idxn(4,n)
1030    and the values inserted using
1031 $    idxm(MatStencil_i,1) = i
1032 $    idxm(MatStencil_j,1) = j
1033 $    idxm(MatStencil_k,1) = k
1034 $    idxm(MatStencil_c,1) = c
1035    etc
1036 
1037    For periodic boundary conditions use negative indices for values to the left (below 0; that are to be
1038    obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one
1039    etc to obtain values that obtained by wrapping the values from the left edge.
1040 
1041    For indices that don't mean anything for your case (like the k index when working in 2d) or the c index when you have
1042    a single value per point) you can skip filling those indices.
1043 
1044    Inspired by the structured grid interface to the HYPRE package
1045    (http://www.llnl.gov/CASC/hypre)
1046 
1047    Efficiency Alert:
1048    The routine MatSetValuesBlockedStencil() may offer much better efficiency
1049    for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ).
1050 
1051    Level: beginner
1052 
1053    Concepts: matrices^putting entries in
1054 
1055 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1056           MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil(), DAGetMatrix(), DAVecGetArray(), MatStencil
1057 @*/
1058 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv)
1059 {
1060   PetscErrorCode ierr;
1061   PetscInt       j,i,jdxm[128],jdxn[256],dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp;
1062   PetscInt       *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc);
1063 
1064   PetscFunctionBegin;
1065   if (!m || !n) PetscFunctionReturn(0); /* no values to insert */
1066   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1067   PetscValidType(mat,1);
1068   PetscValidIntPointer(idxm,3);
1069   PetscValidIntPointer(idxn,5);
1070   PetscValidScalarPointer(v,6);
1071 
1072   if (m > 128) SETERRQ1(PETSC_ERR_SUP,"Can only set 128 rows at a time; trying to set %D",m);
1073   if (n > 256) SETERRQ1(PETSC_ERR_SUP,"Can only set 256 columns at a time; trying to set %D",n);
1074 
1075   for (i=0; i<m; i++) {
1076     for (j=0; j<3-sdim; j++) dxm++;
1077     tmp = *dxm++ - starts[0];
1078     for (j=0; j<dim-1; j++) {
1079       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
1080       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
1081     }
1082     if (mat->stencil.noc) dxm++;
1083     jdxm[i] = tmp;
1084   }
1085   for (i=0; i<n; i++) {
1086     for (j=0; j<3-sdim; j++) dxn++;
1087     tmp = *dxn++ - starts[0];
1088     for (j=0; j<dim-1; j++) {
1089       if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
1090       else                                       tmp = tmp*dims[j] + *(dxn-1) - starts[j+1];
1091     }
1092     if (mat->stencil.noc) dxn++;
1093     jdxn[i] = tmp;
1094   }
1095   ierr = MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr);
1096   PetscFunctionReturn(0);
1097 }
1098 
1099 #undef __FUNCT__
1100 #define __FUNCT__ "MatSetValuesBlockedStencil"
1101 /*@C
1102    MatSetValuesBlockedStencil - Inserts or adds a block of values into a matrix.
1103      Using structured grid indexing
1104 
1105    Not Collective
1106 
1107    Input Parameters:
1108 +  mat - the matrix
1109 .  v - a logically two-dimensional array of values
1110 .  m - number of rows being entered
1111 .  idxm - grid coordinates for matrix rows being entered
1112 .  n - number of columns being entered
1113 .  idxn - grid coordinates for matrix columns being entered
1114 -  addv - either ADD_VALUES or INSERT_VALUES, where
1115    ADD_VALUES adds values to any existing entries, and
1116    INSERT_VALUES replaces existing entries with new values
1117 
1118    Notes:
1119    By default the values, v, are row-oriented and unsorted.
1120    See MatSetOption() for other options.
1121 
1122    Calls to MatSetValuesBlockedStencil() with the INSERT_VALUES and ADD_VALUES
1123    options cannot be mixed without intervening calls to the assembly
1124    routines.
1125 
1126    The grid coordinates are across the entire grid, not just the local portion
1127 
1128    MatSetValuesBlockedStencil() uses 0-based row and column numbers in Fortran
1129    as well as in C.
1130 
1131    For setting/accessing vector values via array coordinates you can use the DAVecGetArray() routine
1132 
1133    In order to use this routine you must either obtain the matrix with DAGetMatrix()
1134    or call MatSetLocalToGlobalMapping() and MatSetStencil() first.
1135 
1136    The columns and rows in the stencil passed in MUST be contained within the
1137    ghost region of the given process as set with DACreateXXX() or MatSetStencil(). For example,
1138    if you create a DA with an overlap of one grid level and on a particular process its first
1139    local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the
1140    first i index you can use in your column and row indices in MatSetStencil() is 5.
1141 
1142    In Fortran idxm and idxn should be declared as
1143 $     MatStencil idxm(4,m),idxn(4,n)
1144    and the values inserted using
1145 $    idxm(MatStencil_i,1) = i
1146 $    idxm(MatStencil_j,1) = j
1147 $    idxm(MatStencil_k,1) = k
1148    etc
1149 
1150    Negative indices may be passed in idxm and idxn, these rows and columns are
1151    simply ignored. This allows easily inserting element stiffness matrices
1152    with homogeneous Dirchlet boundary conditions that you don't want represented
1153    in the matrix.
1154 
1155    Inspired by the structured grid interface to the HYPRE package
1156    (http://www.llnl.gov/CASC/hypre)
1157 
1158    Level: beginner
1159 
1160    Concepts: matrices^putting entries in
1161 
1162 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1163           MatSetValues(), MatSetValuesStencil(), MatSetStencil(), DAGetMatrix(), DAVecGetArray(), MatStencil
1164 @*/
1165 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesBlockedStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv)
1166 {
1167   PetscErrorCode ierr;
1168   PetscInt       j,i,jdxm[128],jdxn[256],dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp;
1169   PetscInt       *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc);
1170 
1171   PetscFunctionBegin;
1172   if (!m || !n) PetscFunctionReturn(0); /* no values to insert */
1173   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1174   PetscValidType(mat,1);
1175   PetscValidIntPointer(idxm,3);
1176   PetscValidIntPointer(idxn,5);
1177   PetscValidScalarPointer(v,6);
1178 
1179   if (m > 128) SETERRQ1(PETSC_ERR_SUP,"Can only set 128 rows at a time; trying to set %D",m);
1180   if (n > 128) SETERRQ1(PETSC_ERR_SUP,"Can only set 256 columns at a time; trying to set %D",n);
1181 
1182   for (i=0; i<m; i++) {
1183     for (j=0; j<3-sdim; j++) dxm++;
1184     tmp = *dxm++ - starts[0];
1185     for (j=0; j<sdim-1; j++) {
1186       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
1187       else                                      tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
1188     }
1189     dxm++;
1190     jdxm[i] = tmp;
1191   }
1192   for (i=0; i<n; i++) {
1193     for (j=0; j<3-sdim; j++) dxn++;
1194     tmp = *dxn++ - starts[0];
1195     for (j=0; j<sdim-1; j++) {
1196       if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
1197       else                                       tmp = tmp*dims[j] + *(dxn-1) - starts[j+1];
1198     }
1199     dxn++;
1200     jdxn[i] = tmp;
1201   }
1202   ierr = MatSetValuesBlockedLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr);
1203   PetscFunctionReturn(0);
1204 }
1205 
1206 #undef __FUNCT__
1207 #define __FUNCT__ "MatSetStencil"
1208 /*@
1209    MatSetStencil - Sets the grid information for setting values into a matrix via
1210         MatSetValuesStencil()
1211 
1212    Not Collective
1213 
1214    Input Parameters:
1215 +  mat - the matrix
1216 .  dim - dimension of the grid 1, 2, or 3
1217 .  dims - number of grid points in x, y, and z direction, including ghost points on your processor
1218 .  starts - starting point of ghost nodes on your processor in x, y, and z direction
1219 -  dof - number of degrees of freedom per node
1220 
1221 
1222    Inspired by the structured grid interface to the HYPRE package
1223    (www.llnl.gov/CASC/hyper)
1224 
1225    For matrices generated with DAGetMatrix() this routine is automatically called and so not needed by the
1226    user.
1227 
1228    Level: beginner
1229 
1230    Concepts: matrices^putting entries in
1231 
1232 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1233           MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil()
1234 @*/
1235 PetscErrorCode PETSCMAT_DLLEXPORT MatSetStencil(Mat mat,PetscInt dim,const PetscInt dims[],const PetscInt starts[],PetscInt dof)
1236 {
1237   PetscInt i;
1238 
1239   PetscFunctionBegin;
1240   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1241   PetscValidIntPointer(dims,3);
1242   PetscValidIntPointer(starts,4);
1243 
1244   mat->stencil.dim = dim + (dof > 1);
1245   for (i=0; i<dim; i++) {
1246     mat->stencil.dims[i]   = dims[dim-i-1];      /* copy the values in backwards */
1247     mat->stencil.starts[i] = starts[dim-i-1];
1248   }
1249   mat->stencil.dims[dim]   = dof;
1250   mat->stencil.starts[dim] = 0;
1251   mat->stencil.noc         = (PetscTruth)(dof == 1);
1252   PetscFunctionReturn(0);
1253 }
1254 
1255 #undef __FUNCT__
1256 #define __FUNCT__ "MatSetValuesBlocked"
1257 /*@
1258    MatSetValuesBlocked - Inserts or adds a block of values into a matrix.
1259 
1260    Not Collective
1261 
1262    Input Parameters:
1263 +  mat - the matrix
1264 .  v - a logically two-dimensional array of values
1265 .  m, idxm - the number of block rows and their global block indices
1266 .  n, idxn - the number of block columns and their global block indices
1267 -  addv - either ADD_VALUES or INSERT_VALUES, where
1268    ADD_VALUES adds values to any existing entries, and
1269    INSERT_VALUES replaces existing entries with new values
1270 
1271    Notes:
1272    The m and n count the NUMBER of blocks in the row direction and column direction,
1273    NOT the total number of rows/columns; for example, if the block size is 2 and
1274    you are passing in values for rows 2,3,4,5  then m would be 2 (not 4).
1275    The values in idxm would be 1 2; that is the first index for each block divided by
1276    the block size.
1277 
1278    By default the values, v, are row-oriented and unsorted. So the layout of
1279    v is the same as for MatSetValues(). See MatSetOption() for other options.
1280 
1281    Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES
1282    options cannot be mixed without intervening calls to the assembly
1283    routines.
1284 
1285    MatSetValuesBlocked() uses 0-based row and column numbers in Fortran
1286    as well as in C.
1287 
1288    Negative indices may be passed in idxm and idxn, these rows and columns are
1289    simply ignored. This allows easily inserting element stiffness matrices
1290    with homogeneous Dirchlet boundary conditions that you don't want represented
1291    in the matrix.
1292 
1293    Each time an entry is set within a sparse matrix via MatSetValues(),
1294    internal searching must be done to determine where to place the the
1295    data in the matrix storage space.  By instead inserting blocks of
1296    entries via MatSetValuesBlocked(), the overhead of matrix assembly is
1297    reduced.
1298 
1299    Example:
1300 $   Suppose m=n=2 and block size(bs) = 2 The array is
1301 $
1302 $   1  2  | 3  4
1303 $   5  6  | 7  8
1304 $   - - - | - - -
1305 $   9  10 | 11 12
1306 $   13 14 | 15 16
1307 $
1308 $   v[] should be passed in like
1309 $   v[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
1310 $
1311 $  If you are not using row oriented storage of v (that is you called MatSetOption(mat,MAT_ROW_ORIENTED,PETSC_FALSE)) then
1312 $   v[] = [1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16]
1313 
1314    Level: intermediate
1315 
1316    Concepts: matrices^putting entries in blocked
1317 
1318 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal()
1319 @*/
1320 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesBlocked(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv)
1321 {
1322   PetscErrorCode ierr;
1323 
1324   PetscFunctionBegin;
1325   if (!m || !n) PetscFunctionReturn(0); /* no values to insert */
1326   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1327   PetscValidType(mat,1);
1328   PetscValidIntPointer(idxm,3);
1329   PetscValidIntPointer(idxn,5);
1330   PetscValidScalarPointer(v,6);
1331   ierr = MatPreallocated(mat);CHKERRQ(ierr);
1332   if (mat->insertmode == NOT_SET_VALUES) {
1333     mat->insertmode = addv;
1334   }
1335 #if defined(PETSC_USE_DEBUG)
1336   else if (mat->insertmode != addv) {
1337     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
1338   }
1339   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1340 #endif
1341 
1342   if (mat->assembled) {
1343     mat->was_assembled = PETSC_TRUE;
1344     mat->assembled     = PETSC_FALSE;
1345   }
1346   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
1347   if (mat->ops->setvaluesblocked) {
1348     ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr);
1349   } else {
1350     PetscInt i,j,*iidxm,*iidxn,bs = mat->rmap->bs;
1351     ierr = PetscMalloc2(m*bs,PetscInt,&iidxm,n*bs,PetscInt,&iidxn);CHKERRQ(ierr);
1352     for (i=0; i<m; i++) {
1353       for (j=0; j<bs; j++) {
1354 	iidxm[i*bs+j] = bs*idxm[i] + j;
1355       }
1356     }
1357     for (i=0; i<n; i++) {
1358       for (j=0; j<bs; j++) {
1359 	iidxn[i*bs+j] = bs*idxn[i] + j;
1360       }
1361     }
1362     ierr = MatSetValues(mat,bs*m,iidxm,bs*n,iidxn,v,addv);CHKERRQ(ierr);
1363     ierr = PetscFree2(iidxm,iidxn);CHKERRQ(ierr);
1364   }
1365   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
1366   PetscFunctionReturn(0);
1367 }
1368 
1369 #undef __FUNCT__
1370 #define __FUNCT__ "MatGetValues"
1371 /*@
1372    MatGetValues - Gets a block of values from a matrix.
1373 
1374    Not Collective; currently only returns a local block
1375 
1376    Input Parameters:
1377 +  mat - the matrix
1378 .  v - a logically two-dimensional array for storing the values
1379 .  m, idxm - the number of rows and their global indices
1380 -  n, idxn - the number of columns and their global indices
1381 
1382    Notes:
1383    The user must allocate space (m*n PetscScalars) for the values, v.
1384    The values, v, are then returned in a row-oriented format,
1385    analogous to that used by default in MatSetValues().
1386 
1387    MatGetValues() uses 0-based row and column numbers in
1388    Fortran as well as in C.
1389 
1390    MatGetValues() requires that the matrix has been assembled
1391    with MatAssemblyBegin()/MatAssemblyEnd().  Thus, calls to
1392    MatSetValues() and MatGetValues() CANNOT be made in succession
1393    without intermediate matrix assembly.
1394 
1395    Negative row or column indices will be ignored and those locations in v[] will be
1396    left unchanged.
1397 
1398    Level: advanced
1399 
1400    Concepts: matrices^accessing values
1401 
1402 .seealso: MatGetRow(), MatGetSubMatrices(), MatSetValues()
1403 @*/
1404 PetscErrorCode PETSCMAT_DLLEXPORT MatGetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
1405 {
1406   PetscErrorCode ierr;
1407 
1408   PetscFunctionBegin;
1409   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1410   PetscValidType(mat,1);
1411   PetscValidIntPointer(idxm,3);
1412   PetscValidIntPointer(idxn,5);
1413   PetscValidScalarPointer(v,6);
1414   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1415   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1416   if (!mat->ops->getvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1417   ierr = MatPreallocated(mat);CHKERRQ(ierr);
1418 
1419   ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr);
1420   ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr);
1421   ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr);
1422   PetscFunctionReturn(0);
1423 }
1424 
1425 #undef __FUNCT__
1426 #define __FUNCT__ "MatSetLocalToGlobalMapping"
1427 /*@
1428    MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by
1429    the routine MatSetValuesLocal() to allow users to insert matrix entries
1430    using a local (per-processor) numbering.
1431 
1432    Not Collective
1433 
1434    Input Parameters:
1435 +  x - the matrix
1436 -  mapping - mapping created with ISLocalToGlobalMappingCreate()
1437              or ISLocalToGlobalMappingCreateIS()
1438 
1439    Level: intermediate
1440 
1441    Concepts: matrices^local to global mapping
1442    Concepts: local to global mapping^for matrices
1443 
1444 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal()
1445 @*/
1446 PetscErrorCode PETSCMAT_DLLEXPORT MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping mapping)
1447 {
1448   PetscErrorCode ierr;
1449   PetscFunctionBegin;
1450   PetscValidHeaderSpecific(x,MAT_COOKIE,1);
1451   PetscValidType(x,1);
1452   PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE,2);
1453   if (x->mapping) {
1454     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix");
1455   }
1456   ierr = MatPreallocated(x);CHKERRQ(ierr);
1457 
1458   if (x->ops->setlocaltoglobalmapping) {
1459     ierr = (*x->ops->setlocaltoglobalmapping)(x,mapping);CHKERRQ(ierr);
1460   } else {
1461     ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr);
1462     if (x->mapping) { ierr = ISLocalToGlobalMappingDestroy(x->mapping);CHKERRQ(ierr); }
1463     x->mapping = mapping;
1464   }
1465   PetscFunctionReturn(0);
1466 }
1467 
1468 #undef __FUNCT__
1469 #define __FUNCT__ "MatSetLocalToGlobalMappingBlock"
1470 /*@
1471    MatSetLocalToGlobalMappingBlock - Sets a local-to-global numbering for use
1472    by the routine MatSetValuesBlockedLocal() to allow users to insert matrix
1473    entries using a local (per-processor) numbering.
1474 
1475    Not Collective
1476 
1477    Input Parameters:
1478 +  x - the matrix
1479 -  mapping - mapping created with ISLocalToGlobalMappingCreate() or
1480              ISLocalToGlobalMappingCreateIS()
1481 
1482    Level: intermediate
1483 
1484    Concepts: matrices^local to global mapping blocked
1485    Concepts: local to global mapping^for matrices, blocked
1486 
1487 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal(),
1488            MatSetValuesBlocked(), MatSetValuesLocal()
1489 @*/
1490 PetscErrorCode PETSCMAT_DLLEXPORT MatSetLocalToGlobalMappingBlock(Mat x,ISLocalToGlobalMapping mapping)
1491 {
1492   PetscErrorCode ierr;
1493   PetscFunctionBegin;
1494   PetscValidHeaderSpecific(x,MAT_COOKIE,1);
1495   PetscValidType(x,1);
1496   PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE,2);
1497   if (x->bmapping) {
1498     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix");
1499   }
1500   ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr);
1501   if (x->bmapping) { ierr = ISLocalToGlobalMappingDestroy(x->mapping);CHKERRQ(ierr); }
1502   x->bmapping = mapping;
1503   PetscFunctionReturn(0);
1504 }
1505 
1506 #undef __FUNCT__
1507 #define __FUNCT__ "MatSetValuesLocal"
1508 /*@
1509    MatSetValuesLocal - Inserts or adds values into certain locations of a matrix,
1510    using a local ordering of the nodes.
1511 
1512    Not Collective
1513 
1514    Input Parameters:
1515 +  x - the matrix
1516 .  nrow, irow - number of rows and their local indices
1517 .  ncol, icol - number of columns and their local indices
1518 .  y -  a logically two-dimensional array of values
1519 -  addv - either INSERT_VALUES or ADD_VALUES, where
1520    ADD_VALUES adds values to any existing entries, and
1521    INSERT_VALUES replaces existing entries with new values
1522 
1523    Notes:
1524    Before calling MatSetValuesLocal(), the user must first set the
1525    local-to-global mapping by calling MatSetLocalToGlobalMapping().
1526 
1527    Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES
1528    options cannot be mixed without intervening calls to the assembly
1529    routines.
1530 
1531    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
1532    MUST be called after all calls to MatSetValuesLocal() have been completed.
1533 
1534    Level: intermediate
1535 
1536    Concepts: matrices^putting entries in with local numbering
1537 
1538 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(),
1539            MatSetValueLocal()
1540 @*/
1541 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv)
1542 {
1543   PetscErrorCode ierr;
1544   PetscInt       irowm[2048],icolm[2048];
1545 
1546   PetscFunctionBegin;
1547   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1548   PetscValidType(mat,1);
1549   PetscValidIntPointer(irow,3);
1550   PetscValidIntPointer(icol,5);
1551   PetscValidScalarPointer(y,6);
1552   ierr = MatPreallocated(mat);CHKERRQ(ierr);
1553   if (mat->insertmode == NOT_SET_VALUES) {
1554     mat->insertmode = addv;
1555   }
1556 #if defined(PETSC_USE_DEBUG)
1557   else if (mat->insertmode != addv) {
1558     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
1559   }
1560   if (!mat->ops->setvalueslocal && (nrow > 2048 || ncol > 2048)) {
1561     SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %D %D",nrow,ncol);
1562   }
1563   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1564 #endif
1565 
1566   if (mat->assembled) {
1567     mat->was_assembled = PETSC_TRUE;
1568     mat->assembled     = PETSC_FALSE;
1569   }
1570   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
1571   if (!mat->ops->setvalueslocal) {
1572     ierr = ISLocalToGlobalMappingApply(mat->mapping,nrow,irow,irowm);CHKERRQ(ierr);
1573     ierr = ISLocalToGlobalMappingApply(mat->mapping,ncol,icol,icolm);CHKERRQ(ierr);
1574     ierr = (*mat->ops->setvalues)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr);
1575   } else {
1576     ierr = (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr);
1577   }
1578   mat->same_nonzero = PETSC_FALSE;
1579   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
1580   PetscFunctionReturn(0);
1581 }
1582 
1583 #undef __FUNCT__
1584 #define __FUNCT__ "MatSetValuesBlockedLocal"
1585 /*@
1586    MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix,
1587    using a local ordering of the nodes a block at a time.
1588 
1589    Not Collective
1590 
1591    Input Parameters:
1592 +  x - the matrix
1593 .  nrow, irow - number of rows and their local indices
1594 .  ncol, icol - number of columns and their local indices
1595 .  y -  a logically two-dimensional array of values
1596 -  addv - either INSERT_VALUES or ADD_VALUES, where
1597    ADD_VALUES adds values to any existing entries, and
1598    INSERT_VALUES replaces existing entries with new values
1599 
1600    Notes:
1601    Before calling MatSetValuesBlockedLocal(), the user must first set the
1602    local-to-global mapping by calling MatSetLocalToGlobalMappingBlock(),
1603    where the mapping MUST be set for matrix blocks, not for matrix elements.
1604 
1605    Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES
1606    options cannot be mixed without intervening calls to the assembly
1607    routines.
1608 
1609    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
1610    MUST be called after all calls to MatSetValuesBlockedLocal() have been completed.
1611 
1612    Level: intermediate
1613 
1614    Concepts: matrices^putting blocked values in with local numbering
1615 
1616 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesLocal(), MatSetLocalToGlobalMappingBlock(), MatSetValuesBlocked()
1617 @*/
1618 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesBlockedLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv)
1619 {
1620   PetscErrorCode ierr;
1621   PetscInt       irowm[2048],icolm[2048];
1622 
1623   PetscFunctionBegin;
1624   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1625   PetscValidType(mat,1);
1626   PetscValidIntPointer(irow,3);
1627   PetscValidIntPointer(icol,5);
1628   PetscValidScalarPointer(y,6);
1629   ierr = MatPreallocated(mat);CHKERRQ(ierr);
1630   if (mat->insertmode == NOT_SET_VALUES) {
1631     mat->insertmode = addv;
1632   }
1633 #if defined(PETSC_USE_DEBUG)
1634   else if (mat->insertmode != addv) {
1635     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
1636   }
1637   if (!mat->bmapping) {
1638     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Local to global never set with MatSetLocalToGlobalMappingBlock()");
1639   }
1640   if (nrow > 2048 || ncol > 2048) {
1641     SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %D %D",nrow,ncol);
1642   }
1643   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1644 #endif
1645 
1646   if (mat->assembled) {
1647     mat->was_assembled = PETSC_TRUE;
1648     mat->assembled     = PETSC_FALSE;
1649   }
1650   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
1651   ierr = ISLocalToGlobalMappingApply(mat->bmapping,nrow,irow,irowm);CHKERRQ(ierr);
1652   ierr = ISLocalToGlobalMappingApply(mat->bmapping,ncol,icol,icolm);CHKERRQ(ierr);
1653   if (mat->ops->setvaluesblocked) {
1654   ierr = (*mat->ops->setvaluesblocked)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr);
1655   } else {
1656     PetscInt i,j,*iirowm,*iicolm,bs = mat->rmap->bs;
1657     ierr = PetscMalloc2(nrow*bs,PetscInt,&iirowm,ncol*bs,PetscInt,&iicolm);CHKERRQ(ierr);
1658     for (i=0; i<nrow; i++) {
1659       for (j=0; j<bs; j++) {
1660 	iirowm[i*bs+j] = bs*irowm[i] + j;
1661       }
1662     }
1663     for (i=0; i<ncol; i++) {
1664       for (j=0; j<bs; j++) {
1665 	iicolm[i*bs+j] = bs*icolm[i] + j;
1666       }
1667     }
1668     ierr = MatSetValues(mat,bs*nrow,iirowm,bs*ncol,iicolm,y,addv);CHKERRQ(ierr);
1669     ierr = PetscFree2(iirowm,iicolm);CHKERRQ(ierr);
1670 
1671   }
1672   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
1673   PetscFunctionReturn(0);
1674 }
1675 
1676 /* --------------------------------------------------------*/
1677 #undef __FUNCT__
1678 #define __FUNCT__ "MatMult"
1679 /*@
1680    MatMult - Computes the matrix-vector product, y = Ax.
1681 
1682    Collective on Mat and Vec
1683 
1684    Input Parameters:
1685 +  mat - the matrix
1686 -  x   - the vector to be multiplied
1687 
1688    Output Parameters:
1689 .  y - the result
1690 
1691    Notes:
1692    The vectors x and y cannot be the same.  I.e., one cannot
1693    call MatMult(A,y,y).
1694 
1695    Level: beginner
1696 
1697    Concepts: matrix-vector product
1698 
1699 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
1700 @*/
1701 PetscErrorCode PETSCMAT_DLLEXPORT MatMult(Mat mat,Vec x,Vec y)
1702 {
1703   PetscErrorCode ierr;
1704 
1705   PetscFunctionBegin;
1706   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1707   PetscValidType(mat,1);
1708   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
1709   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
1710 
1711   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1712   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1713   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
1714 #ifndef PETSC_HAVE_CONSTRAINTS
1715   if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
1716   if (mat->rmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N);
1717   if (mat->rmap->n != y->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %D %D",mat->rmap->n,y->map->n);
1718 #endif
1719   ierr = MatPreallocated(mat);CHKERRQ(ierr);
1720 
1721   if (mat->nullsp) {
1722     ierr = MatNullSpaceRemove(mat->nullsp,x,&x);CHKERRQ(ierr);
1723   }
1724 
1725   if (!mat->ops->mult) SETERRQ(PETSC_ERR_SUP,"This matrix type does not have a multiply defined");
1726   ierr = PetscLogEventBegin(MAT_Mult,mat,x,y,0);CHKERRQ(ierr);
1727   ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr);
1728   ierr = PetscLogEventEnd(MAT_Mult,mat,x,y,0);CHKERRQ(ierr);
1729 
1730   if (mat->nullsp) {
1731     ierr = MatNullSpaceRemove(mat->nullsp,y,PETSC_NULL);CHKERRQ(ierr);
1732   }
1733   ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr);
1734   PetscFunctionReturn(0);
1735 }
1736 
1737 #undef __FUNCT__
1738 #define __FUNCT__ "MatMultTranspose"
1739 /*@
1740    MatMultTranspose - Computes matrix transpose times a vector.
1741 
1742    Collective on Mat and Vec
1743 
1744    Input Parameters:
1745 +  mat - the matrix
1746 -  x   - the vector to be multilplied
1747 
1748    Output Parameters:
1749 .  y - the result
1750 
1751    Notes:
1752    The vectors x and y cannot be the same.  I.e., one cannot
1753    call MatMultTranspose(A,y,y).
1754 
1755    Level: beginner
1756 
1757    Concepts: matrix vector product^transpose
1758 
1759 .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd()
1760 @*/
1761 PetscErrorCode PETSCMAT_DLLEXPORT MatMultTranspose(Mat mat,Vec x,Vec y)
1762 {
1763   PetscErrorCode ierr;
1764 
1765   PetscFunctionBegin;
1766   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1767   PetscValidType(mat,1);
1768   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
1769   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
1770 
1771   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1772   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1773   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
1774 #ifndef PETSC_HAVE_CONSTRAINTS
1775   if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N);
1776   if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N);
1777 #endif
1778   ierr = MatPreallocated(mat);CHKERRQ(ierr);
1779 
1780   if (!mat->ops->multtranspose) SETERRQ(PETSC_ERR_SUP,"This matrix type does not have a multiply tranpose defined");
1781   ierr = PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr);
1782   ierr = (*mat->ops->multtranspose)(mat,x,y);CHKERRQ(ierr);
1783   ierr = PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr);
1784   ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr);
1785   PetscFunctionReturn(0);
1786 }
1787 
1788 #undef __FUNCT__
1789 #define __FUNCT__ "MatMultAdd"
1790 /*@
1791     MatMultAdd -  Computes v3 = v2 + A * v1.
1792 
1793     Collective on Mat and Vec
1794 
1795     Input Parameters:
1796 +   mat - the matrix
1797 -   v1, v2 - the vectors
1798 
1799     Output Parameters:
1800 .   v3 - the result
1801 
1802     Notes:
1803     The vectors v1 and v3 cannot be the same.  I.e., one cannot
1804     call MatMultAdd(A,v1,v2,v1).
1805 
1806     Level: beginner
1807 
1808     Concepts: matrix vector product^addition
1809 
1810 .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd()
1811 @*/
1812 PetscErrorCode PETSCMAT_DLLEXPORT MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3)
1813 {
1814   PetscErrorCode ierr;
1815 
1816   PetscFunctionBegin;
1817   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1818   PetscValidType(mat,1);
1819   PetscValidHeaderSpecific(v1,VEC_COOKIE,2);
1820   PetscValidHeaderSpecific(v2,VEC_COOKIE,3);
1821   PetscValidHeaderSpecific(v3,VEC_COOKIE,4);
1822 
1823   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1824   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1825   if (mat->cmap->N != v1->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %D %D",mat->cmap->N,v1->map->N);
1826   if (mat->rmap->N != v2->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %D %D",mat->rmap->N,v2->map->N);
1827   if (mat->rmap->N != v3->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %D %D",mat->rmap->N,v3->map->N);
1828   if (mat->rmap->n != v3->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: local dim %D %D",mat->rmap->n,v3->map->n);
1829   if (mat->rmap->n != v2->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: local dim %D %D",mat->rmap->n,v2->map->n);
1830   if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
1831   ierr = MatPreallocated(mat);CHKERRQ(ierr);
1832 
1833   ierr = PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1834   ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr);
1835   ierr = PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1836   ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr);
1837   PetscFunctionReturn(0);
1838 }
1839 
1840 #undef __FUNCT__
1841 #define __FUNCT__ "MatMultTransposeAdd"
1842 /*@
1843    MatMultTransposeAdd - Computes v3 = v2 + A' * v1.
1844 
1845    Collective on Mat and Vec
1846 
1847    Input Parameters:
1848 +  mat - the matrix
1849 -  v1, v2 - the vectors
1850 
1851    Output Parameters:
1852 .  v3 - the result
1853 
1854    Notes:
1855    The vectors v1 and v3 cannot be the same.  I.e., one cannot
1856    call MatMultTransposeAdd(A,v1,v2,v1).
1857 
1858    Level: beginner
1859 
1860    Concepts: matrix vector product^transpose and addition
1861 
1862 .seealso: MatMultTranspose(), MatMultAdd(), MatMult()
1863 @*/
1864 PetscErrorCode PETSCMAT_DLLEXPORT MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
1865 {
1866   PetscErrorCode ierr;
1867 
1868   PetscFunctionBegin;
1869   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1870   PetscValidType(mat,1);
1871   PetscValidHeaderSpecific(v1,VEC_COOKIE,2);
1872   PetscValidHeaderSpecific(v2,VEC_COOKIE,3);
1873   PetscValidHeaderSpecific(v3,VEC_COOKIE,4);
1874 
1875   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1876   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1877   if (!mat->ops->multtransposeadd) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1878   if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
1879   if (mat->rmap->N != v1->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %D %D",mat->rmap->N,v1->map->N);
1880   if (mat->cmap->N != v2->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %D %D",mat->cmap->N,v2->map->N);
1881   if (mat->cmap->N != v3->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %D %D",mat->cmap->N,v3->map->N);
1882   ierr = MatPreallocated(mat);CHKERRQ(ierr);
1883 
1884   ierr = PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1885   ierr = (*mat->ops->multtransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr);
1886   ierr = PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1887   ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr);
1888   PetscFunctionReturn(0);
1889 }
1890 
1891 #undef __FUNCT__
1892 #define __FUNCT__ "MatMultConstrained"
1893 /*@
1894    MatMultConstrained - The inner multiplication routine for a
1895    constrained matrix P^T A P.
1896 
1897    Collective on Mat and Vec
1898 
1899    Input Parameters:
1900 +  mat - the matrix
1901 -  x   - the vector to be multilplied
1902 
1903    Output Parameters:
1904 .  y - the result
1905 
1906    Notes:
1907    The vectors x and y cannot be the same.  I.e., one cannot
1908    call MatMult(A,y,y).
1909 
1910    Level: beginner
1911 
1912 .keywords: matrix, multiply, matrix-vector product, constraint
1913 .seealso: MatMult(), MatMultTrans(), MatMultAdd(), MatMultTransAdd()
1914 @*/
1915 PetscErrorCode PETSCMAT_DLLEXPORT MatMultConstrained(Mat mat,Vec x,Vec y)
1916 {
1917   PetscErrorCode ierr;
1918 
1919   PetscFunctionBegin;
1920   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1921   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
1922   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
1923   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1924   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1925   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
1926   if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
1927   if (mat->rmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N);
1928   if (mat->rmap->n != y->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %D %D",mat->rmap->n,y->map->n);
1929 
1930   ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr);
1931   ierr = (*mat->ops->multconstrained)(mat,x,y);CHKERRQ(ierr);
1932   ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr);
1933   ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr);
1934 
1935   PetscFunctionReturn(0);
1936 }
1937 
1938 #undef __FUNCT__
1939 #define __FUNCT__ "MatMultTransposeConstrained"
1940 /*@
1941    MatMultTransposeConstrained - The inner multiplication routine for a
1942    constrained matrix P^T A^T P.
1943 
1944    Collective on Mat and Vec
1945 
1946    Input Parameters:
1947 +  mat - the matrix
1948 -  x   - the vector to be multilplied
1949 
1950    Output Parameters:
1951 .  y - the result
1952 
1953    Notes:
1954    The vectors x and y cannot be the same.  I.e., one cannot
1955    call MatMult(A,y,y).
1956 
1957    Level: beginner
1958 
1959 .keywords: matrix, multiply, matrix-vector product, constraint
1960 .seealso: MatMult(), MatMultTrans(), MatMultAdd(), MatMultTransAdd()
1961 @*/
1962 PetscErrorCode PETSCMAT_DLLEXPORT MatMultTransposeConstrained(Mat mat,Vec x,Vec y)
1963 {
1964   PetscErrorCode ierr;
1965 
1966   PetscFunctionBegin;
1967   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
1968   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
1969   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
1970   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1971   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1972   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
1973   if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
1974   if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N);
1975 
1976   ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr);
1977   ierr = (*mat->ops->multtransposeconstrained)(mat,x,y);CHKERRQ(ierr);
1978   ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr);
1979   ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr);
1980 
1981   PetscFunctionReturn(0);
1982 }
1983 /* ------------------------------------------------------------*/
1984 #undef __FUNCT__
1985 #define __FUNCT__ "MatGetInfo"
1986 /*@
1987    MatGetInfo - Returns information about matrix storage (number of
1988    nonzeros, memory, etc.).
1989 
1990    Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used
1991    as the flag
1992 
1993    Input Parameters:
1994 .  mat - the matrix
1995 
1996    Output Parameters:
1997 +  flag - flag indicating the type of parameters to be returned
1998    (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors,
1999    MAT_GLOBAL_SUM - sum over all processors)
2000 -  info - matrix information context
2001 
2002    Notes:
2003    The MatInfo context contains a variety of matrix data, including
2004    number of nonzeros allocated and used, number of mallocs during
2005    matrix assembly, etc.  Additional information for factored matrices
2006    is provided (such as the fill ratio, number of mallocs during
2007    factorization, etc.).  Much of this info is printed to PETSC_STDOUT
2008    when using the runtime options
2009 $       -info -mat_view_info
2010 
2011    Example for C/C++ Users:
2012    See the file ${PETSC_DIR}/include/petscmat.h for a complete list of
2013    data within the MatInfo context.  For example,
2014 .vb
2015       MatInfo info;
2016       Mat     A;
2017       double  mal, nz_a, nz_u;
2018 
2019       MatGetInfo(A,MAT_LOCAL,&info);
2020       mal  = info.mallocs;
2021       nz_a = info.nz_allocated;
2022 .ve
2023 
2024    Example for Fortran Users:
2025    Fortran users should declare info as a double precision
2026    array of dimension MAT_INFO_SIZE, and then extract the parameters
2027    of interest.  See the file ${PETSC_DIR}/include/finclude/petscmat.h
2028    a complete list of parameter names.
2029 .vb
2030       double  precision info(MAT_INFO_SIZE)
2031       double  precision mal, nz_a
2032       Mat     A
2033       integer ierr
2034 
2035       call MatGetInfo(A,MAT_LOCAL,info,ierr)
2036       mal = info(MAT_INFO_MALLOCS)
2037       nz_a = info(MAT_INFO_NZ_ALLOCATED)
2038 .ve
2039 
2040     Level: intermediate
2041 
2042     Concepts: matrices^getting information on
2043 
2044 @*/
2045 PetscErrorCode PETSCMAT_DLLEXPORT MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info)
2046 {
2047   PetscErrorCode ierr;
2048 
2049   PetscFunctionBegin;
2050   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2051   PetscValidType(mat,1);
2052   PetscValidPointer(info,3);
2053   if (!mat->ops->getinfo) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2054   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2055   ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr);
2056   PetscFunctionReturn(0);
2057 }
2058 
2059 /* ----------------------------------------------------------*/
2060 #undef __FUNCT__
2061 #define __FUNCT__ "MatILUDTFactor"
2062 /*@C
2063    MatILUDTFactor - Performs a drop tolerance ILU factorization.
2064 
2065    Collective on Mat
2066 
2067    Input Parameters:
2068 +  mat - the matrix
2069 .  row - row permutation
2070 .  col - column permutation
2071 -  info - information about the factorization to be done
2072 
2073    Output Parameters:
2074 .  fact - the factored matrix
2075 
2076    Level: developer
2077 
2078    Notes:
2079    Most users should employ the simplified KSP interface for linear solvers
2080    instead of working directly with matrix algebra routines such as this.
2081    See, e.g., KSPCreate().
2082 
2083    This is currently only supported for the SeqAIJ matrix format using code
2084    from Yousef Saad's SPARSEKIT2  package (translated to C with f2c) and/or
2085    Matlab. SPARSEKIT2 is copyrighted by Yousef Saad with the GNU copyright
2086    and thus can be distributed with PETSc.
2087 
2088     Concepts: matrices^ILUDT factorization
2089 
2090 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo
2091 @*/
2092 PetscErrorCode PETSCMAT_DLLEXPORT MatILUDTFactor(Mat mat,IS row,IS col,const MatFactorInfo *info,Mat *fact)
2093 {
2094   PetscErrorCode ierr;
2095 
2096   PetscFunctionBegin;
2097   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2098   PetscValidType(mat,1);
2099   if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2);
2100   if (col) PetscValidHeaderSpecific(col,IS_COOKIE,3);
2101   PetscValidPointer(info,4);
2102   PetscValidPointer(fact,5);
2103   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2104   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2105   if (!mat->ops->iludtfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2106   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2107   ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
2108   ierr = (*mat->ops->iludtfactor)(mat,row,col,info,fact);CHKERRQ(ierr);
2109   ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
2110   ierr = PetscObjectStateIncrease((PetscObject)*fact);CHKERRQ(ierr);
2111 
2112   PetscFunctionReturn(0);
2113 }
2114 
2115 #undef __FUNCT__
2116 #define __FUNCT__ "MatLUFactor"
2117 /*@
2118    MatLUFactor - Performs in-place LU factorization of matrix.
2119 
2120    Collective on Mat
2121 
2122    Input Parameters:
2123 +  mat - the matrix
2124 .  row - row permutation
2125 .  col - column permutation
2126 -  info - options for factorization, includes
2127 $          fill - expected fill as ratio of original fill.
2128 $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
2129 $                   Run with the option -info to determine an optimal value to use
2130 
2131    Notes:
2132    Most users should employ the simplified KSP interface for linear solvers
2133    instead of working directly with matrix algebra routines such as this.
2134    See, e.g., KSPCreate().
2135 
2136    This changes the state of the matrix to a factored matrix; it cannot be used
2137    for example with MatSetValues() unless one first calls MatSetUnfactored().
2138 
2139    Level: developer
2140 
2141    Concepts: matrices^LU factorization
2142 
2143 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(),
2144           MatGetOrdering(), MatSetUnfactored(), MatFactorInfo
2145 
2146 @*/
2147 PetscErrorCode PETSCMAT_DLLEXPORT MatLUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info)
2148 {
2149   PetscErrorCode ierr;
2150 
2151   PetscFunctionBegin;
2152   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2153   if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2);
2154   if (col) PetscValidHeaderSpecific(col,IS_COOKIE,3);
2155   PetscValidPointer(info,4);
2156   PetscValidType(mat,1);
2157   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2158   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2159   if (!mat->ops->lufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2160   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2161 
2162   ierr = PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr);
2163   ierr = (*mat->ops->lufactor)(mat,row,col,info);CHKERRQ(ierr);
2164   ierr = PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr);
2165   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
2166   PetscFunctionReturn(0);
2167 }
2168 
2169 #undef __FUNCT__
2170 #define __FUNCT__ "MatILUFactor"
2171 /*@
2172    MatILUFactor - Performs in-place ILU factorization of matrix.
2173 
2174    Collective on Mat
2175 
2176    Input Parameters:
2177 +  mat - the matrix
2178 .  row - row permutation
2179 .  col - column permutation
2180 -  info - structure containing
2181 $      levels - number of levels of fill.
2182 $      expected fill - as ratio of original fill.
2183 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
2184                 missing diagonal entries)
2185 
2186    Notes:
2187    Probably really in-place only when level of fill is zero, otherwise allocates
2188    new space to store factored matrix and deletes previous memory.
2189 
2190    Most users should employ the simplified KSP interface for linear solvers
2191    instead of working directly with matrix algebra routines such as this.
2192    See, e.g., KSPCreate().
2193 
2194    Level: developer
2195 
2196    Concepts: matrices^ILU factorization
2197 
2198 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo
2199 @*/
2200 PetscErrorCode PETSCMAT_DLLEXPORT MatILUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info)
2201 {
2202   PetscErrorCode ierr;
2203 
2204   PetscFunctionBegin;
2205   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2206   if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2);
2207   if (col) PetscValidHeaderSpecific(col,IS_COOKIE,3);
2208   PetscValidPointer(info,4);
2209   PetscValidType(mat,1);
2210   if (mat->rmap->N != mat->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
2211   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2212   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2213   if (!mat->ops->ilufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2214   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2215 
2216   ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
2217   ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr);
2218   ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
2219   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
2220   PetscFunctionReturn(0);
2221 }
2222 
2223 #undef __FUNCT__
2224 #define __FUNCT__ "MatLUFactorSymbolic"
2225 /*@
2226    MatLUFactorSymbolic - Performs symbolic LU factorization of matrix.
2227    Call this routine before calling MatLUFactorNumeric().
2228 
2229    Collective on Mat
2230 
2231    Input Parameters:
2232 +  fact - the factor matrix obtained with MatGetFactor()
2233 .  mat - the matrix
2234 .  row, col - row and column permutations
2235 -  info - options for factorization, includes
2236 $          fill - expected fill as ratio of original fill.
2237 $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
2238 $                   Run with the option -info to determine an optimal value to use
2239 
2240 
2241    Notes:
2242    See the users manual for additional information about
2243    choosing the fill factor for better efficiency.
2244 
2245    Most users should employ the simplified KSP interface for linear solvers
2246    instead of working directly with matrix algebra routines such as this.
2247    See, e.g., KSPCreate().
2248 
2249    Level: developer
2250 
2251    Concepts: matrices^LU symbolic factorization
2252 
2253 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo
2254 @*/
2255 PetscErrorCode PETSCMAT_DLLEXPORT MatLUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
2256 {
2257   PetscErrorCode ierr;
2258 
2259   PetscFunctionBegin;
2260   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2261   if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2);
2262   if (col) PetscValidHeaderSpecific(col,IS_COOKIE,3);
2263   PetscValidPointer(info,4);
2264   PetscValidType(mat,1);
2265   PetscValidPointer(fact,5);
2266   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2267   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2268   if (!(fact)->ops->lufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic LU",((PetscObject)mat)->type_name);
2269   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2270 
2271   ierr = PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
2272   ierr = (fact->ops->lufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr);
2273   ierr = PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
2274   ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr);
2275   PetscFunctionReturn(0);
2276 }
2277 
2278 #undef __FUNCT__
2279 #define __FUNCT__ "MatLUFactorNumeric"
2280 /*@
2281    MatLUFactorNumeric - Performs numeric LU factorization of a matrix.
2282    Call this routine after first calling MatLUFactorSymbolic().
2283 
2284    Collective on Mat
2285 
2286    Input Parameters:
2287 +  fact - the factor matrix obtained with MatGetFactor()
2288 .  mat - the matrix
2289 -  info - options for factorization
2290 
2291    Notes:
2292    See MatLUFactor() for in-place factorization.  See
2293    MatCholeskyFactorNumeric() for the symmetric, positive definite case.
2294 
2295    Most users should employ the simplified KSP interface for linear solvers
2296    instead of working directly with matrix algebra routines such as this.
2297    See, e.g., KSPCreate().
2298 
2299    Level: developer
2300 
2301    Concepts: matrices^LU numeric factorization
2302 
2303 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor()
2304 @*/
2305 PetscErrorCode PETSCMAT_DLLEXPORT MatLUFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info)
2306 {
2307   PetscErrorCode ierr;
2308 
2309   PetscFunctionBegin;
2310   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2311   PetscValidType(mat,1);
2312   PetscValidPointer(fact,2);
2313   PetscValidHeaderSpecific(fact,MAT_COOKIE,2);
2314   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2315   if (mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N) {
2316     SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dimensions are different %D should = %D %D should = %D",mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N);
2317   }
2318   if (!(fact)->ops->lufactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2319   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2320   ierr = PetscLogEventBegin(MAT_LUFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);
2321   ierr = (fact->ops->lufactornumeric)(fact,mat,info);CHKERRQ(ierr);
2322   ierr = PetscLogEventEnd(MAT_LUFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);
2323 
2324   ierr = MatView_Private(fact);CHKERRQ(ierr);
2325   ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr);
2326   PetscFunctionReturn(0);
2327 }
2328 
2329 #undef __FUNCT__
2330 #define __FUNCT__ "MatCholeskyFactor"
2331 /*@
2332    MatCholeskyFactor - Performs in-place Cholesky factorization of a
2333    symmetric matrix.
2334 
2335    Collective on Mat
2336 
2337    Input Parameters:
2338 +  mat - the matrix
2339 .  perm - row and column permutations
2340 -  f - expected fill as ratio of original fill
2341 
2342    Notes:
2343    See MatLUFactor() for the nonsymmetric case.  See also
2344    MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric().
2345 
2346    Most users should employ the simplified KSP interface for linear solvers
2347    instead of working directly with matrix algebra routines such as this.
2348    See, e.g., KSPCreate().
2349 
2350    Level: developer
2351 
2352    Concepts: matrices^Cholesky factorization
2353 
2354 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric()
2355           MatGetOrdering()
2356 
2357 @*/
2358 PetscErrorCode PETSCMAT_DLLEXPORT MatCholeskyFactor(Mat mat,IS perm,const MatFactorInfo *info)
2359 {
2360   PetscErrorCode ierr;
2361 
2362   PetscFunctionBegin;
2363   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2364   PetscValidType(mat,1);
2365   PetscValidHeaderSpecific(perm,IS_COOKIE,2);
2366   PetscValidPointer(info,3);
2367   if (mat->rmap->N != mat->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square");
2368   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2369   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2370   if (!mat->ops->choleskyfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2371   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2372 
2373   ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr);
2374   ierr = (*mat->ops->choleskyfactor)(mat,perm,info);CHKERRQ(ierr);
2375   ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr);
2376   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
2377   PetscFunctionReturn(0);
2378 }
2379 
2380 #undef __FUNCT__
2381 #define __FUNCT__ "MatCholeskyFactorSymbolic"
2382 /*@
2383    MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization
2384    of a symmetric matrix.
2385 
2386    Collective on Mat
2387 
2388    Input Parameters:
2389 +  fact - the factor matrix obtained with MatGetFactor()
2390 .  mat - the matrix
2391 .  perm - row and column permutations
2392 -  info - options for factorization, includes
2393 $          fill - expected fill as ratio of original fill.
2394 $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
2395 $                   Run with the option -info to determine an optimal value to use
2396 
2397    Notes:
2398    See MatLUFactorSymbolic() for the nonsymmetric case.  See also
2399    MatCholeskyFactor() and MatCholeskyFactorNumeric().
2400 
2401    Most users should employ the simplified KSP interface for linear solvers
2402    instead of working directly with matrix algebra routines such as this.
2403    See, e.g., KSPCreate().
2404 
2405    Level: developer
2406 
2407    Concepts: matrices^Cholesky symbolic factorization
2408 
2409 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric()
2410           MatGetOrdering()
2411 
2412 @*/
2413 PetscErrorCode PETSCMAT_DLLEXPORT MatCholeskyFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
2414 {
2415   PetscErrorCode ierr;
2416 
2417   PetscFunctionBegin;
2418   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2419   PetscValidType(mat,1);
2420   if (perm) PetscValidHeaderSpecific(perm,IS_COOKIE,2);
2421   PetscValidPointer(info,3);
2422   PetscValidPointer(fact,4);
2423   if (mat->rmap->N != mat->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square");
2424   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2425   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2426   if (!(fact)->ops->choleskyfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2427   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2428 
2429   ierr = PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
2430   ierr = (fact->ops->choleskyfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr);
2431   ierr = PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
2432   ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr);
2433   PetscFunctionReturn(0);
2434 }
2435 
2436 #undef __FUNCT__
2437 #define __FUNCT__ "MatCholeskyFactorNumeric"
2438 /*@
2439    MatCholeskyFactorNumeric - Performs numeric Cholesky factorization
2440    of a symmetric matrix. Call this routine after first calling
2441    MatCholeskyFactorSymbolic().
2442 
2443    Collective on Mat
2444 
2445    Input Parameters:
2446 +  fact - the factor matrix obtained with MatGetFactor()
2447 .  mat - the initial matrix
2448 .  info - options for factorization
2449 -  fact - the symbolic factor of mat
2450 
2451 
2452    Notes:
2453    Most users should employ the simplified KSP interface for linear solvers
2454    instead of working directly with matrix algebra routines such as this.
2455    See, e.g., KSPCreate().
2456 
2457    Level: developer
2458 
2459    Concepts: matrices^Cholesky numeric factorization
2460 
2461 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric()
2462 @*/
2463 PetscErrorCode PETSCMAT_DLLEXPORT MatCholeskyFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info)
2464 {
2465   PetscErrorCode ierr;
2466 
2467   PetscFunctionBegin;
2468   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2469   PetscValidType(mat,1);
2470   PetscValidPointer(fact,2);
2471   PetscValidHeaderSpecific(fact,MAT_COOKIE,2);
2472   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2473   if (!(fact)->ops->choleskyfactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2474   if (mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N) {
2475     SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dim %D should = %D %D should = %D",mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N);
2476   }
2477   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2478 
2479   ierr = PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);
2480   ierr = (fact->ops->choleskyfactornumeric)(fact,mat,info);CHKERRQ(ierr);
2481   ierr = PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);
2482 
2483   ierr = MatView_Private(fact);CHKERRQ(ierr);
2484   ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr);
2485   PetscFunctionReturn(0);
2486 }
2487 
2488 /* ----------------------------------------------------------------*/
2489 #undef __FUNCT__
2490 #define __FUNCT__ "MatSolve"
2491 /*@
2492    MatSolve - Solves A x = b, given a factored matrix.
2493 
2494    Collective on Mat and Vec
2495 
2496    Input Parameters:
2497 +  mat - the factored matrix
2498 -  b - the right-hand-side vector
2499 
2500    Output Parameter:
2501 .  x - the result vector
2502 
2503    Notes:
2504    The vectors b and x cannot be the same.  I.e., one cannot
2505    call MatSolve(A,x,x).
2506 
2507    Notes:
2508    Most users should employ the simplified KSP interface for linear solvers
2509    instead of working directly with matrix algebra routines such as this.
2510    See, e.g., KSPCreate().
2511 
2512    Level: developer
2513 
2514    Concepts: matrices^triangular solves
2515 
2516 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd()
2517 @*/
2518 PetscErrorCode PETSCMAT_DLLEXPORT MatSolve(Mat mat,Vec b,Vec x)
2519 {
2520   PetscErrorCode ierr;
2521 
2522   PetscFunctionBegin;
2523   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2524   PetscValidType(mat,1);
2525   PetscValidHeaderSpecific(b,VEC_COOKIE,2);
2526   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
2527   PetscCheckSameComm(mat,1,b,2);
2528   PetscCheckSameComm(mat,1,x,3);
2529   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2530   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2531   if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
2532   if (mat->rmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N);
2533   if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n);
2534   if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0);
2535   if (!mat->ops->solve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2536   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2537 
2538   ierr = PetscLogEventBegin(MAT_Solve,mat,b,x,0);CHKERRQ(ierr);
2539   ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr);
2540   ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr);
2541   ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
2542   PetscFunctionReturn(0);
2543 }
2544 
2545 #undef __FUNCT__
2546 #define __FUNCT__ "MatMatSolve_Basic"
2547 PetscErrorCode PETSCMAT_DLLEXPORT MatMatSolve_Basic(Mat A,Mat B,Mat X)
2548 {
2549   PetscErrorCode ierr;
2550   Vec            b,x;
2551   PetscInt       m,N,i;
2552   PetscScalar    *bb,*xx;
2553 
2554   PetscFunctionBegin;
2555   ierr = MatGetArray(B,&bb);CHKERRQ(ierr);
2556   ierr = MatGetArray(X,&xx);CHKERRQ(ierr);
2557   ierr = MatGetLocalSize(B,&m,PETSC_NULL);CHKERRQ(ierr);  /* number local rows */
2558   ierr = MatGetSize(B,PETSC_NULL,&N);CHKERRQ(ierr);       /* total columns in dense matrix */
2559   ierr = VecCreateMPIWithArray(((PetscObject)A)->comm,m,PETSC_DETERMINE,PETSC_NULL,&b);CHKERRQ(ierr);
2560   ierr = VecCreateMPIWithArray(((PetscObject)A)->comm,m,PETSC_DETERMINE,PETSC_NULL,&x);CHKERRQ(ierr);
2561   for (i=0; i<N; i++) {
2562     ierr = VecPlaceArray(b,bb + i*m);CHKERRQ(ierr);
2563     ierr = VecPlaceArray(x,xx + i*m);CHKERRQ(ierr);
2564     ierr = MatSolve(A,b,x);CHKERRQ(ierr);
2565     ierr = VecResetArray(x);CHKERRQ(ierr);
2566     ierr = VecResetArray(b);CHKERRQ(ierr);
2567   }
2568   ierr = VecDestroy(b);CHKERRQ(ierr);
2569   ierr = VecDestroy(x);CHKERRQ(ierr);
2570   ierr = MatRestoreArray(B,&bb);CHKERRQ(ierr);
2571   ierr = MatRestoreArray(X,&xx);CHKERRQ(ierr);
2572   PetscFunctionReturn(0);
2573 }
2574 
2575 #undef __FUNCT__
2576 #define __FUNCT__ "MatMatSolve"
2577 /*@
2578    MatMatSolve - Solves A X = B, given a factored matrix.
2579 
2580    Collective on Mat
2581 
2582    Input Parameters:
2583 +  mat - the factored matrix
2584 -  B - the right-hand-side matrix  (dense matrix)
2585 
2586    Output Parameter:
2587 .  X - the result matrix (dense matrix)
2588 
2589    Notes:
2590    The matrices b and x cannot be the same.  I.e., one cannot
2591    call MatMatSolve(A,x,x).
2592 
2593    Notes:
2594    Most users should usually employ the simplified KSP interface for linear solvers
2595    instead of working directly with matrix algebra routines such as this.
2596    See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X)
2597    at a time.
2598 
2599    Level: developer
2600 
2601    Concepts: matrices^triangular solves
2602 
2603 .seealso: MatMatSolveAdd(), MatMatSolveTranspose(), MatMatSolveTransposeAdd(), MatLUFactor(), MatCholeskyFactor()
2604 @*/
2605 PetscErrorCode PETSCMAT_DLLEXPORT MatMatSolve(Mat A,Mat B,Mat X)
2606 {
2607   PetscErrorCode ierr;
2608 
2609   PetscFunctionBegin;
2610   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
2611   PetscValidType(A,1);
2612   PetscValidHeaderSpecific(B,MAT_COOKIE,2);
2613   PetscValidHeaderSpecific(X,MAT_COOKIE,3);
2614   PetscCheckSameComm(A,1,B,2);
2615   PetscCheckSameComm(A,1,X,3);
2616   if (X == B) SETERRQ(PETSC_ERR_ARG_IDN,"X and B must be different matrices");
2617   if (!A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2618   if (A->cmap->N != X->rmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat A,Mat X: global dim %D %D",A->cmap->N,X->rmap->N);
2619   if (A->rmap->N != B->rmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %D %D",A->rmap->N,B->rmap->N);
2620   if (A->rmap->n != B->rmap->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: local dim %D %D",A->rmap->n,B->rmap->n);
2621   if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0);
2622   ierr = MatPreallocated(A);CHKERRQ(ierr);
2623 
2624   ierr = PetscLogEventBegin(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr);
2625   if (!A->ops->matsolve) {
2626     ierr = PetscInfo1(A,"Mat type %s using basic MatMatSolve",((PetscObject)A)->type_name);CHKERRQ(ierr);
2627     ierr = MatMatSolve_Basic(A,B,X);CHKERRQ(ierr);
2628   } else {
2629     ierr = (*A->ops->matsolve)(A,B,X);CHKERRQ(ierr);
2630   }
2631   ierr = PetscLogEventEnd(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr);
2632   ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr);
2633   PetscFunctionReturn(0);
2634 }
2635 
2636 
2637 #undef __FUNCT__
2638 #define __FUNCT__ "MatForwardSolve"
2639 /* @
2640    MatForwardSolve - Solves L x = b, given a factored matrix, A = LU, or
2641                             U^T*D^(1/2) x = b, given a factored symmetric matrix, A = U^T*D*U,
2642 
2643    Collective on Mat and Vec
2644 
2645    Input Parameters:
2646 +  mat - the factored matrix
2647 -  b - the right-hand-side vector
2648 
2649    Output Parameter:
2650 .  x - the result vector
2651 
2652    Notes:
2653    MatSolve() should be used for most applications, as it performs
2654    a forward solve followed by a backward solve.
2655 
2656    The vectors b and x cannot be the same,  i.e., one cannot
2657    call MatForwardSolve(A,x,x).
2658 
2659    For matrix in seqsbaij format with block size larger than 1,
2660    the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet.
2661    MatForwardSolve() solves U^T*D y = b, and
2662    MatBackwardSolve() solves U x = y.
2663    Thus they do not provide a symmetric preconditioner.
2664 
2665    Most users should employ the simplified KSP interface for linear solvers
2666    instead of working directly with matrix algebra routines such as this.
2667    See, e.g., KSPCreate().
2668 
2669    Level: developer
2670 
2671    Concepts: matrices^forward solves
2672 
2673 .seealso: MatSolve(), MatBackwardSolve()
2674 @ */
2675 PetscErrorCode PETSCMAT_DLLEXPORT MatForwardSolve(Mat mat,Vec b,Vec x)
2676 {
2677   PetscErrorCode ierr;
2678 
2679   PetscFunctionBegin;
2680   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2681   PetscValidType(mat,1);
2682   PetscValidHeaderSpecific(b,VEC_COOKIE,2);
2683   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
2684   PetscCheckSameComm(mat,1,b,2);
2685   PetscCheckSameComm(mat,1,x,3);
2686   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2687   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2688   if (!mat->ops->forwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2689   if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
2690   if (mat->rmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N);
2691   if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n);
2692   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2693   ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr);
2694   ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr);
2695   ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr);
2696   ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
2697   PetscFunctionReturn(0);
2698 }
2699 
2700 #undef __FUNCT__
2701 #define __FUNCT__ "MatBackwardSolve"
2702 /* @
2703    MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU.
2704                              D^(1/2) U x = b, given a factored symmetric matrix, A = U^T*D*U,
2705 
2706    Collective on Mat and Vec
2707 
2708    Input Parameters:
2709 +  mat - the factored matrix
2710 -  b - the right-hand-side vector
2711 
2712    Output Parameter:
2713 .  x - the result vector
2714 
2715    Notes:
2716    MatSolve() should be used for most applications, as it performs
2717    a forward solve followed by a backward solve.
2718 
2719    The vectors b and x cannot be the same.  I.e., one cannot
2720    call MatBackwardSolve(A,x,x).
2721 
2722    For matrix in seqsbaij format with block size larger than 1,
2723    the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet.
2724    MatForwardSolve() solves U^T*D y = b, and
2725    MatBackwardSolve() solves U x = y.
2726    Thus they do not provide a symmetric preconditioner.
2727 
2728    Most users should employ the simplified KSP interface for linear solvers
2729    instead of working directly with matrix algebra routines such as this.
2730    See, e.g., KSPCreate().
2731 
2732    Level: developer
2733 
2734    Concepts: matrices^backward solves
2735 
2736 .seealso: MatSolve(), MatForwardSolve()
2737 @ */
2738 PetscErrorCode PETSCMAT_DLLEXPORT MatBackwardSolve(Mat mat,Vec b,Vec x)
2739 {
2740   PetscErrorCode ierr;
2741 
2742   PetscFunctionBegin;
2743   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2744   PetscValidType(mat,1);
2745   PetscValidHeaderSpecific(b,VEC_COOKIE,2);
2746   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
2747   PetscCheckSameComm(mat,1,b,2);
2748   PetscCheckSameComm(mat,1,x,3);
2749   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2750   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2751   if (!mat->ops->backwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2752   if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
2753   if (mat->rmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N);
2754   if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n);
2755   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2756 
2757   ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr);
2758   ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr);
2759   ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr);
2760   ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
2761   PetscFunctionReturn(0);
2762 }
2763 
2764 #undef __FUNCT__
2765 #define __FUNCT__ "MatSolveAdd"
2766 /*@
2767    MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix.
2768 
2769    Collective on Mat and Vec
2770 
2771    Input Parameters:
2772 +  mat - the factored matrix
2773 .  b - the right-hand-side vector
2774 -  y - the vector to be added to
2775 
2776    Output Parameter:
2777 .  x - the result vector
2778 
2779    Notes:
2780    The vectors b and x cannot be the same.  I.e., one cannot
2781    call MatSolveAdd(A,x,y,x).
2782 
2783    Most users should employ the simplified KSP interface for linear solvers
2784    instead of working directly with matrix algebra routines such as this.
2785    See, e.g., KSPCreate().
2786 
2787    Level: developer
2788 
2789    Concepts: matrices^triangular solves
2790 
2791 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd()
2792 @*/
2793 PetscErrorCode PETSCMAT_DLLEXPORT MatSolveAdd(Mat mat,Vec b,Vec y,Vec x)
2794 {
2795   PetscScalar    one = 1.0;
2796   Vec            tmp;
2797   PetscErrorCode ierr;
2798 
2799   PetscFunctionBegin;
2800   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2801   PetscValidType(mat,1);
2802   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
2803   PetscValidHeaderSpecific(b,VEC_COOKIE,3);
2804   PetscValidHeaderSpecific(x,VEC_COOKIE,4);
2805   PetscCheckSameComm(mat,1,b,2);
2806   PetscCheckSameComm(mat,1,y,2);
2807   PetscCheckSameComm(mat,1,x,3);
2808   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2809   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2810   if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
2811   if (mat->rmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N);
2812   if (mat->rmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N);
2813   if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n);
2814   if (x->map->n != y->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %D %D",x->map->n,y->map->n);
2815   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2816 
2817   ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr);
2818   if (mat->ops->solveadd)  {
2819     ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr);
2820   } else {
2821     /* do the solve then the add manually */
2822     if (x != y) {
2823       ierr = MatSolve(mat,b,x);CHKERRQ(ierr);
2824       ierr = VecAXPY(x,one,y);CHKERRQ(ierr);
2825     } else {
2826       ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr);
2827       ierr = PetscLogObjectParent(mat,tmp);CHKERRQ(ierr);
2828       ierr = VecCopy(x,tmp);CHKERRQ(ierr);
2829       ierr = MatSolve(mat,b,x);CHKERRQ(ierr);
2830       ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr);
2831       ierr = VecDestroy(tmp);CHKERRQ(ierr);
2832     }
2833   }
2834   ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr);
2835   ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
2836   PetscFunctionReturn(0);
2837 }
2838 
2839 #undef __FUNCT__
2840 #define __FUNCT__ "MatSolveTranspose"
2841 /*@
2842    MatSolveTranspose - Solves A' x = b, given a factored matrix.
2843 
2844    Collective on Mat and Vec
2845 
2846    Input Parameters:
2847 +  mat - the factored matrix
2848 -  b - the right-hand-side vector
2849 
2850    Output Parameter:
2851 .  x - the result vector
2852 
2853    Notes:
2854    The vectors b and x cannot be the same.  I.e., one cannot
2855    call MatSolveTranspose(A,x,x).
2856 
2857    Most users should employ the simplified KSP interface for linear solvers
2858    instead of working directly with matrix algebra routines such as this.
2859    See, e.g., KSPCreate().
2860 
2861    Level: developer
2862 
2863    Concepts: matrices^triangular solves
2864 
2865 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd()
2866 @*/
2867 PetscErrorCode PETSCMAT_DLLEXPORT MatSolveTranspose(Mat mat,Vec b,Vec x)
2868 {
2869   PetscErrorCode ierr;
2870 
2871   PetscFunctionBegin;
2872   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2873   PetscValidType(mat,1);
2874   PetscValidHeaderSpecific(b,VEC_COOKIE,2);
2875   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
2876   PetscCheckSameComm(mat,1,b,2);
2877   PetscCheckSameComm(mat,1,x,3);
2878   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2879   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2880   if (!mat->ops->solvetranspose) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s",((PetscObject)mat)->type_name);
2881   if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N);
2882   if (mat->cmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->cmap->N,b->map->N);
2883   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2884   ierr = PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr);
2885   ierr = (*mat->ops->solvetranspose)(mat,b,x);CHKERRQ(ierr);
2886   ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr);
2887   ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
2888   PetscFunctionReturn(0);
2889 }
2890 
2891 #undef __FUNCT__
2892 #define __FUNCT__ "MatSolveTransposeAdd"
2893 /*@
2894    MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a
2895                       factored matrix.
2896 
2897    Collective on Mat and Vec
2898 
2899    Input Parameters:
2900 +  mat - the factored matrix
2901 .  b - the right-hand-side vector
2902 -  y - the vector to be added to
2903 
2904    Output Parameter:
2905 .  x - the result vector
2906 
2907    Notes:
2908    The vectors b and x cannot be the same.  I.e., one cannot
2909    call MatSolveTransposeAdd(A,x,y,x).
2910 
2911    Most users should employ the simplified KSP interface for linear solvers
2912    instead of working directly with matrix algebra routines such as this.
2913    See, e.g., KSPCreate().
2914 
2915    Level: developer
2916 
2917    Concepts: matrices^triangular solves
2918 
2919 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose()
2920 @*/
2921 PetscErrorCode PETSCMAT_DLLEXPORT MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x)
2922 {
2923   PetscScalar    one = 1.0;
2924   PetscErrorCode ierr;
2925   Vec            tmp;
2926 
2927   PetscFunctionBegin;
2928   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
2929   PetscValidType(mat,1);
2930   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
2931   PetscValidHeaderSpecific(b,VEC_COOKIE,3);
2932   PetscValidHeaderSpecific(x,VEC_COOKIE,4);
2933   PetscCheckSameComm(mat,1,b,2);
2934   PetscCheckSameComm(mat,1,y,3);
2935   PetscCheckSameComm(mat,1,x,4);
2936   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2937   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2938   if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N);
2939   if (mat->cmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->cmap->N,b->map->N);
2940   if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N);
2941   if (x->map->n != y->map->n)   SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %D %D",x->map->n,y->map->n);
2942   ierr = MatPreallocated(mat);CHKERRQ(ierr);
2943 
2944   ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr);
2945   if (mat->ops->solvetransposeadd) {
2946     ierr = (*mat->ops->solvetransposeadd)(mat,b,y,x);CHKERRQ(ierr);
2947   } else {
2948     /* do the solve then the add manually */
2949     if (x != y) {
2950       ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr);
2951       ierr = VecAXPY(x,one,y);CHKERRQ(ierr);
2952     } else {
2953       ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr);
2954       ierr = PetscLogObjectParent(mat,tmp);CHKERRQ(ierr);
2955       ierr = VecCopy(x,tmp);CHKERRQ(ierr);
2956       ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr);
2957       ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr);
2958       ierr = VecDestroy(tmp);CHKERRQ(ierr);
2959     }
2960   }
2961   ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr);
2962   ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
2963   PetscFunctionReturn(0);
2964 }
2965 /* ----------------------------------------------------------------*/
2966 
2967 #undef __FUNCT__
2968 #define __FUNCT__ "MatRelax"
2969 /*@
2970    MatRelax - Computes relaxation (SOR, Gauss-Seidel) sweeps.
2971 
2972    Collective on Mat and Vec
2973 
2974    Input Parameters:
2975 +  mat - the matrix
2976 .  b - the right hand side
2977 .  omega - the relaxation factor
2978 .  flag - flag indicating the type of SOR (see below)
2979 .  shift -  diagonal shift
2980 .  its - the number of iterations
2981 -  lits - the number of local iterations
2982 
2983    Output Parameters:
2984 .  x - the solution (can contain an initial guess)
2985 
2986    SOR Flags:
2987 .     SOR_FORWARD_SWEEP - forward SOR
2988 .     SOR_BACKWARD_SWEEP - backward SOR
2989 .     SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR)
2990 .     SOR_LOCAL_FORWARD_SWEEP - local forward SOR
2991 .     SOR_LOCAL_BACKWARD_SWEEP - local forward SOR
2992 .     SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR
2993 .     SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies
2994          upper/lower triangular part of matrix to
2995          vector (with omega)
2996 .     SOR_ZERO_INITIAL_GUESS - zero initial guess
2997 
2998    Notes:
2999    SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and
3000    SOR_LOCAL_SYMMETRIC_SWEEP perform separate independent smoothings
3001    on each processor.
3002 
3003    Application programmers will not generally use MatRelax() directly,
3004    but instead will employ the KSP/PC interface.
3005 
3006    Notes for Advanced Users:
3007    The flags are implemented as bitwise inclusive or operations.
3008    For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP)
3009    to specify a zero initial guess for SSOR.
3010 
3011    Most users should employ the simplified KSP interface for linear solvers
3012    instead of working directly with matrix algebra routines such as this.
3013    See, e.g., KSPCreate().
3014 
3015    See also, MatPBRelax(). This routine will automatically call the point block
3016    version if the point version is not available.
3017 
3018    Level: developer
3019 
3020    Concepts: matrices^relaxation
3021    Concepts: matrices^SOR
3022    Concepts: matrices^Gauss-Seidel
3023 
3024 @*/
3025 PetscErrorCode PETSCMAT_DLLEXPORT MatRelax(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec x)
3026 {
3027   PetscErrorCode ierr;
3028 
3029   PetscFunctionBegin;
3030   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3031   PetscValidType(mat,1);
3032   PetscValidHeaderSpecific(b,VEC_COOKIE,2);
3033   PetscValidHeaderSpecific(x,VEC_COOKIE,8);
3034   PetscCheckSameComm(mat,1,b,2);
3035   PetscCheckSameComm(mat,1,x,8);
3036   if (!mat->ops->relax && !mat->ops->pbrelax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3037   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3038   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3039   if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
3040   if (mat->rmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N);
3041   if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n);
3042   if (its <= 0) SETERRQ1(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D positive",its);
3043   if (lits <= 0) SETERRQ1(PETSC_ERR_ARG_WRONG,"Relaxation requires local its %D positive",lits);
3044 
3045   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3046   ierr = PetscLogEventBegin(MAT_Relax,mat,b,x,0);CHKERRQ(ierr);
3047   if (mat->ops->relax) {
3048     ierr =(*mat->ops->relax)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr);
3049   } else {
3050     ierr =(*mat->ops->pbrelax)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr);
3051   }
3052   ierr = PetscLogEventEnd(MAT_Relax,mat,b,x,0);CHKERRQ(ierr);
3053   ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
3054   PetscFunctionReturn(0);
3055 }
3056 
3057 #undef __FUNCT__
3058 #define __FUNCT__ "MatPBRelax"
3059 /*@
3060    MatPBRelax - Computes relaxation (SOR, Gauss-Seidel) sweeps.
3061 
3062    Collective on Mat and Vec
3063 
3064    See MatRelax() for usage
3065 
3066    For multi-component PDEs where the Jacobian is stored in a point block format
3067    (with the PETSc BAIJ matrix formats) the relaxation is done one point block at
3068    a time. That is, the small (for example, 4 by 4) blocks along the diagonal are solved
3069    simultaneously (that is a 4 by 4 linear solve is done) to update all the values at a point.
3070 
3071    Level: developer
3072 
3073 @*/
3074 PetscErrorCode PETSCMAT_DLLEXPORT MatPBRelax(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec x)
3075 {
3076   PetscErrorCode ierr;
3077 
3078   PetscFunctionBegin;
3079   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3080   PetscValidType(mat,1);
3081   PetscValidHeaderSpecific(b,VEC_COOKIE,2);
3082   PetscValidHeaderSpecific(x,VEC_COOKIE,8);
3083   PetscCheckSameComm(mat,1,b,2);
3084   PetscCheckSameComm(mat,1,x,8);
3085   if (!mat->ops->pbrelax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3086   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3087   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3088   if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
3089   if (mat->rmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N);
3090   if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n);
3091   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3092 
3093   ierr = PetscLogEventBegin(MAT_Relax,mat,b,x,0);CHKERRQ(ierr);
3094   ierr =(*mat->ops->pbrelax)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr);
3095   ierr = PetscLogEventEnd(MAT_Relax,mat,b,x,0);CHKERRQ(ierr);
3096   ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
3097   PetscFunctionReturn(0);
3098 }
3099 
3100 #undef __FUNCT__
3101 #define __FUNCT__ "MatCopy_Basic"
3102 /*
3103       Default matrix copy routine.
3104 */
3105 PetscErrorCode MatCopy_Basic(Mat A,Mat B,MatStructure str)
3106 {
3107   PetscErrorCode    ierr;
3108   PetscInt          i,rstart,rend,nz;
3109   const PetscInt    *cwork;
3110   const PetscScalar *vwork;
3111 
3112   PetscFunctionBegin;
3113   if (B->assembled) {
3114     ierr = MatZeroEntries(B);CHKERRQ(ierr);
3115   }
3116   ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr);
3117   for (i=rstart; i<rend; i++) {
3118     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
3119     ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
3120     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
3121   }
3122   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3123   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3124   ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr);
3125   PetscFunctionReturn(0);
3126 }
3127 
3128 #undef __FUNCT__
3129 #define __FUNCT__ "MatCopy"
3130 /*@
3131    MatCopy - Copys a matrix to another matrix.
3132 
3133    Collective on Mat
3134 
3135    Input Parameters:
3136 +  A - the matrix
3137 -  str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN
3138 
3139    Output Parameter:
3140 .  B - where the copy is put
3141 
3142    Notes:
3143    If you use SAME_NONZERO_PATTERN then the two matrices had better have the
3144    same nonzero pattern or the routine will crash.
3145 
3146    MatCopy() copies the matrix entries of a matrix to another existing
3147    matrix (after first zeroing the second matrix).  A related routine is
3148    MatConvert(), which first creates a new matrix and then copies the data.
3149 
3150    Level: intermediate
3151 
3152    Concepts: matrices^copying
3153 
3154 .seealso: MatConvert(), MatDuplicate()
3155 
3156 @*/
3157 PetscErrorCode PETSCMAT_DLLEXPORT MatCopy(Mat A,Mat B,MatStructure str)
3158 {
3159   PetscErrorCode ierr;
3160 
3161   PetscFunctionBegin;
3162   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
3163   PetscValidHeaderSpecific(B,MAT_COOKIE,2);
3164   PetscValidType(A,1);
3165   PetscValidType(B,2);
3166   MatPreallocated(B);
3167   PetscCheckSameComm(A,1,B,2);
3168   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3169   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3170   if (A->rmap->N != B->rmap->N || A->cmap->N != B->cmap->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim (%D,%D) (%D,%D)",A->rmap->N,B->rmap->N,A->cmap->N,B->cmap->N);
3171   ierr = MatPreallocated(A);CHKERRQ(ierr);
3172 
3173   ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr);
3174   if (A->ops->copy) {
3175     ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr);
3176   } else { /* generic conversion */
3177     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
3178   }
3179   if (A->mapping) {
3180     if (B->mapping) {ierr = ISLocalToGlobalMappingDestroy(B->mapping);CHKERRQ(ierr);B->mapping = 0;}
3181     ierr = MatSetLocalToGlobalMapping(B,A->mapping);CHKERRQ(ierr);
3182   }
3183   if (A->bmapping) {
3184     if (B->bmapping) {ierr = ISLocalToGlobalMappingDestroy(B->bmapping);CHKERRQ(ierr);B->bmapping = 0;}
3185     ierr = MatSetLocalToGlobalMappingBlock(B,A->mapping);CHKERRQ(ierr);
3186   }
3187   ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr);
3188   ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr);
3189   PetscFunctionReturn(0);
3190 }
3191 
3192 #undef __FUNCT__
3193 #define __FUNCT__ "MatConvert"
3194 /*@C
3195    MatConvert - Converts a matrix to another matrix, either of the same
3196    or different type.
3197 
3198    Collective on Mat
3199 
3200    Input Parameters:
3201 +  mat - the matrix
3202 .  newtype - new matrix type.  Use MATSAME to create a new matrix of the
3203    same type as the original matrix.
3204 -  reuse - denotes if the destination matrix is to be created or reused.  Currently
3205    MAT_REUSE_MATRIX is only supported for inplace conversion, otherwise use
3206    MAT_INITIAL_MATRIX.
3207 
3208    Output Parameter:
3209 .  M - pointer to place new matrix
3210 
3211    Notes:
3212    MatConvert() first creates a new matrix and then copies the data from
3213    the first matrix.  A related routine is MatCopy(), which copies the matrix
3214    entries of one matrix to another already existing matrix context.
3215 
3216    Cannot be used to convert a sequential matrix to parallel or parallel to sequential,
3217    the MPI communicator of the generated matrix is always the same as the communicator
3218    of the input matrix.
3219 
3220    Level: intermediate
3221 
3222    Concepts: matrices^converting between storage formats
3223 
3224 .seealso: MatCopy(), MatDuplicate()
3225 @*/
3226 PetscErrorCode PETSCMAT_DLLEXPORT MatConvert(Mat mat, const MatType newtype,MatReuse reuse,Mat *M)
3227 {
3228   PetscErrorCode         ierr;
3229   PetscTruth             sametype,issame,flg;
3230   char                   convname[256],mtype[256];
3231   Mat                    B;
3232 
3233   PetscFunctionBegin;
3234   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3235   PetscValidType(mat,1);
3236   PetscValidPointer(M,3);
3237   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3238   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3239   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3240 
3241   ierr = PetscOptionsGetString(((PetscObject)mat)->prefix,"-matconvert_type",mtype,256,&flg);CHKERRQ(ierr);
3242   if (flg) {
3243     newtype = mtype;
3244   }
3245   ierr = PetscTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr);
3246   ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr);
3247   if ((reuse == MAT_REUSE_MATRIX) && (mat != *M)) {
3248     SETERRQ(PETSC_ERR_SUP,"MAT_REUSE_MATRIX only supported for in-place conversion currently");
3249   }
3250 
3251   if ((reuse == MAT_REUSE_MATRIX) && (issame || sametype)) PetscFunctionReturn(0);
3252 
3253   if ((sametype || issame) && (reuse==MAT_INITIAL_MATRIX) && mat->ops->duplicate) {
3254     ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr);
3255   } else {
3256     PetscErrorCode (*conv)(Mat, const MatType,MatReuse,Mat*)=PETSC_NULL;
3257     const char     *prefix[3] = {"seq","mpi",""};
3258     PetscInt       i;
3259     /*
3260        Order of precedence:
3261        1) See if a specialized converter is known to the current matrix.
3262        2) See if a specialized converter is known to the desired matrix class.
3263        3) See if a good general converter is registered for the desired class
3264           (as of 6/27/03 only MATMPIADJ falls into this category).
3265        4) See if a good general converter is known for the current matrix.
3266        5) Use a really basic converter.
3267     */
3268 
3269     /* 1) See if a specialized converter is known to the current matrix and the desired class */
3270     for (i=0; i<3; i++) {
3271       ierr = PetscStrcpy(convname,"MatConvert_");CHKERRQ(ierr);
3272       ierr = PetscStrcat(convname,((PetscObject)mat)->type_name);CHKERRQ(ierr);
3273       ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
3274       ierr = PetscStrcat(convname,prefix[i]);CHKERRQ(ierr);
3275       ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
3276       ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
3277       ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)(void))&conv);CHKERRQ(ierr);
3278       if (conv) goto foundconv;
3279     }
3280 
3281     /* 2)  See if a specialized converter is known to the desired matrix class. */
3282     ierr = MatCreate(((PetscObject)mat)->comm,&B);CHKERRQ(ierr);
3283     ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->N,mat->cmap->N);CHKERRQ(ierr);
3284     ierr = MatSetType(B,newtype);CHKERRQ(ierr);
3285     for (i=0; i<3; i++) {
3286       ierr = PetscStrcpy(convname,"MatConvert_");CHKERRQ(ierr);
3287       ierr = PetscStrcat(convname,((PetscObject)mat)->type_name);CHKERRQ(ierr);
3288       ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
3289       ierr = PetscStrcat(convname,prefix[i]);CHKERRQ(ierr);
3290       ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
3291       ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
3292       ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
3293       if (conv) {
3294         ierr = MatDestroy(B);CHKERRQ(ierr);
3295         goto foundconv;
3296       }
3297     }
3298 
3299     /* 3) See if a good general converter is registered for the desired class */
3300     conv = B->ops->convertfrom;
3301     ierr = MatDestroy(B);CHKERRQ(ierr);
3302     if (conv) goto foundconv;
3303 
3304     /* 4) See if a good general converter is known for the current matrix */
3305     if (mat->ops->convert) {
3306       conv = mat->ops->convert;
3307     }
3308     if (conv) goto foundconv;
3309 
3310     /* 5) Use a really basic converter. */
3311     conv = MatConvert_Basic;
3312 
3313     foundconv:
3314     ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
3315     ierr = (*conv)(mat,newtype,reuse,M);CHKERRQ(ierr);
3316     ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
3317   }
3318   ierr = PetscObjectStateIncrease((PetscObject)*M);CHKERRQ(ierr);
3319   PetscFunctionReturn(0);
3320 }
3321 
3322 #undef __FUNCT__
3323 #define __FUNCT__ "MatGetFactor"
3324 /*@C
3325    MatGetFactor - Returns a matrix suitable to calls to MatXXFactorSymbolic()
3326 
3327    Collective on Mat
3328 
3329    Input Parameters:
3330 +  mat - the matrix
3331 .  type - name of solver type, for example, spooles, superlu, plapack, petsc (to use PETSc's default)
3332 -  ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU,
3333 
3334    Output Parameters:
3335 .  f - the factor matrix used with MatXXFactorSymbolic() calls
3336 
3337    Notes:
3338       Some PETSc matrix formats have alternative solvers available that are contained in alternative packages
3339      such as superlu, mumps, spooles etc.
3340 
3341       PETSc must have been config/configure.py to use the external solver, using the option --download-package
3342 
3343    Level: intermediate
3344 
3345 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable()
3346 @*/
3347 PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor(Mat mat, const MatSolverPackage type,MatFactorType ftype,Mat *f)
3348 {
3349   PetscErrorCode         ierr;
3350   char                   convname[256];
3351   PetscErrorCode         (*conv)(Mat,MatFactorType,Mat*);
3352 
3353   PetscFunctionBegin;
3354   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3355   PetscValidType(mat,1);
3356 
3357   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3358   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3359 
3360   ierr = PetscStrcpy(convname,"MatGetFactor_");CHKERRQ(ierr);
3361   ierr = PetscStrcat(convname,((PetscObject)mat)->type_name);CHKERRQ(ierr);
3362   ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
3363   ierr = PetscStrcat(convname,type);CHKERRQ(ierr);
3364   ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
3365   ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)(void))&conv);CHKERRQ(ierr);
3366   if (!conv) {
3367     PetscTruth flag;
3368     ierr = PetscStrcasecmp(MAT_SOLVER_PETSC,type,&flag);CHKERRQ(ierr);
3369     if (flag) {
3370       SETERRQ1(PETSC_ERR_SUP,"Matrix format %s does not have a built-in PETSc direct solver",((PetscObject)mat)->type_name);
3371     } else {
3372       SETERRQ3(PETSC_ERR_SUP,"Matrix format %s does not have a solver %s. Perhaps you must config/configure.py with --download-%s",((PetscObject)mat)->type_name,type,type);
3373     }
3374   }
3375   ierr = (*conv)(mat,ftype,f);CHKERRQ(ierr);
3376   PetscFunctionReturn(0);
3377 }
3378 
3379 #undef __FUNCT__
3380 #define __FUNCT__ "MatGetFactorAvailable"
3381 /*@C
3382    MatGetFactorAvailable - Returns a a flag if matrix supports particular package and factor type
3383 
3384    Collective on Mat
3385 
3386    Input Parameters:
3387 +  mat - the matrix
3388 .  type - name of solver type, for example, spooles, superlu, plapack, petsc (to use PETSc's default)
3389 -  ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU,
3390 
3391    Output Parameter:
3392 .    flg - PETSC_TRUE if the factorization is available
3393 
3394    Notes:
3395       Some PETSc matrix formats have alternative solvers available that are contained in alternative packages
3396      such as superlu, mumps, spooles etc.
3397 
3398       PETSc must have been config/configure.py to use the external solver, using the option --download-package
3399 
3400    Level: intermediate
3401 
3402 .seealso: MatCopy(), MatDuplicate(), MatGetFactor()
3403 @*/
3404 PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable(Mat mat, const MatSolverPackage type,MatFactorType ftype,PetscTruth *flg)
3405 {
3406   PetscErrorCode         ierr;
3407   char                   convname[256];
3408   PetscErrorCode         (*conv)(Mat,MatFactorType,PetscTruth*);
3409 
3410   PetscFunctionBegin;
3411   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3412   PetscValidType(mat,1);
3413 
3414   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3415   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3416 
3417   ierr = PetscStrcpy(convname,"MatGetFactorAvailable_");CHKERRQ(ierr);
3418   ierr = PetscStrcat(convname,((PetscObject)mat)->type_name);CHKERRQ(ierr);
3419   ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
3420   ierr = PetscStrcat(convname,type);CHKERRQ(ierr);
3421   ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
3422   ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)(void))&conv);CHKERRQ(ierr);
3423   if (!conv) {
3424     *flg = PETSC_FALSE;
3425   } else {
3426     ierr = (*conv)(mat,ftype,flg);CHKERRQ(ierr);
3427   }
3428   PetscFunctionReturn(0);
3429 }
3430 
3431 
3432 #undef __FUNCT__
3433 #define __FUNCT__ "MatDuplicate"
3434 /*@
3435    MatDuplicate - Duplicates a matrix including the non-zero structure.
3436 
3437    Collective on Mat
3438 
3439    Input Parameters:
3440 +  mat - the matrix
3441 -  op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy nonzero
3442         values as well or not
3443 
3444    Output Parameter:
3445 .  M - pointer to place new matrix
3446 
3447    Level: intermediate
3448 
3449    Concepts: matrices^duplicating
3450 
3451 .seealso: MatCopy(), MatConvert()
3452 @*/
3453 PetscErrorCode PETSCMAT_DLLEXPORT MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M)
3454 {
3455   PetscErrorCode ierr;
3456   Mat            B;
3457 
3458   PetscFunctionBegin;
3459   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3460   PetscValidType(mat,1);
3461   PetscValidPointer(M,3);
3462   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3463   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3464   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3465 
3466   *M  = 0;
3467   if (!mat->ops->duplicate) {
3468     SETERRQ(PETSC_ERR_SUP,"Not written for this matrix type");
3469   }
3470   ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
3471   ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr);
3472   B = *M;
3473   if (mat->mapping) {
3474     ierr = MatSetLocalToGlobalMapping(B,mat->mapping);CHKERRQ(ierr);
3475   }
3476   if (mat->bmapping) {
3477     ierr = MatSetLocalToGlobalMappingBlock(B,mat->bmapping);CHKERRQ(ierr);
3478   }
3479   ierr = PetscMapCopy(((PetscObject)mat)->comm,mat->rmap,B->rmap);CHKERRQ(ierr);
3480   ierr = PetscMapCopy(((PetscObject)mat)->comm,mat->cmap,B->cmap);CHKERRQ(ierr);
3481 
3482   ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
3483   ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr);
3484   PetscFunctionReturn(0);
3485 }
3486 
3487 #undef __FUNCT__
3488 #define __FUNCT__ "MatGetDiagonal"
3489 /*@
3490    MatGetDiagonal - Gets the diagonal of a matrix.
3491 
3492    Collective on Mat and Vec
3493 
3494    Input Parameters:
3495 +  mat - the matrix
3496 -  v - the vector for storing the diagonal
3497 
3498    Output Parameter:
3499 .  v - the diagonal of the matrix
3500 
3501    Level: intermediate
3502 
3503    Concepts: matrices^accessing diagonals
3504 
3505 .seealso: MatGetRow(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMaxAbs()
3506 @*/
3507 PetscErrorCode PETSCMAT_DLLEXPORT MatGetDiagonal(Mat mat,Vec v)
3508 {
3509   PetscErrorCode ierr;
3510 
3511   PetscFunctionBegin;
3512   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3513   PetscValidType(mat,1);
3514   PetscValidHeaderSpecific(v,VEC_COOKIE,2);
3515   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3516   if (!mat->ops->getdiagonal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3517   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3518 
3519   ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr);
3520   ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr);
3521   PetscFunctionReturn(0);
3522 }
3523 
3524 #undef __FUNCT__
3525 #define __FUNCT__ "MatGetRowMin"
3526 /*@
3527    MatGetRowMin - Gets the minimum value (of the real part) of each
3528         row of the matrix
3529 
3530    Collective on Mat and Vec
3531 
3532    Input Parameters:
3533 .  mat - the matrix
3534 
3535    Output Parameter:
3536 +  v - the vector for storing the maximums
3537 -  idx - the indices of the column found for each row (optional)
3538 
3539    Level: intermediate
3540 
3541    Notes: The result of this call are the same as if one converted the matrix to dense format
3542       and found the minimum value in each row (i.e. the implicit zeros are counted as zeros).
3543 
3544     This code is only implemented for a couple of matrix formats.
3545 
3546    Concepts: matrices^getting row maximums
3547 
3548 .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMaxAbs(),
3549           MatGetRowMax()
3550 @*/
3551 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowMin(Mat mat,Vec v,PetscInt idx[])
3552 {
3553   PetscErrorCode ierr;
3554 
3555   PetscFunctionBegin;
3556   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3557   PetscValidType(mat,1);
3558   PetscValidHeaderSpecific(v,VEC_COOKIE,2);
3559   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3560   if (!mat->ops->getrowmax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3561   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3562 
3563   ierr = (*mat->ops->getrowmin)(mat,v,idx);CHKERRQ(ierr);
3564   ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr);
3565   PetscFunctionReturn(0);
3566 }
3567 
3568 #undef __FUNCT__
3569 #define __FUNCT__ "MatGetRowMax"
3570 /*@
3571    MatGetRowMax - Gets the maximum value (of the real part) of each
3572         row of the matrix
3573 
3574    Collective on Mat and Vec
3575 
3576    Input Parameters:
3577 .  mat - the matrix
3578 
3579    Output Parameter:
3580 +  v - the vector for storing the maximums
3581 -  idx - the indices of the column found for each row (optional)
3582 
3583    Level: intermediate
3584 
3585    Notes: The result of this call are the same as if one converted the matrix to dense format
3586       and found the minimum value in each row (i.e. the implicit zeros are counted as zeros).
3587 
3588     This code is only implemented for a couple of matrix formats.
3589 
3590    Concepts: matrices^getting row maximums
3591 
3592 .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMaxAbs(), MatGetRowMin()
3593 @*/
3594 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowMax(Mat mat,Vec v,PetscInt idx[])
3595 {
3596   PetscErrorCode ierr;
3597 
3598   PetscFunctionBegin;
3599   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3600   PetscValidType(mat,1);
3601   PetscValidHeaderSpecific(v,VEC_COOKIE,2);
3602   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3603   if (!mat->ops->getrowmax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3604   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3605 
3606   ierr = (*mat->ops->getrowmax)(mat,v,idx);CHKERRQ(ierr);
3607   ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr);
3608   PetscFunctionReturn(0);
3609 }
3610 
3611 #undef __FUNCT__
3612 #define __FUNCT__ "MatGetRowMaxAbs"
3613 /*@
3614    MatGetRowMaxAbs - Gets the maximum value (in absolute value) of each
3615         row of the matrix
3616 
3617    Collective on Mat and Vec
3618 
3619    Input Parameters:
3620 .  mat - the matrix
3621 
3622    Output Parameter:
3623 +  v - the vector for storing the maximums
3624 -  idx - the indices of the column found for each row (optional)
3625 
3626    Level: intermediate
3627 
3628    Notes: if a row is completely empty or has only 0.0 values then the idx[] value for that
3629     row is 0 (the first column).
3630 
3631     This code is only implemented for a couple of matrix formats.
3632 
3633    Concepts: matrices^getting row maximums
3634 
3635 .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMax(), MatGetRowMin()
3636 @*/
3637 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowMaxAbs(Mat mat,Vec v,PetscInt idx[])
3638 {
3639   PetscErrorCode ierr;
3640 
3641   PetscFunctionBegin;
3642   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3643   PetscValidType(mat,1);
3644   PetscValidHeaderSpecific(v,VEC_COOKIE,2);
3645   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3646   if (!mat->ops->getrowmaxabs) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3647   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3648   if (idx) {ierr = PetscMemzero(idx,mat->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);}
3649 
3650   ierr = (*mat->ops->getrowmaxabs)(mat,v,idx);CHKERRQ(ierr);
3651   ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr);
3652   PetscFunctionReturn(0);
3653 }
3654 
3655 #undef __FUNCT__
3656 #define __FUNCT__ "MatGetRowSum"
3657 /*@
3658    MatGetRowSum - Gets the sum of each row of the matrix
3659 
3660    Collective on Mat and Vec
3661 
3662    Input Parameters:
3663 .  mat - the matrix
3664 
3665    Output Parameter:
3666 .  v - the vector for storing the maximums
3667 
3668    Level: intermediate
3669 
3670    Notes: This code is slow since it is not currently specialized for different formats
3671 
3672    Concepts: matrices^getting row sums
3673 
3674 .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMax(), MatGetRowMin()
3675 @*/
3676 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowSum(Mat mat, Vec v)
3677 {
3678   PetscInt       start, end, row;
3679   PetscScalar   *array;
3680   PetscErrorCode ierr;
3681 
3682   PetscFunctionBegin;
3683   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3684   PetscValidType(mat,1);
3685   PetscValidHeaderSpecific(v,VEC_COOKIE,2);
3686   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3687   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3688   ierr = MatGetOwnershipRange(mat, &start, &end);CHKERRQ(ierr);
3689   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
3690   for(row = start; row < end; ++row) {
3691     PetscInt           ncols, col;
3692     const PetscInt    *cols;
3693     const PetscScalar *vals;
3694 
3695     array[row - start] = 0.0;
3696     ierr = MatGetRow(mat, row, &ncols, &cols, &vals);CHKERRQ(ierr);
3697     for(col = 0; col < ncols; col++) {
3698       array[row - start] += vals[col];
3699     }
3700     ierr = MatRestoreRow(mat, row, &ncols, &cols, &vals);CHKERRQ(ierr);
3701   }
3702   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
3703   ierr = PetscObjectStateIncrease((PetscObject) v);CHKERRQ(ierr);
3704   PetscFunctionReturn(0);
3705 }
3706 
3707 #undef __FUNCT__
3708 #define __FUNCT__ "MatTranspose"
3709 /*@
3710    MatTranspose - Computes an in-place or out-of-place transpose of a matrix.
3711 
3712    Collective on Mat
3713 
3714    Input Parameter:
3715 +  mat - the matrix to transpose
3716 -  reuse - store the transpose matrix in the provided B
3717 
3718    Output Parameters:
3719 .  B - the transpose
3720 
3721    Notes:
3722      If you  pass in &mat for B the transpose will be done in place
3723 
3724    Level: intermediate
3725 
3726    Concepts: matrices^transposing
3727 
3728 .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose()
3729 @*/
3730 PetscErrorCode PETSCMAT_DLLEXPORT MatTranspose(Mat mat,MatReuse reuse,Mat *B)
3731 {
3732   PetscErrorCode ierr;
3733 
3734   PetscFunctionBegin;
3735   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3736   PetscValidType(mat,1);
3737   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3738   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3739   if (!mat->ops->transpose) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3740   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3741 
3742   ierr = PetscLogEventBegin(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr);
3743   ierr = (*mat->ops->transpose)(mat,reuse,B);CHKERRQ(ierr);
3744   ierr = PetscLogEventEnd(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr);
3745   if (B) {ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr);}
3746   PetscFunctionReturn(0);
3747 }
3748 
3749 #undef __FUNCT__
3750 #define __FUNCT__ "MatIsTranspose"
3751 /*@
3752    MatIsTranspose - Test whether a matrix is another one's transpose,
3753         or its own, in which case it tests symmetry.
3754 
3755    Collective on Mat
3756 
3757    Input Parameter:
3758 +  A - the matrix to test
3759 -  B - the matrix to test against, this can equal the first parameter
3760 
3761    Output Parameters:
3762 .  flg - the result
3763 
3764    Notes:
3765    Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm
3766    has a running time of the order of the number of nonzeros; the parallel
3767    test involves parallel copies of the block-offdiagonal parts of the matrix.
3768 
3769    Level: intermediate
3770 
3771    Concepts: matrices^transposing, matrix^symmetry
3772 
3773 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian()
3774 @*/
3775 PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscTruth *flg)
3776 {
3777   PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscTruth*),(*g)(Mat,Mat,PetscReal,PetscTruth*);
3778 
3779   PetscFunctionBegin;
3780   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
3781   PetscValidHeaderSpecific(B,MAT_COOKIE,2);
3782   PetscValidPointer(flg,3);
3783   ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",(void (**)(void))&f);CHKERRQ(ierr);
3784   ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",(void (**)(void))&g);CHKERRQ(ierr);
3785   if (f && g) {
3786     if (f==g) {
3787       ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr);
3788     } else {
3789       SETERRQ(PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for symmetry test");
3790     }
3791   }
3792   PetscFunctionReturn(0);
3793 }
3794 
3795 #undef __FUNCT__
3796 #define __FUNCT__ "MatIsHermitianTranspose"
3797 /*@
3798    MatIsHermitianTranspose - Test whether a matrix is another one's Hermitian transpose,
3799 
3800    Collective on Mat
3801 
3802    Input Parameter:
3803 +  A - the matrix to test
3804 -  B - the matrix to test against, this can equal the first parameter
3805 
3806    Output Parameters:
3807 .  flg - the result
3808 
3809    Notes:
3810    Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm
3811    has a running time of the order of the number of nonzeros; the parallel
3812    test involves parallel copies of the block-offdiagonal parts of the matrix.
3813 
3814    Level: intermediate
3815 
3816    Concepts: matrices^transposing, matrix^symmetry
3817 
3818 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian(), MatIsTranspose()
3819 @*/
3820 PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose(Mat A,Mat B,PetscReal tol,PetscTruth *flg)
3821 {
3822   PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscTruth*),(*g)(Mat,Mat,PetscReal,PetscTruth*);
3823 
3824   PetscFunctionBegin;
3825   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
3826   PetscValidHeaderSpecific(B,MAT_COOKIE,2);
3827   PetscValidPointer(flg,3);
3828   ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsHermitianTranspose_C",(void (**)(void))&f);CHKERRQ(ierr);
3829   ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsHermitianTranspose_C",(void (**)(void))&g);CHKERRQ(ierr);
3830   if (f && g) {
3831     if (f==g) {
3832       ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr);
3833     } else {
3834       SETERRQ(PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for Hermitian test");
3835     }
3836   }
3837   PetscFunctionReturn(0);
3838 }
3839 
3840 #undef __FUNCT__
3841 #define __FUNCT__ "MatPermute"
3842 /*@
3843    MatPermute - Creates a new matrix with rows and columns permuted from the
3844    original.
3845 
3846    Collective on Mat
3847 
3848    Input Parameters:
3849 +  mat - the matrix to permute
3850 .  row - row permutation, each processor supplies only the permutation for its rows
3851 -  col - column permutation, each processor needs the entire column permutation, that is
3852          this is the same size as the total number of columns in the matrix
3853 
3854    Output Parameters:
3855 .  B - the permuted matrix
3856 
3857    Level: advanced
3858 
3859    Concepts: matrices^permuting
3860 
3861 .seealso: MatGetOrdering()
3862 @*/
3863 PetscErrorCode PETSCMAT_DLLEXPORT MatPermute(Mat mat,IS row,IS col,Mat *B)
3864 {
3865   PetscErrorCode ierr;
3866 
3867   PetscFunctionBegin;
3868   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
3869   PetscValidType(mat,1);
3870   PetscValidHeaderSpecific(row,IS_COOKIE,2);
3871   PetscValidHeaderSpecific(col,IS_COOKIE,3);
3872   PetscValidPointer(B,4);
3873   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3874   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3875   if (!mat->ops->permute) SETERRQ1(PETSC_ERR_SUP,"MatPermute not available for Mat type %s",((PetscObject)mat)->type_name);
3876   ierr = MatPreallocated(mat);CHKERRQ(ierr);
3877 
3878   ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr);
3879   ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr);
3880   PetscFunctionReturn(0);
3881 }
3882 
3883 #undef __FUNCT__
3884 #define __FUNCT__ "MatPermuteSparsify"
3885 /*@
3886   MatPermuteSparsify - Creates a new matrix with rows and columns permuted from the
3887   original and sparsified to the prescribed tolerance.
3888 
3889   Collective on Mat
3890 
3891   Input Parameters:
3892 + A    - The matrix to permute
3893 . band - The half-bandwidth of the sparsified matrix, or PETSC_DECIDE
3894 . frac - The half-bandwidth as a fraction of the total size, or 0.0
3895 . tol  - The drop tolerance
3896 . rowp - The row permutation
3897 - colp - The column permutation
3898 
3899   Output Parameter:
3900 . B    - The permuted, sparsified matrix
3901 
3902   Level: advanced
3903 
3904   Note:
3905   The default behavior (band = PETSC_DECIDE and frac = 0.0) is to
3906   restrict the half-bandwidth of the resulting matrix to 5% of the
3907   total matrix size.
3908 
3909 .keywords: matrix, permute, sparsify
3910 
3911 .seealso: MatGetOrdering(), MatPermute()
3912 @*/
3913 PetscErrorCode PETSCMAT_DLLEXPORT MatPermuteSparsify(Mat A, PetscInt band, PetscReal frac, PetscReal tol, IS rowp, IS colp, Mat *B)
3914 {
3915   IS                irowp, icolp;
3916   const PetscInt    *rows, *cols;
3917   PetscInt          M, N, locRowStart, locRowEnd;
3918   PetscInt          nz, newNz;
3919   const PetscInt    *cwork;
3920   PetscInt          *cnew;
3921   const PetscScalar *vwork;
3922   PetscScalar       *vnew;
3923   PetscInt          bw, issize;
3924   PetscInt          row, locRow, newRow, col, newCol;
3925   PetscErrorCode    ierr;
3926 
3927   PetscFunctionBegin;
3928   PetscValidHeaderSpecific(A,    MAT_COOKIE,1);
3929   PetscValidHeaderSpecific(rowp, IS_COOKIE,5);
3930   PetscValidHeaderSpecific(colp, IS_COOKIE,6);
3931   PetscValidPointer(B,7);
3932   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
3933   if (A->factor)     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3934   if (!A->ops->permutesparsify) {
3935     ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr);
3936     ierr = MatGetOwnershipRange(A, &locRowStart, &locRowEnd);CHKERRQ(ierr);
3937     ierr = ISGetSize(rowp, &issize);CHKERRQ(ierr);
3938     if (issize != M) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %D for row permutation, should be %D", issize, M);
3939     ierr = ISGetSize(colp, &issize);CHKERRQ(ierr);
3940     if (issize != N) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %D for column permutation, should be %D", issize, N);
3941     ierr = ISInvertPermutation(rowp, 0, &irowp);CHKERRQ(ierr);
3942     ierr = ISGetIndices(irowp, &rows);CHKERRQ(ierr);
3943     ierr = ISInvertPermutation(colp, 0, &icolp);CHKERRQ(ierr);
3944     ierr = ISGetIndices(icolp, &cols);CHKERRQ(ierr);
3945     ierr = PetscMalloc(N * sizeof(PetscInt),         &cnew);CHKERRQ(ierr);
3946     ierr = PetscMalloc(N * sizeof(PetscScalar), &vnew);CHKERRQ(ierr);
3947 
3948     /* Setup bandwidth to include */
3949     if (band == PETSC_DECIDE) {
3950       if (frac <= 0.0)
3951         bw = (PetscInt) (M * 0.05);
3952       else
3953         bw = (PetscInt) (M * frac);
3954     } else {
3955       if (band <= 0) SETERRQ(PETSC_ERR_ARG_WRONG, "Bandwidth must be a positive integer");
3956       bw = band;
3957     }
3958 
3959     /* Put values into new matrix */
3960     ierr = MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, B);CHKERRQ(ierr);
3961     for(row = locRowStart, locRow = 0; row < locRowEnd; row++, locRow++) {
3962       ierr = MatGetRow(A, row, &nz, &cwork, &vwork);CHKERRQ(ierr);
3963       newRow   = rows[locRow]+locRowStart;
3964       for(col = 0, newNz = 0; col < nz; col++) {
3965         newCol = cols[cwork[col]];
3966         if ((newCol >= newRow - bw) && (newCol < newRow + bw) && (PetscAbsScalar(vwork[col]) >= tol)) {
3967           cnew[newNz] = newCol;
3968           vnew[newNz] = vwork[col];
3969           newNz++;
3970         }
3971       }
3972       ierr = MatSetValues(*B, 1, &newRow, newNz, cnew, vnew, INSERT_VALUES);CHKERRQ(ierr);
3973       ierr = MatRestoreRow(A, row, &nz, &cwork, &vwork);CHKERRQ(ierr);
3974     }
3975     ierr = PetscFree(cnew);CHKERRQ(ierr);
3976     ierr = PetscFree(vnew);CHKERRQ(ierr);
3977     ierr = MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3978     ierr = MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3979     ierr = ISRestoreIndices(irowp, &rows);CHKERRQ(ierr);
3980     ierr = ISRestoreIndices(icolp, &cols);CHKERRQ(ierr);
3981     ierr = ISDestroy(irowp);CHKERRQ(ierr);
3982     ierr = ISDestroy(icolp);CHKERRQ(ierr);
3983   } else {
3984     ierr = (*A->ops->permutesparsify)(A, band, frac, tol, rowp, colp, B);CHKERRQ(ierr);
3985   }
3986   ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr);
3987   PetscFunctionReturn(0);
3988 }
3989 
3990 #undef __FUNCT__
3991 #define __FUNCT__ "MatEqual"
3992 /*@
3993    MatEqual - Compares two matrices.
3994 
3995    Collective on Mat
3996 
3997    Input Parameters:
3998 +  A - the first matrix
3999 -  B - the second matrix
4000 
4001    Output Parameter:
4002 .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.
4003 
4004    Level: intermediate
4005 
4006    Concepts: matrices^equality between
4007 @*/
4008 PetscErrorCode PETSCMAT_DLLEXPORT MatEqual(Mat A,Mat B,PetscTruth *flg)
4009 {
4010   PetscErrorCode ierr;
4011 
4012   PetscFunctionBegin;
4013   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
4014   PetscValidHeaderSpecific(B,MAT_COOKIE,2);
4015   PetscValidType(A,1);
4016   PetscValidType(B,2);
4017   MatPreallocated(B);
4018   PetscValidIntPointer(flg,3);
4019   PetscCheckSameComm(A,1,B,2);
4020   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4021   if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4022   if (A->rmap->N != B->rmap->N || A->cmap->N != B->cmap->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %D %D %D %D",A->rmap->N,B->rmap->N,A->cmap->N,B->cmap->N);
4023   if (!A->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name);
4024   if (!B->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)B)->type_name);
4025   if (A->ops->equal != B->ops->equal) SETERRQ2(PETSC_ERR_ARG_INCOMP,"A is type: %s\nB is type: %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
4026   ierr = MatPreallocated(A);CHKERRQ(ierr);
4027 
4028   ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr);
4029   PetscFunctionReturn(0);
4030 }
4031 
4032 #undef __FUNCT__
4033 #define __FUNCT__ "MatDiagonalScale"
4034 /*@
4035    MatDiagonalScale - Scales a matrix on the left and right by diagonal
4036    matrices that are stored as vectors.  Either of the two scaling
4037    matrices can be PETSC_NULL.
4038 
4039    Collective on Mat
4040 
4041    Input Parameters:
4042 +  mat - the matrix to be scaled
4043 .  l - the left scaling vector (or PETSC_NULL)
4044 -  r - the right scaling vector (or PETSC_NULL)
4045 
4046    Notes:
4047    MatDiagonalScale() computes A = LAR, where
4048    L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector)
4049 
4050    Level: intermediate
4051 
4052    Concepts: matrices^diagonal scaling
4053    Concepts: diagonal scaling of matrices
4054 
4055 .seealso: MatScale()
4056 @*/
4057 PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalScale(Mat mat,Vec l,Vec r)
4058 {
4059   PetscErrorCode ierr;
4060 
4061   PetscFunctionBegin;
4062   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4063   PetscValidType(mat,1);
4064   if (!mat->ops->diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4065   if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE,2);PetscCheckSameComm(mat,1,l,2);}
4066   if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE,3);PetscCheckSameComm(mat,1,r,3);}
4067   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4068   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4069   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4070 
4071   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
4072   ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr);
4073   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
4074   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
4075   PetscFunctionReturn(0);
4076 }
4077 
4078 #undef __FUNCT__
4079 #define __FUNCT__ "MatScale"
4080 /*@
4081     MatScale - Scales all elements of a matrix by a given number.
4082 
4083     Collective on Mat
4084 
4085     Input Parameters:
4086 +   mat - the matrix to be scaled
4087 -   a  - the scaling value
4088 
4089     Output Parameter:
4090 .   mat - the scaled matrix
4091 
4092     Level: intermediate
4093 
4094     Concepts: matrices^scaling all entries
4095 
4096 .seealso: MatDiagonalScale()
4097 @*/
4098 PetscErrorCode PETSCMAT_DLLEXPORT MatScale(Mat mat,PetscScalar a)
4099 {
4100   PetscErrorCode ierr;
4101 
4102   PetscFunctionBegin;
4103   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4104   PetscValidType(mat,1);
4105   if (!mat->ops->scale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4106   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4107   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4108   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4109 
4110   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
4111   ierr = (*mat->ops->scale)(mat,a);CHKERRQ(ierr);
4112   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
4113   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
4114   PetscFunctionReturn(0);
4115 }
4116 
4117 #undef __FUNCT__
4118 #define __FUNCT__ "MatNorm"
4119 /*@
4120    MatNorm - Calculates various norms of a matrix.
4121 
4122    Collective on Mat
4123 
4124    Input Parameters:
4125 +  mat - the matrix
4126 -  type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY
4127 
4128    Output Parameters:
4129 .  nrm - the resulting norm
4130 
4131    Level: intermediate
4132 
4133    Concepts: matrices^norm
4134    Concepts: norm^of matrix
4135 @*/
4136 PetscErrorCode PETSCMAT_DLLEXPORT MatNorm(Mat mat,NormType type,PetscReal *nrm)
4137 {
4138   PetscErrorCode ierr;
4139 
4140   PetscFunctionBegin;
4141   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4142   PetscValidType(mat,1);
4143   PetscValidScalarPointer(nrm,3);
4144 
4145   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4146   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4147   if (!mat->ops->norm) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4148   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4149 
4150   ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr);
4151   PetscFunctionReturn(0);
4152 }
4153 
4154 /*
4155      This variable is used to prevent counting of MatAssemblyBegin() that
4156    are called from within a MatAssemblyEnd().
4157 */
4158 static PetscInt MatAssemblyEnd_InUse = 0;
4159 #undef __FUNCT__
4160 #define __FUNCT__ "MatAssemblyBegin"
4161 /*@
4162    MatAssemblyBegin - Begins assembling the matrix.  This routine should
4163    be called after completing all calls to MatSetValues().
4164 
4165    Collective on Mat
4166 
4167    Input Parameters:
4168 +  mat - the matrix
4169 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
4170 
4171    Notes:
4172    MatSetValues() generally caches the values.  The matrix is ready to
4173    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
4174    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
4175    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
4176    using the matrix.
4177 
4178    Level: beginner
4179 
4180    Concepts: matrices^assembling
4181 
4182 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
4183 @*/
4184 PetscErrorCode PETSCMAT_DLLEXPORT MatAssemblyBegin(Mat mat,MatAssemblyType type)
4185 {
4186   PetscErrorCode ierr;
4187 
4188   PetscFunctionBegin;
4189   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4190   PetscValidType(mat,1);
4191   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4192   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?");
4193   if (mat->assembled) {
4194     mat->was_assembled = PETSC_TRUE;
4195     mat->assembled     = PETSC_FALSE;
4196   }
4197   if (!MatAssemblyEnd_InUse) {
4198     ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
4199     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
4200     ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
4201   } else {
4202     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
4203   }
4204   PetscFunctionReturn(0);
4205 }
4206 
4207 #undef __FUNCT__
4208 #define __FUNCT__ "MatAssembed"
4209 /*@
4210    MatAssembled - Indicates if a matrix has been assembled and is ready for
4211      use; for example, in matrix-vector product.
4212 
4213    Collective on Mat
4214 
4215    Input Parameter:
4216 .  mat - the matrix
4217 
4218    Output Parameter:
4219 .  assembled - PETSC_TRUE or PETSC_FALSE
4220 
4221    Level: advanced
4222 
4223    Concepts: matrices^assembled?
4224 
4225 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
4226 @*/
4227 PetscErrorCode PETSCMAT_DLLEXPORT MatAssembled(Mat mat,PetscTruth *assembled)
4228 {
4229   PetscFunctionBegin;
4230   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4231   PetscValidType(mat,1);
4232   PetscValidPointer(assembled,2);
4233   *assembled = mat->assembled;
4234   PetscFunctionReturn(0);
4235 }
4236 
4237 #undef __FUNCT__
4238 #define __FUNCT__ "MatView_Private"
4239 /*
4240     Processes command line options to determine if/how a matrix
4241   is to be viewed. Called by MatAssemblyEnd() and MatLoad().
4242 */
4243 PetscErrorCode MatView_Private(Mat mat)
4244 {
4245   PetscErrorCode    ierr;
4246   PetscTruth        flg1,flg2,flg3,flg4,flg6,flg7,flg8;
4247   static PetscTruth incall = PETSC_FALSE;
4248 #if defined(PETSC_USE_SOCKET_VIEWER)
4249   PetscTruth        flg5;
4250 #endif
4251 
4252   PetscFunctionBegin;
4253   if (incall) PetscFunctionReturn(0);
4254   incall = PETSC_TRUE;
4255   ierr = PetscOptionsBegin(((PetscObject)mat)->comm,((PetscObject)mat)->prefix,"Matrix Options","Mat");CHKERRQ(ierr);
4256     ierr = PetscOptionsName("-mat_view_info","Information on matrix size","MatView",&flg1);CHKERRQ(ierr);
4257     ierr = PetscOptionsName("-mat_view_info_detailed","Nonzeros in the matrix","MatView",&flg2);CHKERRQ(ierr);
4258     ierr = PetscOptionsName("-mat_view","Print matrix to stdout","MatView",&flg3);CHKERRQ(ierr);
4259     ierr = PetscOptionsName("-mat_view_matlab","Print matrix to stdout in a format Matlab can read","MatView",&flg4);CHKERRQ(ierr);
4260 #if defined(PETSC_USE_SOCKET_VIEWER)
4261     ierr = PetscOptionsName("-mat_view_socket","Send matrix to socket (can be read from matlab)","MatView",&flg5);CHKERRQ(ierr);
4262 #endif
4263     ierr = PetscOptionsName("-mat_view_binary","Save matrix to file in binary format","MatView",&flg6);CHKERRQ(ierr);
4264     ierr = PetscOptionsName("-mat_view_draw","Draw the matrix nonzero structure","MatView",&flg7);CHKERRQ(ierr);
4265   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4266 
4267   if (flg1) {
4268     PetscViewer viewer;
4269 
4270     ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr);
4271     ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr);
4272     ierr = MatView(mat,viewer);CHKERRQ(ierr);
4273     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4274   }
4275   if (flg2) {
4276     PetscViewer viewer;
4277 
4278     ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr);
4279     ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
4280     ierr = MatView(mat,viewer);CHKERRQ(ierr);
4281     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4282   }
4283   if (flg3) {
4284     PetscViewer viewer;
4285 
4286     ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr);
4287     ierr = MatView(mat,viewer);CHKERRQ(ierr);
4288   }
4289   if (flg4) {
4290     PetscViewer viewer;
4291 
4292     ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr);
4293     ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr);
4294     ierr = MatView(mat,viewer);CHKERRQ(ierr);
4295     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4296   }
4297 #if defined(PETSC_USE_SOCKET_VIEWER)
4298   if (flg5) {
4299     ierr = MatView(mat,PETSC_VIEWER_SOCKET_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4300     ierr = PetscViewerFlush(PETSC_VIEWER_SOCKET_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4301   }
4302 #endif
4303   if (flg6) {
4304     ierr = MatView(mat,PETSC_VIEWER_BINARY_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4305     ierr = PetscViewerFlush(PETSC_VIEWER_BINARY_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4306   }
4307   if (flg7) {
4308     ierr = PetscOptionsHasName(((PetscObject)mat)->prefix,"-mat_view_contour",&flg8);CHKERRQ(ierr);
4309     if (flg8) {
4310       PetscViewerPushFormat(PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
4311     }
4312     ierr = MatView(mat,PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4313     ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4314     if (flg8) {
4315       PetscViewerPopFormat(PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4316     }
4317   }
4318   incall = PETSC_FALSE;
4319   PetscFunctionReturn(0);
4320 }
4321 
4322 #undef __FUNCT__
4323 #define __FUNCT__ "MatAssemblyEnd"
4324 /*@
4325    MatAssemblyEnd - Completes assembling the matrix.  This routine should
4326    be called after MatAssemblyBegin().
4327 
4328    Collective on Mat
4329 
4330    Input Parameters:
4331 +  mat - the matrix
4332 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
4333 
4334    Options Database Keys:
4335 +  -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly()
4336 .  -mat_view_info_detailed - Prints more detailed info
4337 .  -mat_view - Prints matrix in ASCII format
4338 .  -mat_view_matlab - Prints matrix in Matlab format
4339 .  -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
4340 .  -display <name> - Sets display name (default is host)
4341 .  -draw_pause <sec> - Sets number of seconds to pause after display
4342 .  -mat_view_socket - Sends matrix to socket, can be accessed from Matlab (see users manual)
4343 .  -viewer_socket_machine <machine>
4344 .  -viewer_socket_port <port>
4345 .  -mat_view_binary - save matrix to file in binary format
4346 -  -viewer_binary_filename <name>
4347 
4348    Notes:
4349    MatSetValues() generally caches the values.  The matrix is ready to
4350    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
4351    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
4352    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
4353    using the matrix.
4354 
4355    Level: beginner
4356 
4357 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), MatView(), MatAssembled(), PetscViewerSocketOpen()
4358 @*/
4359 PetscErrorCode PETSCMAT_DLLEXPORT MatAssemblyEnd(Mat mat,MatAssemblyType type)
4360 {
4361   PetscErrorCode  ierr;
4362   static PetscInt inassm = 0;
4363   PetscTruth      flg;
4364 
4365   PetscFunctionBegin;
4366   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4367   PetscValidType(mat,1);
4368 
4369   inassm++;
4370   MatAssemblyEnd_InUse++;
4371   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
4372     ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
4373     if (mat->ops->assemblyend) {
4374       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
4375     }
4376     ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
4377   } else {
4378     if (mat->ops->assemblyend) {
4379       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
4380     }
4381   }
4382 
4383   /* Flush assembly is not a true assembly */
4384   if (type != MAT_FLUSH_ASSEMBLY) {
4385     mat->assembled  = PETSC_TRUE; mat->num_ass++;
4386   }
4387   mat->insertmode = NOT_SET_VALUES;
4388   MatAssemblyEnd_InUse--;
4389   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
4390   if (!mat->symmetric_eternal) {
4391     mat->symmetric_set              = PETSC_FALSE;
4392     mat->hermitian_set              = PETSC_FALSE;
4393     mat->structurally_symmetric_set = PETSC_FALSE;
4394   }
4395   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
4396     ierr = MatView_Private(mat);CHKERRQ(ierr);
4397     ierr = PetscOptionsHasName(((PetscObject)mat)->prefix,"-mat_is_symmetric",&flg);CHKERRQ(ierr);
4398     if (flg) {
4399       PetscReal tol = 0.0;
4400       ierr = PetscOptionsGetReal(((PetscObject)mat)->prefix,"-mat_is_symmetric",&tol,PETSC_NULL);CHKERRQ(ierr);
4401       ierr = MatIsSymmetric(mat,tol,&flg);CHKERRQ(ierr);
4402       if (flg) {
4403         ierr = PetscPrintf(((PetscObject)mat)->comm,"Matrix is symmetric (tolerance %G)\n",tol);CHKERRQ(ierr);
4404       } else {
4405         ierr = PetscPrintf(((PetscObject)mat)->comm,"Matrix is not symmetric (tolerance %G)\n",tol);CHKERRQ(ierr);
4406       }
4407     }
4408   }
4409   inassm--;
4410   PetscFunctionReturn(0);
4411 }
4412 
4413 
4414 #undef __FUNCT__
4415 #define __FUNCT__ "MatCompress"
4416 /*@
4417    MatCompress - Tries to store the matrix in as little space as
4418    possible.  May fail if memory is already fully used, since it
4419    tries to allocate new space.
4420 
4421    Collective on Mat
4422 
4423    Input Parameters:
4424 .  mat - the matrix
4425 
4426    Level: advanced
4427 
4428 @*/
4429 PetscErrorCode PETSCMAT_DLLEXPORT MatCompress(Mat mat)
4430 {
4431   PetscErrorCode ierr;
4432 
4433   PetscFunctionBegin;
4434   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4435   PetscValidType(mat,1);
4436   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4437   if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);}
4438   PetscFunctionReturn(0);
4439 }
4440 
4441 #undef __FUNCT__
4442 #define __FUNCT__ "MatSetOption"
4443 /*@
4444    MatSetOption - Sets a parameter option for a matrix. Some options
4445    may be specific to certain storage formats.  Some options
4446    determine how values will be inserted (or added). Sorted,
4447    row-oriented input will generally assemble the fastest. The default
4448    is row-oriented, nonsorted input.
4449 
4450    Collective on Mat
4451 
4452    Input Parameters:
4453 +  mat - the matrix
4454 .  option - the option, one of those listed below (and possibly others),
4455 -  flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE)
4456 
4457   Options Describing Matrix Structure:
4458 +    MAT_SYMMETRIC - symmetric in terms of both structure and value
4459 .    MAT_HERMITIAN - transpose is the complex conjugation
4460 .    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
4461 -    MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag
4462                             you set to be kept with all future use of the matrix
4463                             including after MatAssemblyBegin/End() which could
4464                             potentially change the symmetry structure, i.e. you
4465                             KNOW the matrix will ALWAYS have the property you set.
4466 
4467 
4468    Options For Use with MatSetValues():
4469    Insert a logically dense subblock, which can be
4470 .    MAT_ROW_ORIENTED - row-oriented (default)
4471 
4472    Note these options reflect the data you pass in with MatSetValues(); it has
4473    nothing to do with how the data is stored internally in the matrix
4474    data structure.
4475 
4476    When (re)assembling a matrix, we can restrict the input for
4477    efficiency/debugging purposes.  These options include
4478 +    MAT_NEW_NONZERO_LOCATIONS - additional insertions will be
4479         allowed if they generate a new nonzero
4480 .    MAT_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only)
4481 .    MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries
4482 .    MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry
4483 -    MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly
4484 
4485    Notes:
4486    Some options are relevant only for particular matrix types and
4487    are thus ignored by others.  Other options are not supported by
4488    certain matrix types and will generate an error message if set.
4489 
4490    If using a Fortran 77 module to compute a matrix, one may need to
4491    use the column-oriented option (or convert to the row-oriented
4492    format).
4493 
4494    MAT_NEW_NONZERO_LOCATIONS set to PETSC_FALSE indicates that any add or insertion
4495    that would generate a new entry in the nonzero structure is instead
4496    ignored.  Thus, if memory has not alredy been allocated for this particular
4497    data, then the insertion is ignored. For dense matrices, in which
4498    the entire array is allocated, no entries are ever ignored.
4499    Set after the first MatAssemblyEnd()
4500 
4501    MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion
4502    that would generate a new entry in the nonzero structure instead produces
4503    an error. (Currently supported for AIJ and BAIJ formats only.)
4504    This is a useful flag when using SAME_NONZERO_PATTERN in calling
4505    KSPSetOperators() to ensure that the nonzero pattern truely does
4506    remain unchanged. Set after the first MatAssemblyEnd()
4507 
4508    MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion
4509    that would generate a new entry that has not been preallocated will
4510    instead produce an error. (Currently supported for AIJ and BAIJ formats
4511    only.) This is a useful flag when debugging matrix memory preallocation.
4512 
4513    MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for
4514    other processors should be dropped, rather than stashed.
4515    This is useful if you know that the "owning" processor is also
4516    always generating the correct matrix entries, so that PETSc need
4517    not transfer duplicate entries generated on another processor.
4518 
4519    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
4520    searches during matrix assembly. When this flag is set, the hash table
4521    is created during the first Matrix Assembly. This hash table is
4522    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
4523    to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag
4524    should be used with MAT_USE_HASH_TABLE flag. This option is currently
4525    supported by MATMPIBAIJ format only.
4526 
4527    MAT_KEEP_ZEROED_ROWS indicates when MatZeroRows() is called the zeroed entries
4528    are kept in the nonzero structure
4529 
4530    MAT_IGNORE_ZERO_ENTRIES - for AIJ/IS matrices this will stop zero values from creating
4531    a zero location in the matrix
4532 
4533    MAT_USE_INODES - indicates using inode version of the code - works with AIJ and
4534    ROWBS matrix types
4535 
4536    Level: intermediate
4537 
4538    Concepts: matrices^setting options
4539 
4540 @*/
4541 PetscErrorCode PETSCMAT_DLLEXPORT MatSetOption(Mat mat,MatOption op,PetscTruth flg)
4542 {
4543   PetscErrorCode ierr;
4544 
4545   PetscFunctionBegin;
4546   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4547   PetscValidType(mat,1);
4548   if (((int) op) < 0 || ((int) op) > 16) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Options %d is out of range",(int)op);
4549   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4550   switch (op) {
4551   case MAT_SYMMETRIC:
4552     mat->symmetric                  = flg;
4553     if (flg) mat->structurally_symmetric     = PETSC_TRUE;
4554     mat->symmetric_set              = PETSC_TRUE;
4555     mat->structurally_symmetric_set = flg;
4556     break;
4557   case MAT_HERMITIAN:
4558     mat->hermitian                  = flg;
4559     if (flg) mat->structurally_symmetric     = PETSC_TRUE;
4560     mat->hermitian_set              = PETSC_TRUE;
4561     mat->structurally_symmetric_set = flg;
4562     break;
4563   case MAT_STRUCTURALLY_SYMMETRIC:
4564     mat->structurally_symmetric     = flg;
4565     mat->structurally_symmetric_set = PETSC_TRUE;
4566     break;
4567   case MAT_SYMMETRY_ETERNAL:
4568     mat->symmetric_eternal          = flg;
4569     break;
4570   default:
4571     break;
4572   }
4573   if (mat->ops->setoption) {
4574     ierr = (*mat->ops->setoption)(mat,op,flg);CHKERRQ(ierr);
4575   }
4576   PetscFunctionReturn(0);
4577 }
4578 
4579 #undef __FUNCT__
4580 #define __FUNCT__ "MatZeroEntries"
4581 /*@
4582    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
4583    this routine retains the old nonzero structure.
4584 
4585    Collective on Mat
4586 
4587    Input Parameters:
4588 .  mat - the matrix
4589 
4590    Level: intermediate
4591 
4592    Concepts: matrices^zeroing
4593 
4594 .seealso: MatZeroRows()
4595 @*/
4596 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroEntries(Mat mat)
4597 {
4598   PetscErrorCode ierr;
4599 
4600   PetscFunctionBegin;
4601   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4602   PetscValidType(mat,1);
4603   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4604   if (mat->insertmode != NOT_SET_VALUES) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for matrices where you have set values but not yet assembled");
4605   if (!mat->ops->zeroentries) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4606   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4607 
4608   ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
4609   ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr);
4610   ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
4611   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
4612   PetscFunctionReturn(0);
4613 }
4614 
4615 #undef __FUNCT__
4616 #define __FUNCT__ "MatZeroRows"
4617 /*@C
4618    MatZeroRows - Zeros all entries (except possibly the main diagonal)
4619    of a set of rows of a matrix.
4620 
4621    Collective on Mat
4622 
4623    Input Parameters:
4624 +  mat - the matrix
4625 .  numRows - the number of rows to remove
4626 .  rows - the global row indices
4627 -  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
4628 
4629    Notes:
4630    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
4631    but does not release memory.  For the dense and block diagonal
4632    formats this does not alter the nonzero structure.
4633 
4634    If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS,PETSC_TRUE) the nonzero structure
4635    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
4636    merely zeroed.
4637 
4638    The user can set a value in the diagonal entry (or for the AIJ and
4639    row formats can optionally remove the main diagonal entry from the
4640    nonzero structure as well, by passing 0.0 as the final argument).
4641 
4642    For the parallel case, all processes that share the matrix (i.e.,
4643    those in the communicator used for matrix creation) MUST call this
4644    routine, regardless of whether any rows being zeroed are owned by
4645    them.
4646 
4647    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
4648    list only rows local to itself).
4649 
4650    Level: intermediate
4651 
4652    Concepts: matrices^zeroing rows
4653 
4654 .seealso: MatZeroRowsIS(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
4655 @*/
4656 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag)
4657 {
4658   PetscErrorCode ierr;
4659 
4660   PetscFunctionBegin;
4661   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4662   PetscValidType(mat,1);
4663   if (numRows) PetscValidIntPointer(rows,3);
4664   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4665   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4666   if (!mat->ops->zerorows) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4667   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4668 
4669   ierr = (*mat->ops->zerorows)(mat,numRows,rows,diag);CHKERRQ(ierr);
4670   ierr = MatView_Private(mat);CHKERRQ(ierr);
4671   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
4672   PetscFunctionReturn(0);
4673 }
4674 
4675 #undef __FUNCT__
4676 #define __FUNCT__ "MatZeroRowsIS"
4677 /*@C
4678    MatZeroRowsIS - Zeros all entries (except possibly the main diagonal)
4679    of a set of rows of a matrix.
4680 
4681    Collective on Mat
4682 
4683    Input Parameters:
4684 +  mat - the matrix
4685 .  is - index set of rows to remove
4686 -  diag - value put in all diagonals of eliminated rows
4687 
4688    Notes:
4689    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
4690    but does not release memory.  For the dense and block diagonal
4691    formats this does not alter the nonzero structure.
4692 
4693    If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS,PETSC_TRUE) the nonzero structure
4694    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
4695    merely zeroed.
4696 
4697    The user can set a value in the diagonal entry (or for the AIJ and
4698    row formats can optionally remove the main diagonal entry from the
4699    nonzero structure as well, by passing 0.0 as the final argument).
4700 
4701    For the parallel case, all processes that share the matrix (i.e.,
4702    those in the communicator used for matrix creation) MUST call this
4703    routine, regardless of whether any rows being zeroed are owned by
4704    them.
4705 
4706    Each processor should list the rows that IT wants zeroed
4707 
4708    Level: intermediate
4709 
4710    Concepts: matrices^zeroing rows
4711 
4712 .seealso: MatZeroRows(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
4713 @*/
4714 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroRowsIS(Mat mat,IS is,PetscScalar diag)
4715 {
4716   PetscInt       numRows;
4717   const PetscInt *rows;
4718   PetscErrorCode ierr;
4719 
4720   PetscFunctionBegin;
4721   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4722   PetscValidType(mat,1);
4723   PetscValidHeaderSpecific(is,IS_COOKIE,2);
4724   ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr);
4725   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
4726   ierr = MatZeroRows(mat,numRows,rows,diag);CHKERRQ(ierr);
4727   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
4728   PetscFunctionReturn(0);
4729 }
4730 
4731 #undef __FUNCT__
4732 #define __FUNCT__ "MatZeroRowsLocal"
4733 /*@C
4734    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
4735    of a set of rows of a matrix; using local numbering of rows.
4736 
4737    Collective on Mat
4738 
4739    Input Parameters:
4740 +  mat - the matrix
4741 .  numRows - the number of rows to remove
4742 .  rows - the global row indices
4743 -  diag - value put in all diagonals of eliminated rows
4744 
4745    Notes:
4746    Before calling MatZeroRowsLocal(), the user must first set the
4747    local-to-global mapping by calling MatSetLocalToGlobalMapping().
4748 
4749    For the AIJ matrix formats this removes the old nonzero structure,
4750    but does not release memory.  For the dense and block diagonal
4751    formats this does not alter the nonzero structure.
4752 
4753    If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS,PETSC_TRUE) the nonzero structure
4754    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
4755    merely zeroed.
4756 
4757    The user can set a value in the diagonal entry (or for the AIJ and
4758    row formats can optionally remove the main diagonal entry from the
4759    nonzero structure as well, by passing 0.0 as the final argument).
4760 
4761    Level: intermediate
4762 
4763    Concepts: matrices^zeroing
4764 
4765 .seealso: MatZeroRows(), MatZeroRowsLocalIS(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
4766 @*/
4767 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag)
4768 {
4769   PetscErrorCode ierr;
4770 
4771   PetscFunctionBegin;
4772   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4773   PetscValidType(mat,1);
4774   if (numRows) PetscValidIntPointer(rows,3);
4775   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4776   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4777   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4778 
4779   if (mat->ops->zerorowslocal) {
4780     ierr = (*mat->ops->zerorowslocal)(mat,numRows,rows,diag);CHKERRQ(ierr);
4781   } else {
4782     IS             is, newis;
4783     const PetscInt *newRows;
4784 
4785     if (!mat->mapping) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
4786     ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,&is);CHKERRQ(ierr);
4787     ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr);
4788     ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr);
4789     ierr = (*mat->ops->zerorows)(mat,numRows,newRows,diag);CHKERRQ(ierr);
4790     ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr);
4791     ierr = ISDestroy(newis);CHKERRQ(ierr);
4792     ierr = ISDestroy(is);CHKERRQ(ierr);
4793   }
4794   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
4795   PetscFunctionReturn(0);
4796 }
4797 
4798 #undef __FUNCT__
4799 #define __FUNCT__ "MatZeroRowsLocalIS"
4800 /*@C
4801    MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal)
4802    of a set of rows of a matrix; using local numbering of rows.
4803 
4804    Collective on Mat
4805 
4806    Input Parameters:
4807 +  mat - the matrix
4808 .  is - index set of rows to remove
4809 -  diag - value put in all diagonals of eliminated rows
4810 
4811    Notes:
4812    Before calling MatZeroRowsLocalIS(), the user must first set the
4813    local-to-global mapping by calling MatSetLocalToGlobalMapping().
4814 
4815    For the AIJ matrix formats this removes the old nonzero structure,
4816    but does not release memory.  For the dense and block diagonal
4817    formats this does not alter the nonzero structure.
4818 
4819    If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS,PETSC_TRUE) the nonzero structure
4820    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
4821    merely zeroed.
4822 
4823    The user can set a value in the diagonal entry (or for the AIJ and
4824    row formats can optionally remove the main diagonal entry from the
4825    nonzero structure as well, by passing 0.0 as the final argument).
4826 
4827    Level: intermediate
4828 
4829    Concepts: matrices^zeroing
4830 
4831 .seealso: MatZeroRows(), MatZeroRowsLocal(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
4832 @*/
4833 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag)
4834 {
4835   PetscErrorCode ierr;
4836   PetscInt       numRows;
4837   const PetscInt *rows;
4838 
4839   PetscFunctionBegin;
4840   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4841   PetscValidType(mat,1);
4842   PetscValidHeaderSpecific(is,IS_COOKIE,2);
4843   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4844   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4845   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4846 
4847   ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr);
4848   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
4849   ierr = MatZeroRowsLocal(mat,numRows,rows,diag);CHKERRQ(ierr);
4850   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
4851   PetscFunctionReturn(0);
4852 }
4853 
4854 #undef __FUNCT__
4855 #define __FUNCT__ "MatGetSize"
4856 /*@
4857    MatGetSize - Returns the numbers of rows and columns in a matrix.
4858 
4859    Not Collective
4860 
4861    Input Parameter:
4862 .  mat - the matrix
4863 
4864    Output Parameters:
4865 +  m - the number of global rows
4866 -  n - the number of global columns
4867 
4868    Note: both output parameters can be PETSC_NULL on input.
4869 
4870    Level: beginner
4871 
4872    Concepts: matrices^size
4873 
4874 .seealso: MatGetLocalSize()
4875 @*/
4876 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSize(Mat mat,PetscInt *m,PetscInt* n)
4877 {
4878   PetscFunctionBegin;
4879   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4880   if (m) *m = mat->rmap->N;
4881   if (n) *n = mat->cmap->N;
4882   PetscFunctionReturn(0);
4883 }
4884 
4885 #undef __FUNCT__
4886 #define __FUNCT__ "MatGetLocalSize"
4887 /*@
4888    MatGetLocalSize - Returns the number of rows and columns in a matrix
4889    stored locally.  This information may be implementation dependent, so
4890    use with care.
4891 
4892    Not Collective
4893 
4894    Input Parameters:
4895 .  mat - the matrix
4896 
4897    Output Parameters:
4898 +  m - the number of local rows
4899 -  n - the number of local columns
4900 
4901    Note: both output parameters can be PETSC_NULL on input.
4902 
4903    Level: beginner
4904 
4905    Concepts: matrices^local size
4906 
4907 .seealso: MatGetSize()
4908 @*/
4909 PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalSize(Mat mat,PetscInt *m,PetscInt* n)
4910 {
4911   PetscFunctionBegin;
4912   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4913   if (m) PetscValidIntPointer(m,2);
4914   if (n) PetscValidIntPointer(n,3);
4915   if (m) *m = mat->rmap->n;
4916   if (n) *n = mat->cmap->n;
4917   PetscFunctionReturn(0);
4918 }
4919 
4920 #undef __FUNCT__
4921 #define __FUNCT__ "MatGetOwnershipRangeColumn"
4922 /*@
4923    MatGetOwnershipRangeColumn - Returns the range of matrix columns owned by
4924    this processor.
4925 
4926    Not Collective, unless matrix has not been allocated, then collective on Mat
4927 
4928    Input Parameters:
4929 .  mat - the matrix
4930 
4931    Output Parameters:
4932 +  m - the global index of the first local column
4933 -  n - one more than the global index of the last local column
4934 
4935    Notes: both output parameters can be PETSC_NULL on input.
4936 
4937    Level: developer
4938 
4939    Concepts: matrices^column ownership
4940 
4941 .seealso:  MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn()
4942 
4943 @*/
4944 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt* n)
4945 {
4946   PetscErrorCode ierr;
4947 
4948   PetscFunctionBegin;
4949   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4950   PetscValidType(mat,1);
4951   if (m) PetscValidIntPointer(m,2);
4952   if (n) PetscValidIntPointer(n,3);
4953   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4954   if (m) *m = mat->cmap->rstart;
4955   if (n) *n = mat->cmap->rend;
4956   PetscFunctionReturn(0);
4957 }
4958 
4959 #undef __FUNCT__
4960 #define __FUNCT__ "MatGetOwnershipRange"
4961 /*@
4962    MatGetOwnershipRange - Returns the range of matrix rows owned by
4963    this processor, assuming that the matrix is laid out with the first
4964    n1 rows on the first processor, the next n2 rows on the second, etc.
4965    For certain parallel layouts this range may not be well defined.
4966 
4967    Not Collective, unless matrix has not been allocated, then collective on Mat
4968 
4969    Input Parameters:
4970 .  mat - the matrix
4971 
4972    Output Parameters:
4973 +  m - the global index of the first local row
4974 -  n - one more than the global index of the last local row
4975 
4976    Note: both output parameters can be PETSC_NULL on input.
4977 
4978    Level: beginner
4979 
4980    Concepts: matrices^row ownership
4981 
4982 .seealso:   MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()
4983 
4984 @*/
4985 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt* n)
4986 {
4987   PetscErrorCode ierr;
4988 
4989   PetscFunctionBegin;
4990   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
4991   PetscValidType(mat,1);
4992   if (m) PetscValidIntPointer(m,2);
4993   if (n) PetscValidIntPointer(n,3);
4994   ierr = MatPreallocated(mat);CHKERRQ(ierr);
4995   if (m) *m = mat->rmap->rstart;
4996   if (n) *n = mat->rmap->rend;
4997   PetscFunctionReturn(0);
4998 }
4999 
5000 #undef __FUNCT__
5001 #define __FUNCT__ "MatGetOwnershipRanges"
5002 /*@C
5003    MatGetOwnershipRanges - Returns the range of matrix rows owned by
5004    each process
5005 
5006    Not Collective, unless matrix has not been allocated, then collective on Mat
5007 
5008    Input Parameters:
5009 .  mat - the matrix
5010 
5011    Output Parameters:
5012 .  ranges - start of each processors portion plus one more then the total length at the end
5013 
5014    Level: beginner
5015 
5016    Concepts: matrices^row ownership
5017 
5018 .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()
5019 
5020 @*/
5021 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRanges(Mat mat,const PetscInt **ranges)
5022 {
5023   PetscErrorCode ierr;
5024 
5025   PetscFunctionBegin;
5026   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5027   PetscValidType(mat,1);
5028   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5029   ierr = PetscMapGetRanges(mat->rmap,ranges);CHKERRQ(ierr);
5030   PetscFunctionReturn(0);
5031 }
5032 
5033 #undef __FUNCT__
5034 #define __FUNCT__ "MatGetOwnershipRangesColumn"
5035 /*@C
5036    MatGetOwnershipRangesColumn - Returns the range of local columns for each process
5037 
5038    Not Collective, unless matrix has not been allocated, then collective on Mat
5039 
5040    Input Parameters:
5041 .  mat - the matrix
5042 
5043    Output Parameters:
5044 .  ranges - start of each processors portion plus one more then the total length at the end
5045 
5046    Level: beginner
5047 
5048    Concepts: matrices^column ownership
5049 
5050 .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges()
5051 
5052 @*/
5053 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges)
5054 {
5055   PetscErrorCode ierr;
5056 
5057   PetscFunctionBegin;
5058   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5059   PetscValidType(mat,1);
5060   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5061   ierr = PetscMapGetRanges(mat->cmap,ranges);CHKERRQ(ierr);
5062   PetscFunctionReturn(0);
5063 }
5064 
5065 #undef __FUNCT__
5066 #define __FUNCT__ "MatILUFactorSymbolic"
5067 /*@
5068    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
5069    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
5070    to complete the factorization.
5071 
5072    Collective on Mat
5073 
5074    Input Parameters:
5075 +  mat - the matrix
5076 .  row - row permutation
5077 .  column - column permutation
5078 -  info - structure containing
5079 $      levels - number of levels of fill.
5080 $      expected fill - as ratio of original fill.
5081 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
5082                 missing diagonal entries)
5083 
5084    Output Parameters:
5085 .  fact - new matrix that has been symbolically factored
5086 
5087    Notes:
5088    See the users manual for additional information about
5089    choosing the fill factor for better efficiency.
5090 
5091    Most users should employ the simplified KSP interface for linear solvers
5092    instead of working directly with matrix algebra routines such as this.
5093    See, e.g., KSPCreate().
5094 
5095    Level: developer
5096 
5097   Concepts: matrices^symbolic LU factorization
5098   Concepts: matrices^factorization
5099   Concepts: LU^symbolic factorization
5100 
5101 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
5102           MatGetOrdering(), MatFactorInfo
5103 
5104 @*/
5105 PetscErrorCode PETSCMAT_DLLEXPORT MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
5106 {
5107   PetscErrorCode ierr;
5108 
5109   PetscFunctionBegin;
5110   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5111   PetscValidType(mat,1);
5112   PetscValidHeaderSpecific(row,IS_COOKIE,2);
5113   PetscValidHeaderSpecific(col,IS_COOKIE,3);
5114   PetscValidPointer(info,4);
5115   PetscValidPointer(fact,5);
5116   if (info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels);
5117   if (info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %G",info->fill);
5118   if (!(fact)->ops->ilufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic ILU",((PetscObject)mat)->type_name);
5119   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5120   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5121   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5122 
5123   ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
5124   ierr = (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr);
5125   ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
5126   PetscFunctionReturn(0);
5127 }
5128 
5129 #undef __FUNCT__
5130 #define __FUNCT__ "MatICCFactorSymbolic"
5131 /*@
5132    MatICCFactorSymbolic - Performs symbolic incomplete
5133    Cholesky factorization for a symmetric matrix.  Use
5134    MatCholeskyFactorNumeric() to complete the factorization.
5135 
5136    Collective on Mat
5137 
5138    Input Parameters:
5139 +  mat - the matrix
5140 .  perm - row and column permutation
5141 -  info - structure containing
5142 $      levels - number of levels of fill.
5143 $      expected fill - as ratio of original fill.
5144 
5145    Output Parameter:
5146 .  fact - the factored matrix
5147 
5148    Notes:
5149    Most users should employ the KSP interface for linear solvers
5150    instead of working directly with matrix algebra routines such as this.
5151    See, e.g., KSPCreate().
5152 
5153    Level: developer
5154 
5155   Concepts: matrices^symbolic incomplete Cholesky factorization
5156   Concepts: matrices^factorization
5157   Concepts: Cholsky^symbolic factorization
5158 
5159 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo
5160 @*/
5161 PetscErrorCode PETSCMAT_DLLEXPORT MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
5162 {
5163   PetscErrorCode ierr;
5164 
5165   PetscFunctionBegin;
5166   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5167   PetscValidType(mat,1);
5168   PetscValidHeaderSpecific(perm,IS_COOKIE,2);
5169   PetscValidPointer(info,3);
5170   PetscValidPointer(fact,4);
5171   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5172   if (info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels);
5173   if (info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %G",info->fill);
5174   if (!(fact)->ops->iccfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic ICC",((PetscObject)mat)->type_name);
5175   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5176   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5177 
5178   ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
5179   ierr = (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr);
5180   ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
5181   PetscFunctionReturn(0);
5182 }
5183 
5184 #undef __FUNCT__
5185 #define __FUNCT__ "MatGetArray"
5186 /*@C
5187    MatGetArray - Returns a pointer to the element values in the matrix.
5188    The result of this routine is dependent on the underlying matrix data
5189    structure, and may not even work for certain matrix types.  You MUST
5190    call MatRestoreArray() when you no longer need to access the array.
5191 
5192    Not Collective
5193 
5194    Input Parameter:
5195 .  mat - the matrix
5196 
5197    Output Parameter:
5198 .  v - the location of the values
5199 
5200 
5201    Fortran Note:
5202    This routine is used differently from Fortran, e.g.,
5203 .vb
5204         Mat         mat
5205         PetscScalar mat_array(1)
5206         PetscOffset i_mat
5207         PetscErrorCode ierr
5208         call MatGetArray(mat,mat_array,i_mat,ierr)
5209 
5210   C  Access first local entry in matrix; note that array is
5211   C  treated as one dimensional
5212         value = mat_array(i_mat + 1)
5213 
5214         [... other code ...]
5215         call MatRestoreArray(mat,mat_array,i_mat,ierr)
5216 .ve
5217 
5218    See the Fortran chapter of the users manual and
5219    petsc/src/mat/examples/tests for details.
5220 
5221    Level: advanced
5222 
5223    Concepts: matrices^access array
5224 
5225 .seealso: MatRestoreArray(), MatGetArrayF90(), MatGetRowIJ()
5226 @*/
5227 PetscErrorCode PETSCMAT_DLLEXPORT MatGetArray(Mat mat,PetscScalar *v[])
5228 {
5229   PetscErrorCode ierr;
5230 
5231   PetscFunctionBegin;
5232   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5233   PetscValidType(mat,1);
5234   PetscValidPointer(v,2);
5235   if (!mat->ops->getarray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5236   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5237   ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr);
5238   CHKMEMQ;
5239   PetscFunctionReturn(0);
5240 }
5241 
5242 #undef __FUNCT__
5243 #define __FUNCT__ "MatRestoreArray"
5244 /*@C
5245    MatRestoreArray - Restores the matrix after MatGetArray() has been called.
5246 
5247    Not Collective
5248 
5249    Input Parameter:
5250 +  mat - the matrix
5251 -  v - the location of the values
5252 
5253    Fortran Note:
5254    This routine is used differently from Fortran, e.g.,
5255 .vb
5256         Mat         mat
5257         PetscScalar mat_array(1)
5258         PetscOffset i_mat
5259         PetscErrorCode ierr
5260         call MatGetArray(mat,mat_array,i_mat,ierr)
5261 
5262   C  Access first local entry in matrix; note that array is
5263   C  treated as one dimensional
5264         value = mat_array(i_mat + 1)
5265 
5266         [... other code ...]
5267         call MatRestoreArray(mat,mat_array,i_mat,ierr)
5268 .ve
5269 
5270    See the Fortran chapter of the users manual and
5271    petsc/src/mat/examples/tests for details
5272 
5273    Level: advanced
5274 
5275 .seealso: MatGetArray(), MatRestoreArrayF90()
5276 @*/
5277 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreArray(Mat mat,PetscScalar *v[])
5278 {
5279   PetscErrorCode ierr;
5280 
5281   PetscFunctionBegin;
5282   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5283   PetscValidType(mat,1);
5284   PetscValidPointer(v,2);
5285 #if defined(PETSC_USE_DEBUG)
5286   CHKMEMQ;
5287 #endif
5288   if (!mat->ops->restorearray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5289   ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr);
5290   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5291   PetscFunctionReturn(0);
5292 }
5293 
5294 #undef __FUNCT__
5295 #define __FUNCT__ "MatGetSubMatrices"
5296 /*@C
5297    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
5298    points to an array of valid matrices, they may be reused to store the new
5299    submatrices.
5300 
5301    Collective on Mat
5302 
5303    Input Parameters:
5304 +  mat - the matrix
5305 .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
5306 .  irow, icol - index sets of rows and columns to extract
5307 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5308 
5309    Output Parameter:
5310 .  submat - the array of submatrices
5311 
5312    Notes:
5313    MatGetSubMatrices() can extract ONLY sequential submatrices
5314    (from both sequential and parallel matrices). Use MatGetSubMatrix()
5315    to extract a parallel submatrix.
5316 
5317    When extracting submatrices from a parallel matrix, each processor can
5318    form a different submatrix by setting the rows and columns of its
5319    individual index sets according to the local submatrix desired.
5320 
5321    When finished using the submatrices, the user should destroy
5322    them with MatDestroyMatrices().
5323 
5324    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
5325    original matrix has not changed from that last call to MatGetSubMatrices().
5326 
5327    This routine creates the matrices in submat; you should NOT create them before
5328    calling it. It also allocates the array of matrix pointers submat.
5329 
5330    For BAIJ matrices the index sets must respect the block structure, that is if they
5331    request one row/column in a block, they must request all rows/columns that are in
5332    that block. For example, if the block size is 2 you cannot request just row 0 and
5333    column 0.
5334 
5335    Fortran Note:
5336    The Fortran interface is slightly different from that given below; it
5337    requires one to pass in  as submat a Mat (integer) array of size at least m.
5338 
5339    Level: advanced
5340 
5341    Concepts: matrices^accessing submatrices
5342    Concepts: submatrices
5343 
5344 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal()
5345 @*/
5346 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
5347 {
5348   PetscErrorCode ierr;
5349   PetscInt        i;
5350   PetscTruth      eq;
5351 
5352   PetscFunctionBegin;
5353   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5354   PetscValidType(mat,1);
5355   if (n) {
5356     PetscValidPointer(irow,3);
5357     PetscValidHeaderSpecific(*irow,IS_COOKIE,3);
5358     PetscValidPointer(icol,4);
5359     PetscValidHeaderSpecific(*icol,IS_COOKIE,4);
5360   }
5361   PetscValidPointer(submat,6);
5362   if (n && scall == MAT_REUSE_MATRIX) {
5363     PetscValidPointer(*submat,6);
5364     PetscValidHeaderSpecific(**submat,MAT_COOKIE,6);
5365   }
5366   if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5367   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5368   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5369   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5370 
5371   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
5372   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
5373   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
5374   for (i=0; i<n; i++) {
5375     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
5376       ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr);
5377       if (eq) {
5378 	if (mat->symmetric){
5379 	  ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
5380 	} else if (mat->hermitian) {
5381 	  ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
5382 	} else if (mat->structurally_symmetric) {
5383 	  ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
5384 	}
5385       }
5386     }
5387   }
5388   PetscFunctionReturn(0);
5389 }
5390 
5391 #undef __FUNCT__
5392 #define __FUNCT__ "MatDestroyMatrices"
5393 /*@C
5394    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
5395 
5396    Collective on Mat
5397 
5398    Input Parameters:
5399 +  n - the number of local matrices
5400 -  mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling
5401                        sequence of MatGetSubMatrices())
5402 
5403    Level: advanced
5404 
5405     Notes: Frees not only the matrices, but also the array that contains the matrices
5406 
5407 .seealso: MatGetSubMatrices()
5408 @*/
5409 PetscErrorCode PETSCMAT_DLLEXPORT MatDestroyMatrices(PetscInt n,Mat *mat[])
5410 {
5411   PetscErrorCode ierr;
5412   PetscInt       i;
5413 
5414   PetscFunctionBegin;
5415   if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);
5416   PetscValidPointer(mat,2);
5417   for (i=0; i<n; i++) {
5418     ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr);
5419   }
5420   /* memory is allocated even if n = 0 */
5421   ierr = PetscFree(*mat);CHKERRQ(ierr);
5422   PetscFunctionReturn(0);
5423 }
5424 
5425 #undef __FUNCT__
5426 #define __FUNCT__ "MatGetSeqNonzeroStructure"
5427 /*@C
5428    MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix.
5429 
5430    Collective on Mat
5431 
5432    Input Parameters:
5433 .  mat - the matrix
5434 
5435    Output Parameter:
5436 .  matstruct - the sequential matrix with the nonzero structure of mat
5437 
5438   Level: intermediate
5439 
5440 .seealso: MatDestroySeqNonzeroStructure(), MatGetSubMatrices(), MatDestroyMatrices()
5441 @*/
5442 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct[])
5443 {
5444   PetscErrorCode ierr;
5445 
5446   PetscFunctionBegin;
5447   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5448   PetscValidPointer(matstruct,2);
5449 
5450   PetscValidType(mat,1);
5451   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5452   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5453 
5454   ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
5455   ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr);
5456   ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
5457   PetscFunctionReturn(0);
5458 }
5459 
5460 #undef __FUNCT__
5461 #define __FUNCT__ "MatDestroySeqNonzeroStructure"
5462 /*@C
5463    MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure().
5464 
5465    Collective on Mat
5466 
5467    Input Parameters:
5468 .  mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling
5469                        sequence of MatGetSequentialNonzeroStructure())
5470 
5471    Level: advanced
5472 
5473     Notes: Frees not only the matrices, but also the array that contains the matrices
5474 
5475 .seealso: MatGetSeqNonzeroStructure()
5476 @*/
5477 PetscErrorCode PETSCMAT_DLLEXPORT MatDestroySeqNonzeroStructure(Mat *mat[])
5478 {
5479   PetscErrorCode ierr;
5480 
5481   PetscFunctionBegin;
5482   PetscValidPointer(mat,1);
5483   ierr = MatDestroyMatrices(1,mat);CHKERRQ(ierr);
5484   PetscFunctionReturn(0);
5485 }
5486 
5487 #undef __FUNCT__
5488 #define __FUNCT__ "MatIncreaseOverlap"
5489 /*@
5490    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
5491    replaces the index sets by larger ones that represent submatrices with
5492    additional overlap.
5493 
5494    Collective on Mat
5495 
5496    Input Parameters:
5497 +  mat - the matrix
5498 .  n   - the number of index sets
5499 .  is  - the array of index sets (these index sets will changed during the call)
5500 -  ov  - the additional overlap requested
5501 
5502    Level: developer
5503 
5504    Concepts: overlap
5505    Concepts: ASM^computing overlap
5506 
5507 .seealso: MatGetSubMatrices()
5508 @*/
5509 PetscErrorCode PETSCMAT_DLLEXPORT MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov)
5510 {
5511   PetscErrorCode ierr;
5512 
5513   PetscFunctionBegin;
5514   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5515   PetscValidType(mat,1);
5516   if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
5517   if (n) {
5518     PetscValidPointer(is,3);
5519     PetscValidHeaderSpecific(*is,IS_COOKIE,3);
5520   }
5521   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5522   if (mat->factor)     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5523   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5524 
5525   if (!ov) PetscFunctionReturn(0);
5526   if (!mat->ops->increaseoverlap) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5527   ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
5528   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
5529   ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
5530   PetscFunctionReturn(0);
5531 }
5532 
5533 #undef __FUNCT__
5534 #define __FUNCT__ "MatGetBlockSize"
5535 /*@
5536    MatGetBlockSize - Returns the matrix block size; useful especially for the
5537    block row and block diagonal formats.
5538 
5539    Not Collective
5540 
5541    Input Parameter:
5542 .  mat - the matrix
5543 
5544    Output Parameter:
5545 .  bs - block size
5546 
5547    Notes:
5548    Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ
5549 
5550    Level: intermediate
5551 
5552    Concepts: matrices^block size
5553 
5554 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ()
5555 @*/
5556 PetscErrorCode PETSCMAT_DLLEXPORT MatGetBlockSize(Mat mat,PetscInt *bs)
5557 {
5558   PetscErrorCode ierr;
5559 
5560   PetscFunctionBegin;
5561   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5562   PetscValidType(mat,1);
5563   PetscValidIntPointer(bs,2);
5564   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5565   *bs = mat->rmap->bs;
5566   PetscFunctionReturn(0);
5567 }
5568 
5569 #undef __FUNCT__
5570 #define __FUNCT__ "MatSetBlockSize"
5571 /*@
5572    MatSetBlockSize - Sets the matrix block size; for many matrix types you
5573      cannot use this and MUST set the blocksize when you preallocate the matrix
5574 
5575    Collective on Mat
5576 
5577    Input Parameters:
5578 +  mat - the matrix
5579 -  bs - block size
5580 
5581    Notes:
5582      Only works for shell and AIJ matrices
5583 
5584    Level: intermediate
5585 
5586    Concepts: matrices^block size
5587 
5588 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatGetBlockSize()
5589 @*/
5590 PetscErrorCode PETSCMAT_DLLEXPORT MatSetBlockSize(Mat mat,PetscInt bs)
5591 {
5592   PetscErrorCode ierr;
5593 
5594   PetscFunctionBegin;
5595   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5596   PetscValidType(mat,1);
5597   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5598   if (mat->ops->setblocksize) {
5599     mat->rmap->bs = bs;
5600     ierr = (*mat->ops->setblocksize)(mat,bs);CHKERRQ(ierr);
5601   } else {
5602     SETERRQ1(PETSC_ERR_ARG_INCOMP,"Cannot set the blocksize for matrix type %s",((PetscObject)mat)->type_name);
5603   }
5604   PetscFunctionReturn(0);
5605 }
5606 
5607 #undef __FUNCT__
5608 #define __FUNCT__ "MatGetRowIJ"
5609 /*@C
5610     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
5611 
5612    Collective on Mat
5613 
5614     Input Parameters:
5615 +   mat - the matrix
5616 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
5617 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
5618                 symmetrized
5619 -   blockcompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
5620                  blockcompressed matrix is desired or not [inode, baij have blockcompressed
5621                  nonzero structure which is different than the full nonzero structure]
5622 
5623     Output Parameters:
5624 +   n - number of rows in the (possibly compressed) matrix
5625 .   ia - the row pointers [of length n+1]
5626 .   ja - the column indices
5627 -   done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers
5628            are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set
5629 
5630     Level: developer
5631 
5632     Notes: You CANNOT change any of the ia[] or ja[] values.
5633 
5634            Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values
5635 
5636     Fortran Node
5637 
5638            In Fortran use
5639 $           PetscInt ia(1), ja(1)
5640 $           PetscOffset iia, jja
5641 $      call MatGetRowIJ(mat,shift,symmetric,blockcompressed,n,ia,iia,ja,jja,done,ierr)
5642 
5643        Acess the ith and jth entries via ia(iia + i) and ja(jja + j)
5644 
5645 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatGetArray()
5646 @*/
5647 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done)
5648 {
5649   PetscErrorCode ierr;
5650 
5651   PetscFunctionBegin;
5652   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5653   PetscValidType(mat,1);
5654   PetscValidIntPointer(n,4);
5655   if (ia) PetscValidIntPointer(ia,5);
5656   if (ja) PetscValidIntPointer(ja,6);
5657   PetscValidIntPointer(done,7);
5658   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5659   if (!mat->ops->getrowij) *done = PETSC_FALSE;
5660   else {
5661     *done = PETSC_TRUE;
5662     ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
5663     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,blockcompressed,n,ia,ja,done);CHKERRQ(ierr);
5664     ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
5665   }
5666   PetscFunctionReturn(0);
5667 }
5668 
5669 #undef __FUNCT__
5670 #define __FUNCT__ "MatGetColumnIJ"
5671 /*@C
5672     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
5673 
5674     Collective on Mat
5675 
5676     Input Parameters:
5677 +   mat - the matrix
5678 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
5679 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
5680                 symmetrized
5681 -   blockcompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
5682                  blockcompressed matrix is desired or not [inode, baij have blockcompressed
5683                  nonzero structure which is different than the full nonzero structure]
5684 
5685     Output Parameters:
5686 +   n - number of columns in the (possibly compressed) matrix
5687 .   ia - the column pointers
5688 .   ja - the row indices
5689 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
5690 
5691     Level: developer
5692 
5693 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
5694 @*/
5695 PetscErrorCode PETSCMAT_DLLEXPORT MatGetColumnIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done)
5696 {
5697   PetscErrorCode ierr;
5698 
5699   PetscFunctionBegin;
5700   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5701   PetscValidType(mat,1);
5702   PetscValidIntPointer(n,4);
5703   if (ia) PetscValidIntPointer(ia,5);
5704   if (ja) PetscValidIntPointer(ja,6);
5705   PetscValidIntPointer(done,7);
5706   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5707   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
5708   else {
5709     *done = PETSC_TRUE;
5710     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,blockcompressed,n,ia,ja,done);CHKERRQ(ierr);
5711   }
5712   PetscFunctionReturn(0);
5713 }
5714 
5715 #undef __FUNCT__
5716 #define __FUNCT__ "MatRestoreRowIJ"
5717 /*@C
5718     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
5719     MatGetRowIJ().
5720 
5721     Collective on Mat
5722 
5723     Input Parameters:
5724 +   mat - the matrix
5725 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
5726 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
5727                 symmetrized
5728 -   blockcompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
5729                  blockcompressed matrix is desired or not [inode, baij have blockcompressed
5730                  nonzero structure which is different than the full nonzero structure]
5731 
5732     Output Parameters:
5733 +   n - size of (possibly compressed) matrix
5734 .   ia - the row pointers
5735 .   ja - the column indices
5736 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
5737 
5738     Level: developer
5739 
5740 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
5741 @*/
5742 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreRowIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done)
5743 {
5744   PetscErrorCode ierr;
5745 
5746   PetscFunctionBegin;
5747   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5748   PetscValidType(mat,1);
5749   if (ia) PetscValidIntPointer(ia,5);
5750   if (ja) PetscValidIntPointer(ja,6);
5751   PetscValidIntPointer(done,7);
5752   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5753 
5754   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
5755   else {
5756     *done = PETSC_TRUE;
5757     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,blockcompressed,n,ia,ja,done);CHKERRQ(ierr);
5758   }
5759   PetscFunctionReturn(0);
5760 }
5761 
5762 #undef __FUNCT__
5763 #define __FUNCT__ "MatRestoreColumnIJ"
5764 /*@C
5765     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
5766     MatGetColumnIJ().
5767 
5768     Collective on Mat
5769 
5770     Input Parameters:
5771 +   mat - the matrix
5772 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
5773 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
5774                 symmetrized
5775 -   blockcompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
5776                  blockcompressed matrix is desired or not [inode, baij have blockcompressed
5777                  nonzero structure which is different than the full nonzero structure]
5778 
5779     Output Parameters:
5780 +   n - size of (possibly compressed) matrix
5781 .   ia - the column pointers
5782 .   ja - the row indices
5783 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
5784 
5785     Level: developer
5786 
5787 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
5788 @*/
5789 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done)
5790 {
5791   PetscErrorCode ierr;
5792 
5793   PetscFunctionBegin;
5794   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5795   PetscValidType(mat,1);
5796   if (ia) PetscValidIntPointer(ia,5);
5797   if (ja) PetscValidIntPointer(ja,6);
5798   PetscValidIntPointer(done,7);
5799   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5800 
5801   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
5802   else {
5803     *done = PETSC_TRUE;
5804     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,blockcompressed,n,ia,ja,done);CHKERRQ(ierr);
5805   }
5806   PetscFunctionReturn(0);
5807 }
5808 
5809 #undef __FUNCT__
5810 #define __FUNCT__ "MatColoringPatch"
5811 /*@C
5812     MatColoringPatch -Used inside matrix coloring routines that
5813     use MatGetRowIJ() and/or MatGetColumnIJ().
5814 
5815     Collective on Mat
5816 
5817     Input Parameters:
5818 +   mat - the matrix
5819 .   ncolors - max color value
5820 .   n   - number of entries in colorarray
5821 -   colorarray - array indicating color for each column
5822 
5823     Output Parameters:
5824 .   iscoloring - coloring generated using colorarray information
5825 
5826     Level: developer
5827 
5828 .seealso: MatGetRowIJ(), MatGetColumnIJ()
5829 
5830 @*/
5831 PetscErrorCode PETSCMAT_DLLEXPORT MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring)
5832 {
5833   PetscErrorCode ierr;
5834 
5835   PetscFunctionBegin;
5836   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5837   PetscValidType(mat,1);
5838   PetscValidIntPointer(colorarray,4);
5839   PetscValidPointer(iscoloring,5);
5840   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5841 
5842   if (!mat->ops->coloringpatch){
5843     ierr = ISColoringCreate(((PetscObject)mat)->comm,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
5844   } else {
5845     ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
5846   }
5847   PetscFunctionReturn(0);
5848 }
5849 
5850 
5851 #undef __FUNCT__
5852 #define __FUNCT__ "MatSetUnfactored"
5853 /*@
5854    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
5855 
5856    Collective on Mat
5857 
5858    Input Parameter:
5859 .  mat - the factored matrix to be reset
5860 
5861    Notes:
5862    This routine should be used only with factored matrices formed by in-place
5863    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
5864    format).  This option can save memory, for example, when solving nonlinear
5865    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
5866    ILU(0) preconditioner.
5867 
5868    Note that one can specify in-place ILU(0) factorization by calling
5869 .vb
5870      PCType(pc,PCILU);
5871      PCFactorSeUseInPlace(pc);
5872 .ve
5873    or by using the options -pc_type ilu -pc_factor_in_place
5874 
5875    In-place factorization ILU(0) can also be used as a local
5876    solver for the blocks within the block Jacobi or additive Schwarz
5877    methods (runtime option: -sub_pc_factor_in_place).  See the discussion
5878    of these preconditioners in the users manual for details on setting
5879    local solver options.
5880 
5881    Most users should employ the simplified KSP interface for linear solvers
5882    instead of working directly with matrix algebra routines such as this.
5883    See, e.g., KSPCreate().
5884 
5885    Level: developer
5886 
5887 .seealso: PCFactorSetUseInPlace()
5888 
5889    Concepts: matrices^unfactored
5890 
5891 @*/
5892 PetscErrorCode PETSCMAT_DLLEXPORT MatSetUnfactored(Mat mat)
5893 {
5894   PetscErrorCode ierr;
5895 
5896   PetscFunctionBegin;
5897   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
5898   PetscValidType(mat,1);
5899   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5900   mat->factor = MAT_FACTOR_NONE;
5901   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
5902   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
5903   PetscFunctionReturn(0);
5904 }
5905 
5906 /*MC
5907     MatGetArrayF90 - Accesses a matrix array from Fortran90.
5908 
5909     Synopsis:
5910     MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
5911 
5912     Not collective
5913 
5914     Input Parameter:
5915 .   x - matrix
5916 
5917     Output Parameters:
5918 +   xx_v - the Fortran90 pointer to the array
5919 -   ierr - error code
5920 
5921     Example of Usage:
5922 .vb
5923       PetscScalar, pointer xx_v(:)
5924       ....
5925       call MatGetArrayF90(x,xx_v,ierr)
5926       a = xx_v(3)
5927       call MatRestoreArrayF90(x,xx_v,ierr)
5928 .ve
5929 
5930     Notes:
5931     Not yet supported for all F90 compilers
5932 
5933     Level: advanced
5934 
5935 .seealso:  MatRestoreArrayF90(), MatGetArray(), MatRestoreArray()
5936 
5937     Concepts: matrices^accessing array
5938 
5939 M*/
5940 
5941 /*MC
5942     MatRestoreArrayF90 - Restores a matrix array that has been
5943     accessed with MatGetArrayF90().
5944 
5945     Synopsis:
5946     MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
5947 
5948     Not collective
5949 
5950     Input Parameters:
5951 +   x - matrix
5952 -   xx_v - the Fortran90 pointer to the array
5953 
5954     Output Parameter:
5955 .   ierr - error code
5956 
5957     Example of Usage:
5958 .vb
5959        PetscScalar, pointer xx_v(:)
5960        ....
5961        call MatGetArrayF90(x,xx_v,ierr)
5962        a = xx_v(3)
5963        call MatRestoreArrayF90(x,xx_v,ierr)
5964 .ve
5965 
5966     Notes:
5967     Not yet supported for all F90 compilers
5968 
5969     Level: advanced
5970 
5971 .seealso:  MatGetArrayF90(), MatGetArray(), MatRestoreArray()
5972 
5973 M*/
5974 
5975 
5976 #undef __FUNCT__
5977 #define __FUNCT__ "MatGetSubMatrix"
5978 /*@
5979     MatGetSubMatrix - Gets a single submatrix on the same number of processors
5980                       as the original matrix.
5981 
5982     Collective on Mat
5983 
5984     Input Parameters:
5985 +   mat - the original matrix
5986 .   isrow - rows this processor should obtain
5987 .   iscol - columns for all processors you wish to keep
5988 .   csize - number of columns "local" to this processor (does nothing for sequential
5989             matrices). This should match the result from VecGetLocalSize(x,...) if you
5990             plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE
5991 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5992 
5993     Output Parameter:
5994 .   newmat - the new submatrix, of the same type as the old
5995 
5996     Level: advanced
5997 
5998     Notes: the iscol argument MUST be the same on each processor. You might be
5999     able to create the iscol argument with ISAllGather(). The rows is isrow will be
6000     sorted into the same order as the original matrix.
6001 
6002       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
6003    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
6004    to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX
6005    will reuse the matrix generated the first time.  You should call MatDestroy() on newmat when
6006    you are finished using it.
6007 
6008     The communicator of the newly obtained matrix is ALWAYS the same as the communicator of
6009     the input matrix.
6010 
6011     If iscol is PETSC_NULL then all columns are obtained (not supported in Fortran), you should
6012     use csize = PETSC_DECIDE also in this case.
6013 
6014     Concepts: matrices^submatrices
6015 
6016 .seealso: MatGetSubMatrices(), ISAllGather()
6017 @*/
6018 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSubMatrix(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse cll,Mat *newmat)
6019 {
6020   PetscErrorCode ierr;
6021   PetscMPIInt    size;
6022   Mat            *local;
6023   IS             iscoltmp;
6024 
6025   PetscFunctionBegin;
6026   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6027   PetscValidHeaderSpecific(isrow,IS_COOKIE,2);
6028   if (iscol) PetscValidHeaderSpecific(iscol,IS_COOKIE,3);
6029   PetscValidPointer(newmat,6);
6030   if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_COOKIE,6);
6031   PetscValidType(mat,1);
6032   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6033   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6034   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
6035 
6036   if (!iscol) {
6037     if (csize == PETSC_DECIDE) csize = mat->cmap->n;
6038     ierr = ISCreateStride(((PetscObject)mat)->comm,mat->cmap->N,0,1,&iscoltmp);CHKERRQ(ierr);
6039   } else {
6040     iscoltmp = iscol;
6041   }
6042 
6043   /* if original matrix is on just one processor then use submatrix generated */
6044   if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
6045     ierr = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
6046     if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);}
6047     PetscFunctionReturn(0);
6048   } else if (!mat->ops->getsubmatrix && size == 1) {
6049     ierr    = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
6050     *newmat = *local;
6051     ierr    = PetscFree(local);CHKERRQ(ierr);
6052     if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);}
6053     PetscFunctionReturn(0);
6054   }
6055 
6056   if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6057   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscoltmp,csize,cll,newmat);CHKERRQ(ierr);
6058   if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);}
6059   ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr);
6060   PetscFunctionReturn(0);
6061 }
6062 
6063 #undef __FUNCT__
6064 #define __FUNCT__ "MatGetSubMatrixRaw"
6065 /*@
6066     MatGetSubMatrixRaw - Gets a single submatrix on the same number of processors
6067                          as the original matrix.
6068 
6069     Collective on Mat
6070 
6071     Input Parameters:
6072 +   mat - the original matrix
6073 .   nrows - the number of rows this processor should obtain
6074 .   rows - rows this processor should obtain
6075 .   ncols - the number of columns for all processors you wish to keep
6076 .   cols - columns for all processors you wish to keep
6077 .   csize - number of columns "local" to this processor (does nothing for sequential
6078             matrices). This should match the result from VecGetLocalSize(x,...) if you
6079             plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE
6080 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
6081 
6082     Output Parameter:
6083 .   newmat - the new submatrix, of the same type as the old
6084 
6085     Level: advanced
6086 
6087     Notes: the iscol argument MUST be the same on each processor. You might be
6088     able to create the iscol argument with ISAllGather().
6089 
6090       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
6091    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
6092    to this routine with a mat of the same nonzero structure and with a cll of MAT_REUSE_MATRIX
6093    will reuse the matrix generated the first time.
6094 
6095     Concepts: matrices^submatrices
6096 
6097 .seealso: MatGetSubMatrices(), ISAllGather()
6098 @*/
6099 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSubMatrixRaw(Mat mat,PetscInt nrows,const PetscInt rows[],PetscInt ncols,const PetscInt cols[],PetscInt csize,MatReuse cll,Mat *newmat)
6100 {
6101   IS             isrow, iscol;
6102   PetscErrorCode ierr;
6103 
6104   PetscFunctionBegin;
6105   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6106   PetscValidIntPointer(rows,2);
6107   PetscValidIntPointer(cols,3);
6108   PetscValidPointer(newmat,6);
6109   if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_COOKIE,6);
6110   PetscValidType(mat,1);
6111   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6112   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6113   ierr = ISCreateGeneralWithArray(PETSC_COMM_SELF, nrows, (PetscInt *) rows, &isrow);CHKERRQ(ierr);
6114   ierr = ISCreateGeneralWithArray(PETSC_COMM_SELF, ncols, (PetscInt *) cols, &iscol);CHKERRQ(ierr);
6115   ierr = MatGetSubMatrix(mat, isrow, iscol, csize, cll, newmat);CHKERRQ(ierr);
6116   ierr = ISDestroy(isrow);CHKERRQ(ierr);
6117   ierr = ISDestroy(iscol);CHKERRQ(ierr);
6118   PetscFunctionReturn(0);
6119 }
6120 
6121 #undef __FUNCT__
6122 #define __FUNCT__ "MatStashSetInitialSize"
6123 /*@
6124    MatStashSetInitialSize - sets the sizes of the matrix stash, that is
6125    used during the assembly process to store values that belong to
6126    other processors.
6127 
6128    Not Collective
6129 
6130    Input Parameters:
6131 +  mat   - the matrix
6132 .  size  - the initial size of the stash.
6133 -  bsize - the initial size of the block-stash(if used).
6134 
6135    Options Database Keys:
6136 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
6137 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
6138 
6139    Level: intermediate
6140 
6141    Notes:
6142      The block-stash is used for values set with MatSetValuesBlocked() while
6143      the stash is used for values set with MatSetValues()
6144 
6145      Run with the option -info and look for output of the form
6146      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
6147      to determine the appropriate value, MM, to use for size and
6148      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
6149      to determine the value, BMM to use for bsize
6150 
6151    Concepts: stash^setting matrix size
6152    Concepts: matrices^stash
6153 
6154 @*/
6155 PetscErrorCode PETSCMAT_DLLEXPORT MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize)
6156 {
6157   PetscErrorCode ierr;
6158 
6159   PetscFunctionBegin;
6160   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6161   PetscValidType(mat,1);
6162   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
6163   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
6164   PetscFunctionReturn(0);
6165 }
6166 
6167 #undef __FUNCT__
6168 #define __FUNCT__ "MatInterpolateAdd"
6169 /*@
6170    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
6171      the matrix
6172 
6173    Collective on Mat
6174 
6175    Input Parameters:
6176 +  mat   - the matrix
6177 .  x,y - the vectors
6178 -  w - where the result is stored
6179 
6180    Level: intermediate
6181 
6182    Notes:
6183     w may be the same vector as y.
6184 
6185     This allows one to use either the restriction or interpolation (its transpose)
6186     matrix to do the interpolation
6187 
6188     Concepts: interpolation
6189 
6190 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
6191 
6192 @*/
6193 PetscErrorCode PETSCMAT_DLLEXPORT MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
6194 {
6195   PetscErrorCode ierr;
6196   PetscInt       M,N;
6197 
6198   PetscFunctionBegin;
6199   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
6200   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
6201   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
6202   PetscValidHeaderSpecific(w,VEC_COOKIE,4);
6203   PetscValidType(A,1);
6204   ierr = MatPreallocated(A);CHKERRQ(ierr);
6205   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
6206   if (N > M) {
6207     ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr);
6208   } else {
6209     ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr);
6210   }
6211   PetscFunctionReturn(0);
6212 }
6213 
6214 #undef __FUNCT__
6215 #define __FUNCT__ "MatInterpolate"
6216 /*@
6217    MatInterpolate - y = A*x or A'*x depending on the shape of
6218      the matrix
6219 
6220    Collective on Mat
6221 
6222    Input Parameters:
6223 +  mat   - the matrix
6224 -  x,y - the vectors
6225 
6226    Level: intermediate
6227 
6228    Notes:
6229     This allows one to use either the restriction or interpolation (its transpose)
6230     matrix to do the interpolation
6231 
6232    Concepts: matrices^interpolation
6233 
6234 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
6235 
6236 @*/
6237 PetscErrorCode PETSCMAT_DLLEXPORT MatInterpolate(Mat A,Vec x,Vec y)
6238 {
6239   PetscErrorCode ierr;
6240   PetscInt       M,N;
6241 
6242   PetscFunctionBegin;
6243   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
6244   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
6245   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
6246   PetscValidType(A,1);
6247   ierr = MatPreallocated(A);CHKERRQ(ierr);
6248   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
6249   if (N > M) {
6250     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
6251   } else {
6252     ierr = MatMult(A,x,y);CHKERRQ(ierr);
6253   }
6254   PetscFunctionReturn(0);
6255 }
6256 
6257 #undef __FUNCT__
6258 #define __FUNCT__ "MatRestrict"
6259 /*@
6260    MatRestrict - y = A*x or A'*x
6261 
6262    Collective on Mat
6263 
6264    Input Parameters:
6265 +  mat   - the matrix
6266 -  x,y - the vectors
6267 
6268    Level: intermediate
6269 
6270    Notes:
6271     This allows one to use either the restriction or interpolation (its transpose)
6272     matrix to do the restriction
6273 
6274    Concepts: matrices^restriction
6275 
6276 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()
6277 
6278 @*/
6279 PetscErrorCode PETSCMAT_DLLEXPORT MatRestrict(Mat A,Vec x,Vec y)
6280 {
6281   PetscErrorCode ierr;
6282   PetscInt       M,N;
6283 
6284   PetscFunctionBegin;
6285   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
6286   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
6287   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
6288   PetscValidType(A,1);
6289   ierr = MatPreallocated(A);CHKERRQ(ierr);
6290 
6291   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
6292   if (N > M) {
6293     ierr = MatMult(A,x,y);CHKERRQ(ierr);
6294   } else {
6295     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
6296   }
6297   PetscFunctionReturn(0);
6298 }
6299 
6300 #undef __FUNCT__
6301 #define __FUNCT__ "MatNullSpaceAttach"
6302 /*@
6303    MatNullSpaceAttach - attaches a null space to a matrix.
6304         This null space will be removed from the resulting vector whenever
6305         MatMult() is called
6306 
6307    Collective on Mat
6308 
6309    Input Parameters:
6310 +  mat - the matrix
6311 -  nullsp - the null space object
6312 
6313    Level: developer
6314 
6315    Notes:
6316       Overwrites any previous null space that may have been attached
6317 
6318    Concepts: null space^attaching to matrix
6319 
6320 .seealso: MatCreate(), MatNullSpaceCreate()
6321 @*/
6322 PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceAttach(Mat mat,MatNullSpace nullsp)
6323 {
6324   PetscErrorCode ierr;
6325 
6326   PetscFunctionBegin;
6327   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6328   PetscValidType(mat,1);
6329   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_COOKIE,2);
6330   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6331   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
6332   if (mat->nullsp) { ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr); }
6333   mat->nullsp = nullsp;
6334   PetscFunctionReturn(0);
6335 }
6336 
6337 #undef __FUNCT__
6338 #define __FUNCT__ "MatICCFactor"
6339 /*@
6340    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.
6341 
6342    Collective on Mat
6343 
6344    Input Parameters:
6345 +  mat - the matrix
6346 .  row - row/column permutation
6347 .  fill - expected fill factor >= 1.0
6348 -  level - level of fill, for ICC(k)
6349 
6350    Notes:
6351    Probably really in-place only when level of fill is zero, otherwise allocates
6352    new space to store factored matrix and deletes previous memory.
6353 
6354    Most users should employ the simplified KSP interface for linear solvers
6355    instead of working directly with matrix algebra routines such as this.
6356    See, e.g., KSPCreate().
6357 
6358    Level: developer
6359 
6360    Concepts: matrices^incomplete Cholesky factorization
6361    Concepts: Cholesky factorization
6362 
6363 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
6364 @*/
6365 PetscErrorCode PETSCMAT_DLLEXPORT MatICCFactor(Mat mat,IS row,const MatFactorInfo* info)
6366 {
6367   PetscErrorCode ierr;
6368 
6369   PetscFunctionBegin;
6370   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6371   PetscValidType(mat,1);
6372   if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2);
6373   PetscValidPointer(info,3);
6374   if (mat->rmap->N != mat->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
6375   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6376   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6377   if (!mat->ops->iccfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6378   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6379   ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr);
6380   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
6381   PetscFunctionReturn(0);
6382 }
6383 
6384 #undef __FUNCT__
6385 #define __FUNCT__ "MatSetValuesAdic"
6386 /*@
6387    MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix.
6388 
6389    Not Collective
6390 
6391    Input Parameters:
6392 +  mat - the matrix
6393 -  v - the values compute with ADIC
6394 
6395    Level: developer
6396 
6397    Notes:
6398      Must call MatSetColoring() before using this routine. Also this matrix must already
6399      have its nonzero pattern determined.
6400 
6401 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
6402           MatSetValues(), MatSetColoring(), MatSetValuesAdifor()
6403 @*/
6404 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesAdic(Mat mat,void *v)
6405 {
6406   PetscErrorCode ierr;
6407 
6408   PetscFunctionBegin;
6409   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6410   PetscValidType(mat,1);
6411   PetscValidPointer(mat,2);
6412 
6413   if (!mat->assembled) {
6414     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
6415   }
6416   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
6417   if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6418   ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr);
6419   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
6420   ierr = MatView_Private(mat);CHKERRQ(ierr);
6421   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
6422   PetscFunctionReturn(0);
6423 }
6424 
6425 
6426 #undef __FUNCT__
6427 #define __FUNCT__ "MatSetColoring"
6428 /*@
6429    MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic()
6430 
6431    Not Collective
6432 
6433    Input Parameters:
6434 +  mat - the matrix
6435 -  coloring - the coloring
6436 
6437    Level: developer
6438 
6439 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
6440           MatSetValues(), MatSetValuesAdic()
6441 @*/
6442 PetscErrorCode PETSCMAT_DLLEXPORT MatSetColoring(Mat mat,ISColoring coloring)
6443 {
6444   PetscErrorCode ierr;
6445 
6446   PetscFunctionBegin;
6447   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6448   PetscValidType(mat,1);
6449   PetscValidPointer(coloring,2);
6450 
6451   if (!mat->assembled) {
6452     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
6453   }
6454   if (!mat->ops->setcoloring) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6455   ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr);
6456   PetscFunctionReturn(0);
6457 }
6458 
6459 #undef __FUNCT__
6460 #define __FUNCT__ "MatSetValuesAdifor"
6461 /*@
6462    MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix.
6463 
6464    Not Collective
6465 
6466    Input Parameters:
6467 +  mat - the matrix
6468 .  nl - leading dimension of v
6469 -  v - the values compute with ADIFOR
6470 
6471    Level: developer
6472 
6473    Notes:
6474      Must call MatSetColoring() before using this routine. Also this matrix must already
6475      have its nonzero pattern determined.
6476 
6477 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
6478           MatSetValues(), MatSetColoring()
6479 @*/
6480 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesAdifor(Mat mat,PetscInt nl,void *v)
6481 {
6482   PetscErrorCode ierr;
6483 
6484   PetscFunctionBegin;
6485   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6486   PetscValidType(mat,1);
6487   PetscValidPointer(v,3);
6488 
6489   if (!mat->assembled) {
6490     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
6491   }
6492   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
6493   if (!mat->ops->setvaluesadifor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6494   ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr);
6495   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
6496   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
6497   PetscFunctionReturn(0);
6498 }
6499 
6500 #undef __FUNCT__
6501 #define __FUNCT__ "MatDiagonalScaleLocal"
6502 /*@
6503    MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the
6504          ghosted ones.
6505 
6506    Not Collective
6507 
6508    Input Parameters:
6509 +  mat - the matrix
6510 -  diag = the diagonal values, including ghost ones
6511 
6512    Level: developer
6513 
6514    Notes: Works only for MPIAIJ and MPIBAIJ matrices
6515 
6516 .seealso: MatDiagonalScale()
6517 @*/
6518 PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalScaleLocal(Mat mat,Vec diag)
6519 {
6520   PetscErrorCode ierr;
6521   PetscMPIInt    size;
6522 
6523   PetscFunctionBegin;
6524   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6525   PetscValidHeaderSpecific(diag,VEC_COOKIE,2);
6526   PetscValidType(mat,1);
6527 
6528   if (!mat->assembled) {
6529     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
6530   }
6531   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
6532   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
6533   if (size == 1) {
6534     PetscInt n,m;
6535     ierr = VecGetSize(diag,&n);CHKERRQ(ierr);
6536     ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr);
6537     if (m == n) {
6538       ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr);
6539     } else {
6540       SETERRQ(PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions");
6541     }
6542   } else {
6543     PetscErrorCode (*f)(Mat,Vec);
6544     ierr = PetscObjectQueryFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",(void (**)(void))&f);CHKERRQ(ierr);
6545     if (f) {
6546       ierr = (*f)(mat,diag);CHKERRQ(ierr);
6547     } else {
6548       SETERRQ(PETSC_ERR_SUP,"Only supported for MPIAIJ and MPIBAIJ parallel matrices");
6549     }
6550   }
6551   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
6552   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
6553   PetscFunctionReturn(0);
6554 }
6555 
6556 #undef __FUNCT__
6557 #define __FUNCT__ "MatGetInertia"
6558 /*@
6559    MatGetInertia - Gets the inertia from a factored matrix
6560 
6561    Collective on Mat
6562 
6563    Input Parameter:
6564 .  mat - the matrix
6565 
6566    Output Parameters:
6567 +   nneg - number of negative eigenvalues
6568 .   nzero - number of zero eigenvalues
6569 -   npos - number of positive eigenvalues
6570 
6571    Level: advanced
6572 
6573    Notes: Matrix must have been factored by MatCholeskyFactor()
6574 
6575 
6576 @*/
6577 PetscErrorCode PETSCMAT_DLLEXPORT MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
6578 {
6579   PetscErrorCode ierr;
6580 
6581   PetscFunctionBegin;
6582   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6583   PetscValidType(mat,1);
6584   if (!mat->factor)    SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
6585   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled");
6586   if (!mat->ops->getinertia) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6587   ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr);
6588   PetscFunctionReturn(0);
6589 }
6590 
6591 /* ----------------------------------------------------------------*/
6592 #undef __FUNCT__
6593 #define __FUNCT__ "MatSolves"
6594 /*@
6595    MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors
6596 
6597    Collective on Mat and Vecs
6598 
6599    Input Parameters:
6600 +  mat - the factored matrix
6601 -  b - the right-hand-side vectors
6602 
6603    Output Parameter:
6604 .  x - the result vectors
6605 
6606    Notes:
6607    The vectors b and x cannot be the same.  I.e., one cannot
6608    call MatSolves(A,x,x).
6609 
6610    Notes:
6611    Most users should employ the simplified KSP interface for linear solvers
6612    instead of working directly with matrix algebra routines such as this.
6613    See, e.g., KSPCreate().
6614 
6615    Level: developer
6616 
6617    Concepts: matrices^triangular solves
6618 
6619 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve()
6620 @*/
6621 PetscErrorCode PETSCMAT_DLLEXPORT MatSolves(Mat mat,Vecs b,Vecs x)
6622 {
6623   PetscErrorCode ierr;
6624 
6625   PetscFunctionBegin;
6626   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6627   PetscValidType(mat,1);
6628   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
6629   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
6630   if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0);
6631 
6632   if (!mat->ops->solves) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6633   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6634   ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
6635   ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr);
6636   ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
6637   PetscFunctionReturn(0);
6638 }
6639 
6640 #undef __FUNCT__
6641 #define __FUNCT__ "MatIsSymmetric"
6642 /*@
6643    MatIsSymmetric - Test whether a matrix is symmetric
6644 
6645    Collective on Mat
6646 
6647    Input Parameter:
6648 +  A - the matrix to test
6649 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose)
6650 
6651    Output Parameters:
6652 .  flg - the result
6653 
6654    Level: intermediate
6655 
6656    Concepts: matrix^symmetry
6657 
6658 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
6659 @*/
6660 PetscErrorCode PETSCMAT_DLLEXPORT MatIsSymmetric(Mat A,PetscReal tol,PetscTruth *flg)
6661 {
6662   PetscErrorCode ierr;
6663 
6664   PetscFunctionBegin;
6665   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
6666   PetscValidPointer(flg,2);
6667   if (!A->symmetric_set) {
6668     if (!A->ops->issymmetric) {
6669       const MatType mattype;
6670       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
6671       SETERRQ1(PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
6672     }
6673     ierr = (*A->ops->issymmetric)(A,tol,&A->symmetric);CHKERRQ(ierr);
6674     A->symmetric_set = PETSC_TRUE;
6675     if (A->symmetric) {
6676       A->structurally_symmetric_set = PETSC_TRUE;
6677       A->structurally_symmetric     = PETSC_TRUE;
6678     }
6679   }
6680   *flg = A->symmetric;
6681   PetscFunctionReturn(0);
6682 }
6683 
6684 #undef __FUNCT__
6685 #define __FUNCT__ "MatIsHermitian"
6686 /*@
6687    MatIsHermitian - Test whether a matrix is Hermitian
6688 
6689    Collective on Mat
6690 
6691    Input Parameter:
6692 +  A - the matrix to test
6693 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian)
6694 
6695    Output Parameters:
6696 .  flg - the result
6697 
6698    Level: intermediate
6699 
6700    Concepts: matrix^symmetry
6701 
6702 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
6703 @*/
6704 PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitian(Mat A,PetscReal tol,PetscTruth *flg)
6705 {
6706   PetscErrorCode ierr;
6707 
6708   PetscFunctionBegin;
6709   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
6710   PetscValidPointer(flg,2);
6711   if (!A->hermitian_set) {
6712     if (!A->ops->ishermitian) {
6713       const MatType mattype;
6714       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
6715       SETERRQ1(PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for Hermitian",mattype);
6716     }
6717     ierr = (*A->ops->ishermitian)(A,tol,&A->hermitian);CHKERRQ(ierr);
6718     A->hermitian_set = PETSC_TRUE;
6719     if (A->hermitian) {
6720       A->structurally_symmetric_set = PETSC_TRUE;
6721       A->structurally_symmetric     = PETSC_TRUE;
6722     }
6723   }
6724   *flg = A->hermitian;
6725   PetscFunctionReturn(0);
6726 }
6727 
6728 #undef __FUNCT__
6729 #define __FUNCT__ "MatIsSymmetricKnown"
6730 /*@
6731    MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric.
6732 
6733    Collective on Mat
6734 
6735    Input Parameter:
6736 .  A - the matrix to check
6737 
6738    Output Parameters:
6739 +  set - if the symmetric flag is set (this tells you if the next flag is valid)
6740 -  flg - the result
6741 
6742    Level: advanced
6743 
6744    Concepts: matrix^symmetry
6745 
6746    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric()
6747          if you want it explicitly checked
6748 
6749 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
6750 @*/
6751 PetscErrorCode PETSCMAT_DLLEXPORT MatIsSymmetricKnown(Mat A,PetscTruth *set,PetscTruth *flg)
6752 {
6753   PetscFunctionBegin;
6754   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
6755   PetscValidPointer(set,2);
6756   PetscValidPointer(flg,3);
6757   if (A->symmetric_set) {
6758     *set = PETSC_TRUE;
6759     *flg = A->symmetric;
6760   } else {
6761     *set = PETSC_FALSE;
6762   }
6763   PetscFunctionReturn(0);
6764 }
6765 
6766 #undef __FUNCT__
6767 #define __FUNCT__ "MatIsHermitianKnown"
6768 /*@
6769    MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian.
6770 
6771    Collective on Mat
6772 
6773    Input Parameter:
6774 .  A - the matrix to check
6775 
6776    Output Parameters:
6777 +  set - if the hermitian flag is set (this tells you if the next flag is valid)
6778 -  flg - the result
6779 
6780    Level: advanced
6781 
6782    Concepts: matrix^symmetry
6783 
6784    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian()
6785          if you want it explicitly checked
6786 
6787 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
6788 @*/
6789 PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianKnown(Mat A,PetscTruth *set,PetscTruth *flg)
6790 {
6791   PetscFunctionBegin;
6792   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
6793   PetscValidPointer(set,2);
6794   PetscValidPointer(flg,3);
6795   if (A->hermitian_set) {
6796     *set = PETSC_TRUE;
6797     *flg = A->hermitian;
6798   } else {
6799     *set = PETSC_FALSE;
6800   }
6801   PetscFunctionReturn(0);
6802 }
6803 
6804 #undef __FUNCT__
6805 #define __FUNCT__ "MatIsStructurallySymmetric"
6806 /*@
6807    MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric
6808 
6809    Collective on Mat
6810 
6811    Input Parameter:
6812 .  A - the matrix to test
6813 
6814    Output Parameters:
6815 .  flg - the result
6816 
6817    Level: intermediate
6818 
6819    Concepts: matrix^symmetry
6820 
6821 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption()
6822 @*/
6823 PetscErrorCode PETSCMAT_DLLEXPORT MatIsStructurallySymmetric(Mat A,PetscTruth *flg)
6824 {
6825   PetscErrorCode ierr;
6826 
6827   PetscFunctionBegin;
6828   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
6829   PetscValidPointer(flg,2);
6830   if (!A->structurally_symmetric_set) {
6831     if (!A->ops->isstructurallysymmetric) SETERRQ(PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric");
6832     ierr = (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);CHKERRQ(ierr);
6833     A->structurally_symmetric_set = PETSC_TRUE;
6834   }
6835   *flg = A->structurally_symmetric;
6836   PetscFunctionReturn(0);
6837 }
6838 
6839 #undef __FUNCT__
6840 #define __FUNCT__ "MatStashGetInfo"
6841 extern PetscErrorCode MatStashGetInfo_Private(MatStash*,PetscInt*,PetscInt*);
6842 /*@
6843    MatStashGetInfo - Gets how many values are currently in the vector stash, i.e. need
6844        to be communicated to other processors during the MatAssemblyBegin/End() process
6845 
6846     Not collective
6847 
6848    Input Parameter:
6849 .   vec - the vector
6850 
6851    Output Parameters:
6852 +   nstash   - the size of the stash
6853 .   reallocs - the number of additional mallocs incurred.
6854 .   bnstash   - the size of the block stash
6855 -   breallocs - the number of additional mallocs incurred.in the block stash
6856 
6857    Level: advanced
6858 
6859 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize()
6860 
6861 @*/
6862 PetscErrorCode PETSCMAT_DLLEXPORT MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
6863 {
6864   PetscErrorCode ierr;
6865   PetscFunctionBegin;
6866   ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr);
6867   ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr);
6868   PetscFunctionReturn(0);
6869 }
6870 
6871 #undef __FUNCT__
6872 #define __FUNCT__ "MatGetVecs"
6873 /*@C
6874    MatGetVecs - Get vector(s) compatible with the matrix, i.e. with the same
6875      parallel layout
6876 
6877    Collective on Mat
6878 
6879    Input Parameter:
6880 .  mat - the matrix
6881 
6882    Output Parameter:
6883 +   right - (optional) vector that the matrix can be multiplied against
6884 -   left - (optional) vector that the matrix vector product can be stored in
6885 
6886   Level: advanced
6887 
6888 .seealso: MatCreate()
6889 @*/
6890 PetscErrorCode PETSCMAT_DLLEXPORT MatGetVecs(Mat mat,Vec *right,Vec *left)
6891 {
6892   PetscErrorCode ierr;
6893 
6894   PetscFunctionBegin;
6895   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
6896   PetscValidType(mat,1);
6897   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6898   if (mat->ops->getvecs) {
6899     ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr);
6900   } else {
6901     PetscMPIInt size;
6902     ierr = MPI_Comm_size(((PetscObject)mat)->comm, &size);CHKERRQ(ierr);
6903     if (right) {
6904       ierr = VecCreate(((PetscObject)mat)->comm,right);CHKERRQ(ierr);
6905       ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
6906       ierr = VecSetBlockSize(*right,mat->rmap->bs);CHKERRQ(ierr);
6907       if (size > 1) {
6908         /* New vectors uses Mat cmap and does not create a new one */
6909         ierr = PetscMapDestroy((*right)->map);CHKERRQ(ierr);
6910         (*right)->map = mat->cmap;
6911         mat->cmap->refcnt++;
6912 
6913         ierr = VecSetType(*right,VECMPI);CHKERRQ(ierr);
6914       } else {ierr = VecSetType(*right,VECSEQ);CHKERRQ(ierr);}
6915     }
6916     if (left) {
6917       ierr = VecCreate(((PetscObject)mat)->comm,left);CHKERRQ(ierr);
6918       ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
6919       ierr = VecSetBlockSize(*left,mat->rmap->bs);CHKERRQ(ierr);
6920       if (size > 1) {
6921         /* New vectors uses Mat rmap and does not create a new one */
6922         ierr = PetscMapDestroy((*left)->map);CHKERRQ(ierr);
6923         (*left)->map = mat->rmap;
6924         mat->rmap->refcnt++;
6925 
6926         ierr = VecSetType(*left,VECMPI);CHKERRQ(ierr);
6927       } else {ierr = VecSetType(*left,VECSEQ);CHKERRQ(ierr);}
6928     }
6929   }
6930   if (mat->mapping) {
6931     if (right) {ierr = VecSetLocalToGlobalMapping(*right,mat->mapping);CHKERRQ(ierr);}
6932     if (left) {ierr = VecSetLocalToGlobalMapping(*left,mat->mapping);CHKERRQ(ierr);}
6933   }
6934   if (mat->bmapping) {
6935     if (right) {ierr = VecSetLocalToGlobalMappingBlock(*right,mat->bmapping);CHKERRQ(ierr);}
6936     if (left) {ierr = VecSetLocalToGlobalMappingBlock(*left,mat->bmapping);CHKERRQ(ierr);}
6937   }
6938   PetscFunctionReturn(0);
6939 }
6940 
6941 #undef __FUNCT__
6942 #define __FUNCT__ "MatFactorInfoInitialize"
6943 /*@
6944    MatFactorInfoInitialize - Initializes a MatFactorInfo data structure
6945      with default values.
6946 
6947    Not Collective
6948 
6949    Input Parameters:
6950 .    info - the MatFactorInfo data structure
6951 
6952 
6953    Notes: The solvers are generally used through the KSP and PC objects, for example
6954           PCLU, PCILU, PCCHOLESKY, PCICC
6955 
6956    Level: developer
6957 
6958 .seealso: MatFactorInfo
6959 @*/
6960 
6961 PetscErrorCode PETSCMAT_DLLEXPORT MatFactorInfoInitialize(MatFactorInfo *info)
6962 {
6963   PetscErrorCode ierr;
6964 
6965   PetscFunctionBegin;
6966   ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr);
6967   PetscFunctionReturn(0);
6968 }
6969 
6970 #undef __FUNCT__
6971 #define __FUNCT__ "MatPtAP"
6972 /*@
6973    MatPtAP - Creates the matrix projection C = P^T * A * P
6974 
6975    Collective on Mat
6976 
6977    Input Parameters:
6978 +  A - the matrix
6979 .  P - the projection matrix
6980 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
6981 -  fill - expected fill as ratio of nnz(C)/nnz(A)
6982 
6983    Output Parameters:
6984 .  C - the product matrix
6985 
6986    Notes:
6987    C will be created and must be destroyed by the user with MatDestroy().
6988 
6989    This routine is currently only implemented for pairs of AIJ matrices and classes
6990    which inherit from AIJ.
6991 
6992    Level: intermediate
6993 
6994 .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult()
6995 @*/
6996 PetscErrorCode PETSCMAT_DLLEXPORT MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
6997 {
6998   PetscErrorCode ierr;
6999 
7000   PetscFunctionBegin;
7001   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
7002   PetscValidType(A,1);
7003   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7004   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7005   PetscValidHeaderSpecific(P,MAT_COOKIE,2);
7006   PetscValidType(P,2);
7007   MatPreallocated(P);
7008   if (!P->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7009   if (P->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7010   PetscValidPointer(C,3);
7011   if (P->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N);
7012   if (fill < 1.0) SETERRQ1(PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
7013   ierr = MatPreallocated(A);CHKERRQ(ierr);
7014 
7015   ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
7016   ierr = (*A->ops->ptap)(A,P,scall,fill,C);CHKERRQ(ierr);
7017   ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
7018 
7019   PetscFunctionReturn(0);
7020 }
7021 
7022 #undef __FUNCT__
7023 #define __FUNCT__ "MatPtAPNumeric"
7024 /*@
7025    MatPtAPNumeric - Computes the matrix projection C = P^T * A * P
7026 
7027    Collective on Mat
7028 
7029    Input Parameters:
7030 +  A - the matrix
7031 -  P - the projection matrix
7032 
7033    Output Parameters:
7034 .  C - the product matrix
7035 
7036    Notes:
7037    C must have been created by calling MatPtAPSymbolic and must be destroyed by
7038    the user using MatDeatroy().
7039 
7040    This routine is currently only implemented for pairs of AIJ matrices and classes
7041    which inherit from AIJ.  C will be of type MATAIJ.
7042 
7043    Level: intermediate
7044 
7045 .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric()
7046 @*/
7047 PetscErrorCode PETSCMAT_DLLEXPORT MatPtAPNumeric(Mat A,Mat P,Mat C)
7048 {
7049   PetscErrorCode ierr;
7050 
7051   PetscFunctionBegin;
7052   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
7053   PetscValidType(A,1);
7054   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7055   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7056   PetscValidHeaderSpecific(P,MAT_COOKIE,2);
7057   PetscValidType(P,2);
7058   MatPreallocated(P);
7059   if (!P->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7060   if (P->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7061   PetscValidHeaderSpecific(C,MAT_COOKIE,3);
7062   PetscValidType(C,3);
7063   MatPreallocated(C);
7064   if (C->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7065   if (P->cmap->N!=C->rmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->rmap->N);
7066   if (P->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N);
7067   if (A->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
7068   if (P->cmap->N!=C->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->cmap->N);
7069   ierr = MatPreallocated(A);CHKERRQ(ierr);
7070 
7071   ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
7072   ierr = (*A->ops->ptapnumeric)(A,P,C);CHKERRQ(ierr);
7073   ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
7074   PetscFunctionReturn(0);
7075 }
7076 
7077 #undef __FUNCT__
7078 #define __FUNCT__ "MatPtAPSymbolic"
7079 /*@
7080    MatPtAPSymbolic - Creates the (i,j) structure of the matrix projection C = P^T * A * P
7081 
7082    Collective on Mat
7083 
7084    Input Parameters:
7085 +  A - the matrix
7086 -  P - the projection matrix
7087 
7088    Output Parameters:
7089 .  C - the (i,j) structure of the product matrix
7090 
7091    Notes:
7092    C will be created and must be destroyed by the user with MatDestroy().
7093 
7094    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
7095    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
7096    this (i,j) structure by calling MatPtAPNumeric().
7097 
7098    Level: intermediate
7099 
7100 .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic()
7101 @*/
7102 PetscErrorCode PETSCMAT_DLLEXPORT MatPtAPSymbolic(Mat A,Mat P,PetscReal fill,Mat *C)
7103 {
7104   PetscErrorCode ierr;
7105 
7106   PetscFunctionBegin;
7107   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
7108   PetscValidType(A,1);
7109   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7110   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7111   if (fill <1.0) SETERRQ1(PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
7112   PetscValidHeaderSpecific(P,MAT_COOKIE,2);
7113   PetscValidType(P,2);
7114   MatPreallocated(P);
7115   if (!P->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7116   if (P->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7117   PetscValidPointer(C,3);
7118 
7119   if (P->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N);
7120   if (A->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
7121   ierr = MatPreallocated(A);CHKERRQ(ierr);
7122   ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
7123   ierr = (*A->ops->ptapsymbolic)(A,P,fill,C);CHKERRQ(ierr);
7124   ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
7125 
7126   ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr);
7127 
7128   PetscFunctionReturn(0);
7129 }
7130 
7131 #undef __FUNCT__
7132 #define __FUNCT__ "MatMatMult"
7133 /*@
7134    MatMatMult - Performs Matrix-Matrix Multiplication C=A*B.
7135 
7136    Collective on Mat
7137 
7138    Input Parameters:
7139 +  A - the left matrix
7140 .  B - the right matrix
7141 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
7142 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate
7143           if the result is a dense matrix this is irrelevent
7144 
7145    Output Parameters:
7146 .  C - the product matrix
7147 
7148    Notes:
7149    Unless scall is MAT_REUSE_MATRIX C will be created.
7150 
7151    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
7152 
7153    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
7154    actually needed.
7155 
7156    If you have many matrices with the same non-zero structure to multiply, you
7157    should either
7158 $   1) use MAT_REUSE_MATRIX in all calls but the first or
7159 $   2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed
7160 
7161    Level: intermediate
7162 
7163 .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatPtAP()
7164 @*/
7165 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
7166 {
7167   PetscErrorCode ierr;
7168   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
7169   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
7170   PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat *)=PETSC_NULL;
7171 
7172   PetscFunctionBegin;
7173   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
7174   PetscValidType(A,1);
7175   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7176   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7177   PetscValidHeaderSpecific(B,MAT_COOKIE,2);
7178   PetscValidType(B,2);
7179   MatPreallocated(B);
7180   if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7181   if (B->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7182   PetscValidPointer(C,3);
7183   if (B->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
7184   if (scall == MAT_REUSE_MATRIX){
7185     PetscValidPointer(*C,5);
7186     PetscValidHeaderSpecific(*C,MAT_COOKIE,5);
7187   }
7188   if (fill == PETSC_DEFAULT) fill = 2.0;
7189   if (fill < 1.0) SETERRQ1(PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
7190   ierr = MatPreallocated(A);CHKERRQ(ierr);
7191 
7192   fA = A->ops->matmult;
7193   fB = B->ops->matmult;
7194   if (fB == fA) {
7195     if (!fB) SETERRQ1(PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name);
7196     mult = fB;
7197   } else {
7198     /* dispatch based on the type of A and B */
7199     char  multname[256];
7200     ierr = PetscStrcpy(multname,"MatMatMult_");CHKERRQ(ierr);
7201     ierr = PetscStrcat(multname,((PetscObject)A)->type_name);CHKERRQ(ierr);
7202     ierr = PetscStrcat(multname,"_");CHKERRQ(ierr);
7203     ierr = PetscStrcat(multname,((PetscObject)B)->type_name);CHKERRQ(ierr);
7204     ierr = PetscStrcat(multname,"_C");CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */
7205     ierr = PetscObjectQueryFunction((PetscObject)B,multname,(void (**)(void))&mult);CHKERRQ(ierr);
7206     if (!mult) SETERRQ2(PETSC_ERR_ARG_INCOMP,"MatMatMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
7207   }
7208   ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
7209   ierr = (*mult)(A,B,scall,fill,C);CHKERRQ(ierr);
7210   ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
7211   PetscFunctionReturn(0);
7212 }
7213 
7214 #undef __FUNCT__
7215 #define __FUNCT__ "MatMatMultSymbolic"
7216 /*@
7217    MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure
7218    of the matrix-matrix product C=A*B.  Call this routine before calling MatMatMultNumeric().
7219 
7220    Collective on Mat
7221 
7222    Input Parameters:
7223 +  A - the left matrix
7224 .  B - the right matrix
7225 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate,
7226       if C is a dense matrix this is irrelevent
7227 
7228    Output Parameters:
7229 .  C - the product matrix
7230 
7231    Notes:
7232    Unless scall is MAT_REUSE_MATRIX C will be created.
7233 
7234    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
7235    actually needed.
7236 
7237    This routine is currently implemented for
7238     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ
7239     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
7240     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
7241 
7242    Level: intermediate
7243 
7244 .seealso: MatMatMult(), MatMatMultNumeric()
7245 @*/
7246 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C)
7247 {
7248   PetscErrorCode ierr;
7249   PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat *);
7250   PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat *);
7251   PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat *)=PETSC_NULL;
7252 
7253   PetscFunctionBegin;
7254   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
7255   PetscValidType(A,1);
7256   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7257   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7258 
7259   PetscValidHeaderSpecific(B,MAT_COOKIE,2);
7260   PetscValidType(B,2);
7261   MatPreallocated(B);
7262   if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7263   if (B->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7264   PetscValidPointer(C,3);
7265 
7266   if (B->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
7267   if (fill == PETSC_DEFAULT) fill = 2.0;
7268   if (fill < 1.0) SETERRQ1(PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
7269   ierr = MatPreallocated(A);CHKERRQ(ierr);
7270 
7271   Asymbolic = A->ops->matmultsymbolic;
7272   Bsymbolic = B->ops->matmultsymbolic;
7273   if (Asymbolic == Bsymbolic){
7274     if (!Bsymbolic) SETERRQ1(PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name);
7275     symbolic = Bsymbolic;
7276   } else { /* dispatch based on the type of A and B */
7277     char  symbolicname[256];
7278     ierr = PetscStrcpy(symbolicname,"MatMatMultSymbolic_");CHKERRQ(ierr);
7279     ierr = PetscStrcat(symbolicname,((PetscObject)A)->type_name);CHKERRQ(ierr);
7280     ierr = PetscStrcat(symbolicname,"_");CHKERRQ(ierr);
7281     ierr = PetscStrcat(symbolicname,((PetscObject)B)->type_name);CHKERRQ(ierr);
7282     ierr = PetscStrcat(symbolicname,"_C");CHKERRQ(ierr);
7283     ierr = PetscObjectQueryFunction((PetscObject)B,symbolicname,(void (**)(void))&symbolic);CHKERRQ(ierr);
7284     if (!symbolic) SETERRQ2(PETSC_ERR_ARG_INCOMP,"MatMatMultSymbolic requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
7285   }
7286   ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
7287   ierr = (*symbolic)(A,B,fill,C);CHKERRQ(ierr);
7288   ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
7289   PetscFunctionReturn(0);
7290 }
7291 
7292 #undef __FUNCT__
7293 #define __FUNCT__ "MatMatMultNumeric"
7294 /*@
7295    MatMatMultNumeric - Performs the numeric matrix-matrix product.
7296    Call this routine after first calling MatMatMultSymbolic().
7297 
7298    Collective on Mat
7299 
7300    Input Parameters:
7301 +  A - the left matrix
7302 -  B - the right matrix
7303 
7304    Output Parameters:
7305 .  C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult().
7306 
7307    Notes:
7308    C must have been created with MatMatMultSymbolic().
7309 
7310    This routine is currently implemented for
7311     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ.
7312     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
7313     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
7314 
7315    Level: intermediate
7316 
7317 .seealso: MatMatMult(), MatMatMultSymbolic()
7318 @*/
7319 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMultNumeric(Mat A,Mat B,Mat C)
7320 {
7321   PetscErrorCode ierr;
7322   PetscErrorCode (*Anumeric)(Mat,Mat,Mat);
7323   PetscErrorCode (*Bnumeric)(Mat,Mat,Mat);
7324   PetscErrorCode (*numeric)(Mat,Mat,Mat)=PETSC_NULL;
7325 
7326   PetscFunctionBegin;
7327   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
7328   PetscValidType(A,1);
7329   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7330   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7331 
7332   PetscValidHeaderSpecific(B,MAT_COOKIE,2);
7333   PetscValidType(B,2);
7334   MatPreallocated(B);
7335   if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7336   if (B->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7337 
7338   PetscValidHeaderSpecific(C,MAT_COOKIE,3);
7339   PetscValidType(C,3);
7340   MatPreallocated(C);
7341   if (!C->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7342   if (C->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7343 
7344   if (B->cmap->N!=C->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->cmap->N,C->cmap->N);
7345   if (B->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
7346   if (A->rmap->N!=C->rmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",A->rmap->N,C->rmap->N);
7347   ierr = MatPreallocated(A);CHKERRQ(ierr);
7348 
7349   Anumeric = A->ops->matmultnumeric;
7350   Bnumeric = B->ops->matmultnumeric;
7351   if (Anumeric == Bnumeric){
7352     if (!Bnumeric) SETERRQ1(PETSC_ERR_SUP,"MatMatMultNumeric not supported for B of type %s",((PetscObject)B)->type_name);
7353     numeric = Bnumeric;
7354   } else {
7355     char  numericname[256];
7356     ierr = PetscStrcpy(numericname,"MatMatMultNumeric_");CHKERRQ(ierr);
7357     ierr = PetscStrcat(numericname,((PetscObject)A)->type_name);CHKERRQ(ierr);
7358     ierr = PetscStrcat(numericname,"_");CHKERRQ(ierr);
7359     ierr = PetscStrcat(numericname,((PetscObject)B)->type_name);CHKERRQ(ierr);
7360     ierr = PetscStrcat(numericname,"_C");CHKERRQ(ierr);
7361     ierr = PetscObjectQueryFunction((PetscObject)B,numericname,(void (**)(void))&numeric);CHKERRQ(ierr);
7362     if (!numeric)
7363       SETERRQ2(PETSC_ERR_ARG_INCOMP,"MatMatMultNumeric requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
7364   }
7365   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
7366   ierr = (*numeric)(A,B,C);CHKERRQ(ierr);
7367   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
7368   PetscFunctionReturn(0);
7369 }
7370 
7371 #undef __FUNCT__
7372 #define __FUNCT__ "MatMatMultTranspose"
7373 /*@
7374    MatMatMultTranspose - Performs Matrix-Matrix Multiplication C=A^T*B.
7375 
7376    Collective on Mat
7377 
7378    Input Parameters:
7379 +  A - the left matrix
7380 .  B - the right matrix
7381 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
7382 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known
7383 
7384    Output Parameters:
7385 .  C - the product matrix
7386 
7387    Notes:
7388    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().
7389 
7390    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
7391 
7392   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
7393    actually needed.
7394 
7395    This routine is currently only implemented for pairs of SeqAIJ matrices and pairs of SeqDense matrices and classes
7396    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.
7397 
7398    Level: intermediate
7399 
7400 .seealso: MatMatMultTransposeSymbolic(), MatMatMultTransposeNumeric(), MatPtAP()
7401 @*/
7402 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMultTranspose(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
7403 {
7404   PetscErrorCode ierr;
7405   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
7406   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
7407 
7408   PetscFunctionBegin;
7409   PetscValidHeaderSpecific(A,MAT_COOKIE,1);
7410   PetscValidType(A,1);
7411   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7412   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7413   PetscValidHeaderSpecific(B,MAT_COOKIE,2);
7414   PetscValidType(B,2);
7415   MatPreallocated(B);
7416   if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7417   if (B->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7418   PetscValidPointer(C,3);
7419   if (B->rmap->N!=A->rmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->rmap->N);
7420   if (fill < 1.0) SETERRQ1(PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
7421   ierr = MatPreallocated(A);CHKERRQ(ierr);
7422 
7423   fA = A->ops->matmulttranspose;
7424   if (!fA) SETERRQ1(PETSC_ERR_SUP,"MatMatMultTranspose not supported for A of type %s",((PetscObject)A)->type_name);
7425   fB = B->ops->matmulttranspose;
7426   if (!fB) SETERRQ1(PETSC_ERR_SUP,"MatMatMultTranspose not supported for B of type %s",((PetscObject)B)->type_name);
7427   if (fB!=fA) SETERRQ2(PETSC_ERR_ARG_INCOMP,"MatMatMultTranspose requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
7428 
7429   ierr = PetscLogEventBegin(MAT_MatMultTranspose,A,B,0,0);CHKERRQ(ierr);
7430   ierr = (*A->ops->matmulttranspose)(A,B,scall,fill,C);CHKERRQ(ierr);
7431   ierr = PetscLogEventEnd(MAT_MatMultTranspose,A,B,0,0);CHKERRQ(ierr);
7432 
7433   PetscFunctionReturn(0);
7434 }
7435 
7436 #undef __FUNCT__
7437 #define __FUNCT__ "MatGetRedundantMatrix"
7438 /*@C
7439    MatGetRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators.
7440 
7441    Collective on Mat
7442 
7443    Input Parameters:
7444 +  mat - the matrix
7445 .  nsubcomm - the number of subcommunicators (= number of redundant pareallel or sequential matrices)
7446 .  subcomm - MPI communicator split from the communicator where mat resides in
7447 .  mlocal_red - number of local rows of the redundant matrix
7448 -  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
7449 
7450    Output Parameter:
7451 .  matredundant - redundant matrix
7452 
7453    Notes:
7454    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
7455    original matrix has not changed from that last call to MatGetRedundantMatrix().
7456 
7457    This routine creates the duplicated matrices in subcommunicators; you should NOT create them before
7458    calling it.
7459 
7460    Only MPIAIJ matrix is supported.
7461 
7462    Level: advanced
7463 
7464    Concepts: subcommunicator
7465    Concepts: duplicate matrix
7466 
7467 .seealso: MatDestroy()
7468 @*/
7469 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_red,MatReuse reuse,Mat *matredundant)
7470 {
7471   PetscErrorCode ierr;
7472 
7473   PetscFunctionBegin;
7474   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
7475   if (nsubcomm && reuse == MAT_REUSE_MATRIX) {
7476     PetscValidPointer(*matredundant,6);
7477     PetscValidHeaderSpecific(*matredundant,MAT_COOKIE,6);
7478   }
7479   if (!mat->ops->getredundantmatrix) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7480   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7481   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7482   ierr = MatPreallocated(mat);CHKERRQ(ierr);
7483 
7484   ierr = PetscLogEventBegin(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
7485   ierr = (*mat->ops->getredundantmatrix)(mat,nsubcomm,subcomm,mlocal_red,reuse,matredundant);CHKERRQ(ierr);
7486   ierr = PetscLogEventEnd(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
7487   PetscFunctionReturn(0);
7488 }
7489