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