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