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