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