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