xref: /petsc/src/mat/interface/matrix.c (revision ac2a4f0d24b3b6a4ee93edbcad41f4bb9e923944)
1 /*$Id: matrix.c,v 1.349 1999/10/13 20:37:14 bsmith Exp bsmith $*/
2 
3 /*
4    This is where the abstract matrix operations are defined
5 */
6 
7 #include "src/mat/matimpl.h"        /*I "mat.h" I*/
8 #include "src/vec/vecimpl.h"
9 
10 #undef __FUNC__
11 #define __FUNC__ "MatGetRow"
12 /*@C
13    MatGetRow - Gets a row of a matrix.  You MUST call MatRestoreRow()
14    for each row that you get to ensure that your application does
15    not bleed memory.
16 
17    Not Collective
18 
19    Input Parameters:
20 +  mat - the matrix
21 -  row - the row to get
22 
23    Output Parameters:
24 +  ncols -  the number of nonzeros in the row
25 .  cols - if nonzero, the column numbers
26 -  vals - if nonzero, the values
27 
28    Notes:
29    This routine is provided for people who need to have direct access
30    to the structure of a matrix.  We hope that we provide enough
31    high-level matrix routines that few users will need it.
32 
33    MatGetRow() always returns 0-based column indices, regardless of
34    whether the internal representation is 0-based (default) or 1-based.
35 
36    For better efficiency, set cols and/or vals to PETSC_NULL if you do
37    not wish to extract these quantities.
38 
39    The user can only examine the values extracted with MatGetRow();
40    the values cannot be altered.  To change the matrix entries, one
41    must use MatSetValues().
42 
43    You can only have one call to MatGetRow() outstanding for a particular
44    matrix at a time.
45 
46    Fortran Notes:
47    The calling sequence from Fortran is
48 .vb
49    MatGetRow(matrix,row,ncols,cols,values,ierr)
50          Mat     matrix (input)
51          integer row    (input)
52          integer ncols  (output)
53          integer cols(maxcols) (output)
54          double precision (or double complex) values(maxcols) output
55 .ve
56    where maxcols >= maximum nonzeros in any row of the matrix.
57 
58    Caution:
59    Do not try to change the contents of the output arrays (cols and vals).
60    In some cases, this may corrupt the matrix.
61 
62    Level: advanced
63 
64 .keywords: matrix, row, get, extract
65 
66 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatGetSubmatrices(), MatGetDiagonal()
67 @*/
68 int MatGetRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals)
69 {
70   int   ierr;
71 
72   PetscFunctionBegin;
73   PetscValidHeaderSpecific(mat,MAT_COOKIE);
74   PetscValidIntPointer(ncols);
75   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
76   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
77   if (!mat->ops->getrow) SETERRQ(PETSC_ERR_SUP,0,"");
78   PLogEventBegin(MAT_GetRow,mat,0,0,0);
79   ierr = (*mat->ops->getrow)(mat,row,ncols,cols,vals);CHKERRQ(ierr);
80   PLogEventEnd(MAT_GetRow,mat,0,0,0);
81   PetscFunctionReturn(0);
82 }
83 
84 #undef __FUNC__
85 #define __FUNC__ "MatRestoreRow"
86 /*@C
87    MatRestoreRow - Frees any temporary space allocated by MatGetRow().
88 
89    Not Collective
90 
91    Input Parameters:
92 +  mat - the matrix
93 .  row - the row to get
94 .  ncols, cols - the number of nonzeros and their columns
95 -  vals - if nonzero the column values
96 
97    Notes:
98    This routine should be called after you have finished examining the entries.
99 
100    Fortran Notes:
101    The calling sequence from Fortran is
102 .vb
103    MatRestoreRow(matrix,row,ncols,cols,values,ierr)
104       Mat     matrix (input)
105       integer row    (input)
106       integer ncols  (output)
107       integer cols(maxcols) (output)
108       double precision (or double complex) values(maxcols) output
109 .ve
110    Where maxcols >= maximum nonzeros in any row of the matrix.
111 
112    In Fortran MatRestoreRow() MUST be called after MatGetRow()
113    before another call to MatGetRow() can be made.
114 
115    Level: advanced
116 
117 .keywords: matrix, row, restore
118 
119 .seealso:  MatGetRow()
120 @*/
121 int MatRestoreRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals)
122 {
123   int ierr;
124 
125   PetscFunctionBegin;
126   PetscValidHeaderSpecific(mat,MAT_COOKIE);
127   PetscValidIntPointer(ncols);
128   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
129   if (!mat->ops->restorerow) PetscFunctionReturn(0);
130   ierr = (*mat->ops->restorerow)(mat,row,ncols,cols,vals);CHKERRQ(ierr);
131   PetscFunctionReturn(0);
132 }
133 
134 #undef __FUNC__
135 #define __FUNC__ "MatView"
136 /*@C
137    MatView - Visualizes a matrix object.
138 
139    Collective on Mat unless Viewer is VIEWER_STDOUT_SELF
140 
141    Input Parameters:
142 +  mat - the matrix
143 -  ptr - visualization context
144 
145   Notes:
146   The available visualization contexts include
147 +    VIEWER_STDOUT_SELF - standard output (default)
148 .    VIEWER_STDOUT_WORLD - synchronized standard
149         output where only the first processor opens
150         the file.  All other processors send their
151         data to the first processor to print.
152 -     VIEWER_DRAW_WORLD - graphical display of nonzero structure
153 
154    The user can open alternative visualization contexts with
155 +    ViewerASCIIOpen() - Outputs matrix to a specified file
156 .    ViewerBinaryOpen() - Outputs matrix in binary to a
157          specified file; corresponding input uses MatLoad()
158 .    ViewerDrawOpen() - Outputs nonzero matrix structure to
159          an X window display
160 -    ViewerSocketOpen() - Outputs matrix to Socket viewer.
161          Currently only the sequential dense and AIJ
162          matrix types support the Socket viewer.
163 
164    The user can call ViewerSetFormat() to specify the output
165    format of ASCII printed objects (when using VIEWER_STDOUT_SELF,
166    VIEWER_STDOUT_WORLD and ViewerASCIIOpen).  Available formats include
167 +    VIEWER_FORMAT_ASCII_DEFAULT - default, prints matrix contents
168 .    VIEWER_FORMAT_ASCII_MATLAB - prints matrix contents in Matlab format
169 .    VIEWER_FORMAT_ASCII_DENSE - prints entire matrix including zeros
170 .    VIEWER_FORMAT_ASCII_COMMON - prints matrix contents, using a sparse
171          format common among all matrix types
172 .    VIEWER_FORMAT_ASCII_IMPL - prints matrix contents, using an implementation-specific
173          format (which is in many cases the same as the default)
174 .    VIEWER_FORMAT_ASCII_INFO - prints basic information about the matrix
175          size and structure (not the matrix entries)
176 -    VIEWER_FORMAT_ASCII_INFO_LONG - prints more detailed information about
177          the matrix structure
178 
179    Level: beginner
180 
181 .keywords: matrix, view, visualize, output, print, write, draw
182 
183 .seealso: ViewerSetFormat(), ViewerASCIIOpen(), ViewerDrawOpen(),
184           ViewerSocketOpen(), ViewerBinaryOpen(), MatLoad()
185 @*/
186 int MatView(Mat mat,Viewer viewer)
187 {
188   int        format, ierr, rows, cols;
189   PetscTruth isascii;
190   char       *cstr;
191 
192   PetscFunctionBegin;
193   PetscValidHeaderSpecific(mat,MAT_COOKIE);
194   if (!viewer) viewer = VIEWER_STDOUT_SELF;
195   PetscValidHeaderSpecific(viewer,VIEWER_COOKIE);
196   PetscCheckSameComm(mat,viewer);
197 
198   ierr = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr);
199   if (isascii) {
200     ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr);
201     if (format == VIEWER_FORMAT_ASCII_INFO || format == VIEWER_FORMAT_ASCII_INFO_LONG) {
202       ierr = ViewerASCIIPrintf(viewer,"Matrix Object:\n");CHKERRQ(ierr);
203       ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr);
204       ierr = MatGetType(mat,PETSC_NULL,&cstr);CHKERRQ(ierr);
205       ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr);
206       ierr = ViewerASCIIPrintf(viewer,"type=%s, rows=%d, cols=%d\n",cstr,rows,cols);CHKERRQ(ierr);
207       if (mat->ops->getinfo) {
208         MatInfo info;
209         ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr);
210         ierr = ViewerASCIIPrintf(viewer,"total: nonzeros=%d, allocated nonzeros=%d\n",
211                           (int)info.nz_used,(int)info.nz_allocated);CHKERRQ(ierr);
212       }
213     }
214   }
215   if (mat->ops->view) {
216     ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr);
217     ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr);
218     ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr);
219   } else if (!isascii) {
220     SETERRQ1(1,1,"Viewer type %s not supported",((PetscObject)viewer)->type_name);
221   }
222   if (isascii) {
223     ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr);
224     if (format == VIEWER_FORMAT_ASCII_INFO || format == VIEWER_FORMAT_ASCII_INFO_LONG) {
225       ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr);
226     }
227   }
228   PetscFunctionReturn(0);
229 }
230 
231 #undef __FUNC__
232 #define __FUNC__ "MatScaleSystem"
233 /*@C
234    MatScaleSystem - Scale a vector solution and right hand side to
235    match the scaling of a scaled matrix.
236 
237    Collective on Mat
238 
239    Input Parameter:
240 +  mat - the matrix
241 .  x - solution vector (or PETSC_NULL)
242 +  b - right hand side vector (or PETSC_NULL)
243 
244 
245    Notes:
246    For AIJ, BAIJ, and BDiag matrix formats, the matrices are not
247    internally scaled, so this does nothing. For MPIROWBS it
248    permutes and diagonally scales.
249 
250    The SLES methods automatically call this routine when required
251    (via PCPreSolve()) so it is rarely used directly.
252 
253    Level: Developer
254 
255 .keywords: matrix, scale
256 
257 .seealso: MatUseScaledForm(), MatUnScaleSystem()
258 @*/
259 int MatScaleSystem(Mat mat,Vec x,Vec b)
260 {
261   int ierr;
262 
263   PetscFunctionBegin;
264   PetscValidHeaderSpecific(mat,MAT_COOKIE);
265   if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);}
266   if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,b);}
267 
268   if (mat->ops->scalesystem) {
269     ierr = (*mat->ops->scalesystem)(mat,x,b);CHKERRQ(ierr);
270   }
271   PetscFunctionReturn(0);
272 }
273 
274 #undef __FUNC__
275 #define __FUNC__ "MatUnScaleSystem"
276 /*@C
277    MatUnScaleSystem - Unscales a vector solution and right hand side to
278    match the original scaling of a scaled matrix.
279 
280    Collective on Mat
281 
282    Input Parameter:
283 +  mat - the matrix
284 .  x - solution vector (or PETSC_NULL)
285 +  b - right hand side vector (or PETSC_NULL)
286 
287 
288    Notes:
289    For AIJ, BAIJ, and BDiag matrix formats, the matrices are not
290    internally scaled, so this does nothing. For MPIROWBS it
291    permutes and diagonally scales.
292 
293    The SLES methods automatically call this routine when required
294    (via PCPreSolve()) so it is rarely used directly.
295 
296    Level: Developer
297 
298 .keywords: matrix, scale
299 
300 .seealso: MatUseScaledForm(), MatScaleSystem()
301 @*/
302 int MatUnScaleSystem(Mat mat,Vec x,Vec b)
303 {
304   int ierr;
305 
306   PetscFunctionBegin;
307   PetscValidHeaderSpecific(mat,MAT_COOKIE);
308   if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);}
309   if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,x);}
310   if (mat->ops->unscalesystem) {
311     ierr = (*mat->ops->unscalesystem)(mat,x,b);CHKERRQ(ierr);
312   }
313   PetscFunctionReturn(0);
314 }
315 
316 #undef __FUNC__
317 #define __FUNC__ "MatUseScaledForm"
318 /*@C
319    MatUseScaledForm - For matrix storage formats that scale the
320    matrix (for example MPIRowBS matrices are diagonally scaled on
321    assembly) indicates matrix operations (MatMult() etc) are
322    applied using the scaled matrix.
323 
324    Collective on Mat
325 
326    Input Parameter:
327 +  mat - the matrix
328 -  scaled - PETSC_TRUE for applying the scaled, PETSC_FALSE for
329             applying the original matrix
330 
331    Notes:
332    For scaled matrix formats, applying the original, unscaled matrix
333    will be slightly more expensive
334 
335    Level: Developer
336 
337 .keywords: matrix, scale
338 
339 .seealso: MatScaleSystem(), MatUnScaleSystem()
340 @*/
341 int MatUseScaledForm(Mat mat,PetscTruth scaled)
342 {
343   int ierr;
344 
345   PetscFunctionBegin;
346   PetscValidHeaderSpecific(mat,MAT_COOKIE);
347   if (mat->ops->usescaledform) {
348     ierr = (*mat->ops->usescaledform)(mat,scaled);CHKERRQ(ierr);
349   }
350   PetscFunctionReturn(0);
351 }
352 
353 #undef __FUNC__
354 #define __FUNC__ "MatDestroy"
355 /*@C
356    MatDestroy - Frees space taken by a matrix.
357 
358    Collective on Mat
359 
360    Input Parameter:
361 .  mat - the matrix
362 
363    Level: beginner
364 
365 .keywords: matrix, destroy
366 @*/
367 int MatDestroy(Mat mat)
368 {
369   int ierr;
370 
371   PetscFunctionBegin;
372   PetscValidHeaderSpecific(mat,MAT_COOKIE);
373   if (--mat->refct > 0) PetscFunctionReturn(0);
374 
375   /* if memory was published with AMS then destroy it */
376   ierr = PetscObjectDepublish(mat);CHKERRQ(ierr);
377 
378   ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr);
379   PetscFunctionReturn(0);
380 }
381 
382 #undef __FUNC__
383 #define __FUNC__ "MatValid"
384 /*@
385    MatValid - Checks whether a matrix object is valid.
386 
387    Collective on Mat
388 
389    Input Parameter:
390 .  m - the matrix to check
391 
392    Output Parameter:
393    flg - flag indicating matrix status, either
394    PETSC_TRUE if matrix is valid, or PETSC_FALSE otherwise.
395 
396    Level: developer
397 
398 .keywords: matrix, valid
399 @*/
400 int MatValid(Mat m,PetscTruth *flg)
401 {
402   PetscFunctionBegin;
403   PetscValidIntPointer(flg);
404   if (!m)                           *flg = PETSC_FALSE;
405   else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE;
406   else                              *flg = PETSC_TRUE;
407   PetscFunctionReturn(0);
408 }
409 
410 #undef __FUNC__
411 #define __FUNC__ "MatSetValues"
412 /*@
413    MatSetValues - Inserts or adds a block of values into a matrix.
414    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
415    MUST be called after all calls to MatSetValues() have been completed.
416 
417    Not Collective
418 
419    Input Parameters:
420 +  mat - the matrix
421 .  v - a logically two-dimensional array of values
422 .  m, idxm - the number of rows and their global indices
423 .  n, idxn - the number of columns and their global indices
424 -  addv - either ADD_VALUES or INSERT_VALUES, where
425    ADD_VALUES adds values to any existing entries, and
426    INSERT_VALUES replaces existing entries with new values
427 
428    Notes:
429    By default the values, v, are row-oriented and unsorted.
430    See MatSetOption() for other options.
431 
432    Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES
433    options cannot be mixed without intervening calls to the assembly
434    routines.
435 
436    MatSetValues() uses 0-based row and column numbers in Fortran
437    as well as in C.
438 
439    Negative indices may be passed in idxm and idxn, these rows and columns are
440    simply ignored. This allows easily inserting element stiffness matrices
441    with homogeneous Dirchlet boundary conditions that you don't want represented
442    in the matrix.
443 
444    Efficiency Alert:
445    The routine MatSetValuesBlocked() may offer much better efficiency
446    for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ).
447 
448    Level: beginner
449 
450 .keywords: matrix, insert, add, set, values
451 
452 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
453 @*/
454 int MatSetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv)
455 {
456   int ierr;
457 
458   PetscFunctionBegin;
459   if (!m || !n) PetscFunctionReturn(0); /* no values to insert */
460   PetscValidHeaderSpecific(mat,MAT_COOKIE);
461   PetscValidIntPointer(idxm);
462   PetscValidIntPointer(idxn);
463   PetscValidScalarPointer(v);
464   if (mat->insertmode == NOT_SET_VALUES) {
465     mat->insertmode = addv;
466   }
467 #if defined(PETSC_USE_BOPT_g)
468   else if (mat->insertmode != addv) {
469     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values");
470   }
471   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
472 #endif
473 
474   if (mat->assembled) {
475     mat->was_assembled = PETSC_TRUE;
476     mat->assembled     = PETSC_FALSE;
477   }
478   PLogEventBegin(MAT_SetValues,mat,0,0,0);
479   if (!mat->ops->setvalues) SETERRQ(PETSC_ERR_SUP,1,"Not supported for this matrix type");
480   ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr);
481   PLogEventEnd(MAT_SetValues,mat,0,0,0);
482   PetscFunctionReturn(0);
483 }
484 
485 #undef __FUNC__
486 #define __FUNC__ "MatSetValuesBlocked"
487 /*@
488    MatSetValuesBlocked - Inserts or adds a block of values into a matrix.
489 
490    Not Collective
491 
492    Input Parameters:
493 +  mat - the matrix
494 .  v - a logically two-dimensional array of values
495 .  m, idxm - the number of block rows and their global block indices
496 .  n, idxn - the number of block columns and their global block indices
497 -  addv - either ADD_VALUES or INSERT_VALUES, where
498    ADD_VALUES adds values to any existing entries, and
499    INSERT_VALUES replaces existing entries with new values
500 
501    Notes:
502    By default the values, v, are row-oriented and unsorted. So the layout of
503    v is the same as for MatSetValues(). See MatSetOption() for other options.
504 
505    Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES
506    options cannot be mixed without intervening calls to the assembly
507    routines.
508 
509    MatSetValuesBlocked() uses 0-based row and column numbers in Fortran
510    as well as in C.
511 
512    Negative indices may be passed in idxm and idxn, these rows and columns are
513    simply ignored. This allows easily inserting element stiffness matrices
514    with homogeneous Dirchlet boundary conditions that you don't want represented
515    in the matrix.
516 
517    Each time an entry is set within a sparse matrix via MatSetValues(),
518    internal searching must be done to determine where to place the the
519    data in the matrix storage space.  By instead inserting blocks of
520    entries via MatSetValuesBlocked(), the overhead of matrix assembly is
521    reduced.
522 
523    Restrictions:
524    MatSetValuesBlocked() is currently supported only for the block AIJ
525    matrix format (MATSEQBAIJ and MATMPIBAIJ, which are created via
526    MatCreateSeqBAIJ() and MatCreateMPIBAIJ()).
527 
528    Level: intermediate
529 
530 .keywords: matrix, insert, add, set, values
531 
532 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal()
533 @*/
534 int MatSetValuesBlocked(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv)
535 {
536   int ierr;
537 
538   PetscFunctionBegin;
539   if (!m || !n) PetscFunctionReturn(0); /* no values to insert */
540   PetscValidHeaderSpecific(mat,MAT_COOKIE);
541   PetscValidIntPointer(idxm);
542   PetscValidIntPointer(idxn);
543   PetscValidScalarPointer(v);
544   if (mat->insertmode == NOT_SET_VALUES) {
545     mat->insertmode = addv;
546   }
547 #if defined(PETSC_USE_BOPT_g)
548   else if (mat->insertmode != addv) {
549     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values");
550   }
551   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
552 #endif
553 
554   if (mat->assembled) {
555     mat->was_assembled = PETSC_TRUE;
556     mat->assembled     = PETSC_FALSE;
557   }
558   PLogEventBegin(MAT_SetValues,mat,0,0,0);
559   if (!mat->ops->setvaluesblocked) SETERRQ(PETSC_ERR_SUP,1,"Not supported for this matrix type");
560   ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr);
561   PLogEventEnd(MAT_SetValues,mat,0,0,0);
562   PetscFunctionReturn(0);
563 }
564 
565 /*MC
566    MatSetValue - Set a single entry into a matrix.
567 
568    Synopsis:
569    void MatSetValue(Mat m,int row,int col,Scalar value,InsertMode mode);
570 
571    Not collective
572 
573    Input Parameters:
574 +  m - the matrix
575 .  row - the row location of the entry
576 .  col - the column location of the entry
577 .  value - the value to insert
578 -  mode - either INSERT_VALUES or ADD_VALUES
579 
580    Notes:
581    For efficiency one should use MatSetValues() and set several or many
582    values simultaneously if possible.
583 
584    Note that VecSetValue() does NOT return an error code (since this
585    is checked internally).
586 
587    Level: beginner
588 
589 .seealso: MatSetValues()
590 M*/
591 
592 #undef __FUNC__
593 #define __FUNC__ "MatGetValues"
594 /*@
595    MatGetValues - Gets a block of values from a matrix.
596 
597    Not Collective; currently only returns a local block
598 
599    Input Parameters:
600 +  mat - the matrix
601 .  v - a logically two-dimensional array for storing the values
602 .  m, idxm - the number of rows and their global indices
603 -  n, idxn - the number of columns and their global indices
604 
605    Notes:
606    The user must allocate space (m*n Scalars) for the values, v.
607    The values, v, are then returned in a row-oriented format,
608    analogous to that used by default in MatSetValues().
609 
610    MatGetValues() uses 0-based row and column numbers in
611    Fortran as well as in C.
612 
613    MatGetValues() requires that the matrix has been assembled
614    with MatAssemblyBegin()/MatAssemblyEnd().  Thus, calls to
615    MatSetValues() and MatGetValues() CANNOT be made in succession
616    without intermediate matrix assembly.
617 
618    Level: advanced
619 
620 .keywords: matrix, get, values
621 
622 .seealso: MatGetRow(), MatGetSubmatrices(), MatSetValues()
623 @*/
624 int MatGetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v)
625 {
626   int ierr;
627 
628   PetscFunctionBegin;
629   PetscValidHeaderSpecific(mat,MAT_COOKIE);
630   PetscValidIntPointer(idxm);
631   PetscValidIntPointer(idxn);
632   PetscValidScalarPointer(v);
633   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
634   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
635   if (!mat->ops->getvalues) SETERRQ(PETSC_ERR_SUP,0,"");
636 
637   PLogEventBegin(MAT_GetValues,mat,0,0,0);
638   ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr);
639   PLogEventEnd(MAT_GetValues,mat,0,0,0);
640   PetscFunctionReturn(0);
641 }
642 
643 #undef __FUNC__
644 #define __FUNC__ "MatSetLocalToGlobalMapping"
645 /*@
646    MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by
647    the routine MatSetValuesLocal() to allow users to insert matrix entries
648    using a local (per-processor) numbering.
649 
650    Not Collective
651 
652    Input Parameters:
653 +  x - the matrix
654 -  mapping - mapping created with ISLocalToGlobalMappingCreate()
655              or ISLocalToGlobalMappingCreateIS()
656 
657    Level: intermediate
658 
659 .keywords: matrix, set, values, local, global, mapping
660 
661 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal()
662 @*/
663 int MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping mapping)
664 {
665   PetscFunctionBegin;
666   PetscValidHeaderSpecific(x,MAT_COOKIE);
667   PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE);
668   if (x->mapping) {
669     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Mapping already set for matrix");
670   }
671 
672   x->mapping = mapping;
673   PetscObjectReference((PetscObject)mapping);
674   PetscFunctionReturn(0);
675 }
676 
677 #undef __FUNC__
678 #define __FUNC__ "MatSetLocalToGlobalMappingBlock"
679 /*@
680    MatSetLocalToGlobalMappingBlock - Sets a local-to-global numbering for use
681    by the routine MatSetValuesBlockedLocal() to allow users to insert matrix
682    entries using a local (per-processor) numbering.
683 
684    Not Collective
685 
686    Input Parameters:
687 +  x - the matrix
688 -  mapping - mapping created with ISLocalToGlobalMappingCreate() or
689              ISLocalToGlobalMappingCreateIS()
690 
691    Level: intermediate
692 
693 .keywords: matrix, set, values, local ordering
694 
695 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal(),
696            MatSetValuesBlocked(), MatSetValuesLocal()
697 @*/
698 int MatSetLocalToGlobalMappingBlock(Mat x,ISLocalToGlobalMapping mapping)
699 {
700   PetscFunctionBegin;
701   PetscValidHeaderSpecific(x,MAT_COOKIE);
702   PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE);
703   if (x->bmapping) {
704     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Mapping already set for matrix");
705   }
706 
707   x->bmapping = mapping;
708   PetscObjectReference((PetscObject)mapping);
709   PetscFunctionReturn(0);
710 }
711 
712 #undef __FUNC__
713 #define __FUNC__ "MatSetValuesLocal"
714 /*@
715    MatSetValuesLocal - Inserts or adds values into certain locations of a matrix,
716    using a local ordering of the nodes.
717 
718    Not Collective
719 
720    Input Parameters:
721 +  x - the matrix
722 .  nrow, irow - number of rows and their local indices
723 .  ncol, icol - number of columns and their local indices
724 .  y -  a logically two-dimensional array of values
725 -  addv - either INSERT_VALUES or ADD_VALUES, where
726    ADD_VALUES adds values to any existing entries, and
727    INSERT_VALUES replaces existing entries with new values
728 
729    Notes:
730    Before calling MatSetValuesLocal(), the user must first set the
731    local-to-global mapping by calling MatSetLocalToGlobalMapping().
732 
733    Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES
734    options cannot be mixed without intervening calls to the assembly
735    routines.
736 
737    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
738    MUST be called after all calls to MatSetValuesLocal() have been completed.
739 
740    Level: intermediate
741 
742 .keywords: matrix, set, values, local ordering
743 
744 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping()
745 @*/
746 int MatSetValuesLocal(Mat mat,int nrow,int *irow,int ncol, int *icol,Scalar *y,InsertMode addv)
747 {
748   int ierr,irowm[2048],icolm[2048];
749 
750   PetscFunctionBegin;
751   PetscValidHeaderSpecific(mat,MAT_COOKIE);
752   PetscValidIntPointer(irow);
753   PetscValidIntPointer(icol);
754   PetscValidScalarPointer(y);
755 
756   if (mat->insertmode == NOT_SET_VALUES) {
757     mat->insertmode = addv;
758   }
759 #if defined(PETSC_USE_BOPT_g)
760   else if (mat->insertmode != addv) {
761     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values");
762   }
763   if (!mat->mapping) {
764     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Local to global never set with MatSetLocalToGlobalMapping()");
765   }
766   if (nrow > 2048 || ncol > 2048) {
767     SETERRQ2(PETSC_ERR_SUP,0,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol);
768   }
769   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
770 #endif
771 
772   if (mat->assembled) {
773     mat->was_assembled = PETSC_TRUE;
774     mat->assembled     = PETSC_FALSE;
775   }
776   PLogEventBegin(MAT_SetValues,mat,0,0,0);
777   ierr = ISLocalToGlobalMappingApply(mat->mapping,nrow,irow,irowm);CHKERRQ(ierr);
778   ierr = ISLocalToGlobalMappingApply(mat->mapping,ncol,icol,icolm);CHKERRQ(ierr);
779   ierr = (*mat->ops->setvalues)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr);
780   PLogEventEnd(MAT_SetValues,mat,0,0,0);
781   PetscFunctionReturn(0);
782 }
783 
784 #undef __FUNC__
785 #define __FUNC__ "MatSetValuesBlockedLocal"
786 /*@
787    MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix,
788    using a local ordering of the nodes a block at a time.
789 
790    Not Collective
791 
792    Input Parameters:
793 +  x - the matrix
794 .  nrow, irow - number of rows and their local indices
795 .  ncol, icol - number of columns and their local indices
796 .  y -  a logically two-dimensional array of values
797 -  addv - either INSERT_VALUES or ADD_VALUES, where
798    ADD_VALUES adds values to any existing entries, and
799    INSERT_VALUES replaces existing entries with new values
800 
801    Notes:
802    Before calling MatSetValuesBlockedLocal(), the user must first set the
803    local-to-global mapping by calling MatSetLocalToGlobalMappingBlock(),
804    where the mapping MUST be set for matrix blocks, not for matrix elements.
805 
806    Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES
807    options cannot be mixed without intervening calls to the assembly
808    routines.
809 
810    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
811    MUST be called after all calls to MatSetValuesBlockedLocal() have been completed.
812 
813    Level: intermediate
814 
815 .keywords: matrix, set, values, blocked, local
816 
817 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesLocal(), MatSetLocalToGlobalMappingBlock(), MatSetValuesBlocked()
818 @*/
819 int MatSetValuesBlockedLocal(Mat mat,int nrow,int *irow,int ncol,int *icol,Scalar *y,InsertMode addv)
820 {
821   int ierr,irowm[2048],icolm[2048];
822 
823   PetscFunctionBegin;
824   PetscValidHeaderSpecific(mat,MAT_COOKIE);
825   PetscValidIntPointer(irow);
826   PetscValidIntPointer(icol);
827   PetscValidScalarPointer(y);
828   if (mat->insertmode == NOT_SET_VALUES) {
829     mat->insertmode = addv;
830   }
831 #if defined(PETSC_USE_BOPT_g)
832   else if (mat->insertmode != addv) {
833     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values");
834   }
835   if (!mat->bmapping) {
836     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Local to global never set with MatSetLocalToGlobalMappingBlock()");
837   }
838   if (nrow > 2048 || ncol > 2048) {
839     SETERRQ2(PETSC_ERR_SUP,0,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol);
840   }
841   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
842 #endif
843 
844   if (mat->assembled) {
845     mat->was_assembled = PETSC_TRUE;
846     mat->assembled     = PETSC_FALSE;
847   }
848   PLogEventBegin(MAT_SetValues,mat,0,0,0);
849   ierr = ISLocalToGlobalMappingApply(mat->bmapping,nrow,irow,irowm);CHKERRQ(ierr);
850   ierr = ISLocalToGlobalMappingApply(mat->bmapping,ncol,icol,icolm);CHKERRQ(ierr);
851   ierr = (*mat->ops->setvaluesblocked)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr);
852   PLogEventEnd(MAT_SetValues,mat,0,0,0);
853   PetscFunctionReturn(0);
854 }
855 
856 /* --------------------------------------------------------*/
857 #undef __FUNC__
858 #define __FUNC__ "MatMult"
859 /*@
860    MatMult - Computes the matrix-vector product, y = Ax.
861 
862    Collective on Mat and Vec
863 
864    Input Parameters:
865 +  mat - the matrix
866 -  x   - the vector to be multilplied
867 
868    Output Parameters:
869 .  y - the result
870 
871    Notes:
872    The vectors x and y cannot be the same.  I.e., one cannot
873    call MatMult(A,y,y).
874 
875    Level: beginner
876 
877 .keywords: matrix, multiply, matrix-vector product
878 
879 .seealso: MatMultTrans(), MatMultAdd(), MatMultTransAdd()
880 @*/
881 int MatMult(Mat mat,Vec x,Vec y)
882 {
883   int ierr;
884 
885   PetscFunctionBegin;
886   PetscValidHeaderSpecific(mat,MAT_COOKIE);
887   PetscValidHeaderSpecific(x,VEC_COOKIE);
888   PetscValidHeaderSpecific(y,VEC_COOKIE);
889   PetscCheckSameComm(mat,x);
890   PetscCheckSameComm(mat,y);
891   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
892   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
893   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"x and y must be different vectors");
894   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
895   if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->M,y->N);
896   if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: local dim %d %d",mat->m,y->n);
897 
898   PLogEventBegin(MAT_Mult,mat,x,y,0);
899   ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr);
900   PLogEventEnd(MAT_Mult,mat,x,y,0);
901 
902   PetscFunctionReturn(0);
903 }
904 
905 #undef __FUNC__
906 #define __FUNC__ "MatMultTrans"
907 /*@
908    MatMultTrans - Computes matrix transpose times a vector.
909 
910    Collective on Mat and Vec
911 
912    Input Parameters:
913 +  mat - the matrix
914 -  x   - the vector to be multilplied
915 
916    Output Parameters:
917 .  y - the result
918 
919    Notes:
920    The vectors x and y cannot be the same.  I.e., one cannot
921    call MatMultTrans(A,y,y).
922 
923    Level: beginner
924 
925 .keywords: matrix, multiply, matrix-vector product, transpose
926 
927 .seealso: MatMult(), MatMultAdd(), MatMultTransAdd()
928 @*/
929 int MatMultTrans(Mat mat,Vec x,Vec y)
930 {
931   int ierr;
932 
933   PetscFunctionBegin;
934   PetscValidHeaderSpecific(mat,MAT_COOKIE);
935   PetscValidHeaderSpecific(x,VEC_COOKIE);
936   PetscValidHeaderSpecific(y,VEC_COOKIE);
937   PetscCheckSameComm(mat,x);
938   PetscCheckSameComm(mat,y);
939   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
940   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
941   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"x and y must be different vectors");
942   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->M,x->N);
943   if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->N,y->N);
944 
945   PLogEventBegin(MAT_MultTrans,mat,x,y,0);
946   ierr = (*mat->ops->multtrans)(mat,x,y);CHKERRQ(ierr);
947   PLogEventEnd(MAT_MultTrans,mat,x,y,0);
948   PetscFunctionReturn(0);
949 }
950 
951 #undef __FUNC__
952 #define __FUNC__ "MatMultAdd"
953 /*@
954     MatMultAdd -  Computes v3 = v2 + A * v1.
955 
956     Collective on Mat and Vec
957 
958     Input Parameters:
959 +   mat - the matrix
960 -   v1, v2 - the vectors
961 
962     Output Parameters:
963 .   v3 - the result
964 
965     Notes:
966     The vectors v1 and v3 cannot be the same.  I.e., one cannot
967     call MatMultAdd(A,v1,v2,v1).
968 
969     Level: beginner
970 
971 .keywords: matrix, multiply, matrix-vector product, add
972 
973 .seealso: MatMultTrans(), MatMult(), MatMultTransAdd()
974 @*/
975 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3)
976 {
977   int ierr;
978 
979   PetscFunctionBegin;
980   PetscValidHeaderSpecific(mat,MAT_COOKIE);
981   PetscValidHeaderSpecific(v1,VEC_COOKIE);
982   PetscValidHeaderSpecific(v2,VEC_COOKIE);
983   PetscValidHeaderSpecific(v3,VEC_COOKIE);
984   PetscCheckSameComm(mat,v1);
985   PetscCheckSameComm(mat,v2);
986   PetscCheckSameComm(mat,v3);
987   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
988   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
989   if (mat->N != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v1: global dim %d %d",mat->N,v1->N);
990   if (mat->M != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v2: global dim %d %d",mat->M,v2->N);
991   if (mat->M != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v3: global dim %d %d",mat->M,v3->N);
992   if (mat->m != v3->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v3: local dim %d %d",mat->m,v3->n);
993   if (mat->m != v2->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v2: local dim %d %d",mat->m,v2->n);
994   if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,0,"v1 and v3 must be different vectors");
995 
996   PLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);
997   ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr);
998   PLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);
999   PetscFunctionReturn(0);
1000 }
1001 
1002 #undef __FUNC__
1003 #define __FUNC__ "MatMultTransAdd"
1004 /*@
1005    MatMultTransAdd - Computes v3 = v2 + A' * v1.
1006 
1007    Collective on Mat and Vec
1008 
1009    Input Parameters:
1010 +  mat - the matrix
1011 -  v1, v2 - the vectors
1012 
1013    Output Parameters:
1014 .  v3 - the result
1015 
1016    Notes:
1017    The vectors v1 and v3 cannot be the same.  I.e., one cannot
1018    call MatMultTransAdd(A,v1,v2,v1).
1019 
1020    Level: beginner
1021 
1022 .keywords: matrix, multiply, matrix-vector product, transpose, add
1023 
1024 .seealso: MatMultTrans(), MatMultAdd(), MatMult()
1025 @*/
1026 int MatMultTransAdd(Mat mat,Vec v1,Vec v2,Vec v3)
1027 {
1028   int ierr;
1029 
1030   PetscFunctionBegin;
1031   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1032   PetscValidHeaderSpecific(v1,VEC_COOKIE);
1033   PetscValidHeaderSpecific(v2,VEC_COOKIE);
1034   PetscValidHeaderSpecific(v3,VEC_COOKIE);
1035   PetscCheckSameComm(mat,v1);
1036   PetscCheckSameComm(mat,v2);
1037   PetscCheckSameComm(mat,v3);
1038   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1039   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
1040   if (!mat->ops->multtransadd) SETERRQ(PETSC_ERR_SUP,0,"");
1041   if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,0,"v1 and v3 must be different vectors");
1042   if (mat->M != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v1: global dim %d %d",mat->M,v1->N);
1043   if (mat->N != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v2: global dim %d %d",mat->N,v2->N);
1044   if (mat->N != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v3: global dim %d %d",mat->N,v3->N);
1045 
1046   PLogEventBegin(MAT_MultTransAdd,mat,v1,v2,v3);
1047   ierr = (*mat->ops->multtransadd)(mat,v1,v2,v3);CHKERRQ(ierr);
1048   PLogEventEnd(MAT_MultTransAdd,mat,v1,v2,v3);
1049   PetscFunctionReturn(0);
1050 }
1051 /* ------------------------------------------------------------*/
1052 #undef __FUNC__
1053 #define __FUNC__ "MatGetInfo"
1054 /*@C
1055    MatGetInfo - Returns information about matrix storage (number of
1056    nonzeros, memory, etc.).
1057 
1058    Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used
1059    as the flag
1060 
1061    Input Parameters:
1062 .  mat - the matrix
1063 
1064    Output Parameters:
1065 +  flag - flag indicating the type of parameters to be returned
1066    (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors,
1067    MAT_GLOBAL_SUM - sum over all processors)
1068 -  info - matrix information context
1069 
1070    Notes:
1071    The MatInfo context contains a variety of matrix data, including
1072    number of nonzeros allocated and used, number of mallocs during
1073    matrix assembly, etc.  Additional information for factored matrices
1074    is provided (such as the fill ratio, number of mallocs during
1075    factorization, etc.).  Much of this info is printed to STDOUT
1076    when using the runtime options
1077 $       -log_info -mat_view_info
1078 
1079    Example for C/C++ Users:
1080    See the file ${PETSC_DIR}/include/mat.h for a complete list of
1081    data within the MatInfo context.  For example,
1082 .vb
1083       MatInfo info;
1084       Mat     A;
1085       double  mal, nz_a, nz_u;
1086 
1087       MatGetInfo(A,MAT_LOCAL,&info);
1088       mal  = info.mallocs;
1089       nz_a = info.nz_allocated;
1090 .ve
1091 
1092    Example for Fortran Users:
1093    Fortran users should declare info as a double precision
1094    array of dimension MAT_INFO_SIZE, and then extract the parameters
1095    of interest.  See the file ${PETSC_DIR}/include/finclude/mat.h
1096    a complete list of parameter names.
1097 .vb
1098       double  precision info(MAT_INFO_SIZE)
1099       double  precision mal, nz_a
1100       Mat     A
1101       integer ierr
1102 
1103       call MatGetInfo(A,MAT_LOCAL,info,ierr)
1104       mal = info(MAT_INFO_MALLOCS)
1105       nz_a = info(MAT_INFO_NZ_ALLOCATED)
1106 .ve
1107 
1108     Level: intermediate
1109 
1110 .keywords: matrix, get, info, storage, nonzeros, memory, fill
1111 @*/
1112 int MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info)
1113 {
1114   int ierr;
1115 
1116   PetscFunctionBegin;
1117   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1118   PetscValidPointer(info);
1119   if (!mat->ops->getinfo) SETERRQ(PETSC_ERR_SUP,0,"");
1120   ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr);
1121   PetscFunctionReturn(0);
1122 }
1123 
1124 /* ----------------------------------------------------------*/
1125 #undef __FUNC__
1126 #define __FUNC__ "MatILUDTFactor"
1127 /*@
1128    MatILUDTFactor - Performs a drop tolerance ILU factorization.
1129 
1130    Collective on Mat
1131 
1132    Input Parameters:
1133 +  mat - the matrix
1134 .  dt  - the drop tolerance
1135 .  maxnz - the maximum number of nonzeros per row allowed
1136 .  row - row permutation
1137 -  col - column permutation
1138 
1139    Output Parameters:
1140 .  fact - the factored matrix
1141 
1142    Notes:
1143    Most users should employ the simplified SLES interface for linear solvers
1144    instead of working directly with matrix algebra routines such as this.
1145    See, e.g., SLESCreate().
1146 
1147    Level: developer
1148 
1149 .keywords: matrix, factor, LU, in-place
1150 
1151 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
1152 @*/
1153 int MatILUDTFactor(Mat mat,double dt,int maxnz,IS row,IS col,Mat *fact)
1154 {
1155   int ierr;
1156 
1157   PetscFunctionBegin;
1158   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1159   PetscValidPointer(fact);
1160   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1161   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
1162   if (!mat->ops->iludtfactor) SETERRQ(PETSC_ERR_SUP,0,"");
1163 
1164   PLogEventBegin(MAT_ILUFactor,mat,row,col,0);
1165   ierr = (*mat->ops->iludtfactor)(mat,dt,maxnz,row,col,fact);CHKERRQ(ierr);
1166   PLogEventEnd(MAT_ILUFactor,mat,row,col,0);
1167 
1168   PetscFunctionReturn(0);
1169 }
1170 
1171 #undef __FUNC__
1172 #define __FUNC__ "MatLUFactor"
1173 /*@
1174    MatLUFactor - Performs in-place LU factorization of matrix.
1175 
1176    Collective on Mat
1177 
1178    Input Parameters:
1179 +  mat - the matrix
1180 .  row - row permutation
1181 .  col - column permutation
1182 -  f - expected fill as ratio of original fill.
1183 
1184    Notes:
1185    Most users should employ the simplified SLES interface for linear solvers
1186    instead of working directly with matrix algebra routines such as this.
1187    See, e.g., SLESCreate().
1188 
1189    Level: developer
1190 
1191 .keywords: matrix, factor, LU, in-place
1192 
1193 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(),
1194           MatGetOrdering()
1195 
1196 @*/
1197 int MatLUFactor(Mat mat,IS row,IS col,double f)
1198 {
1199   int ierr;
1200 
1201   PetscFunctionBegin;
1202   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1203   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square");
1204   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1205   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
1206   if (!mat->ops->lufactor) SETERRQ(PETSC_ERR_SUP,0,"");
1207 
1208   PLogEventBegin(MAT_LUFactor,mat,row,col,0);
1209   ierr = (*mat->ops->lufactor)(mat,row,col,f);CHKERRQ(ierr);
1210   PLogEventEnd(MAT_LUFactor,mat,row,col,0);
1211   PetscFunctionReturn(0);
1212 }
1213 
1214 #undef __FUNC__
1215 #define __FUNC__ "MatILUFactor"
1216 /*@
1217    MatILUFactor - Performs in-place ILU factorization of matrix.
1218 
1219    Collective on Mat
1220 
1221    Input Parameters:
1222 +  mat - the matrix
1223 .  row - row permutation
1224 .  col - column permutation
1225 -  info - structure containing
1226 $      levels - number of levels of fill.
1227 $      expected fill - as ratio of original fill.
1228 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
1229                 missing diagonal entries)
1230 
1231    Notes:
1232    Probably really in-place only when level of fill is zero, otherwise allocates
1233    new space to store factored matrix and deletes previous memory.
1234 
1235    Most users should employ the simplified SLES interface for linear solvers
1236    instead of working directly with matrix algebra routines such as this.
1237    See, e.g., SLESCreate().
1238 
1239    Level: developer
1240 
1241 .keywords: matrix, factor, ILU, in-place
1242 
1243 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
1244 @*/
1245 int MatILUFactor(Mat mat,IS row,IS col,MatILUInfo *info)
1246 {
1247   int ierr;
1248 
1249   PetscFunctionBegin;
1250   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1251   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square");
1252   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1253   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
1254   if (!mat->ops->ilufactor) SETERRQ(PETSC_ERR_SUP,0,"");
1255 
1256   PLogEventBegin(MAT_ILUFactor,mat,row,col,0);
1257   ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr);
1258   PLogEventEnd(MAT_ILUFactor,mat,row,col,0);
1259   PetscFunctionReturn(0);
1260 }
1261 
1262 #undef __FUNC__
1263 #define __FUNC__ "MatLUFactorSymbolic"
1264 /*@
1265    MatLUFactorSymbolic - Performs symbolic LU factorization of matrix.
1266    Call this routine before calling MatLUFactorNumeric().
1267 
1268    Collective on Mat
1269 
1270    Input Parameters:
1271 +  mat - the matrix
1272 .  row, col - row and column permutations
1273 -  f - expected fill as ratio of the original number of nonzeros,
1274        for example 3.0; choosing this parameter well can result in
1275        more efficient use of time and space. Run with the option -log_info
1276        to determine an optimal value to use
1277 
1278    Output Parameter:
1279 .  fact - new matrix that has been symbolically factored
1280 
1281    Notes:
1282    See the users manual for additional information about
1283    choosing the fill factor for better efficiency.
1284 
1285    Most users should employ the simplified SLES interface for linear solvers
1286    instead of working directly with matrix algebra routines such as this.
1287    See, e.g., SLESCreate().
1288 
1289    Level: developer
1290 
1291 .keywords: matrix, factor, LU, symbolic, fill
1292 
1293 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor()
1294 @*/
1295 int MatLUFactorSymbolic(Mat mat,IS row,IS col,double f,Mat *fact)
1296 {
1297   int ierr;
1298 
1299   PetscFunctionBegin;
1300   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1301   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square");
1302   PetscValidPointer(fact);
1303   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1304   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
1305   if (!mat->ops->lufactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,"");
1306 
1307   PLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);
1308   ierr = (*mat->ops->lufactorsymbolic)(mat,row,col,f,fact);CHKERRQ(ierr);
1309   PLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);
1310   PetscFunctionReturn(0);
1311 }
1312 
1313 #undef __FUNC__
1314 #define __FUNC__ "MatLUFactorNumeric"
1315 /*@
1316    MatLUFactorNumeric - Performs numeric LU factorization of a matrix.
1317    Call this routine after first calling MatLUFactorSymbolic().
1318 
1319    Collective on Mat
1320 
1321    Input Parameters:
1322 +  mat - the matrix
1323 -  row, col - row and  column permutations
1324 
1325    Output Parameters:
1326 .  fact - symbolically factored matrix that must have been generated
1327           by MatLUFactorSymbolic()
1328 
1329    Notes:
1330    See MatLUFactor() for in-place factorization.  See
1331    MatCholeskyFactorNumeric() for the symmetric, positive definite case.
1332 
1333    Most users should employ the simplified SLES interface for linear solvers
1334    instead of working directly with matrix algebra routines such as this.
1335    See, e.g., SLESCreate().
1336 
1337    Level: developer
1338 
1339 .keywords: matrix, factor, LU, numeric
1340 
1341 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor()
1342 @*/
1343 int MatLUFactorNumeric(Mat mat,Mat *fact)
1344 {
1345   int ierr,flg;
1346 
1347   PetscFunctionBegin;
1348   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1349   PetscValidPointer(fact);
1350   PetscValidHeaderSpecific(*fact,MAT_COOKIE);
1351   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1352   if (mat->M != (*fact)->M || mat->N != (*fact)->N) {
1353     SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat mat,Mat *fact: global dimensions are different %d should = %d %d should = %d",
1354             mat->M,(*fact)->M,mat->N,(*fact)->N);
1355   }
1356   if (!(*fact)->ops->lufactornumeric) SETERRQ(PETSC_ERR_SUP,0,"");
1357 
1358   PLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0);
1359   ierr = (*(*fact)->ops->lufactornumeric)(mat,fact);CHKERRQ(ierr);
1360   PLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0);
1361   ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg);CHKERRQ(ierr);
1362   if (flg) {
1363     ierr = OptionsHasName(PETSC_NULL,"-mat_view_contour",&flg);CHKERRQ(ierr);
1364     if (flg) {
1365       ViewerPushFormat(VIEWER_DRAW_(mat->comm),VIEWER_FORMAT_DRAW_CONTOUR,0);CHKERRQ(ierr);
1366     }
1367     ierr = MatView(*fact,VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1368     ierr = ViewerFlush(VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1369     if (flg) {
1370       ViewerPopFormat(VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1371     }
1372   }
1373   PetscFunctionReturn(0);
1374 }
1375 
1376 #undef __FUNC__
1377 #define __FUNC__ "MatCholeskyFactor"
1378 /*@
1379    MatCholeskyFactor - Performs in-place Cholesky factorization of a
1380    symmetric matrix.
1381 
1382    Collective on Mat
1383 
1384    Input Parameters:
1385 +  mat - the matrix
1386 .  perm - row and column permutations
1387 -  f - expected fill as ratio of original fill
1388 
1389    Notes:
1390    See MatLUFactor() for the nonsymmetric case.  See also
1391    MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric().
1392 
1393    Most users should employ the simplified SLES interface for linear solvers
1394    instead of working directly with matrix algebra routines such as this.
1395    See, e.g., SLESCreate().
1396 
1397    Level: developer
1398 
1399 .keywords: matrix, factor, in-place, Cholesky
1400 
1401 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric()
1402           MatGetOrdering()
1403 
1404 @*/
1405 int MatCholeskyFactor(Mat mat,IS perm,double f)
1406 {
1407   int ierr;
1408 
1409   PetscFunctionBegin;
1410   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1411   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"Matrix must be square");
1412   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1413   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
1414   if (!mat->ops->choleskyfactor) SETERRQ(PETSC_ERR_SUP,0,"");
1415 
1416   PLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);
1417   ierr = (*mat->ops->choleskyfactor)(mat,perm,f);CHKERRQ(ierr);
1418   PLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);
1419   PetscFunctionReturn(0);
1420 }
1421 
1422 #undef __FUNC__
1423 #define __FUNC__ "MatCholeskyFactorSymbolic"
1424 /*@
1425    MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization
1426    of a symmetric matrix.
1427 
1428    Collective on Mat
1429 
1430    Input Parameters:
1431 +  mat - the matrix
1432 .  perm - row and column permutations
1433 -  f - expected fill as ratio of original
1434 
1435    Output Parameter:
1436 .  fact - the factored matrix
1437 
1438    Notes:
1439    See MatLUFactorSymbolic() for the nonsymmetric case.  See also
1440    MatCholeskyFactor() and MatCholeskyFactorNumeric().
1441 
1442    Most users should employ the simplified SLES interface for linear solvers
1443    instead of working directly with matrix algebra routines such as this.
1444    See, e.g., SLESCreate().
1445 
1446    Level: developer
1447 
1448 .keywords: matrix, factor, factorization, symbolic, Cholesky
1449 
1450 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric()
1451           MatGetOrdering()
1452 
1453 @*/
1454 int MatCholeskyFactorSymbolic(Mat mat,IS perm,double f,Mat *fact)
1455 {
1456   int ierr;
1457 
1458   PetscFunctionBegin;
1459   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1460   PetscValidPointer(fact);
1461   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"Matrix must be square");
1462   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1463   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
1464   if (!mat->ops->choleskyfactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,"");
1465 
1466   PLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);
1467   ierr = (*mat->ops->choleskyfactorsymbolic)(mat,perm,f,fact);CHKERRQ(ierr);
1468   PLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);
1469   PetscFunctionReturn(0);
1470 }
1471 
1472 #undef __FUNC__
1473 #define __FUNC__ "MatCholeskyFactorNumeric"
1474 /*@
1475    MatCholeskyFactorNumeric - Performs numeric Cholesky factorization
1476    of a symmetric matrix. Call this routine after first calling
1477    MatCholeskyFactorSymbolic().
1478 
1479    Collective on Mat
1480 
1481    Input Parameter:
1482 .  mat - the initial matrix
1483 
1484    Output Parameter:
1485 .  fact - the factored matrix
1486 
1487    Notes:
1488    Most users should employ the simplified SLES interface for linear solvers
1489    instead of working directly with matrix algebra routines such as this.
1490    See, e.g., SLESCreate().
1491 
1492    Level: developer
1493 
1494 .keywords: matrix, factor, numeric, Cholesky
1495 
1496 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric()
1497 @*/
1498 int MatCholeskyFactorNumeric(Mat mat,Mat *fact)
1499 {
1500   int ierr;
1501 
1502   PetscFunctionBegin;
1503   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1504   PetscValidPointer(fact);
1505   if (!mat->ops->choleskyfactornumeric) SETERRQ(PETSC_ERR_SUP,0,"");
1506   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1507   if (mat->M != (*fact)->M || mat->N != (*fact)->N) {
1508     SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat mat,Mat *fact: global dim %d should = %d %d should = %d",
1509             mat->M,(*fact)->M,mat->N,(*fact)->N);
1510   }
1511 
1512   PLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0);
1513   ierr = (*mat->ops->choleskyfactornumeric)(mat,fact);CHKERRQ(ierr);
1514   PLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0);
1515   PetscFunctionReturn(0);
1516 }
1517 
1518 /* ----------------------------------------------------------------*/
1519 #undef __FUNC__
1520 #define __FUNC__ "MatSolve"
1521 /*@
1522    MatSolve - Solves A x = b, given a factored matrix.
1523 
1524    Collective on Mat and Vec
1525 
1526    Input Parameters:
1527 +  mat - the factored matrix
1528 -  b - the right-hand-side vector
1529 
1530    Output Parameter:
1531 .  x - the result vector
1532 
1533    Notes:
1534    The vectors b and x cannot be the same.  I.e., one cannot
1535    call MatSolve(A,x,x).
1536 
1537    Notes:
1538    Most users should employ the simplified SLES interface for linear solvers
1539    instead of working directly with matrix algebra routines such as this.
1540    See, e.g., SLESCreate().
1541 
1542    Level: developer
1543 
1544 .keywords: matrix, linear system, solve, LU, Cholesky, triangular solve
1545 
1546 .seealso: MatSolveAdd(), MatSolveTrans(), MatSolveTransAdd()
1547 @*/
1548 int MatSolve(Mat mat,Vec b,Vec x)
1549 {
1550   int ierr;
1551 
1552   PetscFunctionBegin;
1553   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1554   PetscValidHeaderSpecific(b,VEC_COOKIE);
1555   PetscValidHeaderSpecific(x,VEC_COOKIE);
1556   PetscCheckSameComm(mat,b);
1557   PetscCheckSameComm(mat,x);
1558   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors");
1559   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix");
1560   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1561   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1562   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1563   if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0);
1564 
1565   if (!mat->ops->solve) SETERRQ(PETSC_ERR_SUP,0,"");
1566   PLogEventBegin(MAT_Solve,mat,b,x,0);
1567   ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr);
1568   PLogEventEnd(MAT_Solve,mat,b,x,0);
1569   PetscFunctionReturn(0);
1570 }
1571 
1572 #undef __FUNC__
1573 #define __FUNC__ "MatForwardSolve"
1574 /* @
1575    MatForwardSolve - Solves L x = b, given a factored matrix, A = LU.
1576 
1577    Collective on Mat and Vec
1578 
1579    Input Parameters:
1580 +  mat - the factored matrix
1581 -  b - the right-hand-side vector
1582 
1583    Output Parameter:
1584 .  x - the result vector
1585 
1586    Notes:
1587    MatSolve() should be used for most applications, as it performs
1588    a forward solve followed by a backward solve.
1589 
1590    The vectors b and x cannot be the same.  I.e., one cannot
1591    call MatForwardSolve(A,x,x).
1592 
1593    Most users should employ the simplified SLES interface for linear solvers
1594    instead of working directly with matrix algebra routines such as this.
1595    See, e.g., SLESCreate().
1596 
1597    Level: developer
1598 
1599 .keywords: matrix, forward, LU, Cholesky, triangular solve
1600 
1601 .seealso: MatSolve(), MatBackwardSolve()
1602 @ */
1603 int MatForwardSolve(Mat mat,Vec b,Vec x)
1604 {
1605   int ierr;
1606 
1607   PetscFunctionBegin;
1608   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1609   PetscValidHeaderSpecific(b,VEC_COOKIE);
1610   PetscValidHeaderSpecific(x,VEC_COOKIE);
1611   PetscCheckSameComm(mat,b);
1612   PetscCheckSameComm(mat,x);
1613   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors");
1614   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix");
1615   if (!mat->ops->forwardsolve) SETERRQ(PETSC_ERR_SUP,0,"");
1616   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1617   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1618   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1619 
1620   PLogEventBegin(MAT_ForwardSolve,mat,b,x,0);
1621   ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr);
1622   PLogEventEnd(MAT_ForwardSolve,mat,b,x,0);
1623   PetscFunctionReturn(0);
1624 }
1625 
1626 #undef __FUNC__
1627 #define __FUNC__ "MatBackwardSolve"
1628 /* @
1629    MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU.
1630 
1631    Collective on Mat and Vec
1632 
1633    Input Parameters:
1634 +  mat - the factored matrix
1635 -  b - the right-hand-side vector
1636 
1637    Output Parameter:
1638 .  x - the result vector
1639 
1640    Notes:
1641    MatSolve() should be used for most applications, as it performs
1642    a forward solve followed by a backward solve.
1643 
1644    The vectors b and x cannot be the same.  I.e., one cannot
1645    call MatBackwardSolve(A,x,x).
1646 
1647    Most users should employ the simplified SLES interface for linear solvers
1648    instead of working directly with matrix algebra routines such as this.
1649    See, e.g., SLESCreate().
1650 
1651    Level: developer
1652 
1653 .keywords: matrix, backward, LU, Cholesky, triangular solve
1654 
1655 .seealso: MatSolve(), MatForwardSolve()
1656 @ */
1657 int MatBackwardSolve(Mat mat,Vec b,Vec x)
1658 {
1659   int ierr;
1660 
1661   PetscFunctionBegin;
1662   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1663   PetscValidHeaderSpecific(b,VEC_COOKIE);
1664   PetscValidHeaderSpecific(x,VEC_COOKIE);
1665   PetscCheckSameComm(mat,b);
1666   PetscCheckSameComm(mat,x);
1667   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors");
1668   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix");
1669   if (!mat->ops->backwardsolve) SETERRQ(PETSC_ERR_SUP,0,"");
1670   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1671   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1672   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1673 
1674   PLogEventBegin(MAT_BackwardSolve,mat,b,x,0);
1675   ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr);
1676   PLogEventEnd(MAT_BackwardSolve,mat,b,x,0);
1677   PetscFunctionReturn(0);
1678 }
1679 
1680 #undef __FUNC__
1681 #define __FUNC__ "MatSolveAdd"
1682 /*@
1683    MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix.
1684 
1685    Collective on Mat and Vec
1686 
1687    Input Parameters:
1688 +  mat - the factored matrix
1689 .  b - the right-hand-side vector
1690 -  y - the vector to be added to
1691 
1692    Output Parameter:
1693 .  x - the result vector
1694 
1695    Notes:
1696    The vectors b and x cannot be the same.  I.e., one cannot
1697    call MatSolveAdd(A,x,y,x).
1698 
1699    Most users should employ the simplified SLES interface for linear solvers
1700    instead of working directly with matrix algebra routines such as this.
1701    See, e.g., SLESCreate().
1702 
1703    Level: developer
1704 
1705 .keywords: matrix, linear system, solve, LU, Cholesky, add
1706 
1707 .seealso: MatSolve(), MatSolveTrans(), MatSolveTransAdd()
1708 @*/
1709 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x)
1710 {
1711   Scalar one = 1.0;
1712   Vec    tmp;
1713   int    ierr;
1714 
1715   PetscFunctionBegin;
1716   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1717   PetscValidHeaderSpecific(y,VEC_COOKIE);
1718   PetscValidHeaderSpecific(b,VEC_COOKIE);
1719   PetscValidHeaderSpecific(x,VEC_COOKIE);
1720   PetscCheckSameComm(mat,b);
1721   PetscCheckSameComm(mat,y);
1722   PetscCheckSameComm(mat,x);
1723   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors");
1724   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix");
1725   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1726   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1727   if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->M,y->N);
1728   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1729   if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Vec x,Vec y: local dim %d %d",x->n,y->n);
1730 
1731   PLogEventBegin(MAT_SolveAdd,mat,b,x,y);
1732   if (mat->ops->solveadd)  {
1733     ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr);
1734   } else {
1735     /* do the solve then the add manually */
1736     if (x != y) {
1737       ierr = MatSolve(mat,b,x);CHKERRQ(ierr);
1738       ierr = VecAXPY(&one,y,x);CHKERRQ(ierr);
1739     } else {
1740       ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr);
1741       PLogObjectParent(mat,tmp);
1742       ierr = VecCopy(x,tmp);CHKERRQ(ierr);
1743       ierr = MatSolve(mat,b,x);CHKERRQ(ierr);
1744       ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr);
1745       ierr = VecDestroy(tmp);CHKERRQ(ierr);
1746     }
1747   }
1748   PLogEventEnd(MAT_SolveAdd,mat,b,x,y);
1749   PetscFunctionReturn(0);
1750 }
1751 
1752 #undef __FUNC__
1753 #define __FUNC__ "MatSolveTrans"
1754 /*@
1755    MatSolveTrans - Solves A' x = b, given a factored matrix.
1756 
1757    Collective on Mat and Vec
1758 
1759    Input Parameters:
1760 +  mat - the factored matrix
1761 -  b - the right-hand-side vector
1762 
1763    Output Parameter:
1764 .  x - the result vector
1765 
1766    Notes:
1767    The vectors b and x cannot be the same.  I.e., one cannot
1768    call MatSolveTrans(A,x,x).
1769 
1770    Most users should employ the simplified SLES interface for linear solvers
1771    instead of working directly with matrix algebra routines such as this.
1772    See, e.g., SLESCreate().
1773 
1774    Level: developer
1775 
1776 .keywords: matrix, linear system, solve, LU, Cholesky, transpose
1777 
1778 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransAdd()
1779 @*/
1780 int MatSolveTrans(Mat mat,Vec b,Vec x)
1781 {
1782   int ierr;
1783 
1784   PetscFunctionBegin;
1785   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1786   PetscValidHeaderSpecific(b,VEC_COOKIE);
1787   PetscValidHeaderSpecific(x,VEC_COOKIE);
1788   PetscCheckSameComm(mat,b);
1789   PetscCheckSameComm(mat,x);
1790   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix");
1791   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors");
1792   if (!mat->ops->solvetrans) SETERRQ(PETSC_ERR_SUP,0,"");
1793   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->M,x->N);
1794   if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->N,b->N);
1795 
1796   PLogEventBegin(MAT_SolveTrans,mat,b,x,0);
1797   ierr = (*mat->ops->solvetrans)(mat,b,x);CHKERRQ(ierr);
1798   PLogEventEnd(MAT_SolveTrans,mat,b,x,0);
1799   PetscFunctionReturn(0);
1800 }
1801 
1802 #undef __FUNC__
1803 #define __FUNC__ "MatSolveTransAdd"
1804 /*@
1805    MatSolveTransAdd - Computes x = y + inv(trans(A)) b, given a
1806                       factored matrix.
1807 
1808    Collective on Mat and Vec
1809 
1810    Input Parameters:
1811 +  mat - the factored matrix
1812 .  b - the right-hand-side vector
1813 -  y - the vector to be added to
1814 
1815    Output Parameter:
1816 .  x - the result vector
1817 
1818    Notes:
1819    The vectors b and x cannot be the same.  I.e., one cannot
1820    call MatSolveTransAdd(A,x,y,x).
1821 
1822    Most users should employ the simplified SLES interface for linear solvers
1823    instead of working directly with matrix algebra routines such as this.
1824    See, e.g., SLESCreate().
1825 
1826    Level: developer
1827 
1828 .keywords: matrix, linear system, solve, LU, Cholesky, transpose, add
1829 
1830 .seealso: MatSolve(), MatSolveAdd(), MatSolveTrans()
1831 @*/
1832 int MatSolveTransAdd(Mat mat,Vec b,Vec y,Vec x)
1833 {
1834   Scalar one = 1.0;
1835   int    ierr;
1836   Vec    tmp;
1837 
1838   PetscFunctionBegin;
1839   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1840   PetscValidHeaderSpecific(y,VEC_COOKIE);
1841   PetscValidHeaderSpecific(b,VEC_COOKIE);
1842   PetscValidHeaderSpecific(x,VEC_COOKIE);
1843   PetscCheckSameComm(mat,b);
1844   PetscCheckSameComm(mat,y);
1845   PetscCheckSameComm(mat,x);
1846   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors");
1847   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix");
1848   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->M,x->N);
1849   if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->N,b->N);
1850   if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->N,y->N);
1851   if (x->n != y->n)   SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Vec x,Vec y: local dim %d %d",x->n,y->n);
1852 
1853   PLogEventBegin(MAT_SolveTransAdd,mat,b,x,y);
1854   if (mat->ops->solvetransadd) {
1855     ierr = (*mat->ops->solvetransadd)(mat,b,y,x);CHKERRQ(ierr);
1856   } else {
1857     /* do the solve then the add manually */
1858     if (x != y) {
1859       ierr = MatSolveTrans(mat,b,x);CHKERRQ(ierr);
1860       ierr = VecAXPY(&one,y,x);CHKERRQ(ierr);
1861     } else {
1862       ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr);
1863       PLogObjectParent(mat,tmp);
1864       ierr = VecCopy(x,tmp);CHKERRQ(ierr);
1865       ierr = MatSolveTrans(mat,b,x);CHKERRQ(ierr);
1866       ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr);
1867       ierr = VecDestroy(tmp);CHKERRQ(ierr);
1868     }
1869   }
1870   PLogEventEnd(MAT_SolveTransAdd,mat,b,x,y);
1871   PetscFunctionReturn(0);
1872 }
1873 /* ----------------------------------------------------------------*/
1874 
1875 #undef __FUNC__
1876 #define __FUNC__ "MatRelax"
1877 /*@
1878    MatRelax - Computes one relaxation sweep.
1879 
1880    Collective on Mat and Vec
1881 
1882    Input Parameters:
1883 +  mat - the matrix
1884 .  b - the right hand side
1885 .  omega - the relaxation factor
1886 .  flag - flag indicating the type of SOR (see below)
1887 .  shift -  diagonal shift
1888 -  its - the number of iterations
1889 
1890    Output Parameters:
1891 .  x - the solution (can contain an initial guess)
1892 
1893    SOR Flags:
1894 .     SOR_FORWARD_SWEEP - forward SOR
1895 .     SOR_BACKWARD_SWEEP - backward SOR
1896 .     SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR)
1897 .     SOR_LOCAL_FORWARD_SWEEP - local forward SOR
1898 .     SOR_LOCAL_BACKWARD_SWEEP - local forward SOR
1899 .     SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR
1900 .     SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies
1901          upper/lower triangular part of matrix to
1902          vector (with omega)
1903 .     SOR_ZERO_INITIAL_GUESS - zero initial guess
1904 
1905    Notes:
1906    SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and
1907    SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings
1908    on each processor.
1909 
1910    Application programmers will not generally use MatRelax() directly,
1911    but instead will employ the SLES/PC interface.
1912 
1913    Notes for Advanced Users:
1914    The flags are implemented as bitwise inclusive or operations.
1915    For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP)
1916    to specify a zero initial guess for SSOR.
1917 
1918    Most users should employ the simplified SLES interface for linear solvers
1919    instead of working directly with matrix algebra routines such as this.
1920    See, e.g., SLESCreate().
1921 
1922    Level: developer
1923 
1924 .keywords: matrix, relax, relaxation, sweep
1925 @*/
1926 int MatRelax(Mat mat,Vec b,double omega,MatSORType flag,double shift,int its,Vec x)
1927 {
1928   int ierr;
1929 
1930   PetscFunctionBegin;
1931   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1932   PetscValidHeaderSpecific(b,VEC_COOKIE);
1933   PetscValidHeaderSpecific(x,VEC_COOKIE);
1934   PetscCheckSameComm(mat,b);
1935   PetscCheckSameComm(mat,x);
1936   if (!mat->ops->relax) SETERRQ(PETSC_ERR_SUP,0,"");
1937   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1938   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
1939   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1940   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1941   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1942 
1943   PLogEventBegin(MAT_Relax,mat,b,x,0);
1944   ierr =(*mat->ops->relax)(mat,b,omega,flag,shift,its,x);CHKERRQ(ierr);
1945   PLogEventEnd(MAT_Relax,mat,b,x,0);
1946   PetscFunctionReturn(0);
1947 }
1948 
1949 #undef __FUNC__
1950 #define __FUNC__ "MatCopy_Basic"
1951 /*
1952       Default matrix copy routine.
1953 */
1954 int MatCopy_Basic(Mat A,Mat B,MatStructure str)
1955 {
1956   int    ierr,i,rstart,rend,nz,*cwork;
1957   Scalar *vwork;
1958 
1959   PetscFunctionBegin;
1960   ierr = MatZeroEntries(B);CHKERRQ(ierr);
1961   ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr);
1962   for (i=rstart; i<rend; i++) {
1963     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
1964     ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
1965     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
1966   }
1967   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1968   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1969   PetscFunctionReturn(0);
1970 }
1971 
1972 #undef __FUNC__
1973 #define __FUNC__ "MatCopy"
1974 /*@C
1975    MatCopy - Copys a matrix to another matrix.
1976 
1977    Collective on Mat
1978 
1979    Input Parameters:
1980 +  A - the matrix
1981 -  str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN
1982 
1983    Output Parameter:
1984 .  B - where the copy is put
1985 
1986    Notes:
1987    If you use SAME_NONZERO_PATTERN then the zero matrices had better have the
1988    same nonzero pattern or the routine will crash.
1989 
1990    MatCopy() copies the matrix entries of a matrix to another existing
1991    matrix (after first zeroing the second matrix).  A related routine is
1992    MatConvert(), which first creates a new matrix and then copies the data.
1993 
1994    Level: intermediate
1995 
1996 .keywords: matrix, copy, convert
1997 
1998 .seealso: MatConvert()
1999 @*/
2000 int MatCopy(Mat A,Mat B,MatStructure str)
2001 {
2002   int ierr;
2003 
2004   PetscFunctionBegin;
2005   PetscValidHeaderSpecific(A,MAT_COOKIE);
2006   PetscValidHeaderSpecific(B,MAT_COOKIE);
2007   PetscCheckSameComm(A,B);
2008   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2009   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2010   if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat A,Mat B: global dim %d %d",A->M,B->M,
2011                                              A->N,B->N);
2012 
2013   PLogEventBegin(MAT_Copy,A,B,0,0);
2014   if (A->ops->copy) {
2015     ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr);
2016   } else { /* generic conversion */
2017     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2018   }
2019   PLogEventEnd(MAT_Copy,A,B,0,0);
2020   PetscFunctionReturn(0);
2021 }
2022 
2023 static int MatConvertersSet = 0;
2024 static int (*MatConverters[MAX_MATRIX_TYPES][MAX_MATRIX_TYPES])(Mat,MatType,Mat*) =
2025            {{0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0},
2026             {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0},
2027             {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0},
2028             {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0},
2029             {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0},
2030             {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}};
2031 
2032 #undef __FUNC__
2033 #define __FUNC__ "MatConvertRegister"
2034 /*@C
2035     MatConvertRegister - Allows one to register a routine that converts between
2036     two matrix types.
2037 
2038     Not Collective
2039 
2040     Input Parameters:
2041 +   intype - the type of matrix (defined in include/mat.h), for example, MATSEQAIJ.
2042 -   outtype - new matrix type, or MATSAME
2043 
2044     Level: advanced
2045 
2046 .seealso: MatConvertRegisterAll()
2047 @*/
2048 int MatConvertRegister(MatType intype,MatType outtype,int (*converter)(Mat,MatType,Mat*))
2049 {
2050   PetscFunctionBegin;
2051   MatConverters[intype][outtype] = converter;
2052   MatConvertersSet               = 1;
2053   PetscFunctionReturn(0);
2054 }
2055 
2056 #undef __FUNC__
2057 #define __FUNC__ "MatConvert"
2058 /*@C
2059    MatConvert - Converts a matrix to another matrix, either of the same
2060    or different type.
2061 
2062    Collective on Mat
2063 
2064    Input Parameters:
2065 +  mat - the matrix
2066 -  newtype - new matrix type.  Use MATSAME to create a new matrix of the
2067    same type as the original matrix.
2068 
2069    Output Parameter:
2070 .  M - pointer to place new matrix
2071 
2072    Notes:
2073    MatConvert() first creates a new matrix and then copies the data from
2074    the first matrix.  A related routine is MatCopy(), which copies the matrix
2075    entries of one matrix to another already existing matrix context.
2076 
2077    Level: intermediate
2078 
2079 .keywords: matrix, copy, convert
2080 
2081 .seealso: MatCopy(), MatDuplicate()
2082 @*/
2083 int MatConvert(Mat mat,MatType newtype,Mat *M)
2084 {
2085   int ierr;
2086 
2087   PetscFunctionBegin;
2088   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2089   PetscValidPointer(M);
2090   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2091   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2092 
2093   if (newtype > MAX_MATRIX_TYPES || newtype < -1) {
2094     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,1,"Not a valid matrix type");
2095   }
2096   *M  = 0;
2097 
2098   if (!MatConvertersSet) {
2099     ierr = MatLoadRegisterAll();CHKERRQ(ierr);
2100   }
2101 
2102   PLogEventBegin(MAT_Convert,mat,0,0,0);
2103   if ((newtype == mat->type || newtype == MATSAME) && mat->ops->duplicate) {
2104     ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr);
2105   } else {
2106     if (!MatConvertersSet) {
2107       ierr = MatConvertRegisterAll();CHKERRQ(ierr);
2108     }
2109     if (!MatConverters[mat->type][newtype]) {
2110       SETERRQ(PETSC_ERR_ARG_WRONG,1,"Invalid matrix type, or matrix converter not registered");
2111     }
2112     ierr = (*MatConverters[mat->type][newtype])(mat,newtype,M);CHKERRQ(ierr);
2113   }
2114   PLogEventEnd(MAT_Convert,mat,0,0,0);
2115   PetscFunctionReturn(0);
2116 }
2117 
2118 #undef __FUNC__
2119 #define __FUNC__ "MatDuplicate"
2120 /*@C
2121    MatDuplicate - Duplicates a matrix including the non-zero structure.
2122 
2123    Collective on Mat
2124 
2125    Input Parameters:
2126 +  mat - the matrix
2127 -  op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy nonzero
2128         values as well or not
2129 
2130    Output Parameter:
2131 .  M - pointer to place new matrix
2132 
2133    Level: intermediate
2134 
2135 .keywords: matrix, copy, convert, duplicate
2136 
2137 .seealso: MatCopy(), MatConvert()
2138 @*/
2139 int MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M)
2140 {
2141   int ierr;
2142 
2143   PetscFunctionBegin;
2144   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2145   PetscValidPointer(M);
2146   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2147   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2148 
2149   *M  = 0;
2150   PLogEventBegin(MAT_Convert,mat,0,0,0);
2151   if (!mat->ops->duplicate) {
2152     SETERRQ(PETSC_ERR_SUP,1,"Not written for this matrix type");
2153   }
2154   ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr);
2155   PLogEventEnd(MAT_Convert,mat,0,0,0);
2156   PetscFunctionReturn(0);
2157 }
2158 
2159 #undef __FUNC__
2160 #define __FUNC__ "MatGetDiagonal"
2161 /*@
2162    MatGetDiagonal - Gets the diagonal of a matrix.
2163 
2164    Collective on Mat and Vec
2165 
2166    Input Parameters:
2167 +  mat - the matrix
2168 -  v - the vector for storing the diagonal
2169 
2170    Output Parameter:
2171 .  v - the diagonal of the matrix
2172 
2173    Notes:
2174    For the SeqAIJ matrix format, this routine may also be called
2175    on a LU factored matrix; in that case it routines the reciprocal of
2176    the diagonal entries in U. It returns the entries permuted by the
2177    row and column permutation used during the symbolic factorization.
2178 
2179    Level: intermediate
2180 
2181 .keywords: matrix, get, diagonal
2182 
2183 .seealso: MatGetRow(), MatGetSubmatrices(), MatGetSubmatrix()
2184 @*/
2185 int MatGetDiagonal(Mat mat,Vec v)
2186 {
2187   int ierr;
2188 
2189   PetscFunctionBegin;
2190   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2191   PetscValidHeaderSpecific(v,VEC_COOKIE);
2192   /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */
2193   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2194   if (!mat->ops->getdiagonal) SETERRQ(PETSC_ERR_SUP,0,"");
2195 
2196   ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr);
2197   PetscFunctionReturn(0);
2198 }
2199 
2200 #undef __FUNC__
2201 #define __FUNC__ "MatTranspose"
2202 /*@C
2203    MatTranspose - Computes an in-place or out-of-place transpose of a matrix.
2204 
2205    Collective on Mat
2206 
2207    Input Parameter:
2208 .  mat - the matrix to transpose
2209 
2210    Output Parameters:
2211 .  B - the transpose (or pass in PETSC_NULL for an in-place transpose)
2212 
2213    Level: intermediate
2214 
2215 .keywords: matrix, transpose
2216 
2217 .seealso: MatMultTrans(), MatMultTransAdd()
2218 @*/
2219 int MatTranspose(Mat mat,Mat *B)
2220 {
2221   int ierr;
2222 
2223   PetscFunctionBegin;
2224   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2225   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2226   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2227   if (!mat->ops->transpose) SETERRQ(PETSC_ERR_SUP,0,"");
2228   ierr = (*mat->ops->transpose)(mat,B);CHKERRQ(ierr);
2229   PetscFunctionReturn(0);
2230 }
2231 
2232 #undef __FUNC__
2233 #define __FUNC__ "MatPermute"
2234 /*@C
2235    MatPermute - Creates a new matrix with rows and columns permuted from the
2236    original.
2237 
2238    Collective on Mat
2239 
2240    Input Parameters:
2241 +  mat - the matrix to permute
2242 .  row - row permutation
2243 -  col - column permutation
2244 
2245    Output Parameters:
2246 .  B - the permuted matrix
2247 
2248    Level: advanced
2249 
2250 .keywords: matrix, transpose
2251 
2252 .seealso: MatGetOrdering()
2253 @*/
2254 int MatPermute(Mat mat,IS row,IS col,Mat *B)
2255 {
2256   int ierr;
2257 
2258   PetscFunctionBegin;
2259   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2260   PetscValidHeaderSpecific(row,IS_COOKIE);
2261   PetscValidHeaderSpecific(col,IS_COOKIE);
2262   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2263   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2264   if (!mat->ops->permute) SETERRQ(PETSC_ERR_SUP,0,"");
2265   ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr);
2266   PetscFunctionReturn(0);
2267 }
2268 
2269 #undef __FUNC__
2270 #define __FUNC__ "MatEqual"
2271 /*@
2272    MatEqual - Compares two matrices.
2273 
2274    Collective on Mat
2275 
2276    Input Parameters:
2277 +  A - the first matrix
2278 -  B - the second matrix
2279 
2280    Output Parameter:
2281 .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.
2282 
2283    Level: intermediate
2284 
2285 .keywords: matrix, equal, equivalent
2286 @*/
2287 int MatEqual(Mat A,Mat B,PetscTruth *flg)
2288 {
2289   int ierr;
2290 
2291   PetscFunctionBegin;
2292   PetscValidHeaderSpecific(A,MAT_COOKIE);
2293   PetscValidHeaderSpecific(B,MAT_COOKIE);
2294   PetscValidIntPointer(flg);
2295   PetscCheckSameComm(A,B);
2296   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2297   if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2298   if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat A,Mat B: global dim %d %d %d %d",
2299                                              A->M,B->M,A->N,B->N);
2300   if (!A->ops->equal) SETERRQ(PETSC_ERR_SUP,0,"");
2301   ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr);
2302   PetscFunctionReturn(0);
2303 }
2304 
2305 #undef __FUNC__
2306 #define __FUNC__ "MatDiagonalScale"
2307 /*@
2308    MatDiagonalScale - Scales a matrix on the left and right by diagonal
2309    matrices that are stored as vectors.  Either of the two scaling
2310    matrices can be PETSC_NULL.
2311 
2312    Collective on Mat
2313 
2314    Input Parameters:
2315 +  mat - the matrix to be scaled
2316 .  l - the left scaling vector (or PETSC_NULL)
2317 -  r - the right scaling vector (or PETSC_NULL)
2318 
2319    Notes:
2320    MatDiagonalScale() computes A = LAR, where
2321    L = a diagonal matrix, R = a diagonal matrix
2322 
2323    Level: intermediate
2324 
2325 .keywords: matrix, diagonal, scale
2326 
2327 .seealso: MatScale()
2328 @*/
2329 int MatDiagonalScale(Mat mat,Vec l,Vec r)
2330 {
2331   int ierr;
2332 
2333   PetscFunctionBegin;
2334   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2335   if (!mat->ops->diagonalscale) SETERRQ(PETSC_ERR_SUP,0,"");
2336   if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE);PetscCheckSameComm(mat,l);}
2337   if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE);PetscCheckSameComm(mat,r);}
2338   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2339   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2340 
2341   PLogEventBegin(MAT_Scale,mat,0,0,0);
2342   ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr);
2343   PLogEventEnd(MAT_Scale,mat,0,0,0);
2344   PetscFunctionReturn(0);
2345 }
2346 
2347 #undef __FUNC__
2348 #define __FUNC__ "MatScale"
2349 /*@
2350     MatScale - Scales all elements of a matrix by a given number.
2351 
2352     Collective on Mat
2353 
2354     Input Parameters:
2355 +   mat - the matrix to be scaled
2356 -   a  - the scaling value
2357 
2358     Output Parameter:
2359 .   mat - the scaled matrix
2360 
2361     Level: intermediate
2362 
2363 .keywords: matrix, scale
2364 
2365 .seealso: MatDiagonalScale()
2366 @*/
2367 int MatScale(Scalar *a,Mat mat)
2368 {
2369   int ierr;
2370 
2371   PetscFunctionBegin;
2372   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2373   PetscValidScalarPointer(a);
2374   if (!mat->ops->scale) SETERRQ(PETSC_ERR_SUP,0,"");
2375   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2376   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2377 
2378   PLogEventBegin(MAT_Scale,mat,0,0,0);
2379   ierr = (*mat->ops->scale)(a,mat);CHKERRQ(ierr);
2380   PLogEventEnd(MAT_Scale,mat,0,0,0);
2381   PetscFunctionReturn(0);
2382 }
2383 
2384 #undef __FUNC__
2385 #define __FUNC__ "MatNorm"
2386 /*@
2387    MatNorm - Calculates various norms of a matrix.
2388 
2389    Collective on Mat
2390 
2391    Input Parameters:
2392 +  mat - the matrix
2393 -  type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY
2394 
2395    Output Parameters:
2396 .  norm - the resulting norm
2397 
2398    Level: intermediate
2399 
2400 .keywords: matrix, norm, Frobenius
2401 @*/
2402 int MatNorm(Mat mat,NormType type,double *norm)
2403 {
2404   int ierr;
2405 
2406   PetscFunctionBegin;
2407   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2408   PetscValidScalarPointer(norm);
2409 
2410   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2411   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2412   if (!mat->ops->norm) SETERRQ(PETSC_ERR_SUP,0,"Not for this matrix type");
2413   ierr = (*mat->ops->norm)(mat,type,norm);CHKERRQ(ierr);
2414   PetscFunctionReturn(0);
2415 }
2416 
2417 /*
2418      This variable is used to prevent counting of MatAssemblyBegin() that
2419    are called from within a MatAssemblyEnd().
2420 */
2421 static int MatAssemblyEnd_InUse = 0;
2422 #undef __FUNC__
2423 #define __FUNC__ "MatAssemblyBegin"
2424 /*@
2425    MatAssemblyBegin - Begins assembling the matrix.  This routine should
2426    be called after completing all calls to MatSetValues().
2427 
2428    Collective on Mat
2429 
2430    Input Parameters:
2431 +  mat - the matrix
2432 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
2433 
2434    Notes:
2435    MatSetValues() generally caches the values.  The matrix is ready to
2436    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
2437    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
2438    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
2439    using the matrix.
2440 
2441    Level: beginner
2442 
2443 .keywords: matrix, assembly, assemble, begin
2444 
2445 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
2446 @*/
2447 int MatAssemblyBegin(Mat mat,MatAssemblyType type)
2448 {
2449   int ierr;
2450 
2451   PetscFunctionBegin;
2452   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2453   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix.\n did you forget to call MatSetUnfactored()?");
2454   if (mat->assembled) {
2455     mat->was_assembled = PETSC_TRUE;
2456     mat->assembled     = PETSC_FALSE;
2457   }
2458   if (!MatAssemblyEnd_InUse) {
2459     PLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);
2460     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
2461     PLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);
2462   } else {
2463     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
2464   }
2465   PetscFunctionReturn(0);
2466 }
2467 
2468 #undef __FUNC__
2469 #define __FUNC__ "MatAssembed"
2470 /*@
2471    MatAssembled - Indicates if a matrix has been assembled and is ready for
2472      use; for example, in matrix-vector product.
2473 
2474    Collective on Mat
2475 
2476    Input Parameter:
2477 .  mat - the matrix
2478 
2479    Output Parameter:
2480 .  assembled - PETSC_TRUE or PETSC_FALSE
2481 
2482    Level: advanced
2483 
2484 .keywords: matrix, assembly, assemble, begin
2485 
2486 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
2487 @*/
2488 int MatAssembled(Mat mat,PetscTruth *assembled)
2489 {
2490   PetscFunctionBegin;
2491   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2492   *assembled = mat->assembled;
2493   PetscFunctionReturn(0);
2494 }
2495 
2496 #undef __FUNC__
2497 #define __FUNC__ "MatView_Private"
2498 /*
2499     Processes command line options to determine if/how a matrix
2500   is to be viewed. Called by MatAssemblyEnd() and MatLoad().
2501 */
2502 int MatView_Private(Mat mat)
2503 {
2504   int ierr,flg;
2505 
2506   PetscFunctionBegin;
2507   ierr = OptionsHasName(mat->prefix,"-mat_view_info",&flg);CHKERRQ(ierr);
2508   if (flg) {
2509     ierr = ViewerPushFormat(VIEWER_STDOUT_(mat->comm),VIEWER_FORMAT_ASCII_INFO,0);CHKERRQ(ierr);
2510     ierr = MatView(mat,VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2511     ierr = ViewerPopFormat(VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2512   }
2513   ierr = OptionsHasName(mat->prefix,"-mat_view_info_detailed",&flg);CHKERRQ(ierr);
2514   if (flg) {
2515     ierr = ViewerPushFormat(VIEWER_STDOUT_(mat->comm),VIEWER_FORMAT_ASCII_INFO_LONG,0);CHKERRQ(ierr);
2516     ierr = MatView(mat,VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2517     ierr = ViewerPopFormat(VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2518   }
2519   ierr = OptionsHasName(mat->prefix,"-mat_view",&flg);CHKERRQ(ierr);
2520   if (flg) {
2521     ierr = MatView(mat,VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2522   }
2523   ierr = OptionsHasName(mat->prefix,"-mat_view_matlab",&flg);CHKERRQ(ierr);
2524   if (flg) {
2525     ierr = ViewerPushFormat(VIEWER_STDOUT_(mat->comm),VIEWER_FORMAT_ASCII_MATLAB,"M");CHKERRQ(ierr);
2526     ierr = MatView(mat,VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2527     ierr = ViewerPopFormat(VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2528   }
2529   ierr = OptionsHasName(mat->prefix,"-mat_view_draw",&flg);CHKERRQ(ierr);
2530   if (flg) {
2531     ierr = OptionsHasName(mat->prefix,"-mat_view_contour",&flg);CHKERRQ(ierr);
2532     if (flg) {
2533       ViewerPushFormat(VIEWER_DRAW_(mat->comm),VIEWER_FORMAT_DRAW_CONTOUR,0);CHKERRQ(ierr);
2534     }
2535     ierr = MatView(mat,VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2536     ierr = ViewerFlush(VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2537     if (flg) {
2538       ViewerPopFormat(VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2539     }
2540   }
2541   ierr = OptionsHasName(mat->prefix,"-mat_view_socket",&flg);CHKERRQ(ierr);
2542   if (flg) {
2543     ierr = MatView(mat,VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
2544     ierr = ViewerFlush(VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
2545   }
2546   PetscFunctionReturn(0);
2547 }
2548 
2549 #undef __FUNC__
2550 #define __FUNC__ "MatAssemblyEnd"
2551 /*@
2552    MatAssemblyEnd - Completes assembling the matrix.  This routine should
2553    be called after MatAssemblyBegin().
2554 
2555    Collective on Mat
2556 
2557    Input Parameters:
2558 +  mat - the matrix
2559 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
2560 
2561    Options Database Keys:
2562 +  -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly()
2563 .  -mat_view_info_detailed - Prints more detailed info
2564 .  -mat_view - Prints matrix in ASCII format
2565 .  -mat_view_matlab - Prints matrix in Matlab format
2566 .  -mat_view_draw - Draws nonzero structure of matrix, using MatView() and DrawOpenX().
2567 .  -display <name> - Sets display name (default is host)
2568 -  -draw_pause <sec> - Sets number of seconds to pause after display
2569 
2570    Notes:
2571    MatSetValues() generally caches the values.  The matrix is ready to
2572    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
2573    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
2574    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
2575    using the matrix.
2576 
2577    Level: beginner
2578 
2579 .keywords: matrix, assembly, assemble, end
2580 
2581 .seealso: MatAssemblyBegin(), MatSetValues(), DrawOpenX(), MatView(), MatAssembled()
2582 @*/
2583 int MatAssemblyEnd(Mat mat,MatAssemblyType type)
2584 {
2585   int        ierr;
2586   static int inassm = 0;
2587 
2588   PetscFunctionBegin;
2589   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2590 
2591   inassm++;
2592   MatAssemblyEnd_InUse++;
2593   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
2594     PLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);
2595     if (mat->ops->assemblyend) {
2596       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
2597     }
2598     PLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);
2599   } else {
2600     if (mat->ops->assemblyend) {
2601       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
2602     }
2603   }
2604 
2605   /* Flush assembly is not a true assembly */
2606   if (type != MAT_FLUSH_ASSEMBLY) {
2607     mat->assembled  = PETSC_TRUE; mat->num_ass++;
2608   }
2609   mat->insertmode = NOT_SET_VALUES;
2610   MatAssemblyEnd_InUse--;
2611 
2612   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
2613     ierr = MatView_Private(mat);CHKERRQ(ierr);
2614   }
2615   inassm--;
2616   PetscFunctionReturn(0);
2617 }
2618 
2619 
2620 #undef __FUNC__
2621 #define __FUNC__ "MatCompress"
2622 /*@
2623    MatCompress - Tries to store the matrix in as little space as
2624    possible.  May fail if memory is already fully used, since it
2625    tries to allocate new space.
2626 
2627    Collective on Mat
2628 
2629    Input Parameters:
2630 .  mat - the matrix
2631 
2632    Level: advanced
2633 
2634 .keywords: matrix, compress
2635 @*/
2636 int MatCompress(Mat mat)
2637 {
2638   int ierr;
2639 
2640   PetscFunctionBegin;
2641   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2642   if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);}
2643   PetscFunctionReturn(0);
2644 }
2645 
2646 #undef __FUNC__
2647 #define __FUNC__ "MatSetOption"
2648 /*@
2649    MatSetOption - Sets a parameter option for a matrix. Some options
2650    may be specific to certain storage formats.  Some options
2651    determine how values will be inserted (or added). Sorted,
2652    row-oriented input will generally assemble the fastest. The default
2653    is row-oriented, nonsorted input.
2654 
2655    Collective on Mat
2656 
2657    Input Parameters:
2658 +  mat - the matrix
2659 -  option - the option, one of those listed below (and possibly others),
2660              e.g., MAT_ROWS_SORTED, MAT_NEW_NONZERO_LOCATION_ERR
2661 
2662    Options Describing Matrix Structure:
2663 +    MAT_SYMMETRIC - symmetric in terms of both structure and value
2664 -    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
2665 
2666    Options For Use with MatSetValues():
2667    Insert a logically dense subblock, which can be
2668 +    MAT_ROW_ORIENTED - row-oriented
2669 .    MAT_COLUMN_ORIENTED - column-oriented
2670 .    MAT_ROWS_SORTED - sorted by row
2671 .    MAT_ROWS_UNSORTED - not sorted by row
2672 .    MAT_COLUMNS_SORTED - sorted by column
2673 -    MAT_COLUMNS_UNSORTED - not sorted by column
2674 
2675    Not these options reflect the data you pass in with MatSetValues(); it has
2676    nothing to do with how the data is stored internally in the matrix
2677    data structure.
2678 
2679    When (re)assembling a matrix, we can restrict the input for
2680    efficiency/debugging purposes.  These options include
2681 +    MAT_NO_NEW_NONZERO_LOCATIONS - additional insertions will not be
2682         allowed if they generate a new nonzero
2683 .    MAT_YES_NEW_NONZERO_LOCATIONS - additional insertions will be allowed
2684 .    MAT_NO_NEW_DIAGONALS - additional insertions will not be allowed if
2685          they generate a nonzero in a new diagonal (for block diagonal format only)
2686 .    MAT_YES_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only)
2687 .    MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries
2688 .    MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry
2689 -    MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly
2690 
2691    Notes:
2692    Some options are relevant only for particular matrix types and
2693    are thus ignored by others.  Other options are not supported by
2694    certain matrix types and will generate an error message if set.
2695 
2696    If using a Fortran 77 module to compute a matrix, one may need to
2697    use the column-oriented option (or convert to the row-oriented
2698    format).
2699 
2700    MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion
2701    that would generate a new entry in the nonzero structure is instead
2702    ignored.  Thus, if memory has not alredy been allocated for this particular
2703    data, then the insertion is ignored. For dense matrices, in which
2704    the entire array is allocated, no entries are ever ignored.
2705 
2706    MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion
2707    that would generate a new entry in the nonzero structure instead produces
2708    an error. (Currently supported for AIJ and BAIJ formats only.)
2709    This is a useful flag when using SAME_NONZERO_PATTERN in calling
2710    SLESSetOperators() to ensure that the nonzero pattern truely does
2711    remain unchanged.
2712 
2713    MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion
2714    that would generate a new entry that has not been preallocated will
2715    instead produce an error. (Currently supported for AIJ and BAIJ formats
2716    only.) This is a useful flag when debugging matrix memory preallocation.
2717 
2718    MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for
2719    other processors should be dropped, rather than stashed.
2720    This is useful if you know that the "owning" processor is also
2721    always generating the correct matrix entries, so that PETSc need
2722    not transfer duplicate entries generated on another processor.
2723 
2724    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
2725    searches during matrix assembly. When this flag is set, the hash table
2726    is created during the first Matrix Assembly. This hash table is
2727    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
2728    to improve the searching of indices. MAT_NO_NEW_NONZERO_LOCATIONS flag
2729    should be used with MAT_USE_HASH_TABLE flag. This option is currently
2730    supported by MATMPIBAIJ format only.
2731 
2732    Level: intermediate
2733 
2734 .keywords: matrix, option, row-oriented, column-oriented, sorted, nonzero
2735 @*/
2736 int MatSetOption(Mat mat,MatOption op)
2737 {
2738   int ierr;
2739 
2740   PetscFunctionBegin;
2741   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2742   if (mat->ops->setoption) {ierr = (*mat->ops->setoption)(mat,op);CHKERRQ(ierr);}
2743   PetscFunctionReturn(0);
2744 }
2745 
2746 #undef __FUNC__
2747 #define __FUNC__ "MatZeroEntries"
2748 /*@
2749    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
2750    this routine retains the old nonzero structure.
2751 
2752    Collective on Mat
2753 
2754    Input Parameters:
2755 .  mat - the matrix
2756 
2757    Level: intermediate
2758 
2759 .keywords: matrix, zero, entries
2760 
2761 .seealso: MatZeroRows()
2762 @*/
2763 int MatZeroEntries(Mat mat)
2764 {
2765   int ierr;
2766 
2767   PetscFunctionBegin;
2768   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2769   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2770   if (!mat->ops->zeroentries) SETERRQ(PETSC_ERR_SUP,0,"");
2771 
2772   PLogEventBegin(MAT_ZeroEntries,mat,0,0,0);
2773   ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr);
2774   PLogEventEnd(MAT_ZeroEntries,mat,0,0,0);
2775   PetscFunctionReturn(0);
2776 }
2777 
2778 #undef __FUNC__
2779 #define __FUNC__ "MatZeroRows"
2780 /*@C
2781    MatZeroRows - Zeros all entries (except possibly the main diagonal)
2782    of a set of rows of a matrix.
2783 
2784    Collective on Mat
2785 
2786    Input Parameters:
2787 +  mat - the matrix
2788 .  is - index set of rows to remove
2789 -  diag - pointer to value put in all diagonals of eliminated rows.
2790           Note that diag is not a pointer to an array, but merely a
2791           pointer to a single value.
2792 
2793    Notes:
2794    For the AIJ matrix formats this removes the old nonzero structure,
2795    but does not release memory.  For the dense and block diagonal
2796    formats this does not alter the nonzero structure.
2797 
2798    The user can set a value in the diagonal entry (or for the AIJ and
2799    row formats can optionally remove the main diagonal entry from the
2800    nonzero structure as well, by passing a null pointer (PETSC_NULL
2801    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
2802 
2803    For the parallel case, all processes that share the matrix (i.e.,
2804    those in the communicator used for matrix creation) MUST call this
2805    routine, regardless of whether any rows being zeroed are owned by
2806    them.
2807 
2808    Level: intermediate
2809 
2810 .keywords: matrix, zero, rows, boundary conditions
2811 
2812 .seealso: MatZeroEntries(), MatZeroRowsLocal()
2813 @*/
2814 int MatZeroRows(Mat mat,IS is, Scalar *diag)
2815 {
2816   int ierr;
2817 
2818   PetscFunctionBegin;
2819   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2820   PetscValidHeaderSpecific(is,IS_COOKIE);
2821   if (diag) PetscValidScalarPointer(diag);
2822   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2823   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2824   if (!mat->ops->zerorows) SETERRQ(PETSC_ERR_SUP,0,"");
2825 
2826   ierr = (*mat->ops->zerorows)(mat,is,diag);CHKERRQ(ierr);
2827   ierr = MatView_Private(mat);CHKERRQ(ierr);
2828   PetscFunctionReturn(0);
2829 }
2830 
2831 #undef __FUNC__
2832 #define __FUNC__ "MatZeroRowsLocal"
2833 /*@C
2834    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
2835    of a set of rows of a matrix; using local numbering of rows.
2836 
2837    Collective on Mat
2838 
2839    Input Parameters:
2840 +  mat - the matrix
2841 .  is - index set of rows to remove
2842 -  diag - pointer to value put in all diagonals of eliminated rows.
2843           Note that diag is not a pointer to an array, but merely a
2844           pointer to a single value.
2845 
2846    Notes:
2847    For the AIJ matrix formats this removes the old nonzero structure,
2848    but does not release memory.  For the dense and block diagonal
2849    formats this does not alter the nonzero structure.
2850 
2851    The user can set a value in the diagonal entry (or for the AIJ and
2852    row formats can optionally remove the main diagonal entry from the
2853    nonzero structure as well, by passing a null pointer (PETSC_NULL
2854    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
2855 
2856    Level: intermediate
2857 
2858 .keywords: matrix, zero, rows, boundary conditions
2859 
2860 .seealso: MatZeroEntries(), MatZeroRows()
2861 @*/
2862 int MatZeroRowsLocal(Mat mat,IS is, Scalar *diag)
2863 {
2864   int ierr;
2865   IS  newis;
2866 
2867   PetscFunctionBegin;
2868   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2869   PetscValidHeaderSpecific(is,IS_COOKIE);
2870   if (diag) PetscValidScalarPointer(diag);
2871   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2872   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2873   if (!mat->ops->zerorows) SETERRQ(PETSC_ERR_SUP,0,"");
2874 
2875   ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr);
2876   ierr = (*mat->ops->zerorows)(mat,newis,diag);CHKERRQ(ierr);
2877   ierr = ISDestroy(newis);CHKERRQ(ierr);
2878   PetscFunctionReturn(0);
2879 }
2880 
2881 #undef __FUNC__
2882 #define __FUNC__ "MatGetSize"
2883 /*@
2884    MatGetSize - Returns the numbers of rows and columns in a matrix.
2885 
2886    Not Collective
2887 
2888    Input Parameter:
2889 .  mat - the matrix
2890 
2891    Output Parameters:
2892 +  m - the number of global rows
2893 -  n - the number of global columns
2894 
2895    Level: beginner
2896 
2897 .keywords: matrix, dimension, size, rows, columns, global, get
2898 
2899 .seealso: MatGetLocalSize()
2900 @*/
2901 int MatGetSize(Mat mat,int *m,int* n)
2902 {
2903   int ierr;
2904 
2905   PetscFunctionBegin;
2906   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2907   ierr = (*mat->ops->getsize)(mat,m,n);CHKERRQ(ierr);
2908   PetscFunctionReturn(0);
2909 }
2910 
2911 #undef __FUNC__
2912 #define __FUNC__ "MatGetLocalSize"
2913 /*@
2914    MatGetLocalSize - Returns the number of rows and columns in a matrix
2915    stored locally.  This information may be implementation dependent, so
2916    use with care.
2917 
2918    Not Collective
2919 
2920    Input Parameters:
2921 .  mat - the matrix
2922 
2923    Output Parameters:
2924 +  m - the number of local rows
2925 -  n - the number of local columns
2926 
2927    Level: beginner
2928 
2929 .keywords: matrix, dimension, size, local, rows, columns, get
2930 
2931 .seealso: MatGetSize()
2932 @*/
2933 int MatGetLocalSize(Mat mat,int *m,int* n)
2934 {
2935   int ierr;
2936 
2937   PetscFunctionBegin;
2938   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2939   ierr = (*mat->ops->getlocalsize)(mat,m,n);CHKERRQ(ierr);
2940   PetscFunctionReturn(0);
2941 }
2942 
2943 #undef __FUNC__
2944 #define __FUNC__ "MatGetOwnershipRange"
2945 /*@
2946    MatGetOwnershipRange - Returns the range of matrix rows owned by
2947    this processor, assuming that the matrix is laid out with the first
2948    n1 rows on the first processor, the next n2 rows on the second, etc.
2949    For certain parallel layouts this range may not be well defined.
2950 
2951    Not Collective
2952 
2953    Input Parameters:
2954 .  mat - the matrix
2955 
2956    Output Parameters:
2957 +  m - the global index of the first local row
2958 -  n - one more than the global index of the last local row
2959 
2960    Level: beginner
2961 
2962 .keywords: matrix, get, range, ownership
2963 @*/
2964 int MatGetOwnershipRange(Mat mat,int *m,int* n)
2965 {
2966   int ierr;
2967 
2968   PetscFunctionBegin;
2969   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2970   PetscValidIntPointer(m);
2971   PetscValidIntPointer(n);
2972   if (!mat->ops->getownershiprange) SETERRQ(PETSC_ERR_SUP,0,"");
2973   ierr = (*mat->ops->getownershiprange)(mat,m,n);CHKERRQ(ierr);
2974   PetscFunctionReturn(0);
2975 }
2976 
2977 #undef __FUNC__
2978 #define __FUNC__ "MatILUFactorSymbolic"
2979 /*@
2980    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
2981    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
2982    to complete the factorization.
2983 
2984    Collective on Mat
2985 
2986    Input Parameters:
2987 +  mat - the matrix
2988 .  row - row permutation
2989 .  column - column permutation
2990 -  info - structure containing
2991 $      levels - number of levels of fill.
2992 $      expected fill - as ratio of original fill.
2993 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
2994                 missing diagonal entries)
2995 
2996    Output Parameters:
2997 .  fact - new matrix that has been symbolically factored
2998 
2999    Notes:
3000    See the users manual for additional information about
3001    choosing the fill factor for better efficiency.
3002 
3003    Most users should employ the simplified SLES interface for linear solvers
3004    instead of working directly with matrix algebra routines such as this.
3005    See, e.g., SLESCreate().
3006 
3007    Level: developer
3008 
3009 .keywords: matrix, factor, incomplete, ILU, symbolic, fill
3010 
3011 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
3012           MatGetOrdering()
3013 
3014 @*/
3015 int MatILUFactorSymbolic(Mat mat,IS row,IS col,MatILUInfo *info,Mat *fact)
3016 {
3017   int ierr;
3018 
3019   PetscFunctionBegin;
3020   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3021   PetscValidPointer(fact);
3022   if (info && info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Levels of fill negative %d",info->levels);
3023   if (info && info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Expected fill less than 1.0 %g",info->fill);
3024   if (!mat->ops->ilufactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,"Only MatCreateMPIRowbs() matrices support parallel ILU");
3025   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
3026   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
3027 
3028   PLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);
3029   ierr = (*mat->ops->ilufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr);
3030   PLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);
3031   PetscFunctionReturn(0);
3032 }
3033 
3034 #undef __FUNC__
3035 #define __FUNC__ "MatIncompleteCholeskyFactorSymbolic"
3036 /*@
3037    MatIncompleteCholeskyFactorSymbolic - Performs symbolic incomplete
3038    Cholesky factorization for a symmetric matrix.  Use
3039    MatCholeskyFactorNumeric() to complete the factorization.
3040 
3041    Collective on Mat
3042 
3043    Input Parameters:
3044 +  mat - the matrix
3045 .  perm - row and column permutation
3046 .  fill - levels of fill
3047 -  f - expected fill as ratio of original fill
3048 
3049    Output Parameter:
3050 .  fact - the factored matrix
3051 
3052    Notes:
3053    Currently only no-fill factorization is supported.
3054 
3055    Most users should employ the simplified SLES interface for linear solvers
3056    instead of working directly with matrix algebra routines such as this.
3057    See, e.g., SLESCreate().
3058 
3059    Level: developer
3060 
3061 .keywords: matrix, factor, incomplete, ICC, Cholesky, symbolic, fill
3062 
3063 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor()
3064 @*/
3065 int MatIncompleteCholeskyFactorSymbolic(Mat mat,IS perm,double f,int fill,Mat *fact)
3066 {
3067   int ierr;
3068 
3069   PetscFunctionBegin;
3070   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3071   PetscValidPointer(fact);
3072   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
3073   if (fill < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Fill negative %d",fill);
3074   if (f < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Expected fill less than 1.0 %g",f);
3075   if (!mat->ops->incompletecholeskyfactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,"Currently only MatCreateMPIRowbs() matrices support ICC in parallel");
3076   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
3077 
3078   PLogEventBegin(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0);
3079   ierr = (*mat->ops->incompletecholeskyfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr);
3080   PLogEventEnd(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0);
3081   PetscFunctionReturn(0);
3082 }
3083 
3084 #undef __FUNC__
3085 #define __FUNC__ "MatGetArray"
3086 /*@C
3087    MatGetArray - Returns a pointer to the element values in the matrix.
3088    The result of this routine is dependent on the underlying matrix data
3089    structure, and may not even work for certain matrix types.  You MUST
3090    call MatRestoreArray() when you no longer need to access the array.
3091 
3092    Not Collective
3093 
3094    Input Parameter:
3095 .  mat - the matrix
3096 
3097    Output Parameter:
3098 .  v - the location of the values
3099 
3100    Currently returns an array only for the dense formats, giving access to
3101    the local portion of the matrix in the usual Fortran column-oriented format.
3102 
3103    Fortran Note:
3104    This routine is used differently from Fortran, e.g.,
3105 .vb
3106         Mat         mat
3107         Scalar      mat_array(1)
3108         PetscOffset i_mat
3109         int         ierr
3110         call MatGetArray(mat,mat_array,i_mat,ierr)
3111 
3112   C  Access first local entry in matrix; note that array is
3113   C  treated as one dimensional
3114         value = mat_array(i_mat + 1)
3115 
3116         [... other code ...]
3117         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3118 .ve
3119 
3120    See the Fortran chapter of the users manual and
3121    petsc/src/mat/examples/tests for details.
3122 
3123    Level: advanced
3124 
3125 .keywords: matrix, array, elements, values
3126 
3127 .seealso: MatRestoreArray(), MatGetArrayF90()
3128 @*/
3129 int MatGetArray(Mat mat,Scalar **v)
3130 {
3131   int ierr;
3132 
3133   PetscFunctionBegin;
3134   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3135   PetscValidPointer(v);
3136   if (!mat->ops->getarray) SETERRQ(PETSC_ERR_SUP,0,"");
3137   ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr);
3138   PetscFunctionReturn(0);
3139 }
3140 
3141 #undef __FUNC__
3142 #define __FUNC__ "MatRestoreArray"
3143 /*@C
3144    MatRestoreArray - Restores the matrix after MatGetArray() has been called.
3145 
3146    Not Collective
3147 
3148    Input Parameter:
3149 +  mat - the matrix
3150 -  v - the location of the values
3151 
3152    Fortran Note:
3153    This routine is used differently from Fortran, e.g.,
3154 .vb
3155         Mat         mat
3156         Scalar      mat_array(1)
3157         PetscOffset i_mat
3158         int         ierr
3159         call MatGetArray(mat,mat_array,i_mat,ierr)
3160 
3161   C  Access first local entry in matrix; note that array is
3162   C  treated as one dimensional
3163         value = mat_array(i_mat + 1)
3164 
3165         [... other code ...]
3166         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3167 .ve
3168 
3169    See the Fortran chapter of the users manual and
3170    petsc/src/mat/examples/tests for details
3171 
3172    Level: advanced
3173 
3174 .keywords: matrix, array, elements, values, restore
3175 
3176 .seealso: MatGetArray(), MatRestoreArrayF90()
3177 @*/
3178 int MatRestoreArray(Mat mat,Scalar **v)
3179 {
3180   int ierr;
3181 
3182   PetscFunctionBegin;
3183   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3184   PetscValidPointer(v);
3185   if (!mat->ops->restorearray) SETERRQ(PETSC_ERR_SUP,0,"");
3186   ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr);
3187   PetscFunctionReturn(0);
3188 }
3189 
3190 #undef __FUNC__
3191 #define __FUNC__ "MatGetSubMatrices"
3192 /*@C
3193    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
3194    points to an array of valid matrices, they may be reused to store the new
3195    submatrices.
3196 
3197    Collective on Mat
3198 
3199    Input Parameters:
3200 +  mat - the matrix
3201 .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
3202 .  irow, icol - index sets of rows and columns to extract
3203 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3204 
3205    Output Parameter:
3206 .  submat - the array of submatrices
3207 
3208    Notes:
3209    MatGetSubMatrices() can extract only sequential submatrices
3210    (from both sequential and parallel matrices). Use MatGetSubMatrix()
3211    to extract a parallel submatrix.
3212 
3213    When extracting submatrices from a parallel matrix, each processor can
3214    form a different submatrix by setting the rows and columns of its
3215    individual index sets according to the local submatrix desired.
3216 
3217    When finished using the submatrices, the user should destroy
3218    them with MatDestroySubMatrices().
3219 
3220    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
3221    original matrix has not changed from that last call to MatGetSubMatrices().
3222 
3223    Fortran Note:
3224    The Fortran interface is slightly different from that given below; it
3225    requires one to pass in  as submat a Mat (integer) array of size at least m.
3226 
3227    Level: advanced
3228 
3229 .keywords: matrix, get, submatrix, submatrices
3230 
3231 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal()
3232 @*/
3233 int MatGetSubMatrices(Mat mat,int n,IS *irow,IS *icol,MatReuse scall,Mat **submat)
3234 {
3235   int ierr;
3236 
3237   PetscFunctionBegin;
3238   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3239   if (!mat->ops->getsubmatrices) SETERRQ(PETSC_ERR_SUP,0,"");
3240   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
3241 
3242   PLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);
3243   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
3244   PLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);
3245 
3246   PetscFunctionReturn(0);
3247 }
3248 
3249 #undef __FUNC__
3250 #define __FUNC__ "MatDestroyMatrices"
3251 /*@C
3252    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
3253 
3254    Collective on Mat
3255 
3256    Input Parameters:
3257 +  n - the number of local matrices
3258 -  mat - the matrices
3259 
3260    Level: advanced
3261 
3262 .keywords: matrix, destroy, submatrix, submatrices
3263 
3264 .seealso: MatGetSubMatrices()
3265 @*/
3266 int MatDestroyMatrices(int n,Mat **mat)
3267 {
3268   int ierr,i;
3269 
3270   PetscFunctionBegin;
3271   if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,1,"Trying to destroy negative number of matrices %d",n);
3272   PetscValidPointer(mat);
3273   for ( i=0; i<n; i++ ) {
3274     ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr);
3275   }
3276   if (n) {ierr = PetscFree(*mat);CHKERRQ(ierr);}
3277   PetscFunctionReturn(0);
3278 }
3279 
3280 #undef __FUNC__
3281 #define __FUNC__ "MatIncreaseOverlap"
3282 /*@
3283    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
3284    replaces the index sets by larger ones that represent submatrices with
3285    additional overlap.
3286 
3287    Collective on Mat
3288 
3289    Input Parameters:
3290 +  mat - the matrix
3291 .  n   - the number of index sets
3292 .  is  - the array of pointers to index sets
3293 -  ov  - the additional overlap requested
3294 
3295    Level: developer
3296 
3297 .keywords: matrix, overlap, Schwarz
3298 
3299 .seealso: MatGetSubMatrices()
3300 @*/
3301 int MatIncreaseOverlap(Mat mat,int n, IS *is,int ov)
3302 {
3303   int ierr;
3304 
3305   PetscFunctionBegin;
3306   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3307   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
3308   if (mat->factor)     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
3309 
3310   if (ov == 0) PetscFunctionReturn(0);
3311   if (!mat->ops->increaseoverlap) SETERRQ(PETSC_ERR_SUP,0,"");
3312   PLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);
3313   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
3314   PLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);
3315   PetscFunctionReturn(0);
3316 }
3317 
3318 #undef __FUNC__
3319 #define __FUNC__ "MatPrintHelp"
3320 /*@
3321    MatPrintHelp - Prints all the options for the matrix.
3322 
3323    Collective on Mat
3324 
3325    Input Parameter:
3326 .  mat - the matrix
3327 
3328    Options Database Keys:
3329 +  -help - Prints matrix options
3330 -  -h - Prints matrix options
3331 
3332    Level: developer
3333 
3334 .keywords: mat, help
3335 
3336 .seealso: MatCreate(), MatCreateXXX()
3337 @*/
3338 int MatPrintHelp(Mat mat)
3339 {
3340   static int called = 0;
3341   int        ierr;
3342   MPI_Comm   comm;
3343 
3344   PetscFunctionBegin;
3345   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3346 
3347   comm = mat->comm;
3348   if (!called) {
3349     ierr = (*PetscHelpPrintf)(comm,"General matrix options:\n");CHKERRQ(ierr);
3350     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_info: view basic matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr);
3351     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_info_detailed: view detailed matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr);
3352     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_draw: draw nonzero matrix structure during MatAssemblyEnd()\n");CHKERRQ(ierr);
3353     ierr = (*PetscHelpPrintf)(comm,"      -draw_pause <sec>: set seconds of display pause\n");CHKERRQ(ierr);
3354     ierr = (*PetscHelpPrintf)(comm,"      -display <name>: set alternate display\n");CHKERRQ(ierr);
3355     called = 1;
3356   }
3357   if (mat->ops->printhelp) {
3358     ierr = (*mat->ops->printhelp)(mat);CHKERRQ(ierr);
3359   }
3360   PetscFunctionReturn(0);
3361 }
3362 
3363 #undef __FUNC__
3364 #define __FUNC__ "MatGetBlockSize"
3365 /*@
3366    MatGetBlockSize - Returns the matrix block size; useful especially for the
3367    block row and block diagonal formats.
3368 
3369    Not Collective
3370 
3371    Input Parameter:
3372 .  mat - the matrix
3373 
3374    Output Parameter:
3375 .  bs - block size
3376 
3377    Notes:
3378    Block diagonal formats are MATSEQBDIAG, MATMPIBDIAG.
3379    Block row formats are MATSEQBAIJ, MATMPIBAIJ
3380 
3381    Level: intermediate
3382 
3383 .keywords: matrix, get, block, size
3384 
3385 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag()
3386 @*/
3387 int MatGetBlockSize(Mat mat,int *bs)
3388 {
3389   int ierr;
3390 
3391   PetscFunctionBegin;
3392   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3393   PetscValidIntPointer(bs);
3394   if (!mat->ops->getblocksize) SETERRQ(PETSC_ERR_SUP,0,"");
3395   ierr = (*mat->ops->getblocksize)(mat,bs);CHKERRQ(ierr);
3396   PetscFunctionReturn(0);
3397 }
3398 
3399 #undef __FUNC__
3400 #define __FUNC__ "MatGetRowIJ"
3401 /*@C
3402     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
3403 
3404    Collective on Mat
3405 
3406     Input Parameters:
3407 +   mat - the matrix
3408 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
3409 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3410                 symmetrized
3411 
3412     Output Parameters:
3413 +   n - number of rows in the (possibly compressed) matrix
3414 .   ia - the row pointers
3415 .   ja - the column indices
3416 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
3417 
3418     Level: developer
3419 
3420 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
3421 @*/
3422 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3423 {
3424   int ierr;
3425 
3426   PetscFunctionBegin;
3427   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3428   if (ia) PetscValidIntPointer(ia);
3429   if (ja) PetscValidIntPointer(ja);
3430   PetscValidIntPointer(done);
3431   if (!mat->ops->getrowij) *done = PETSC_FALSE;
3432   else {
3433     *done = PETSC_TRUE;
3434     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3435   }
3436   PetscFunctionReturn(0);
3437 }
3438 
3439 #undef __FUNC__
3440 #define __FUNC__ "MatGetColumnIJ"
3441 /*@C
3442     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
3443 
3444     Collective on Mat
3445 
3446     Input Parameters:
3447 +   mat - the matrix
3448 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3449 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3450                 symmetrized
3451 
3452     Output Parameters:
3453 +   n - number of columns in the (possibly compressed) matrix
3454 .   ia - the column pointers
3455 .   ja - the row indices
3456 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
3457 
3458     Level: developer
3459 
3460 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
3461 @*/
3462 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3463 {
3464   int ierr;
3465 
3466   PetscFunctionBegin;
3467   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3468   if (ia) PetscValidIntPointer(ia);
3469   if (ja) PetscValidIntPointer(ja);
3470   PetscValidIntPointer(done);
3471 
3472   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
3473   else {
3474     *done = PETSC_TRUE;
3475     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3476   }
3477   PetscFunctionReturn(0);
3478 }
3479 
3480 #undef __FUNC__
3481 #define __FUNC__ "MatRestoreRowIJ"
3482 /*@C
3483     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
3484     MatGetRowIJ().
3485 
3486     Collective on Mat
3487 
3488     Input Parameters:
3489 +   mat - the matrix
3490 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3491 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3492                 symmetrized
3493 
3494     Output Parameters:
3495 +   n - size of (possibly compressed) matrix
3496 .   ia - the row pointers
3497 .   ja - the column indices
3498 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
3499 
3500     Level: developer
3501 
3502 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
3503 @*/
3504 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3505 {
3506   int ierr;
3507 
3508   PetscFunctionBegin;
3509   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3510   if (ia) PetscValidIntPointer(ia);
3511   if (ja) PetscValidIntPointer(ja);
3512   PetscValidIntPointer(done);
3513 
3514   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
3515   else {
3516     *done = PETSC_TRUE;
3517     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3518   }
3519   PetscFunctionReturn(0);
3520 }
3521 
3522 #undef __FUNC__
3523 #define __FUNC__ "MatRestoreColumnIJ"
3524 /*@C
3525     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
3526     MatGetColumnIJ().
3527 
3528     Collective on Mat
3529 
3530     Input Parameters:
3531 +   mat - the matrix
3532 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3533 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3534                 symmetrized
3535 
3536     Output Parameters:
3537 +   n - size of (possibly compressed) matrix
3538 .   ia - the column pointers
3539 .   ja - the row indices
3540 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
3541 
3542     Level: developer
3543 
3544 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
3545 @*/
3546 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3547 {
3548   int ierr;
3549 
3550   PetscFunctionBegin;
3551   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3552   if (ia) PetscValidIntPointer(ia);
3553   if (ja) PetscValidIntPointer(ja);
3554   PetscValidIntPointer(done);
3555 
3556   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
3557   else {
3558     *done = PETSC_TRUE;
3559     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3560   }
3561   PetscFunctionReturn(0);
3562 }
3563 
3564 #undef __FUNC__
3565 #define __FUNC__ "MatColoringPatch"
3566 /*@C
3567     MatColoringPatch -Used inside matrix coloring routines that
3568     use MatGetRowIJ() and/or MatGetColumnIJ().
3569 
3570     Collective on Mat
3571 
3572     Input Parameters:
3573 +   mat - the matrix
3574 .   n   - number of colors
3575 -   colorarray - array indicating color for each column
3576 
3577     Output Parameters:
3578 .   iscoloring - coloring generated using colorarray information
3579 
3580     Level: developer
3581 
3582 .seealso: MatGetRowIJ(), MatGetColumnIJ()
3583 
3584 .keywords: mat, coloring, patch
3585 @*/
3586 int MatColoringPatch(Mat mat,int n,int *colorarray,ISColoring *iscoloring)
3587 {
3588   int ierr;
3589 
3590   PetscFunctionBegin;
3591   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3592   PetscValidIntPointer(colorarray);
3593 
3594   if (!mat->ops->coloringpatch) {SETERRQ(PETSC_ERR_SUP,0,"");}
3595   else {
3596     ierr  = (*mat->ops->coloringpatch)(mat,n,colorarray,iscoloring);CHKERRQ(ierr);
3597   }
3598   PetscFunctionReturn(0);
3599 }
3600 
3601 
3602 #undef __FUNC__
3603 #define __FUNC__ "MatSetUnfactored"
3604 /*@
3605    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
3606 
3607    Collective on Mat
3608 
3609    Input Parameter:
3610 .  mat - the factored matrix to be reset
3611 
3612    Notes:
3613    This routine should be used only with factored matrices formed by in-place
3614    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
3615    format).  This option can save memory, for example, when solving nonlinear
3616    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
3617    ILU(0) preconditioner.
3618 
3619    Note that one can specify in-place ILU(0) factorization by calling
3620 .vb
3621      PCType(pc,PCILU);
3622      PCILUSeUseInPlace(pc);
3623 .ve
3624    or by using the options -pc_type ilu -pc_ilu_in_place
3625 
3626    In-place factorization ILU(0) can also be used as a local
3627    solver for the blocks within the block Jacobi or additive Schwarz
3628    methods (runtime option: -sub_pc_ilu_in_place).  See the discussion
3629    of these preconditioners in the users manual for details on setting
3630    local solver options.
3631 
3632    Most users should employ the simplified SLES interface for linear solvers
3633    instead of working directly with matrix algebra routines such as this.
3634    See, e.g., SLESCreate().
3635 
3636    Level: developer
3637 
3638 .seealso: PCILUSetUseInPlace(), PCLUSetUseInPlace()
3639 
3640 .keywords: matrix-free, in-place ILU, in-place LU
3641 @*/
3642 int MatSetUnfactored(Mat mat)
3643 {
3644   int ierr;
3645 
3646   PetscFunctionBegin;
3647   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3648   mat->factor = 0;
3649   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
3650   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
3651   PetscFunctionReturn(0);
3652 }
3653 
3654 #undef __FUNC__
3655 #define __FUNC__ "MatGetType"
3656 /*@C
3657    MatGetType - Gets the matrix type and name (as a string) from the matrix.
3658 
3659    Not Collective
3660 
3661    Input Parameter:
3662 .  mat - the matrix
3663 
3664    Output Parameter:
3665 +  type - the matrix type (or use PETSC_NULL)
3666 -  name - name of matrix type (or use PETSC_NULL)
3667 
3668    Level: intermediate
3669 
3670 .keywords: matrix, get, type, name
3671 @*/
3672 int MatGetType(Mat mat,MatType *type,char **name)
3673 {
3674   int  itype = (int)mat->type;
3675   char *matname[10];
3676 
3677   PetscFunctionBegin;
3678   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3679 
3680   if (type) *type = (MatType) mat->type;
3681   if (name) {
3682     /* Note:  Be sure that this list corresponds to the enum in mat.h */
3683     matname[0] = "MATSEQDENSE";
3684     matname[1] = "MATSEQAIJ";
3685     matname[2] = "MATMPIAIJ";
3686     matname[3] = "MATSHELL";
3687     matname[4] = "MATMPIROWBS";
3688     matname[5] = "MATSEQBDIAG";
3689     matname[6] = "MATMPIBDIAG";
3690     matname[7] = "MATMPIDENSE";
3691     matname[8] = "MATSEQBAIJ";
3692     matname[9] = "MATMPIBAIJ";
3693 
3694     if (itype < 0 || itype > 9) *name = "Unknown matrix type";
3695     else                        *name = matname[itype];
3696   }
3697   PetscFunctionReturn(0);
3698 }
3699 
3700 /*MC
3701     MatGetArrayF90 - Accesses a matrix array from Fortran90.
3702 
3703     Synopsis:
3704     MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
3705 
3706     Not collective
3707 
3708     Input Parameter:
3709 .   x - matrix
3710 
3711     Output Parameters:
3712 +   xx_v - the Fortran90 pointer to the array
3713 -   ierr - error code
3714 
3715     Example of Usage:
3716 .vb
3717       Scalar, pointer xx_v(:)
3718       ....
3719       call MatGetArrayF90(x,xx_v,ierr)
3720       a = xx_v(3)
3721       call MatRestoreArrayF90(x,xx_v,ierr)
3722 .ve
3723 
3724     Notes:
3725     Not yet supported for all F90 compilers
3726 
3727     Level: advanced
3728 
3729 .seealso:  MatRestoreArrayF90(), MatGetArray(), MatRestoreArray()
3730 
3731 .keywords:  matrix, array, f90
3732 M*/
3733 
3734 /*MC
3735     MatRestoreArrayF90 - Restores a matrix array that has been
3736     accessed with MatGetArrayF90().
3737 
3738     Synopsis:
3739     MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
3740 
3741     Not collective
3742 
3743     Input Parameters:
3744 +   x - matrix
3745 -   xx_v - the Fortran90 pointer to the array
3746 
3747     Output Parameter:
3748 .   ierr - error code
3749 
3750     Example of Usage:
3751 .vb
3752        Scalar, pointer xx_v(:)
3753        ....
3754        call MatGetArrayF90(x,xx_v,ierr)
3755        a = xx_v(3)
3756        call MatRestoreArrayF90(x,xx_v,ierr)
3757 .ve
3758 
3759     Notes:
3760     Not yet supported for all F90 compilers
3761 
3762     Level: advanced
3763 
3764 .seealso:  MatGetArrayF90(), MatGetArray(), MatRestoreArray()
3765 
3766 .keywords:  matrix, array, f90
3767 M*/
3768 
3769 
3770 #undef __FUNC__
3771 #define __FUNC__ "MatGetSubMatrix"
3772 /*@
3773     MatGetSubMatrix - Gets a single submatrix on the same number of processors
3774                       as the original matrix.
3775 
3776     Collective on Mat
3777 
3778     Input Parameters:
3779 +   mat - the original matrix
3780 .   isrow - rows this processor should obtain
3781 .   iscol - columns for all processors you wish to keep
3782 .   csize - number of columns "local" to this processor (does nothing for sequential
3783             matrices). This should match the result from VecGetLocalSize(x,...) if you
3784             plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE
3785 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3786 
3787     Output Parameter:
3788 .   newmat - the new submatrix, of the same type as the old
3789 
3790     Level: advanced
3791 
3792 .keywords: matrix, get, submatrix, submatrices
3793 
3794 .seealso: MatGetSubMatrices()
3795 @*/
3796 int MatGetSubMatrix(Mat mat,IS isrow,IS iscol,int csize,MatReuse cll,Mat *newmat)
3797 {
3798   int     ierr, size;
3799   Mat     *local;
3800 
3801   PetscFunctionBegin;
3802   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
3803 
3804   /* if original matrix is on just one processor then use submatrix generated */
3805   if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
3806     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
3807     PetscFunctionReturn(0);
3808   } else if (!mat->ops->getsubmatrix && size == 1) {
3809     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
3810     *newmat = *local;
3811     ierr = PetscFree(local);CHKERRQ(ierr);
3812     PetscFunctionReturn(0);
3813   }
3814 
3815   if (!mat->ops->getsubmatrix) SETERRQ(PETSC_ERR_SUP,0,"Not currently implemented");
3816   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscol,csize,cll,newmat);CHKERRQ(ierr);
3817   PetscFunctionReturn(0);
3818 }
3819 
3820 #undef __FUNC__
3821 #define __FUNC__ "MatGetMaps"
3822 /*@C
3823    MatGetMaps - Returns the maps associated with the matrix.
3824 
3825    Not Collective
3826 
3827    Input Parameter:
3828 .  mat - the matrix
3829 
3830    Output Parameters:
3831 +  rmap - the row (right) map
3832 -  cmap - the column (left) map
3833 
3834    Level: developer
3835 
3836 .keywords: matrix, get, map
3837 @*/
3838 int MatGetMaps(Mat mat,Map *rmap,Map *cmap)
3839 {
3840   int ierr;
3841 
3842   PetscFunctionBegin;
3843   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3844   ierr = (*mat->ops->getmaps)(mat,rmap,cmap);CHKERRQ(ierr);
3845   PetscFunctionReturn(0);
3846 }
3847 
3848 /*
3849       Version that works for all PETSc matrices
3850 */
3851 #undef __FUNC__
3852 #define __FUNC__ "MatGetMaps_Petsc"
3853 int MatGetMaps_Petsc(Mat mat,Map *rmap,Map *cmap)
3854 {
3855   PetscFunctionBegin;
3856   if (rmap) *rmap = mat->rmap;
3857   if (cmap) *cmap = mat->cmap;
3858   PetscFunctionReturn(0);
3859 }
3860 
3861 #undef __FUNC__
3862 #define __FUNC__ "MatSetStashInitialSize"
3863 /*@
3864    MatSetStashInitialSize - sets the sizes of the matrix stash, that is
3865    used during the assembly process to store values that belong to
3866    other processors.
3867 
3868    Not Collective
3869 
3870    Input Parameters:
3871 +  mat   - the matrix
3872 .  size  - the initial size of the stash.
3873 -  bsize - the initial size of the block-stash(if used).
3874 
3875    Options Database Keys:
3876 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
3877 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
3878 
3879    Level: intermediate
3880 
3881    Notes:
3882      The block-stash is used for values set with VecSetValuesBlocked() while
3883      the stash is used for values set with VecSetValues()
3884 
3885      Run with the option -log_info and look for output of the form
3886      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
3887      to determine the appropriate value, MM, to use for size and
3888      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
3889      to determine the value, BMM to use for bsize
3890 
3891 .keywords: matrix, stash, assembly
3892 @*/
3893 int MatSetStashInitialSize(Mat mat,int size, int bsize)
3894 {
3895   int ierr;
3896 
3897   PetscFunctionBegin;
3898   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3899   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
3900   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
3901   PetscFunctionReturn(0);
3902 }
3903