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