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