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