xref: /petsc/src/mat/interface/matrix.c (revision 49aa8e37a22b7abbc56f3d9c9201c71da5442c50)
1 #ifndef lint
2 static char vcid[] = "$Id: matrix.c,v 1.163 1996/04/05 19:14:03 curfman Exp curfman $";
3 #endif
4 
5 /*
6    This is where the abstract matrix operations are defined
7 */
8 
9 #include "petsc.h"
10 #include "matimpl.h"        /*I "mat.h" I*/
11 #include "vec/vecimpl.h"
12 #include "pinclude/pviewer.h"
13 #include "draw.h"
14 
15 /*@C
16    MatGetReordering - Gets a reordering for a matrix to reduce fill or to
17    improve numerical stability of LU factorization.
18 
19    Input Parameters:
20 .  mat - the matrix
21 .  type - type of reordering, one of the following:
22 $      ORDER_NATURAL - Natural
23 $      ORDER_ND - Nested Dissection
24 $      ORDER_1WD - One-way Dissection
25 $      ORDER_RCM - Reverse Cuthill-McGee
26 $      ORDER_QMD - Quotient Minimum Degree
27 
28    Output Parameters:
29 .  rperm - row permutation indices
30 .  cperm - column permutation indices
31 
32    Options Database Keys:
33    To specify the ordering through the options database, use one of
34    the following
35 $    -mat_order natural, -mat_order nd, -mat_order 1wd,
36 $    -mat_order rcm, -mat_order qmd
37 
38    Notes:
39    If the column permutations and row permutations are the same,
40    then MatGetReordering() returns 0 in cperm.
41 
42    The user can define additional orderings; see MatReorderingRegister().
43 
44 .keywords: matrix, set, ordering, factorization, direct, ILU, LU,
45            fill, reordering, natural, Nested Dissection,
46            One-way Dissection, Cholesky, Reverse Cuthill-McGee,
47            Quotient Minimum Degree
48 
49 .seealso:  MatGetReorderingTypeFromOptions(), MatReorderingRegister()
50 @*/
51 int MatGetReordering(Mat mat,MatOrdering type,IS *rperm,IS *cperm)
52 {
53   int         ierr;
54   PetscValidHeaderSpecific(mat,MAT_COOKIE);
55   if (!mat->assembled) SETERRQ(1,"MatGetReordering:Not for unassembled matrix");
56 
57   if (!mat->ops.getreordering) {*rperm = 0; *cperm = 0; return 0;}
58   PLogEventBegin(MAT_GetReordering,mat,0,0,0);
59   ierr = MatGetReorderingTypeFromOptions(0,&type); CHKERRQ(ierr);
60   ierr = (*mat->ops.getreordering)(mat,type,rperm,cperm); CHKERRQ(ierr);
61   PLogEventEnd(MAT_GetReordering,mat,0,0,0);
62   return 0;
63 }
64 
65 /*@C
66    MatGetRow - Gets a row of a matrix.  You MUST call MatRestoreRow()
67    for each row that you get to ensure that your application does
68    not bleed memory.
69 
70    Input Parameters:
71 .  mat - the matrix
72 .  row - the row to get
73 
74    Output Parameters:
75 .  ncols -  the number of nonzeros in the row
76 .  cols - if nonzero, the column numbers
77 .  vals - if nonzero, the values
78 
79    Notes:
80    This routine is provided for people who need to have direct access
81    to the structure of a matrix.  We hope that we provide enough
82    high-level matrix routines that few users will need it.
83 
84    For better efficiency, set cols and/or vals to PETSC_NULL if you do
85    not wish to extract these quantities.
86 
87    The user can only examine the values extracted with MatGetRow();
88    the values cannot be altered.  To change the matrix entries, one
89    must use MatSetValues().
90 
91    Caution:
92    Do not try to chnage the contents of the output arrays (cols and vals).
93    In some cases, this may corrupt the matrix.
94 
95 .keywords: matrix, row, get, extract
96 
97 .seealso: MatRestoreRow(), MatSetValues()
98 @*/
99 int MatGetRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals)
100 {
101   int   ierr;
102   PetscValidHeaderSpecific(mat,MAT_COOKIE);
103   if (!mat->assembled) SETERRQ(1,"MatGetRow:Not for unassembled matrix");
104   PLogEventBegin(MAT_GetRow,mat,0,0,0);
105   ierr = (*mat->ops.getrow)(mat,row,ncols,cols,vals); CHKERRQ(ierr);
106   PLogEventEnd(MAT_GetRow,mat,0,0,0);
107   return 0;
108 }
109 
110 /*@C
111    MatRestoreRow - Frees any temporary space allocated by MatGetRow().
112 
113    Input Parameters:
114 .  mat - the matrix
115 .  row - the row to get
116 .  ncols, cols - the number of nonzeros and their columns
117 .  vals - if nonzero the column values
118 
119 .keywords: matrix, row, restore
120 
121 .seealso:  MatGetRow()
122 @*/
123 int MatRestoreRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals)
124 {
125   PetscValidHeaderSpecific(mat,MAT_COOKIE);
126   if (!mat->assembled) SETERRQ(1,"MatRestoreRow:Not for unassembled matrix");
127   if (!mat->ops.restorerow) return 0;
128   return (*mat->ops.restorerow)(mat,row,ncols,cols,vals);
129 }
130 /*@
131    MatView - Visualizes a matrix object.
132 
133    Input Parameters:
134 .  mat - the matrix
135 .  ptr - visualization context
136 
137    Notes:
138    The available visualization contexts include
139 $     STDOUT_VIEWER_SELF - standard output (default)
140 $     STDOUT_VIEWER_WORLD - synchronized standard
141 $       output where only the first processor opens
142 $       the file.  All other processors send their
143 $       data to the first processor to print.
144 
145    The user can open alternative vistualization contexts with
146 $    ViewerFileOpenASCII() - output to a specified file
147 $    ViewerFileOpenBinary() - output in binary to a
148 $         specified file; corresponding input uses MatLoad()
149 $    ViewerDrawOpenX() - output nonzero matrix structure to
150 $         an X window display
151 $    ViewerMatlabOpen() - output matrix to Matlab viewer.
152 $         Currently only the sequential dense and AIJ
153 $         matrix types support the Matlab viewer.
154 
155    The user can call ViewerSetFormat() to specify the output
156    format of ASCII printed objects (when using STDOUT_VIEWER_SELF,
157    STDOUT_VIEWER_WORLD and ViewerFileOpenASCII).  Available formats include
158 $    ASCII_FORMAT_DEFAULT - default, prints matrix contents
159 $    ASCII_FORMAT_MATLAB - Matlab format
160 $    ASCII_FORMAT_IMPL - implementation-specific format
161 $      (which is in many cases the same as the default)
162 $    ASCII_FORMAT_INFO - basic information about the matrix
163 $      size and structure (not the matrix entries)
164 $    ASCII_FORMAT_INFO_DETAILED - more detailed information about the
165 $      matrix structure
166 
167 .keywords: matrix, view, visualize, output, print, write, draw
168 
169 .seealso: ViewerSetFormat(), ViewerFileOpenASCII(), ViewerDrawOpenX(),
170           ViewerMatlabOpen(), ViewerFileOpenBinary(), MatLoad()
171 @*/
172 int MatView(Mat mat,Viewer viewer)
173 {
174   int          format, ierr, rows, cols,nz, nzalloc, mem;
175   FILE         *fd;
176   char         *cstr;
177   ViewerType   vtype;
178   MPI_Comm     comm = mat->comm;
179 
180   PetscValidHeaderSpecific(mat,MAT_COOKIE);
181   if (!mat->assembled) SETERRQ(1,"MatView:Not for unassembled matrix");
182 
183   if (!viewer) {
184     viewer = STDOUT_VIEWER_SELF;
185   }
186 
187   ierr = ViewerGetType(viewer,&vtype);
188   if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER) {
189     ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr);
190     ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr);
191     if (format == ASCII_FORMAT_INFO || format == ASCII_FORMAT_INFO_DETAILED) {
192       PetscFPrintf(comm,fd,"Matrix Object:\n");
193       ierr = MatGetType(mat,PETSC_NULL,&cstr); CHKERRQ(ierr);
194       ierr = MatGetSize(mat,&rows,&cols); CHKERRQ(ierr);
195       PetscFPrintf(comm,fd,"  type=%s, rows=%d, cols=%d\n",cstr,rows,cols);
196       if (mat->ops.getinfo) {
197         ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&nz,&nzalloc,&mem); CHKERRQ(ierr);
198         PetscFPrintf(comm,fd,"  total: nonzeros=%d, allocated nonzeros=%d\n",nz,
199                      nzalloc);
200       }
201     }
202   }
203   if (mat->view) {ierr = (*mat->view)((PetscObject)mat,viewer); CHKERRQ(ierr);}
204   return 0;
205 }
206 
207 /*@C
208    MatDestroy - Frees space taken by a matrix.
209 
210    Input Parameter:
211 .  mat - the matrix
212 
213 .keywords: matrix, destroy
214 @*/
215 int MatDestroy(Mat mat)
216 {
217   int ierr;
218   PetscValidHeaderSpecific(mat,MAT_COOKIE);
219   ierr = (*mat->destroy)((PetscObject)mat); CHKERRQ(ierr);
220   return 0;
221 }
222 /*@
223    MatValid - Checks whether a matrix object is valid.
224 
225    Input Parameter:
226 .  m - the matrix to check
227 
228    Output Parameter:
229    flg - flag indicating matrix status, either
230 $     PETSC_TRUE if matrix is valid;
231 $     PETSC_FALSE otherwise.
232 
233 .keywords: matrix, valid
234 @*/
235 int MatValid(Mat m,PetscTruth *flg)
236 {
237   if (!m)                           *flg = PETSC_FALSE;
238   else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE;
239   else                              *flg = PETSC_TRUE;
240   return 0;
241 }
242 
243 /*@
244    MatSetValues - Inserts or adds a block of values into a matrix.
245    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
246    MUST be called after all calls to MatSetValues() have been completed.
247 
248    Input Parameters:
249 .  mat - the matrix
250 .  v - a logically two-dimensional array of values
251 .  m, indexm - the number of rows and their global indices
252 .  n, indexn - the number of columns and their global indices
253 .  addv - either ADD_VALUES or INSERT_VALUES, where
254 $     ADD_VALUES - adds values to any existing entries
255 $     INSERT_VALUES - replaces existing entries with new values
256 
257    Notes:
258    By default the values, v, are row-oriented and unsorted.
259    See MatSetOptions() for other options.
260 
261    Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES
262    options cannot be mixed without intervening calls to the assembly
263    routines.
264 
265 .keywords: matrix, insert, add, set, values
266 
267 .seealso: MatSetOptions(), MatAssemblyBegin(), MatAssemblyEnd()
268 @*/
269 int MatSetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,
270                                                         InsertMode addv)
271 {
272   int ierr;
273   PetscValidHeaderSpecific(mat,MAT_COOKIE);
274   if (mat->assembled) {
275     mat->was_assembled = PETSC_TRUE;
276     mat->assembled     = PETSC_FALSE;
277   }
278   PLogEventBegin(MAT_SetValues,mat,0,0,0);
279   ierr = (*mat->ops.setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr);
280   PLogEventEnd(MAT_SetValues,mat,0,0,0);
281   return 0;
282 }
283 
284 /*@
285    MatGetValues - Gets a block of values from a matrix.
286 
287    Input Parameters:
288 .  mat - the matrix
289 .  v - a logically two-dimensional array for storing the values
290 .  m, indexm - the number of rows and their global indices
291 .  n, indexn - the number of columns and their global indices
292 
293    Notes:
294    The user must allocate space (m*n Scalars) for the values, v.
295    The values, v, are then returned in a row-oriented format,
296    analogous to that used by default in MatSetValues().
297 
298 .keywords: matrix, get, values
299 
300 .seealso: MatGetRow(), MatGetSubmatrix(), MatGetSubmatrices(), MatSetValues()
301 @*/
302 int MatGetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v)
303 {
304   int ierr;
305 
306   PetscValidHeaderSpecific(mat,MAT_COOKIE);
307   if (!mat->assembled) SETERRQ(1,"MatGetValues:Not for unassembled matrix");
308 
309   PLogEventBegin(MAT_GetValues,mat,0,0,0);
310   ierr = (*mat->ops.getvalues)(mat,m,idxm,n,idxn,v); CHKERRQ(ierr);
311   PLogEventEnd(MAT_GetValues,mat,0,0,0);
312   return 0;
313 }
314 
315 /* --------------------------------------------------------*/
316 /*@
317    MatMult - Computes matrix-vector product.
318 
319    Input Parameters:
320 .  mat - the matrix
321 .  x   - the vector to be multilplied
322 
323    Output Parameters:
324 .  y - the result
325 
326 .keywords: matrix, multiply, matrix-vector product
327 
328 .seealso: MatMultTrans(), MatMultAdd(), MatMultTransAdd()
329 @*/
330 int MatMult(Mat mat,Vec x,Vec y)
331 {
332   int ierr;
333   PetscValidHeaderSpecific(mat,MAT_COOKIE);
334   PetscValidHeaderSpecific(x,VEC_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE);
335   if (!mat->assembled) SETERRQ(1,"MatMult:Not for unassembled matrix");
336   if (x == y) SETERRQ(1,"MatMult:x and y must be different vectors");
337 
338   PLogEventBegin(MAT_Mult,mat,x,y,0);
339   ierr = (*mat->ops.mult)(mat,x,y); CHKERRQ(ierr);
340   PLogEventEnd(MAT_Mult,mat,x,y,0);
341   return 0;
342 }
343 /*@
344    MatMultTrans - Computes matrix transpose times a vector.
345 
346    Input Parameters:
347 .  mat - the matrix
348 .  x   - the vector to be multilplied
349 
350    Output Parameters:
351 .  y - the result
352 
353 .keywords: matrix, multiply, matrix-vector product, transpose
354 
355 .seealso: MatMult(), MatMultAdd(), MatMultTransAdd()
356 @*/
357 int MatMultTrans(Mat mat,Vec x,Vec y)
358 {
359   int ierr;
360   PetscValidHeaderSpecific(mat,MAT_COOKIE);
361   PetscValidHeaderSpecific(x,VEC_COOKIE); PetscValidHeaderSpecific(y,VEC_COOKIE);
362   if (!mat->assembled) SETERRQ(1,"MatMultTrans:Not for unassembled matrix");
363   if (x == y) SETERRQ(1,"MatMultTrans:x and y must be different vectors");
364 
365   PLogEventBegin(MAT_MultTrans,mat,x,y,0);
366   ierr = (*mat->ops.multtrans)(mat,x,y); CHKERRQ(ierr);
367   PLogEventEnd(MAT_MultTrans,mat,x,y,0);
368   return 0;
369 }
370 /*@
371     MatMultAdd -  Computes v3 = v2 + A * v1.
372 
373   Input Parameters:
374 .    mat - the matrix
375 .    v1, v2 - the vectors
376 
377   Output Parameters:
378 .    v3 - the result
379 
380 .keywords: matrix, multiply, matrix-vector product, add
381 
382 .seealso: MatMultTrans(), MatMult(), MatMultTransAdd()
383 @*/
384 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3)
385 {
386   int ierr;
387   PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v1,VEC_COOKIE);
388   PetscValidHeaderSpecific(v2,VEC_COOKIE); PetscValidHeaderSpecific(v3,VEC_COOKIE);
389   if (!mat->assembled) SETERRQ(1,"MatMultAdd:Not for unassembled matrix");
390 
391   PLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);
392   if (v1 == v3) SETERRQ(1,"MatMultAdd:v1 and v3 must be different vectors");
393   ierr = (*mat->ops.multadd)(mat,v1,v2,v3); CHKERRQ(ierr);
394   PLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);
395   return 0;
396 }
397 /*@
398     MatMultTransAdd - Computes v3 = v2 + A' * v1.
399 
400   Input Parameters:
401 .    mat - the matrix
402 .    v1, v2 - the vectors
403 
404   Output Parameters:
405 .    v3 - the result
406 
407 .keywords: matrix, multiply, matrix-vector product, transpose, add
408 
409 .seealso: MatMultTrans(), MatMultAdd(), MatMult()
410 @*/
411 int MatMultTransAdd(Mat mat,Vec v1,Vec v2,Vec v3)
412 {
413   int ierr;
414   PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v1,VEC_COOKIE);
415   PetscValidHeaderSpecific(v2,VEC_COOKIE);PetscValidHeaderSpecific(v3,VEC_COOKIE);
416   if (!mat->assembled) SETERRQ(1,"MatMultTransAdd:Not for unassembled matrix");
417   if (!mat->ops.multtransadd) SETERRQ(PETSC_ERR_SUP,"MatMultTransAdd");
418   if (v1 == v3) SETERRQ(1,"MatMultTransAdd:v1 and v2 must be different vectors");
419 
420   PLogEventBegin(MAT_MultTransAdd,mat,v1,v2,v3);
421   ierr = (*mat->ops.multtransadd)(mat,v1,v2,v3); CHKERRQ(ierr);
422   PLogEventEnd(MAT_MultTransAdd,mat,v1,v2,v3);
423   return 0;
424 }
425 /* ------------------------------------------------------------*/
426 /*@C
427    MatGetInfo - Returns information about matrix storage (number of
428    nonzeros, memory).
429 
430    Input Parameters:
431 .  mat - the matrix
432 
433    Output Parameters:
434 .  flag - flag indicating the type of parameters to be returned
435 $    flag = MAT_LOCAL: local matrix
436 $    flag = MAT_GLOBAL_MAX: maximum over all processors
437 $    flag = MAT_GLOBAL_SUM: sum over all processors
438 .   nz - the number of nonzeros [or PETSC_NULL]
439 .   nzalloc - the number of allocated nonzeros [or PETSC_NULL]
440 .   mem - the memory used (in bytes)  [or PETSC_NULL]
441 
442 .keywords: matrix, get, info, storage, nonzeros, memory
443 @*/
444 int MatGetInfo(Mat mat,MatInfoType flag,int *nz,int *nzalloc,int *mem)
445 {
446   PetscValidHeaderSpecific(mat,MAT_COOKIE);
447   if (!mat->ops.getinfo) SETERRQ(PETSC_ERR_SUP,"MatGetInfo");
448   return  (*mat->ops.getinfo)(mat,flag,nz,nzalloc,mem);
449 }
450 /* ----------------------------------------------------------*/
451 /*@
452    MatLUFactor - Performs in-place LU factorization of matrix.
453 
454    Input Parameters:
455 .  mat - the matrix
456 .  row - row permutation
457 .  col - column permutation
458 .  f - expected fill as ratio of original fill.
459 
460 .keywords: matrix, factor, LU, in-place
461 
462 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
463 @*/
464 int MatLUFactor(Mat mat,IS row,IS col,double f)
465 {
466   int ierr;
467   PetscValidHeaderSpecific(mat,MAT_COOKIE);
468   if (!mat->ops.lufactor) SETERRQ(PETSC_ERR_SUP,"MatLUFactor");
469   if (!mat->assembled) SETERRQ(1,"MatLUFactor:Not for unassembled matrix");
470 
471   PLogEventBegin(MAT_LUFactor,mat,row,col,0);
472   ierr = (*mat->ops.lufactor)(mat,row,col,f); CHKERRQ(ierr);
473   PLogEventEnd(MAT_LUFactor,mat,row,col,0);
474   return 0;
475 }
476 /*@
477    MatILUFactor - Performs in-place ILU factorization of matrix.
478 
479    Input Parameters:
480 .  mat - the matrix
481 .  row - row permutation
482 .  col - column permutation
483 .  f - expected fill as ratio of original fill.
484 .  level - number of levels of fill.
485 
486    Note: probably really only in-place when level is zero.
487 .keywords: matrix, factor, ILU, in-place
488 
489 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
490 @*/
491 int MatILUFactor(Mat mat,IS row,IS col,double f,int level)
492 {
493   int ierr;
494   PetscValidHeaderSpecific(mat,MAT_COOKIE);
495   if (!mat->ops.ilufactor) SETERRQ(PETSC_ERR_SUP,"MatILUFactor");
496   if (!mat->assembled) SETERRQ(1,"MatILUFactor:Not for unassembled matrix");
497 
498   PLogEventBegin(MAT_ILUFactor,mat,row,col,0);
499   ierr = (*mat->ops.ilufactor)(mat,row,col,f,level); CHKERRQ(ierr);
500   PLogEventEnd(MAT_ILUFactor,mat,row,col,0);
501   return 0;
502 }
503 
504 /*@
505    MatLUFactorSymbolic - Performs symbolic LU factorization of matrix.
506    Call this routine before calling MatLUFactorNumeric().
507 
508    Input Parameters:
509 .  mat - the matrix
510 .  row, col - row and column permutations
511 .  f - expected fill as ratio of the original number of nonzeros,
512        for example 3.0; choosing this parameter well can result in
513        more efficient use of time and space.
514 
515    Output Parameter:
516 .  fact - new matrix that has been symbolically factored
517 
518    Options Database Key:
519 $     -mat_lu_fill <f>, where f is the fill ratio
520 
521    Notes:
522    See the file $(PETSC_DIR)/Performace for additional information about
523    choosing the fill factor for better efficiency.
524 
525 .keywords: matrix, factor, LU, symbolic, fill
526 
527 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor()
528 @*/
529 int MatLUFactorSymbolic(Mat mat,IS row,IS col,double f,Mat *fact)
530 {
531   int ierr,flg;
532   PetscValidHeaderSpecific(mat,MAT_COOKIE);
533   if (!fact) SETERRQ(1,"MatLUFactorSymbolic:Missing factor matrix argument");
534   if (!mat->ops.lufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"MatLUFactorSymbolic");
535   if (!mat->assembled) SETERRQ(1,"MatLUFactorSymbolic:Not for unassembled matrix");
536 
537   ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_fill",&f,&flg); CHKERRQ(ierr);
538   PLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);
539   ierr = (*mat->ops.lufactorsymbolic)(mat,row,col,f,fact); CHKERRQ(ierr);
540   PLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);
541   return 0;
542 }
543 /*@
544    MatLUFactorNumeric - Performs numeric LU factorization of a matrix.
545    Call this routine after first calling MatLUFactorSymbolic().
546 
547    Input Parameters:
548 .  mat - the matrix
549 .  row, col - row and  column permutations
550 
551    Output Parameters:
552 .  fact - symbolically factored matrix that must have been generated
553           by MatLUFactorSymbolic()
554 
555    Notes:
556    See MatLUFactor() for in-place factorization.  See
557    MatCholeskyFactorNumeric() for the symmetric, positive definite case.
558 
559 .keywords: matrix, factor, LU, numeric
560 
561 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor()
562 @*/
563 int MatLUFactorNumeric(Mat mat,Mat *fact)
564 {
565   int ierr,flg;
566 
567   PetscValidHeaderSpecific(mat,MAT_COOKIE);
568   if (!fact) SETERRQ(1,"MatLUFactorNumeric:Missing factor matrix argument");
569   if (!mat->ops.lufactornumeric) SETERRQ(PETSC_ERR_SUP,"MatLUFactorNumeric");
570   if (!mat->assembled) SETERRQ(1,"MatLUFactorNumeric:Not for unassembled matrix");
571 
572   PLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0);
573   ierr = (*mat->ops.lufactornumeric)(mat,fact); CHKERRQ(ierr);
574   PLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0);
575   ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg); CHKERRQ(ierr);
576   if (flg) {
577     Viewer  viewer;
578     ierr = ViewerDrawOpenX((*fact)->comm,0,0,0,0,300,300,&viewer);CHKERRQ(ierr);
579     ierr = MatView(*fact,viewer); CHKERRQ(ierr);
580     ierr = ViewerFlush(viewer); CHKERRQ(ierr);
581     ierr = ViewerDestroy(viewer); CHKERRQ(ierr);
582   }
583   return 0;
584 }
585 /*@
586    MatCholeskyFactor - Performs in-place Cholesky factorization of a
587    symmetric matrix.
588 
589    Input Parameters:
590 .  mat - the matrix
591 .  perm - row and column permutations
592 .  f - expected fill as ratio of original fill
593 
594    Notes:
595    See MatLUFactor() for the nonsymmetric case.  See also
596    MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric().
597 
598 .keywords: matrix, factor, in-place, Cholesky
599 
600 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric()
601 @*/
602 int MatCholeskyFactor(Mat mat,IS perm,double f)
603 {
604   int ierr;
605   PetscValidHeaderSpecific(mat,MAT_COOKIE);
606   if (!mat->ops.choleskyfactor) SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactor");
607   if (!mat->assembled) SETERRQ(1,"MatCholeskyFactor:Not for unassembled matrix");
608 
609   PLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);
610   ierr = (*mat->ops.choleskyfactor)(mat,perm,f); CHKERRQ(ierr);
611   PLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);
612   return 0;
613 }
614 /*@
615    MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization
616    of a symmetric matrix.
617 
618    Input Parameters:
619 .  mat - the matrix
620 .  perm - row and column permutations
621 .  f - expected fill as ratio of original
622 
623    Output Parameter:
624 .  fact - the factored matrix
625 
626    Notes:
627    See MatLUFactorSymbolic() for the nonsymmetric case.  See also
628    MatCholeskyFactor() and MatCholeskyFactorNumeric().
629 
630 .keywords: matrix, factor, factorization, symbolic, Cholesky
631 
632 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric()
633 @*/
634 int MatCholeskyFactorSymbolic(Mat mat,IS perm,double f,Mat *fact)
635 {
636   int ierr;
637   PetscValidHeaderSpecific(mat,MAT_COOKIE);
638   if (!fact) SETERRQ(1,"MatCholeskyFactorSymbolic:Missing factor matrix argument");
639   if (!mat->ops.choleskyfactorsymbolic)SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactorSymbolic");
640   if (!mat->assembled) SETERRQ(1,"MatCholeskyFactorSymbolic:Not for unassembled matrix");
641 
642   PLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);
643   ierr = (*mat->ops.choleskyfactorsymbolic)(mat,perm,f,fact); CHKERRQ(ierr);
644   PLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);
645   return 0;
646 }
647 /*@
648    MatCholeskyFactorNumeric - Performs numeric Cholesky factorization
649    of a symmetric matrix. Call this routine after first calling
650    MatCholeskyFactorSymbolic().
651 
652    Input Parameter:
653 .  mat - the initial matrix
654 
655    Output Parameter:
656 .  fact - the factored matrix
657 
658 .keywords: matrix, factor, numeric, Cholesky
659 
660 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric()
661 @*/
662 int MatCholeskyFactorNumeric(Mat mat,Mat *fact)
663 {
664   int ierr;
665   PetscValidHeaderSpecific(mat,MAT_COOKIE);
666   if (!fact) SETERRQ(1,"MatCholeskyFactorNumeric:Missing factor matrix argument");
667   if (!mat->ops.choleskyfactornumeric) SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactorNumeric");
668   if (!mat->assembled) SETERRQ(1,"MatCholeskyFactorNumeric:Not for unassembled matrix");
669 
670   PLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0);
671   ierr = (*mat->ops.choleskyfactornumeric)(mat,fact); CHKERRQ(ierr);
672   PLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0);
673   return 0;
674 }
675 /* ----------------------------------------------------------------*/
676 /*@
677    MatSolve - Solves A x = b, given a factored matrix.
678 
679    Input Parameters:
680 .  mat - the factored matrix
681 .  b - the right-hand-side vector
682 
683    Output Parameter:
684 .  x - the result vector
685 
686 .keywords: matrix, linear system, solve, LU, Cholesky, triangular solve
687 
688 .seealso: MatSolveAdd(), MatSolveTrans(), MatSolveTransAdd()
689 @*/
690 int MatSolve(Mat mat,Vec b,Vec x)
691 {
692   int ierr;
693   PetscValidHeaderSpecific(mat,MAT_COOKIE);
694   PetscValidHeaderSpecific(b,VEC_COOKIE);  PetscValidHeaderSpecific(x,VEC_COOKIE);
695   if (x == b) SETERRQ(1,"MatSolve:x and y must be different vectors");
696   if (!mat->factor) SETERRQ(1,"MatSolve:Unfactored matrix");
697 
698   if (!mat->ops.solve) SETERRQ(PETSC_ERR_SUP,"MatSolve");
699   PLogEventBegin(MAT_Solve,mat,b,x,0);
700   ierr = (*mat->ops.solve)(mat,b,x); CHKERRQ(ierr);
701   PLogEventEnd(MAT_Solve,mat,b,x,0);
702   return 0;
703 }
704 
705 /* @
706    MatForwardSolve - Solves L x = b, given a factored matrix, A = LU.
707 
708    Input Parameters:
709 .  mat - the factored matrix
710 .  b - the right-hand-side vector
711 
712    Output Parameter:
713 .  x - the result vector
714 
715    Notes:
716    MatSolve() should be used for most applications, as it performs
717    a forward solve followed by a backward solve.
718 
719 .keywords: matrix, forward, LU, Cholesky, triangular solve
720 
721 .seealso: MatSolve(), MatBackwardSolve()
722 @ */
723 int MatForwardSolve(Mat mat,Vec b,Vec x)
724 {
725   int ierr;
726   PetscValidHeaderSpecific(mat,MAT_COOKIE);
727   PetscValidHeaderSpecific(b,VEC_COOKIE);  PetscValidHeaderSpecific(x,VEC_COOKIE);
728   if (x == b) SETERRQ(1,"MatForwardSolve:x and y must be different vectors");
729   if (!mat->factor) SETERRQ(1,"MatForwardSolve:Unfactored matrix");
730   if (!mat->ops.forwardsolve) SETERRQ(PETSC_ERR_SUP,"MatForwardSolve");
731 
732   PLogEventBegin(MAT_ForwardSolve,mat,b,x,0);
733   ierr = (*mat->ops.forwardsolve)(mat,b,x); CHKERRQ(ierr);
734   PLogEventEnd(MAT_ForwardSolve,mat,b,x,0);
735   return 0;
736 }
737 
738 /* @
739    MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU.
740 
741    Input Parameters:
742 .  mat - the factored matrix
743 .  b - the right-hand-side vector
744 
745    Output Parameter:
746 .  x - the result vector
747 
748    Notes:
749    MatSolve() should be used for most applications, as it performs
750    a forward solve followed by a backward solve.
751 
752 .keywords: matrix, backward, LU, Cholesky, triangular solve
753 
754 .seealso: MatSolve(), MatForwardSolve()
755 @ */
756 int MatBackwardSolve(Mat mat,Vec b,Vec x)
757 {
758   int ierr;
759   PetscValidHeaderSpecific(mat,MAT_COOKIE);
760   PetscValidHeaderSpecific(b,VEC_COOKIE);  PetscValidHeaderSpecific(x,VEC_COOKIE);
761   if (x == b) SETERRQ(1,"MatBackwardSolve:x and b must be different vectors");
762   if (!mat->factor) SETERRQ(1,"MatBackwardSolve:Unfactored matrix");
763   if (!mat->ops.backwardsolve) SETERRQ(PETSC_ERR_SUP,"MatBackwardSolve");
764 
765   PLogEventBegin(MAT_BackwardSolve,mat,b,x,0);
766   ierr = (*mat->ops.backwardsolve)(mat,b,x); CHKERRQ(ierr);
767   PLogEventEnd(MAT_BackwardSolve,mat,b,x,0);
768   return 0;
769 }
770 
771 /*@
772    MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix.
773 
774    Input Parameters:
775 .  mat - the factored matrix
776 .  b - the right-hand-side vector
777 .  y - the vector to be added to
778 
779    Output Parameter:
780 .  x - the result vector
781 
782 .keywords: matrix, linear system, solve, LU, Cholesky, add
783 
784 .seealso: MatSolve(), MatSolveTrans(), MatSolveTransAdd()
785 @*/
786 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x)
787 {
788   Scalar one = 1.0;
789   Vec    tmp;
790   int    ierr;
791   PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE);
792   PetscValidHeaderSpecific(b,VEC_COOKIE);  PetscValidHeaderSpecific(x,VEC_COOKIE);
793   if (x == b) SETERRQ(1,"MatSolveAdd:x and b must be different vectors");
794   if (!mat->factor) SETERRQ(1,"MatSolveAdd:Unfactored matrix");
795 
796   PLogEventBegin(MAT_SolveAdd,mat,b,x,y);
797   if (mat->ops.solveadd)  {
798     ierr = (*mat->ops.solveadd)(mat,b,y,x); CHKERRQ(ierr);
799   }
800   else {
801     /* do the solve then the add manually */
802     if (x != y) {
803       ierr = MatSolve(mat,b,x); CHKERRQ(ierr);
804       ierr = VecAXPY(&one,y,x); CHKERRQ(ierr);
805     }
806     else {
807       ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr);
808       PLogObjectParent(mat,tmp);
809       ierr = VecCopy(x,tmp); CHKERRQ(ierr);
810       ierr = MatSolve(mat,b,x); CHKERRQ(ierr);
811       ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr);
812       ierr = VecDestroy(tmp); CHKERRQ(ierr);
813     }
814   }
815   PLogEventEnd(MAT_SolveAdd,mat,b,x,y);
816   return 0;
817 }
818 /*@
819    MatSolveTrans - Solves A' x = b, given a factored matrix.
820 
821    Input Parameters:
822 .  mat - the factored matrix
823 .  b - the right-hand-side vector
824 
825    Output Parameter:
826 .  x - the result vector
827 
828 .keywords: matrix, linear system, solve, LU, Cholesky, transpose
829 
830 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransAdd()
831 @*/
832 int MatSolveTrans(Mat mat,Vec b,Vec x)
833 {
834   int ierr;
835   PetscValidHeaderSpecific(mat,MAT_COOKIE);
836   PetscValidHeaderSpecific(b,VEC_COOKIE);  PetscValidHeaderSpecific(x,VEC_COOKIE);
837   if (!mat->factor) SETERRQ(1,"MatSolveTrans:Unfactored matrix");
838   if (x == b) SETERRQ(1,"MatSolveTrans:x and b must be different vectors");
839   if (!mat->ops.solvetrans) SETERRQ(PETSC_ERR_SUP,"MatSolveTrans");
840 
841   PLogEventBegin(MAT_SolveTrans,mat,b,x,0);
842   ierr = (*mat->ops.solvetrans)(mat,b,x); CHKERRQ(ierr);
843   PLogEventEnd(MAT_SolveTrans,mat,b,x,0);
844   return 0;
845 }
846 /*@
847    MatSolveTransAdd - Computes x = y + inv(trans(A)) b, given a
848                       factored matrix.
849 
850    Input Parameters:
851 .  mat - the factored matrix
852 .  b - the right-hand-side vector
853 .  y - the vector to be added to
854 
855    Output Parameter:
856 .  x - the result vector
857 
858 .keywords: matrix, linear system, solve, LU, Cholesky, transpose, add
859 
860 .seealso: MatSolve(), MatSolveAdd(), MatSolveTrans()
861 @*/
862 int MatSolveTransAdd(Mat mat,Vec b,Vec y,Vec x)
863 {
864   Scalar one = 1.0;
865   int    ierr;
866   Vec    tmp;
867   PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE);
868   PetscValidHeaderSpecific(b,VEC_COOKIE);  PetscValidHeaderSpecific(x,VEC_COOKIE);
869   if (x == b) SETERRQ(1,"MatSolveTransAdd:x and b must be different vectors");
870   if (!mat->factor) SETERRQ(1,"MatSolveTransAdd:Unfactored matrix");
871 
872   PLogEventBegin(MAT_SolveTransAdd,mat,b,x,y);
873   if (mat->ops.solvetransadd) {
874     ierr = (*mat->ops.solvetransadd)(mat,b,y,x); CHKERRQ(ierr);
875   }
876   else {
877     /* do the solve then the add manually */
878     if (x != y) {
879       ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr);
880       ierr = VecAXPY(&one,y,x); CHKERRQ(ierr);
881     }
882     else {
883       ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr);
884       PLogObjectParent(mat,tmp);
885       ierr = VecCopy(x,tmp); CHKERRQ(ierr);
886       ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr);
887       ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr);
888       ierr = VecDestroy(tmp); CHKERRQ(ierr);
889     }
890   }
891   PLogEventEnd(MAT_SolveTransAdd,mat,b,x,y);
892   return 0;
893 }
894 /* ----------------------------------------------------------------*/
895 
896 /*@
897    MatRelax - Computes one relaxation sweep.
898 
899    Input Parameters:
900 .  mat - the matrix
901 .  b - the right hand side
902 .  omega - the relaxation factor
903 .  flag - flag indicating the type of SOR, one of
904 $     SOR_FORWARD_SWEEP
905 $     SOR_BACKWARD_SWEEP
906 $     SOR_SYMMETRIC_SWEEP (SSOR method)
907 $     SOR_LOCAL_FORWARD_SWEEP
908 $     SOR_LOCAL_BACKWARD_SWEEP
909 $     SOR_LOCAL_SYMMETRIC_SWEEP (local SSOR)
910 $     SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies
911 $       upper/lower triangular part of matrix to
912 $       vector (with omega)
913 $     SOR_ZERO_INITIAL_GUESS - zero initial guess
914 .  shift -  diagonal shift
915 .  its - the number of iterations
916 
917    Output Parameters:
918 .  x - the solution (can contain an initial guess)
919 
920    Notes:
921    SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and
922    SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings
923    on each processor.
924 
925    Application programmers will not generally use MatRelax() directly,
926    but instead will employ the SLES/PC interface.
927 
928    Notes for Advanced Users:
929    The flags are implemented as bitwise inclusive or operations.
930    For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP)
931    to specify a zero initial guess for SSOR.
932 
933 .keywords: matrix, relax, relaxation, sweep
934 @*/
935 int MatRelax(Mat mat,Vec b,double omega,MatSORType flag,double shift,
936              int its,Vec x)
937 {
938   int ierr;
939   PetscValidHeaderSpecific(mat,MAT_COOKIE);
940   PetscValidHeaderSpecific(b,VEC_COOKIE);  PetscValidHeaderSpecific(x,VEC_COOKIE);
941   if (!mat->ops.relax) SETERRQ(PETSC_ERR_SUP,"MatRelax");
942   if (!mat->assembled) SETERRQ(1,"MatRelax:Not for unassembled matrix");
943 
944   PLogEventBegin(MAT_Relax,mat,b,x,0);
945   ierr =(*mat->ops.relax)(mat,b,omega,flag,shift,its,x); CHKERRQ(ierr);
946   PLogEventEnd(MAT_Relax,mat,b,x,0);
947   return 0;
948 }
949 
950 /*
951       Default matrix copy routine.
952 */
953 int MatCopy_Basic(Mat A,Mat B)
954 {
955   int    ierr,i,rstart,rend,nz,*cwork;
956   Scalar *vwork;
957 
958   ierr = MatZeroEntries(B); CHKERRQ(ierr);
959   ierr = MatGetOwnershipRange(A,&rstart,&rend); CHKERRQ(ierr);
960   for (i=rstart; i<rend; i++) {
961     ierr = MatGetRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr);
962     ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES); CHKERRQ(ierr);
963     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr);
964   }
965   ierr = MatAssemblyBegin(B,FINAL_ASSEMBLY); CHKERRQ(ierr);
966   ierr = MatAssemblyEnd(B,FINAL_ASSEMBLY); CHKERRQ(ierr);
967   return 0;
968 }
969 
970 /*@C
971    MatCopy - Copys a matrix to another matrix.
972 
973    Input Parameters:
974 .  A - the matrix
975 
976    Output Parameter:
977 .  B - where the copy is put
978 
979    Notes:
980    MatCopy() copies the matrix entries of a matrix to another existing
981    matrix (after first zeroing the second matrix).  A related routine is
982    MatConvert(), which first creates a new matrix and then copies the data.
983 
984 .keywords: matrix, copy, convert
985 
986 .seealso: MatConvert()
987 @*/
988 int MatCopy(Mat A,Mat B)
989 {
990   int ierr;
991   PetscValidHeaderSpecific(A,MAT_COOKIE);PetscValidHeaderSpecific(B,MAT_COOKIE);
992   if (!A->assembled) SETERRQ(1,"MatCopy:Not for unassembled matrix");
993 
994   PLogEventBegin(MAT_Copy,A,B,0,0);
995   if (A->ops.copy) {
996     ierr = (*A->ops.copy)(A,B); CHKERRQ(ierr);
997   }
998   else { /* generic conversion */
999     ierr = MatCopy_Basic(A,B); CHKERRQ(ierr);
1000   }
1001   PLogEventEnd(MAT_Copy,A,B,0,0);
1002   return 0;
1003 }
1004 
1005 /*@C
1006    MatConvert - Converts a matrix to another matrix, either of the same
1007    or different type.
1008 
1009    Input Parameters:
1010 .  mat - the matrix
1011 .  newtype - new matrix type.  Use MATSAME to create a new matrix of the
1012    same type as the original matrix.
1013 
1014    Output Parameter:
1015 .  M - pointer to place new matrix
1016 
1017    Notes:
1018    MatConvert() first creates a new matrix and then copies the data from
1019    the first matrix.  A related routine is MatCopy(), which copies the matrix
1020    entries of one matrix to another already existing matrix context.
1021 
1022 .keywords: matrix, copy, convert
1023 
1024 .seealso: MatCopy()
1025 @*/
1026 int MatConvert(Mat mat,MatType newtype,Mat *M)
1027 {
1028   int ierr;
1029   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1030   if (!M) SETERRQ(1,"MatConvert:Bad new matrix address");
1031   if (!mat->assembled) SETERRQ(1,"MatConvert:Not for unassembled matrix");
1032 
1033   PLogEventBegin(MAT_Convert,mat,0,0,0);
1034   if (newtype == mat->type || newtype == MATSAME) {
1035     if (mat->ops.convertsametype) { /* customized copy */
1036       ierr = (*mat->ops.convertsametype)(mat,M,COPY_VALUES); CHKERRQ(ierr);
1037     }
1038   }
1039   else if (mat->ops.convert) { /* customized conversion */
1040     ierr = (*mat->ops.convert)(mat,newtype,M); CHKERRQ(ierr);
1041   }
1042   else { /* generic conversion */
1043     ierr = MatConvert_Basic(mat,newtype,M); CHKERRQ(ierr);
1044   }
1045   PLogEventEnd(MAT_Convert,mat,0,0,0);
1046   return 0;
1047 }
1048 
1049 /*@
1050    MatGetDiagonal - Gets the diagonal of a matrix.
1051 
1052    Input Parameters:
1053 .  mat - the matrix
1054 .  v - the vector for storing the diagonal
1055 
1056    Output Parameter:
1057 .  v - the diagonal of the matrix
1058 
1059 .keywords: matrix, get, diagonal
1060 @*/
1061 int MatGetDiagonal(Mat mat,Vec v)
1062 {
1063   PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v,VEC_COOKIE);
1064   if (!mat->assembled) SETERRQ(1,"MatGetDiagonal:Not for unassembled matrix");
1065   if (mat->ops.getdiagonal) return (*mat->ops.getdiagonal)(mat,v);
1066   SETERRQ(PETSC_ERR_SUP,"MatGetDiagonal");
1067 }
1068 
1069 /*@C
1070    MatTranspose - Computes an in-place or out-of-place transpose of a matrix.
1071 
1072    Input Parameter:
1073 .  mat - the matrix to transpose
1074 
1075    Output Parameters:
1076 .  B - the transpose (or pass in PETSC_NULL for an in-place transpose)
1077 
1078 .keywords: matrix, transpose
1079 @*/
1080 int MatTranspose(Mat mat,Mat *B)
1081 {
1082   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1083   if (!mat->assembled) SETERRQ(1,"MatTranspose:Not for unassembled matrix");
1084   if (mat->ops.transpose) return (*mat->ops.transpose)(mat,B);
1085   SETERRQ(PETSC_ERR_SUP,"MatTranspose");
1086 }
1087 
1088 /*@
1089    MatEqual - Compares two matrices.
1090 
1091    Input Parameters:
1092 .  mat1 - the first matrix
1093 .  mat2 - the second matrix
1094 
1095    Output Parameter:
1096 .  flg : PETSC_TRUE if the matrices are equal;
1097          PETSC_FALSE otherwise.
1098 
1099 .keywords: matrix, equal, equivalent
1100 @*/
1101 int MatEqual(Mat mat1,Mat mat2,PetscTruth *flg)
1102 {
1103   PetscValidHeaderSpecific(mat1,MAT_COOKIE); PetscValidHeaderSpecific(mat2,MAT_COOKIE);
1104   if (!mat1->assembled) SETERRQ(1,"MatEqual:Not for unassembled matrix");
1105   if (!mat2->assembled) SETERRQ(1,"MatEqual:Not for unassembled matrix");
1106   if (mat1->ops.equal) return (*mat1->ops.equal)(mat1,mat2, flg);
1107   SETERRQ(PETSC_ERR_SUP,"MatEqual");
1108 }
1109 
1110 /*@
1111    MatDiagonalScale - Scales a matrix on the left and right by diagonal
1112    matrices that are stored as vectors.  Either of the two scaling
1113    matrices can be null.
1114 
1115    Input Parameters:
1116 .  mat - the matrix to be scaled
1117 .  l - the left scaling vector (or PETSC_NULL)
1118 .  r - the right scaling vector (or PETSC_NULL)
1119 
1120 .keywords: matrix, diagonal, scale
1121 
1122 .seealso: MatDiagonalScale()
1123 @*/
1124 int MatDiagonalScale(Mat mat,Vec l,Vec r)
1125 {
1126   int ierr;
1127   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1128   if (!mat->ops.scale) SETERRQ(PETSC_ERR_SUP,"MatDiagonalScale");
1129   if (l) PetscValidHeaderSpecific(l,VEC_COOKIE);
1130   if (r) PetscValidHeaderSpecific(r,VEC_COOKIE);
1131   if (!mat->assembled) SETERRQ(1,"MatDiagonalScale:Not for unassembled matrix");
1132 
1133   PLogEventBegin(MAT_Scale,mat,0,0,0);
1134   ierr = (*mat->ops.diagonalscale)(mat,l,r); CHKERRQ(ierr);
1135   PLogEventEnd(MAT_Scale,mat,0,0,0);
1136   return 0;
1137 }
1138 
1139 /*@
1140     MatScale - Scales all elements of a matrix by a given number.
1141 
1142     Input Parameters:
1143 .   mat - the matrix to be scaled
1144 .   a  - the scaling value
1145 
1146     Output Parameter:
1147 .   mat - the scaled matrix
1148 
1149 .keywords: matrix, scale
1150 
1151 .seealso: MatDiagonalScale()
1152 @*/
1153 int MatScale(Scalar *a,Mat mat)
1154 {
1155   int ierr;
1156   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1157   if (!mat->ops.scale) SETERRQ(PETSC_ERR_SUP,"MatScale");
1158   if (!mat->assembled) SETERRQ(1,"MatScale:Not for unassembled matrix");
1159 
1160   PLogEventBegin(MAT_Scale,mat,0,0,0);
1161   ierr = (*mat->ops.scale)(a,mat); CHKERRQ(ierr);
1162   PLogEventEnd(MAT_Scale,mat,0,0,0);
1163   return 0;
1164 }
1165 
1166 /*@
1167    MatNorm - Calculates various norms of a matrix.
1168 
1169    Input Parameters:
1170 .  mat - the matrix
1171 .  type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY
1172 
1173    Output Parameters:
1174 .  norm - the resulting norm
1175 
1176 .keywords: matrix, norm, Frobenius
1177 @*/
1178 int MatNorm(Mat mat,NormType type,double *norm)
1179 {
1180   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1181   if (!norm) SETERRQ(1,"MatNorm:bad addess for value");
1182   if (!mat->assembled) SETERRQ(1,"MatNorm:Not for unassembled matrix");
1183   if (mat->ops.norm) return (*mat->ops.norm)(mat,type,norm);
1184   SETERRQ(PETSC_ERR_SUP,"MatNorm:Not for this matrix type");
1185 }
1186 
1187 /*@
1188    MatAssemblyBegin - Begins assembling the matrix.  This routine should
1189    be called after completing all calls to MatSetValues().
1190 
1191    Input Parameters:
1192 .  mat - the matrix
1193 .  type - type of assembly, either FLUSH_ASSEMBLY or FINAL_ASSEMBLY
1194 
1195    Notes:
1196    MatSetValues() generally caches the values.  The matrix is ready to
1197    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
1198    Use FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
1199    in MatSetValues(); use FINAL_ASSEMBLY for the final assembly before
1200    using the matrix.
1201 
1202 .keywords: matrix, assembly, assemble, begin
1203 
1204 .seealso: MatAssemblyEnd(), MatSetValues()
1205 @*/
1206 int MatAssemblyBegin(Mat mat,MatAssemblyType type)
1207 {
1208   int ierr;
1209   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1210   PLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);
1211   if (mat->ops.assemblybegin){ierr = (*mat->ops.assemblybegin)(mat,type);CHKERRQ(ierr);}
1212   PLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);
1213   return 0;
1214 }
1215 
1216 /*@
1217    MatAssemblyEnd - Completes assembling the matrix.  This routine should
1218    be called after all calls to MatSetValues() and after MatAssemblyBegin().
1219 
1220    Input Parameters:
1221 .  mat - the matrix
1222 .  type - type of assembly, either FLUSH_ASSEMBLY or FINAL_ASSEMBLY
1223 
1224    Options Database Keys:
1225 $  -mat_view_info : Prints info on matrix at
1226 $      conclusion of MatEndAssembly()
1227 $  -mat_view_info_detailed: Prints more detailed info.
1228 $  -mat_view : Prints matrix in ASCII format.
1229 $  -mat_view_matlab : Prints matrix in Matlab format.
1230 $  -mat_view_draw : Draws nonzero structure of matrix,
1231 $      using MatView() and DrawOpenX().
1232 $  -display <name> : Set display name (default is host)
1233 $  -draw_pause <sec> : Set number of seconds to pause after display
1234 
1235    Notes:
1236    MatSetValues() generally caches the values.  The matrix is ready to
1237    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
1238    Use FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
1239    in MatSetValues(); use FINAL_ASSEMBLY for the final assembly before
1240    using the matrix.
1241 
1242 .keywords: matrix, assembly, assemble, end
1243 
1244 .seealso: MatAssemblyBegin(), MatSetValues(), DrawOpenX(), MatView()
1245 @*/
1246 int MatAssemblyEnd(Mat mat,MatAssemblyType type)
1247 {
1248   int        ierr,flg;
1249   static int inassm = 0;
1250 
1251   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1252   inassm++;
1253   PLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);
1254   if (mat->ops.assemblyend) {ierr = (*mat->ops.assemblyend)(mat,type); CHKERRQ(ierr);}
1255   mat->assembled = PETSC_TRUE; mat->num_ass++;
1256   PLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);
1257 
1258   if (inassm == 1) {
1259     ierr = OptionsHasName(PETSC_NULL,"-mat_view_info",&flg); CHKERRQ(ierr);
1260     if (flg) {
1261       Viewer viewer;
1262       ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr);
1263       ierr = ViewerSetFormat(viewer,ASCII_FORMAT_INFO,0);CHKERRQ(ierr);
1264       ierr = MatView(mat,viewer); CHKERRQ(ierr);
1265       ierr = ViewerDestroy(viewer); CHKERRQ(ierr);
1266     }
1267     ierr = OptionsHasName(PETSC_NULL,"-mat_view_info_detailed",&flg);CHKERRQ(ierr);
1268     if (flg) {
1269       Viewer viewer;
1270       ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr);
1271       ierr = ViewerSetFormat(viewer,ASCII_FORMAT_INFO_DETAILED,0);CHKERRQ(ierr);
1272       ierr = MatView(mat,viewer); CHKERRQ(ierr);
1273       ierr = ViewerDestroy(viewer); CHKERRQ(ierr);
1274     }
1275     ierr = OptionsHasName(PETSC_NULL,"-mat_view",&flg); CHKERRQ(ierr);
1276     if (flg) {
1277       Viewer viewer;
1278       ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr);
1279       ierr = MatView(mat,viewer); CHKERRQ(ierr);
1280       ierr = ViewerDestroy(viewer); CHKERRQ(ierr);
1281     }
1282     ierr = OptionsHasName(PETSC_NULL,"-mat_view_matlab",&flg); CHKERRQ(ierr);
1283     if (flg) {
1284       Viewer viewer;
1285       ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr);
1286       ierr = ViewerSetFormat(viewer,ASCII_FORMAT_MATLAB,"M");CHKERRQ(ierr);
1287       ierr = MatView(mat,viewer); CHKERRQ(ierr);
1288       ierr = ViewerDestroy(viewer); CHKERRQ(ierr);
1289     }
1290     ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg); CHKERRQ(ierr);
1291     if (flg) {
1292       Viewer    viewer;
1293       ierr = ViewerDrawOpenX(mat->comm,0,0,0,0,300,300,&viewer); CHKERRQ(ierr);
1294       ierr = MatView(mat,viewer); CHKERRQ(ierr);
1295       ierr = ViewerFlush(viewer); CHKERRQ(ierr);
1296       ierr = ViewerDestroy(viewer); CHKERRQ(ierr);
1297     }
1298   }
1299   inassm--;
1300   return 0;
1301 }
1302 
1303 /*@
1304    MatCompress - Tries to store the matrix in as little space as
1305    possible.  May fail if memory is already fully used, since it
1306    tries to allocate new space.
1307 
1308    Input Parameters:
1309 .  mat - the matrix
1310 
1311 .keywords: matrix, compress
1312 @*/
1313 int MatCompress(Mat mat)
1314 {
1315   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1316   if (mat->ops.compress) return (*mat->ops.compress)(mat);
1317   return 0;
1318 }
1319 /*@
1320    MatSetOption - Sets a parameter option for a matrix. Some options
1321    may be specific to certain storage formats.  Some options
1322    determine how values will be inserted (or added). Sorted,
1323    row-oriented input will generally assemble the fastest. The default
1324    is row-oriented, nonsorted input.
1325 
1326    Input Parameters:
1327 .  mat - the matrix
1328 .  option - the option, one of the following:
1329 $    ROW_ORIENTED
1330 $    COLUMN_ORIENTED,
1331 $    ROWS_SORTED,
1332 $    COLUMNS_SORTED,
1333 $    NO_NEW_NONZERO_LOCATIONS,
1334 $    YES_NEW_NONZERO_LOCATIONS,
1335 $    SYMMETRIC_MATRIX,
1336 $    STRUCTURALLY_SYMMETRIC_MATRIX,
1337 $    NO_NEW_DIAGONALS,
1338 $    YES_NEW_DIAGONALS,
1339 $    and possibly others.
1340 
1341    Notes:
1342    Some options are relevant only for particular matrix types and
1343    are thus ignored by others.  Other options are not supported by
1344    certain matrix types and will generate an error message if set.
1345 
1346    If using a Fortran 77 module to compute a matrix, one may need to
1347    use the column-oriented option (or convert to the row-oriented
1348    format).
1349 
1350    NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion
1351    that will generate a new entry in the nonzero structure is ignored.
1352    What this means is if memory is not allocated for this particular
1353    lot, then the insertion is ignored. For dense matrices, where
1354    the entire array is allocated, no entries are ever ignored.
1355 
1356 .keywords: matrix, option, row-oriented, column-oriented, sorted, nonzero
1357 @*/
1358 int MatSetOption(Mat mat,MatOption op)
1359 {
1360   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1361   if (mat->ops.setoption) return (*mat->ops.setoption)(mat,op);
1362   return 0;
1363 }
1364 
1365 /*@
1366    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
1367    this routine retains the old nonzero structure.
1368 
1369    Input Parameters:
1370 .  mat - the matrix
1371 
1372 .keywords: matrix, zero, entries
1373 
1374 .seealso: MatZeroRows()
1375 @*/
1376 int MatZeroEntries(Mat mat)
1377 {
1378   int ierr;
1379   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1380   if (!mat->ops.zeroentries) SETERRQ(PETSC_ERR_SUP,"MatZeroEntries");
1381 
1382   PLogEventBegin(MAT_ZeroEntries,mat,0,0,0);
1383   ierr = (*mat->ops.zeroentries)(mat); CHKERRQ(ierr);
1384   PLogEventEnd(MAT_ZeroEntries,mat,0,0,0);
1385   return 0;
1386 }
1387 
1388 /*@
1389    MatZeroRows - Zeros all entries (except possibly the main diagonal)
1390    of a set of rows of a matrix.
1391 
1392    Input Parameters:
1393 .  mat - the matrix
1394 .  is - index set of rows to remove
1395 .  diag - pointer to value put in all diagonals of eliminated rows.
1396           Note that diag is not a pointer to an array, but merely a
1397           pointer to a single value.
1398 
1399    Notes:
1400    For the AIJ matrix formats this removes the old nonzero structure,
1401    but does not release memory.  For the dense and block diagonal
1402    formats this does not alter the nonzero structure.
1403 
1404    The user can set a value in the diagonal entry (or for the AIJ and
1405    row formats can optionally remove the main diagonal entry from the
1406    nonzero structure as well, by passing a null pointer as the final
1407    argument).
1408 
1409 .keywords: matrix, zero, rows, boundary conditions
1410 
1411 .seealso: MatZeroEntries(), MatGetSubMatrix(), MatGetSubMatrixInPlace()
1412 @*/
1413 int MatZeroRows(Mat mat,IS is, Scalar *diag)
1414 {
1415   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1416   if (!mat->assembled) SETERRQ(1,"MatZeroRows:Not for unassembled matrix");
1417   if (mat->ops.zerorows) return (*mat->ops.zerorows)(mat,is,diag);
1418   SETERRQ(PETSC_ERR_SUP,"MatZeroRows");
1419 }
1420 
1421 /*@
1422    MatGetSize - Returns the numbers of rows and columns in a matrix.
1423 
1424    Input Parameter:
1425 .  mat - the matrix
1426 
1427    Output Parameters:
1428 .  m - the number of global rows
1429 .  n - the number of global columns
1430 
1431 .keywords: matrix, dimension, size, rows, columns, global, get
1432 
1433 .seealso: MatGetLocalSize()
1434 @*/
1435 int MatGetSize(Mat mat,int *m,int* n)
1436 {
1437   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1438   if (!m || !n) SETERRQ(1,"MatGetSize:Bad address for result");
1439   return (*mat->ops.getsize)(mat,m,n);
1440 }
1441 
1442 /*@
1443    MatGetLocalSize - Returns the number of rows and columns in a matrix
1444    stored locally.  This information may be implementation dependent, so
1445    use with care.
1446 
1447    Input Parameters:
1448 .  mat - the matrix
1449 
1450    Output Parameters:
1451 .  m - the number of local rows
1452 .  n - the number of local columns
1453 
1454 .keywords: matrix, dimension, size, local, rows, columns, get
1455 
1456 .seealso: MatGetSize()
1457 @*/
1458 int MatGetLocalSize(Mat mat,int *m,int* n)
1459 {
1460   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1461   if (!m || !n) SETERRQ(1,"MatGetLocalSize:Bad address for result");
1462   return (*mat->ops.getlocalsize)(mat,m,n);
1463 }
1464 
1465 /*@
1466    MatGetOwnershipRange - Returns the range of matrix rows owned by
1467    this processor, assuming that the matrix is laid out with the first
1468    n1 rows on the first processor, the next n2 rows on the second, etc.
1469    For certain parallel layouts this range may not be well-defined.
1470 
1471    Input Parameters:
1472 .  mat - the matrix
1473 
1474    Output Parameters:
1475 .  m - the first local row
1476 .  n - one more then the last local row
1477 
1478 .keywords: matrix, get, range, ownership
1479 @*/
1480 int MatGetOwnershipRange(Mat mat,int *m,int* n)
1481 {
1482   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1483   if (!m || !n) SETERRQ(1,"MatGetOwnershipRange:Bad address for result");
1484   if (mat->ops.getownershiprange) return (*mat->ops.getownershiprange)(mat,m,n);
1485   SETERRQ(PETSC_ERR_SUP,"MatGetOwnershipRange");
1486 }
1487 
1488 /*@
1489    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
1490    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
1491    to complete the factorization.
1492 
1493    Input Parameters:
1494 .  mat - the matrix
1495 .  row - row permutation
1496 .  column - column permutation
1497 .  fill - number of levels of fill
1498 .  f - expected fill as ratio of the original number of nonzeros,
1499        for example 3.0; choosing this parameter well can result in
1500        more efficient use of time and space.
1501 
1502    Output Parameters:
1503 .  fact - new matrix that has been symbolically factored
1504 
1505    Options Database Key:
1506 $   -mat_ilu_fill <f>, where f is the fill ratio
1507 
1508    Notes:
1509    See the file $(PETSC_DIR)/Performace for additional information about
1510    choosing the fill factor for better efficiency.
1511 
1512 .keywords: matrix, factor, incomplete, ILU, symbolic, fill
1513 
1514 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric()
1515 @*/
1516 int MatILUFactorSymbolic(Mat mat,IS row,IS col,double f,int fill,Mat *fact)
1517 {
1518   int ierr,flg;
1519 
1520   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1521   if (fill < 0) SETERRQ(1,"MatILUFactorSymbolic:Levels of fill negative");
1522   if (!fact) SETERRQ(1,"MatILUFactorSymbolic:Fact argument is missing");
1523   if (!mat->ops.ilufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"MatILUFactorSymbolic");
1524   if (!mat->assembled) SETERRQ(1,"MatILUFactorSymbolic:Not for unassembled matrix");
1525 
1526   ierr = OptionsGetDouble(PETSC_NULL,"-mat_ilu_fill",&f,&flg); CHKERRQ(ierr);
1527   PLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);
1528   ierr = (*mat->ops.ilufactorsymbolic)(mat,row,col,f,fill,fact); CHKERRQ(ierr);
1529   PLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);
1530   return 0;
1531 }
1532 
1533 /*@
1534    MatIncompleteCholeskyFactorSymbolic - Performs symbolic incomplete
1535    Cholesky factorization for a symmetric matrix.  Use
1536    MatCholeskyFactorNumeric() to complete the factorization.
1537 
1538    Input Parameters:
1539 .  mat - the matrix
1540 .  perm - row and column permutation
1541 .  fill - levels of fill
1542 .  f - expected fill as ratio of original fill
1543 
1544    Output Parameter:
1545 .  fact - the factored matrix
1546 
1547    Note:  Currently only no-fill factorization is supported.
1548 
1549 .keywords: matrix, factor, incomplete, ICC, Cholesky, symbolic, fill
1550 
1551 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor()
1552 @*/
1553 int MatIncompleteCholeskyFactorSymbolic(Mat mat,IS perm,double f,int fill,
1554                                         Mat *fact)
1555 {
1556   int ierr;
1557   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1558   if (fill < 0) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Fill negative");
1559   if (!fact) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Missing fact argument");
1560   if (!mat->ops.incompletecholeskyfactorsymbolic)
1561      SETERRQ(PETSC_ERR_SUP,"MatIncompleteCholeskyFactorSymbolic");
1562   if (!mat->assembled)
1563      SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Not for unassembled matrix");
1564 
1565   PLogEventBegin(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0);
1566   ierr = (*mat->ops.incompletecholeskyfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr);
1567   PLogEventEnd(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0);
1568   return 0;
1569 }
1570 
1571 /*@C
1572    MatGetArray - Returns a pointer to the element values in the matrix.
1573    This routine  is implementation dependent, and may not even work for
1574    certain matrix types. You MUST call MatRestoreArray() when you no
1575    longer need to access the array.
1576 
1577    Input Parameter:
1578 .  mat - the matrix
1579 
1580    Output Parameter:
1581 .  v - the location of the values
1582 
1583    Fortran Note:
1584    The Fortran interface is slightly different from that given below.
1585    See the users manual and petsc/src/mat/examples for details.
1586 
1587 .keywords: matrix, array, elements, values
1588 
1589 .seeaols: MatRestoreArray()
1590 @*/
1591 int MatGetArray(Mat mat,Scalar **v)
1592 {
1593   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1594   if (!v) SETERRQ(1,"MatGetArray:Bad input, array pointer location");
1595   if (!mat->ops.getarray) SETERRQ(PETSC_ERR_SUP,"MatGetArray");
1596   return (*mat->ops.getarray)(mat,v);
1597 }
1598 
1599 /*@C
1600    MatRestoreArray - Restores the matrix after MatGetArray has been called.
1601 
1602    Input Parameter:
1603 .  mat - the matrix
1604 .  v - the location of the values
1605 
1606    Fortran Note:
1607    The Fortran interface is slightly different from that given below.
1608    See the users manual and petsc/src/mat/examples for details.
1609 
1610 .keywords: matrix, array, elements, values, resrore
1611 
1612 .seealso: MatGetArray()
1613 @*/
1614 int MatRestoreArray(Mat mat,Scalar **v)
1615 {
1616   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1617   if (!v) SETERRQ(1,"MatRestoreArray:Bad input, array pointer location");
1618   if (!mat->ops.restorearray) SETERRQ(PETSC_ERR_SUP,"MatResroreArray");
1619   return (*mat->ops.restorearray)(mat,v);
1620 }
1621 
1622 /*@C
1623    MatGetSubMatrix - Extracts a submatrix from a matrix. If submat points
1624                      to a valid matrix, it may be reused.
1625 
1626    Input Parameters:
1627 .  mat - the matrix
1628 .  irow, icol - index sets of rows and columns to extract
1629 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
1630 
1631    Output Parameter:
1632 .  submat - the submatrix
1633 
1634    Notes:
1635    MatGetSubMatrix() can be useful in setting boundary conditions.
1636 
1637    Use MatGetSubMatrices() to extract multiple submatrices.
1638 
1639 .keywords: matrix, get, submatrix, boundary conditions
1640 
1641 .seealso: MatZeroRows(), MatGetSubMatrixInPlace(), MatGetSubMatrices()
1642 @*/
1643 int MatGetSubMatrix(Mat mat,IS irow,IS icol,MatGetSubMatrixCall scall,Mat *submat)
1644 {
1645   int ierr;
1646   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1647   if (scall == MAT_REUSE_MATRIX) {
1648     PetscValidHeaderSpecific(*submat,MAT_COOKIE);
1649   }
1650   if (!mat->ops.getsubmatrix) SETERRQ(PETSC_ERR_SUP,"MatGetSubMatrix");
1651   if (!mat->assembled) SETERRQ(1,"MatGetSubMatrix:Not for unassembled matrix");
1652 
1653   /* PLogEventBegin(MAT_GetSubMatrix,mat,irow,icol,0); */
1654   ierr = (*mat->ops.getsubmatrix)(mat,irow,icol,scall,submat); CHKERRQ(ierr);
1655   /* PLogEventEnd(MAT_GetSubMatrix,mat,irow,icol,0); */
1656   return 0;
1657 }
1658 
1659 /*@C
1660    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
1661    points to an array of valid matrices, it may be reused.
1662 
1663    Input Parameters:
1664 .  mat - the matrix
1665 .  irow, icol - index sets of rows and columns to extract
1666 
1667    Output Parameter:
1668 .  submat - the submatrices
1669 
1670    Note:
1671    Use MatGetSubMatrix() for extracting a sinble submatrix.
1672 
1673 .keywords: matrix, get, submatrix, submatrices
1674 
1675 .seealso: MatGetSubMatrix()
1676 @*/
1677 int MatGetSubMatrices(Mat mat,int n, IS *irow,IS *icol,MatGetSubMatrixCall scall,
1678                       Mat **submat)
1679 {
1680   int ierr;
1681   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1682   if (!mat->ops.getsubmatrices) SETERRQ(PETSC_ERR_SUP,"MatGetSubMatrices");
1683   if (!mat->assembled) SETERRQ(1,"MatGetSubMatrices:Not for unassembled matrix");
1684 
1685   PLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);
1686   ierr = (*mat->ops.getsubmatrices)(mat,n,irow,icol,scall,submat); CHKERRQ(ierr);
1687   PLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);
1688   return 0;
1689 }
1690 
1691 /*@
1692    MatGetSubMatrixInPlace - Extracts a submatrix from a matrix, returning
1693    the submatrix in place of the original matrix.
1694 
1695    Input Parameters:
1696 .  mat - the matrix
1697 .  irow, icol - index sets of rows and columns to extract
1698 
1699 .keywords: matrix, get, submatrix, boundary conditions, in-place
1700 
1701 .seealso: MatZeroRows(), MatGetSubMatrix()
1702 @*/
1703 int MatGetSubMatrixInPlace(Mat mat,IS irow,IS icol)
1704 {
1705   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1706   if (!mat->assembled) SETERRQ(1,"MatGetSubMatrixInPlace:Not for unassembled matrix");
1707 
1708   if (!mat->ops.getsubmatrixinplace) SETERRQ(PETSC_ERR_SUP,"MatGetSubmatrixInPlace");
1709   return (*mat->ops.getsubmatrixinplace)(mat,irow,icol);
1710 }
1711 
1712 /*@
1713    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
1714    replaces the index by larger ones that represent submatrices with more
1715    overlap.
1716 
1717    Input Parameters:
1718 .  mat - the matrix
1719 .  n   - the number of index sets
1720 .  is  - the array of pointers to index sets
1721 .  ov  - the additional overlap requested
1722 
1723 .keywords: matrix, overlap, Schwarz
1724 
1725 .seealso: MatGetSubMatrices()
1726 @*/
1727 int MatIncreaseOverlap(Mat mat,int n, IS *is, int ov)
1728 {
1729   int ierr;
1730   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1731   if (!mat->assembled) SETERRQ(1,"MatIncreaseOverlap:Not for unassembled matrix");
1732 
1733   if (ov == 0) return 0;
1734   if (!mat->ops.increaseoverlap) SETERRQ(PETSC_ERR_SUP,"MatIncreaseOverlap");
1735   PLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);
1736   ierr = (*mat->ops.increaseoverlap)(mat,n,is,ov); CHKERRQ(ierr);
1737   PLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);
1738   return 0;
1739 }
1740 
1741 /*@
1742    MatPrintHelp - Prints all the options for the matrix.
1743 
1744    Input Parameter:
1745 .  mat - the matrix
1746 
1747    Options Database Keys:
1748 $  -help, -h
1749 
1750 .keywords: mat, help
1751 
1752 .seealso: MatCreate(), MatCreateXXX()
1753 @*/
1754 int MatPrintHelp(Mat mat)
1755 {
1756   static int called = 0;
1757   MPI_Comm   comm = mat->comm;
1758 
1759   if (!called) {
1760     PetscPrintf(comm,"General matrix options:\n");
1761     PetscPrintf(comm,"  -mat_view_info : view basic matrix info during MatAssemblyEnd()\n");
1762     PetscPrintf(comm,"  -mat_view_info_detailed : view detailed matrix info during MatAssemblyEnd()\n");
1763     PetscPrintf(comm,"  -mat_view_draw : draw nonzero matrix structure during MatAssemblyEnd()\n");
1764     PetscPrintf(comm,"      -draw_pause <sec> : set seconds of display pause\n");
1765     PetscPrintf(comm,"      -display <name> : set alternate display\n");
1766     called = 1;
1767   }
1768   if (mat->ops.printhelp) (*mat->ops.printhelp)(mat);
1769   return 0;
1770 }
1771 
1772