xref: /petsc/src/mat/interface/matrix.c (revision ca33770f16d7f5276f65b694500ffbe3af051cbe)
1 #ifndef lint
2 static char vcid[] = "$Id: matrix.c,v 1.137 1996/02/07 19:42:58 bsmith Exp curfman $";
3 #endif
4 
5 /*
6    This is where the abstract matrix operations are defined
7 */
8 
9 #include "petsc.h"
10 #include "matimpl.h"        /*I "mat.h" I*/
11 #include "vec/vecimpl.h"
12 #include "pinclude/pviewer.h"
13 #include "draw.h"
14 
15 /*@C
16    MatGetReordering - Gets a reordering for a matrix to reduce fill or to
17    improve numerical stability of LU factorization.
18 
19    Input Parameters:
20 .  mat - the matrix
21 .  type - type of reordering, one of the following:
22 $      ORDER_NATURAL - Natural
23 $      ORDER_ND - Nested Dissection
24 $      ORDER_1WD - One-way Dissection
25 $      ORDER_RCM - Reverse Cuthill-McGee
26 $      ORDER_QMD - Quotient Minimum Degree
27 
28    Output Parameters:
29 .  rperm - row permutation indices
30 .  cperm - column permutation indices
31 
32    Options Database Keys:
33    To specify the ordering through the options database, use one of
34    the following
35 $    -mat_order natural, -mat_order nd, -mat_order 1wd,
36 $    -mat_order rcm, -mat_order qmd
37 
38    Notes:
39    If the column permutations and row permutations are the same,
40    then MatGetReordering() returns 0 in cperm.
41 
42    The user can define additional orderings; see MatReorderingRegister().
43 
44 .keywords: matrix, set, ordering, factorization, direct, ILU, LU,
45            fill, reordering, natural, Nested Dissection,
46            One-way Dissection, Cholesky, Reverse Cuthill-McGee,
47            Quotient Minimum Degree
48 
49 .seealso:  MatGetReorderingTypeFromOptions(), MatReorderingRegister()
50 @*/
51 int MatGetReordering(Mat mat,MatOrdering type,IS *rperm,IS *cperm)
52 {
53   int         ierr;
54   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
55   if (!mat->assembled) SETERRQ(1,"MatGetReordering:Not for unassembled matrix");
56 
57   if (!mat->ops.getreordering) {*rperm = 0; *cperm = 0; return 0;}
58   PLogEventBegin(MAT_GetReordering,mat,0,0,0);
59   ierr = MatGetReorderingTypeFromOptions(0,&type); CHKERRQ(ierr);
60   ierr = (*mat->ops.getreordering)(mat,type,rperm,cperm); CHKERRQ(ierr);
61   PLogEventEnd(MAT_GetReordering,mat,0,0,0);
62   return 0;
63 }
64 
65 /*@C
66    MatGetRow - Gets a row of a matrix.  You MUST call MatRestoreRow()
67    for each row that you get to ensure that your application does
68    not bleed memory.
69 
70    Input Parameters:
71 .  mat - the matrix
72 .  row - the row to get
73 
74    Output Parameters:
75 .  ncols -  the number of nonzeros in the row
76 .  cols - if nonzero, the column numbers
77 .  vals - if nonzero, the values
78 
79    Notes:
80    This routine is provided for people who need to have direct access
81    to the structure of a matrix.  We hope that we provide enough
82    high-level matrix routines that few users will need it.
83 
84    For better efficiency, set cols and/or vals to 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    Returns:
1073    Returns 1 if the matrices are equal; returns 0 otherwise.
1074 
1075 .keywords: matrix, equal, equivalent
1076 @*/
1077 int MatEqual(Mat mat1,Mat mat2)
1078 {
1079   PETSCVALIDHEADERSPECIFIC(mat1,MAT_COOKIE); PETSCVALIDHEADERSPECIFIC(mat2,MAT_COOKIE);
1080   if (!mat1->assembled) SETERRQ(1,"MatEqual:Not for unassembled matrix");
1081   if (!mat2->assembled) SETERRQ(1,"MatEqual:Not for unassembled matrix");
1082   if (mat1->ops.equal) return (*mat1->ops.equal)(mat1,mat2);
1083   SETERRQ(PETSC_ERR_SUP,"MatEqual");
1084 }
1085 
1086 /*@
1087    MatDiagonalScale - Scales a matrix on the left and right by diagonal
1088    matrices that are stored as vectors.  Either of the two scaling
1089    matrices can be null.
1090 
1091    Input Parameters:
1092 .  mat - the matrix to be scaled
1093 .  l - the left scaling vector
1094 .  r - the right scaling vector
1095 
1096 .keywords: matrix, scale
1097 @*/
1098 int MatDiagonalScale(Mat mat,Vec l,Vec r)
1099 {
1100   int ierr;
1101   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1102   if (!mat->ops.scale) SETERRQ(PETSC_ERR_SUP,"MatDiagonalScale");
1103   if (l) PETSCVALIDHEADERSPECIFIC(l,VEC_COOKIE);
1104   if (r) PETSCVALIDHEADERSPECIFIC(r,VEC_COOKIE);
1105   if (!mat->assembled) SETERRQ(1,"MatDiagonalScale:Not for unassembled matrix");
1106 
1107   PLogEventBegin(MAT_Scale,mat,0,0,0);
1108   ierr = (*mat->ops.diagonalscale)(mat,l,r); CHKERRQ(ierr);
1109   PLogEventEnd(MAT_Scale,mat,0,0,0);
1110   return 0;
1111 }
1112 
1113 /*@
1114    MatScale - Scales a matrix by a number.
1115 
1116    Input Parameters:
1117 .  mat - the matrix to be scaled
1118 .   a  - the number
1119 
1120    Note: the name of this routine MUST change.
1121 .keywords: matrix, scale
1122 @*/
1123 int MatScale(Scalar *a,Mat mat)
1124 {
1125   int ierr;
1126   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1127   if (!mat->ops.scale) SETERRQ(PETSC_ERR_SUP,"MatScale");
1128   if (!mat->assembled) SETERRQ(1,"MatScale:Not for unassembled matrix");
1129 
1130   PLogEventBegin(MAT_Scale,mat,0,0,0);
1131   ierr = (*mat->ops.scale)(a,mat); CHKERRQ(ierr);
1132   PLogEventEnd(MAT_Scale,mat,0,0,0);
1133   return 0;
1134 }
1135 
1136 /*@
1137    MatNorm - Calculates various norms of a matrix.
1138 
1139    Input Parameters:
1140 .  mat - the matrix
1141 .  type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY
1142 
1143    Output Parameters:
1144 .  norm - the resulting norm
1145 
1146 .keywords: matrix, norm, Frobenius
1147 @*/
1148 int MatNorm(Mat mat,NormType type,double *norm)
1149 {
1150   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1151   if (!norm) SETERRQ(1,"MatNorm:bad addess for value");
1152   if (!mat->assembled) SETERRQ(1,"MatNorm:Not for unassembled matrix");
1153   if (mat->ops.norm) return (*mat->ops.norm)(mat,type,norm);
1154   SETERRQ(PETSC_ERR_SUP,"MatNorm:Not for this matrix type");
1155 }
1156 
1157 /*@
1158    MatAssemblyBegin - Begins assembling the matrix.  This routine should
1159    be called after completing all calls to MatSetValues().
1160 
1161    Input Parameters:
1162 .  mat - the matrix
1163 .  type - type of assembly, either FLUSH_ASSEMBLY or FINAL_ASSEMBLY
1164 
1165    Notes:
1166    MatSetValues() generally caches the values.  The matrix is ready to
1167    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
1168    Use FLUSH_ASSEMBLY when switching between ADD_VALUES and SetValues; use
1169    FINAL_ASSEMBLY for the final assembly before the matrix is used.
1170 
1171 .keywords: matrix, assembly, assemble, begin
1172 
1173 .seealso: MatAssemblyEnd(), MatSetValues()
1174 @*/
1175 int MatAssemblyBegin(Mat mat,MatAssemblyType type)
1176 {
1177   int ierr;
1178   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1179   PLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);
1180   if (mat->ops.assemblybegin) {ierr = (*mat->ops.assemblybegin)(mat,type); CHKERRQ(ierr);}
1181   PLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);
1182   return 0;
1183 }
1184 
1185 /*@
1186    MatAssemblyEnd - Completes assembling the matrix.  This routine should
1187    be called after all calls to MatSetValues() and after MatAssemblyBegin().
1188 
1189    Input Parameters:
1190 .  mat - the matrix
1191 .  type - type of assembly, either FLUSH_ASSEMBLY or FINAL_ASSEMBLY
1192 
1193    Options Database Keys:
1194 $  -mat_view_draw : Draw nonzero structure of matrix at conclusion of MatEndAssembly(),
1195                using MatView() and DrawOpenX().
1196 $  -mat_view_info : Prints info on matrix.
1197 $  -mat_view_info_detailed: More detailed information.
1198 $  -mat_view_ascii : Prints matrix out in ascii.
1199 $  -display <name> : Set display name (default is host)
1200 $  -draw_pause <sec> : Set number of seconds to pause after display
1201 
1202    Note:
1203    MatSetValues() generally caches the values.  The matrix is ready to
1204    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
1205    Use FLUSH_ASSEMBLY when switching between ADD_VALUES and SetValues; use
1206    FINAL_ASSEMBLY for the final assembly before the matrix is used.
1207 
1208 .keywords: matrix, assembly, assemble, end
1209 
1210 .seealso: MatAssemblyBegin(), MatSetValues()
1211 @*/
1212 int MatAssemblyEnd(Mat mat,MatAssemblyType type)
1213 {
1214   int        ierr,flg;
1215   static int inassm = 0;
1216 
1217   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1218   inassm++;
1219   PLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);
1220   if (mat->ops.assemblyend) {ierr = (*mat->ops.assemblyend)(mat,type); CHKERRQ(ierr);}
1221   mat->assembled = PETSC_TRUE; mat->num_ass++;
1222   PLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);
1223 
1224   if (inassm == 1) {
1225     ierr = OptionsHasName(PETSC_NULL,"-mat_view_info",&flg); CHKERRQ(ierr);
1226     if (flg) {
1227       Viewer viewer;
1228       ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr);
1229       ierr = ViewerFileSetFormat(viewer,FILE_FORMAT_INFO,0);CHKERRQ(ierr);
1230       ierr = MatView(mat,viewer); CHKERRQ(ierr);
1231       ierr = ViewerDestroy(viewer); CHKERRQ(ierr);
1232     }
1233     ierr = OptionsHasName(PETSC_NULL,"-mat_view_info_detailed",&flg); CHKERRQ(ierr);
1234     if (flg) {
1235       Viewer viewer;
1236       ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr);
1237       ierr = ViewerFileSetFormat(viewer,FILE_FORMAT_INFO_DETAILED,0);CHKERRQ(ierr);
1238       ierr = MatView(mat,viewer); CHKERRQ(ierr);
1239       ierr = ViewerDestroy(viewer); CHKERRQ(ierr);
1240     }
1241     ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg); CHKERRQ(ierr);
1242     if (flg) {
1243       Draw    win;
1244       ierr = DrawOpenX(mat->comm,0,0,0,0,300,300,&win); CHKERRQ(ierr);
1245       ierr = MatView(mat,(Viewer)win); CHKERRQ(ierr);
1246       ierr = DrawSyncFlush(win); CHKERRQ(ierr);
1247       ierr = DrawDestroy(win); CHKERRQ(ierr);
1248     }
1249   }
1250   inassm--;
1251   return 0;
1252 }
1253 
1254 /*@
1255    MatCompress - Tries to store the matrix in as little space as
1256    possible.  May fail if memory is already fully used, since it
1257    tries to allocate new space.
1258 
1259    Input Parameters:
1260 .  mat - the matrix
1261 
1262 .keywords: matrix, compress
1263 @*/
1264 int MatCompress(Mat mat)
1265 {
1266   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1267   if (mat->ops.compress) return (*mat->ops.compress)(mat);
1268   return 0;
1269 }
1270 /*@
1271    MatSetOption - Sets a parameter option for a matrix. Some options
1272    may be specific to certain storage formats.  Some options
1273    determine how values will be inserted (or added). Sorted,
1274    row-oriented input will generally assemble the fastest. The default
1275    is row-oriented, nonsorted input.
1276 
1277    Input Parameters:
1278 .  mat - the matrix
1279 .  option - the option, one of the following:
1280 $    ROW_ORIENTED
1281 $    COLUMN_ORIENTED,
1282 $    ROWS_SORTED,
1283 $    COLUMNS_SORTED,
1284 $    NO_NEW_NONZERO_LOCATIONS,
1285 $    YES_NEW_NONZERO_LOCATIONS,
1286 $    SYMMETRIC_MATRIX,
1287 $    STRUCTURALLY_SYMMETRIC_MATRIX,
1288 $    NO_NEW_DIAGONALS,
1289 $    YES_NEW_DIAGONALS,
1290 $    and possibly others.
1291 
1292    Notes:
1293    Some options are relevant only for particular matrix types and
1294    are thus ignored by others.  Other options are not supported by
1295    certain matrix types and will generate an error message if set.
1296 
1297    If using a Fortran 77 module to compute a matrix, one may need to
1298    use the column-oriented option (or convert to the row-oriented
1299    format).
1300 
1301    NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion
1302    that will generate a new entry in the nonzero structure is ignored.
1303    What this means is if memory is not allocated for this particular
1304    lot, then the insertion is ignored. For dense matrices, where
1305    the entire array is allocated, no entries are ever ignored.
1306 
1307 .keywords: matrix, option, row-oriented, column-oriented, sorted, nonzero
1308 @*/
1309 int MatSetOption(Mat mat,MatOption op)
1310 {
1311   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1312   if (mat->ops.setoption) return (*mat->ops.setoption)(mat,op);
1313   return 0;
1314 }
1315 
1316 /*@
1317    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
1318    this routine retains the old nonzero structure.
1319 
1320    Input Parameters:
1321 .  mat - the matrix
1322 
1323 .keywords: matrix, zero, entries
1324 
1325 .seealso: MatZeroRows()
1326 @*/
1327 int MatZeroEntries(Mat mat)
1328 {
1329   int ierr;
1330   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1331   if (!mat->ops.zeroentries) SETERRQ(PETSC_ERR_SUP,"MatZeroEntries");
1332 
1333   PLogEventBegin(MAT_ZeroEntries,mat,0,0,0);
1334   ierr = (*mat->ops.zeroentries)(mat); CHKERRQ(ierr);
1335   PLogEventEnd(MAT_ZeroEntries,mat,0,0,0);
1336   return 0;
1337 }
1338 
1339 /*@
1340    MatZeroRows - Zeros all entries (except possibly the main diagonal)
1341    of a set of rows of a matrix.
1342 
1343    Input Parameters:
1344 .  mat - the matrix
1345 .  is - index set of rows to remove
1346 .  diag - pointer to value put in all diagonals of eliminated rows.
1347           Note that diag is not a pointer to an array, but merely a
1348           pointer to a single value.
1349 
1350    Notes:
1351    For the AIJ matrix formats this removes the old nonzero structure,
1352    but does not release memory.  For the dense and block diagonal
1353    formats this does not alter the nonzero structure.
1354 
1355    The user can set a value in the diagonal entry (or for the AIJ and
1356    row formats can optionally remove the main diagonal entry from the
1357    nonzero structure as well, by passing a null pointer as the final
1358    argument).
1359 
1360 .keywords: matrix, zero, rows, boundary conditions
1361 
1362 .seealso: MatZeroEntries(), MatGetSubMatrix(), MatGetSubMatrixInPlace()
1363 @*/
1364 int MatZeroRows(Mat mat,IS is, Scalar *diag)
1365 {
1366   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1367   if (!mat->assembled) SETERRQ(1,"MatZeroRows:Not for unassembled matrix");
1368   if (mat->ops.zerorows) return (*mat->ops.zerorows)(mat,is,diag);
1369   SETERRQ(PETSC_ERR_SUP,"MatZeroRows");
1370 }
1371 
1372 /*@
1373    MatGetSize - Returns the numbers of rows and columns in a matrix.
1374 
1375    Input Parameter:
1376 .  mat - the matrix
1377 
1378    Output Parameters:
1379 .  m - the number of global rows
1380 .  n - the number of global columns
1381 
1382 .keywords: matrix, dimension, size, rows, columns, global, get
1383 
1384 .seealso: MatGetLocalSize()
1385 @*/
1386 int MatGetSize(Mat mat,int *m,int* n)
1387 {
1388   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1389   if (!m || !n) SETERRQ(1,"MatGetSize:Bad address for result");
1390   return (*mat->ops.getsize)(mat,m,n);
1391 }
1392 
1393 /*@
1394    MatGetLocalSize - Returns the number of rows and columns in a matrix
1395    stored locally.  This information may be implementation dependent, so
1396    use with care.
1397 
1398    Input Parameters:
1399 .  mat - the matrix
1400 
1401    Output Parameters:
1402 .  m - the number of local rows
1403 .  n - the number of local columns
1404 
1405 .keywords: matrix, dimension, size, local, rows, columns, get
1406 
1407 .seealso: MatGetSize()
1408 @*/
1409 int MatGetLocalSize(Mat mat,int *m,int* n)
1410 {
1411   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1412   if (!m || !n) SETERRQ(1,"MatGetLocalSize:Bad address for result");
1413   return (*mat->ops.getlocalsize)(mat,m,n);
1414 }
1415 
1416 /*@
1417    MatGetOwnershipRange - Returns the range of matrix rows owned by
1418    this processor, assuming that the matrix is laid out with the first
1419    n1 rows on the first processor, the next n2 rows on the second, etc.
1420    For certain parallel layouts this range may not be well-defined.
1421 
1422    Input Parameters:
1423 .  mat - the matrix
1424 
1425    Output Parameters:
1426 .  m - the first local row
1427 .  n - one more then the last local row
1428 
1429 .keywords: matrix, get, range, ownership
1430 @*/
1431 int MatGetOwnershipRange(Mat mat,int *m,int* n)
1432 {
1433   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1434   if (!m || !n) SETERRQ(1,"MatGetOwnershipRange:Bad address for result");
1435   if (mat->ops.getownershiprange) return (*mat->ops.getownershiprange)(mat,m,n);
1436   SETERRQ(PETSC_ERR_SUP,"MatGetOwnershipRange");
1437 }
1438 
1439 /*@
1440    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
1441    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
1442    to complete the factorization.
1443 
1444    Input Parameters:
1445 .  mat - the matrix
1446 .  row - row permutation
1447 .  column - column permutation
1448 .  fill - number of levels of fill
1449 .  f - expected fill as ratio of the original number of nonzeros,
1450        for example 3.0; choosing this parameter well can result in
1451        more efficient use of time and space.
1452 
1453    Output Parameters:
1454 .  fact - new matrix that has been symbolically factored
1455 
1456    Options Database Key:
1457 $   -mat_ilu_fill <f>, where f is the fill ratio
1458 
1459    Notes:
1460    See the file $(PETSC_DIR)/Performace for additional information about
1461    choosing the fill factor for better efficiency.
1462 
1463 .keywords: matrix, factor, incomplete, ILU, symbolic, fill
1464 
1465 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric()
1466 @*/
1467 int MatILUFactorSymbolic(Mat mat,IS row,IS col,double f,int fill,Mat *fact)
1468 {
1469   int ierr,flg;
1470 
1471   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1472   if (fill < 0) SETERRQ(1,"MatILUFactorSymbolic:Levels of fill negative");
1473   if (!fact) SETERRQ(1,"MatILUFactorSymbolic:Fact argument is missing");
1474   if (!mat->ops.ilufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"MatILUFactorSymbolic");
1475   if (!mat->assembled) SETERRQ(1,"MatILUFactorSymbolic:Not for unassembled matrix");
1476 
1477   ierr = OptionsGetDouble(PETSC_NULL,"-mat_ilu_fill",&f,&flg); CHKERRQ(ierr);
1478   PLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);
1479   ierr = (*mat->ops.ilufactorsymbolic)(mat,row,col,f,fill,fact); CHKERRQ(ierr);
1480   PLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);
1481   return 0;
1482 }
1483 
1484 /*@
1485    MatIncompleteCholeskyFactorSymbolic - Performs symbolic incomplete
1486    Cholesky factorization for a symmetric matrix.  Use
1487    MatCholeskyFactorNumeric() to complete the factorization.
1488 
1489    Input Parameters:
1490 .  mat - the matrix
1491 .  perm - row and column permutation
1492 .  fill - levels of fill
1493 .  f - expected fill as ratio of original fill
1494 
1495    Output Parameter:
1496 .  fact - the factored matrix
1497 
1498    Note:  Currently only no-fill factorization is supported.
1499 
1500 .keywords: matrix, factor, incomplete, ICC, Cholesky, symbolic, fill
1501 
1502 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor()
1503 @*/
1504 int MatIncompleteCholeskyFactorSymbolic(Mat mat,IS perm,double f,int fill,
1505                                         Mat *fact)
1506 {
1507   int ierr;
1508   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1509   if (fill < 0) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Fill negative");
1510   if (!fact) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Missing fact argument");
1511   if (!mat->ops.incompletecholeskyfactorsymbolic)
1512      SETERRQ(PETSC_ERR_SUP,"MatIncompleteCholeskyFactorSymbolic");
1513   if (!mat->assembled)
1514      SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Not for unassembled matrix");
1515 
1516   PLogEventBegin(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0);
1517   ierr = (*mat->ops.incompletecholeskyfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr);
1518   PLogEventEnd(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0);
1519   return 0;
1520 }
1521 
1522 /*@C
1523    MatGetArray - Returns a pointer to the element values in the matrix.
1524    This routine  is implementation dependent, and may not even work for
1525    certain matrix types.
1526 
1527    Input Parameter:
1528 .  mat - the matrix
1529 
1530    Output Parameter:
1531 .  v - the location of the values
1532 
1533    Fortran Note:
1534    The Fortran interface is slightly different from that given below.
1535    See the users manual and petsc/src/mat/examples for details.
1536 
1537 .keywords: matrix, array, elements, values
1538 @*/
1539 int MatGetArray(Mat mat,Scalar **v)
1540 {
1541   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1542   if (!v) SETERRQ(1,"MatGetArray:Bad input, array pointer location");
1543   if (!mat->ops.getarray) SETERRQ(PETSC_ERR_SUP,"MatGetArraye");
1544   return (*mat->ops.getarray)(mat,v);
1545 }
1546 
1547 /*@C
1548    MatGetSubMatrix - Extracts a submatrix from a matrix. If submat points
1549                      to a valid matrix, it may be reused.
1550 
1551    Input Parameters:
1552 .  mat - the matrix
1553 .  irow, icol - index sets of rows and columns to extract
1554 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
1555 
1556    Output Parameter:
1557 .  submat - the submatrix
1558 
1559    Notes:
1560    MatGetSubMatrix() can be useful in setting boundary conditions.
1561 
1562    Use MatGetSubMatrices() to extract multiple submatrices.
1563 
1564 .keywords: matrix, get, submatrix, boundary conditions
1565 
1566 .seealso: MatZeroRows(), MatGetSubMatrixInPlace(), MatGetSubMatrices()
1567 @*/
1568 int MatGetSubMatrix(Mat mat,IS irow,IS icol,MatGetSubMatrixCall scall,Mat *submat)
1569 {
1570   int ierr;
1571   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1572   if (scall == MAT_REUSE_MATRIX) {
1573     PETSCVALIDHEADERSPECIFIC(*submat,MAT_COOKIE);
1574   }
1575   if (!mat->ops.getsubmatrix) SETERRQ(PETSC_ERR_SUP,"MatGetSubMatrix");
1576   if (!mat->assembled) SETERRQ(1,"MatGetSubMatrix:Not for unassembled matrix");
1577 
1578   PLogEventBegin(MAT_GetSubMatrix,mat,irow,icol,0);
1579   ierr = (*mat->ops.getsubmatrix)(mat,irow,icol,scall,submat); CHKERRQ(ierr);
1580   PLogEventEnd(MAT_GetSubMatrix,mat,irow,icol,0);
1581   return 0;
1582 }
1583 
1584 /*@C
1585    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
1586    points to an array of valid matrices, it may be reused.
1587 
1588    Input Parameters:
1589 .  mat - the matrix
1590 .  irow, icol - index sets of rows and columns to extract
1591 
1592    Output Parameter:
1593 .  submat - the submatrices
1594 
1595    Note:
1596    Use MatGetSubMatrix() for extracting a sinble submatrix.
1597 
1598 .keywords: matrix, get, submatrix, submatrices
1599 
1600 .seealso: MatGetSubMatrix()
1601 @*/
1602 int MatGetSubMatrices(Mat mat,int n, IS *irow,IS *icol,MatGetSubMatrixCall scall,
1603                       Mat **submat)
1604 {
1605   int ierr;
1606   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1607   if (!mat->ops.getsubmatrices) SETERRQ(PETSC_ERR_SUP,"MatGetSubMatrices");
1608   if (!mat->assembled) SETERRQ(1,"MatGetSubMatrices:Not for unassembled matrix");
1609 
1610   PLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);
1611   ierr = (*mat->ops.getsubmatrices)(mat,n,irow,icol,scall,submat); CHKERRQ(ierr);
1612   PLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);
1613   return 0;
1614 }
1615 
1616 /*@
1617    MatGetSubMatrixInPlace - Extracts a submatrix from a matrix, returning
1618    the submatrix in place of the original matrix.
1619 
1620    Input Parameters:
1621 .  mat - the matrix
1622 .  irow, icol - index sets of rows and columns to extract
1623 
1624 .keywords: matrix, get, submatrix, boundary conditions, in-place
1625 
1626 .seealso: MatZeroRows(), MatGetSubMatrix()
1627 @*/
1628 int MatGetSubMatrixInPlace(Mat mat,IS irow,IS icol)
1629 {
1630   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1631   if (!mat->assembled) SETERRQ(1,"MatGetSubMatrixInPlace:Not for unassembled matrix");
1632 
1633   if (!mat->ops.getsubmatrixinplace) SETERRQ(PETSC_ERR_SUP,"MatGetSubmatrixInPlace");
1634   return (*mat->ops.getsubmatrixinplace)(mat,irow,icol);
1635 }
1636 
1637 /*@
1638    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
1639    replaces the index by larger ones that represent submatrices with more
1640    overlap.
1641 
1642    Input Parameters:
1643 .  mat - the matrix
1644 .  n   - the number of index sets
1645 .  is  - the array of pointers to index sets
1646 .  ov  - the additional overlap requested
1647 
1648 .keywords: matrix, overlap, Schwarz
1649 
1650 .seealso: MatGetSubMatrices()
1651 @*/
1652 int MatIncreaseOverlap(Mat mat,int n, IS *is, int ov)
1653 {
1654   int ierr;
1655   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
1656   if (!mat->assembled) SETERRQ(1,"MatIncreaseOverlap:Not for unassembled matrix");
1657 
1658   if (ov == 0) return 0;
1659   if (!mat->ops.increaseoverlap) SETERRQ(PETSC_ERR_SUP,"MatIncreaseOverlap");
1660   PLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);
1661   ierr = (*mat->ops.increaseoverlap)(mat,n,is,ov); CHKERRQ(ierr);
1662   PLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);
1663   return 0;
1664 }
1665 
1666 /*@
1667    MatPrintHelp - Prints all the options for the matrix.
1668 
1669    Input Parameter:
1670 .  mat - the matrix
1671 
1672    Options Database Keys:
1673 $  -help, -h
1674 
1675 .keywords: mat, help
1676 
1677 .seealso: MatCreate(), MatCreateXXX()
1678 @*/
1679 int MatPrintHelp(Mat mat)
1680 {
1681   static int called = 0;
1682   MPI_Comm   comm = mat->comm;
1683 
1684   if (!called) {
1685     MPIU_printf(comm,"General matrix options:\n");
1686     MPIU_printf(comm,"  -mat_view_info : view basic matrix info during MatAssemblyEnd()\n");
1687     MPIU_printf(comm,"  -mat_view_info_detailed : view detailed matrix info during MatAssemblyEnd()\n");
1688     MPIU_printf(comm,"  -mat_view_draw : draw nonzero matrix structure during MatAssemblyEnd()\n");
1689     MPIU_printf(comm,"      -draw_pause <sec> : set seconds of display pause\n");
1690     MPIU_printf(comm,"      -display <name> : set alternate display\n");
1691     called = 1;
1692   }
1693   if (mat->ops.printhelp) (*mat->ops.printhelp)(mat);
1694   return 0;
1695 }
1696 
1697