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