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