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