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