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