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