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