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