xref: /petsc/src/mat/interface/matrix.c (revision 7bd18339000a996b4d2a36f55f5bcb5e6ccfe1dd)
1 /*$Id: matrix.c,v 1.404 2001/06/06 15:50:59 bsmith Exp buschelm $*/
2 
3 /*
4    This is where the abstract matrix operations are defined
5 */
6 
7 #include "src/mat/matimpl.h"        /*I "petscmat.h" I*/
8 #include "src/vec/vecimpl.h"
9 
10 #undef __FUNCT__
11 #define __FUNCT__ "MatGetRow"
12 /*@C
13    MatGetRow - Gets a row of a matrix.  You MUST call MatRestoreRow()
14    for each row that you get to ensure that your application does
15    not bleed memory.
16 
17    Not Collective
18 
19    Input Parameters:
20 +  mat - the matrix
21 -  row - the row to get
22 
23    Output Parameters:
24 +  ncols -  the number of nonzeros in the row
25 .  cols - if not NULL, the column numbers
26 -  vals - if not NULL, the values
27 
28    Notes:
29    This routine is provided for people who need to have direct access
30    to the structure of a matrix.  We hope that we provide enough
31    high-level matrix routines that few users will need it.
32 
33    MatGetRow() always returns 0-based column indices, regardless of
34    whether the internal representation is 0-based (default) or 1-based.
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    You can only have one call to MatGetRow() outstanding for a particular
44    matrix at a time, per processor. MatGetRow() can only obtained rows
45    associated with the given processor, it cannot get rows from the
46    other processors; for that we suggest using MatGetSubMatrices(), then
47    MatGetRow() on the submatrix. The row indix passed to MatGetRows()
48    is in the global number of rows.
49 
50    Fortran Notes:
51    The calling sequence from Fortran is
52 .vb
53    MatGetRow(matrix,row,ncols,cols,values,ierr)
54          Mat     matrix (input)
55          integer row    (input)
56          integer ncols  (output)
57          integer cols(maxcols) (output)
58          double precision (or double complex) values(maxcols) output
59 .ve
60    where maxcols >= maximum nonzeros in any row of the matrix.
61 
62    Caution:
63    Do not try to change the contents of the output arrays (cols and vals).
64    In some cases, this may corrupt the matrix.
65 
66    Level: advanced
67 
68    Concepts: matrices^row access
69 
70 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatGetSubmatrices(), MatGetDiagonal()
71 @*/
72 int MatGetRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals)
73 {
74   int   ierr;
75 
76   PetscFunctionBegin;
77   PetscValidHeaderSpecific(mat,MAT_COOKIE);
78   PetscValidIntPointer(ncols);
79   PetscValidType(mat);
80   MatPreallocated(mat);
81   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
82   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
83   if (!mat->ops->getrow) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
84   ierr = PetscLogEventBegin(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr);
85   ierr = (*mat->ops->getrow)(mat,row,ncols,cols,vals);CHKERRQ(ierr);
86   ierr = PetscLogEventEnd(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr);
87   PetscFunctionReturn(0);
88 }
89 
90 #undef __FUNCT__
91 #define __FUNCT__ "MatRestoreRow"
92 /*@C
93    MatRestoreRow - Frees any temporary space allocated by MatGetRow().
94 
95    Not Collective
96 
97    Input Parameters:
98 +  mat - the matrix
99 .  row - the row to get
100 .  ncols, cols - the number of nonzeros and their columns
101 -  vals - if nonzero the column values
102 
103    Notes:
104    This routine should be called after you have finished examining the entries.
105 
106    Fortran Notes:
107    The calling sequence from Fortran is
108 .vb
109    MatRestoreRow(matrix,row,ncols,cols,values,ierr)
110       Mat     matrix (input)
111       integer row    (input)
112       integer ncols  (output)
113       integer cols(maxcols) (output)
114       double precision (or double complex) values(maxcols) output
115 .ve
116    Where maxcols >= maximum nonzeros in any row of the matrix.
117 
118    In Fortran MatRestoreRow() MUST be called after MatGetRow()
119    before another call to MatGetRow() can be made.
120 
121    Level: advanced
122 
123 .seealso:  MatGetRow()
124 @*/
125 int MatRestoreRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals)
126 {
127   int ierr;
128 
129   PetscFunctionBegin;
130   PetscValidHeaderSpecific(mat,MAT_COOKIE);
131   PetscValidIntPointer(ncols);
132   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
133   if (!mat->ops->restorerow) PetscFunctionReturn(0);
134   ierr = (*mat->ops->restorerow)(mat,row,ncols,cols,vals);CHKERRQ(ierr);
135   PetscFunctionReturn(0);
136 }
137 
138 #undef __FUNCT__
139 #define __FUNCT__ "MatView"
140 /*@C
141    MatView - Visualizes a matrix object.
142 
143    Collective on Mat
144 
145    Input Parameters:
146 +  mat - the matrix
147 -  ptr - visualization context
148 
149   Notes:
150   The available visualization contexts include
151 +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
152 .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
153         output where only the first processor opens
154         the file.  All other processors send their
155         data to the first processor to print.
156 -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
157 
158    The user can open alternative visualization contexts with
159 +    PetscViewerASCIIOpen() - Outputs matrix to a specified file
160 .    PetscViewerBinaryOpen() - Outputs matrix in binary to a
161          specified file; corresponding input uses MatLoad()
162 .    PetscViewerDrawOpen() - Outputs nonzero matrix structure to
163          an X window display
164 -    PetscViewerSocketOpen() - Outputs matrix to Socket viewer.
165          Currently only the sequential dense and AIJ
166          matrix types support the Socket viewer.
167 
168    The user can call PetscViewerSetFormat() to specify the output
169    format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF,
170    PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen).  Available formats include
171 +    PETSC_VIEWER_ASCII_DEFAULT - default, prints matrix contents
172 .    PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format
173 .    PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros
174 .    PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse
175          format common among all matrix types
176 .    PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific
177          format (which is in many cases the same as the default)
178 .    PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix
179          size and structure (not the matrix entries)
180 -    PETSC_VIEWER_ASCII_INFO_LONG - prints more detailed information about
181          the matrix structure
182 
183    Level: beginner
184 
185    Concepts: matrices^viewing
186    Concepts: matrices^plotting
187    Concepts: matrices^printing
188 
189 .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
190           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad()
191 @*/
192 int MatView(Mat mat,PetscViewer viewer)
193 {
194   int               ierr,rows,cols;
195   PetscTruth        isascii;
196   char              *cstr;
197   PetscViewerFormat format;
198 
199   PetscFunctionBegin;
200   PetscValidHeaderSpecific(mat,MAT_COOKIE);
201   PetscValidType(mat);
202   MatPreallocated(mat);
203   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(mat->comm);
204   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE);
205   PetscCheckSameComm(mat,viewer);
206   if (!mat->assembled) SETERRQ(1,"Must call MatAssemblyBegin/End() before viewing matrix");
207 
208   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
209   if (isascii) {
210     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
211     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_LONG) {
212       if (mat->prefix) {
213         ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:(%s)\n",mat->prefix);CHKERRQ(ierr);
214       } else {
215         ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:\n");CHKERRQ(ierr);
216       }
217       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
218       ierr = MatGetType(mat,&cstr);CHKERRQ(ierr);
219       ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr);
220       ierr = PetscViewerASCIIPrintf(viewer,"type=%s, rows=%d, cols=%d\n",cstr,rows,cols);CHKERRQ(ierr);
221       if (mat->ops->getinfo) {
222         MatInfo info;
223         ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr);
224         ierr = PetscViewerASCIIPrintf(viewer,"total: nonzeros=%d, allocated nonzeros=%d\n",
225                           (int)info.nz_used,(int)info.nz_allocated);CHKERRQ(ierr);
226       }
227     }
228   }
229   if (mat->ops->view) {
230     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
231     ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr);
232     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
233   } else if (!isascii) {
234     SETERRQ1(1,"Viewer type %s not supported",((PetscObject)viewer)->type_name);
235   }
236   if (isascii) {
237     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
238     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_LONG) {
239       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
240     }
241   }
242   PetscFunctionReturn(0);
243 }
244 
245 #undef __FUNCT__
246 #define __FUNCT__ "MatScaleSystem"
247 /*@C
248    MatScaleSystem - Scale a vector solution and right hand side to
249    match the scaling of a scaled matrix.
250 
251    Collective on Mat
252 
253    Input Parameter:
254 +  mat - the matrix
255 .  x - solution vector (or PETSC_NULL)
256 +  b - right hand side vector (or PETSC_NULL)
257 
258 
259    Notes:
260    For AIJ, BAIJ, and BDiag matrix formats, the matrices are not
261    internally scaled, so this does nothing. For MPIROWBS it
262    permutes and diagonally scales.
263 
264    The SLES methods automatically call this routine when required
265    (via PCPreSolve()) so it is rarely used directly.
266 
267    Level: Developer
268 
269    Concepts: matrices^scaling
270 
271 .seealso: MatUseScaledForm(), MatUnScaleSystem()
272 @*/
273 int MatScaleSystem(Mat mat,Vec x,Vec b)
274 {
275   int ierr;
276 
277   PetscFunctionBegin;
278   PetscValidHeaderSpecific(mat,MAT_COOKIE);
279   PetscValidType(mat);
280   MatPreallocated(mat);
281   if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);}
282   if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,b);}
283 
284   if (mat->ops->scalesystem) {
285     ierr = (*mat->ops->scalesystem)(mat,x,b);CHKERRQ(ierr);
286   }
287   PetscFunctionReturn(0);
288 }
289 
290 #undef __FUNCT__
291 #define __FUNCT__ "MatUnScaleSystem"
292 /*@C
293    MatUnScaleSystem - Unscales a vector solution and right hand side to
294    match the original scaling of a scaled matrix.
295 
296    Collective on Mat
297 
298    Input Parameter:
299 +  mat - the matrix
300 .  x - solution vector (or PETSC_NULL)
301 +  b - right hand side vector (or PETSC_NULL)
302 
303 
304    Notes:
305    For AIJ, BAIJ, and BDiag matrix formats, the matrices are not
306    internally scaled, so this does nothing. For MPIROWBS it
307    permutes and diagonally scales.
308 
309    The SLES methods automatically call this routine when required
310    (via PCPreSolve()) so it is rarely used directly.
311 
312    Level: Developer
313 
314 .seealso: MatUseScaledForm(), MatScaleSystem()
315 @*/
316 int MatUnScaleSystem(Mat mat,Vec x,Vec b)
317 {
318   int ierr;
319 
320   PetscFunctionBegin;
321   PetscValidHeaderSpecific(mat,MAT_COOKIE);
322   PetscValidType(mat);
323   MatPreallocated(mat);
324   if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);}
325   if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,b);}
326   if (mat->ops->unscalesystem) {
327     ierr = (*mat->ops->unscalesystem)(mat,x,b);CHKERRQ(ierr);
328   }
329   PetscFunctionReturn(0);
330 }
331 
332 #undef __FUNCT__
333 #define __FUNCT__ "MatUseScaledForm"
334 /*@C
335    MatUseScaledForm - For matrix storage formats that scale the
336    matrix (for example MPIRowBS matrices are diagonally scaled on
337    assembly) indicates matrix operations (MatMult() etc) are
338    applied using the scaled matrix.
339 
340    Collective on Mat
341 
342    Input Parameter:
343 +  mat - the matrix
344 -  scaled - PETSC_TRUE for applying the scaled, PETSC_FALSE for
345             applying the original matrix
346 
347    Notes:
348    For scaled matrix formats, applying the original, unscaled matrix
349    will be slightly more expensive
350 
351    Level: Developer
352 
353 .seealso: MatScaleSystem(), MatUnScaleSystem()
354 @*/
355 int MatUseScaledForm(Mat mat,PetscTruth scaled)
356 {
357   int ierr;
358 
359   PetscFunctionBegin;
360   PetscValidHeaderSpecific(mat,MAT_COOKIE);
361   PetscValidType(mat);
362   MatPreallocated(mat);
363   if (mat->ops->usescaledform) {
364     ierr = (*mat->ops->usescaledform)(mat,scaled);CHKERRQ(ierr);
365   }
366   PetscFunctionReturn(0);
367 }
368 
369 #undef __FUNCT__
370 #define __FUNCT__ "MatDestroy"
371 /*@C
372    MatDestroy - Frees space taken by a matrix.
373 
374    Collective on Mat
375 
376    Input Parameter:
377 .  A - the matrix
378 
379    Level: beginner
380 
381 @*/
382 int MatDestroy(Mat A)
383 {
384   int ierr;
385 
386   PetscFunctionBegin;
387   PetscValidHeaderSpecific(A,MAT_COOKIE);
388   PetscValidType(A);
389   MatPreallocated(A);
390   if (--A->refct > 0) PetscFunctionReturn(0);
391 
392   /* if memory was published with AMS then destroy it */
393   ierr = PetscObjectDepublish(A);CHKERRQ(ierr);
394   if (A->mapping) {
395     ierr = ISLocalToGlobalMappingDestroy(A->mapping);CHKERRQ(ierr);
396   }
397   if (A->bmapping) {
398     ierr = ISLocalToGlobalMappingDestroy(A->bmapping);CHKERRQ(ierr);
399   }
400   if (A->rmap) {
401     ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
402   }
403   if (A->cmap) {
404     ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
405   }
406 
407   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
408   PetscLogObjectDestroy(A);
409   PetscHeaderDestroy(A);
410   PetscFunctionReturn(0);
411 }
412 
413 #undef __FUNCT__
414 #define __FUNCT__ "MatValid"
415 /*@
416    MatValid - Checks whether a matrix object is valid.
417 
418    Collective on Mat
419 
420    Input Parameter:
421 .  m - the matrix to check
422 
423    Output Parameter:
424    flg - flag indicating matrix status, either
425    PETSC_TRUE if matrix is valid, or PETSC_FALSE otherwise.
426 
427    Level: developer
428 
429    Concepts: matrices^validity
430 @*/
431 int MatValid(Mat m,PetscTruth *flg)
432 {
433   PetscFunctionBegin;
434   PetscValidIntPointer(flg);
435   if (!m)                           *flg = PETSC_FALSE;
436   else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE;
437   else                              *flg = PETSC_TRUE;
438   PetscFunctionReturn(0);
439 }
440 
441 #undef __FUNCT__
442 #define __FUNCT__ "MatSetValues"
443 /*@
444    MatSetValues - Inserts or adds a block of values into a matrix.
445    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
446    MUST be called after all calls to MatSetValues() have been completed.
447 
448    Not Collective
449 
450    Input Parameters:
451 +  mat - the matrix
452 .  v - a logically two-dimensional array of values
453 .  m, idxm - the number of rows and their global indices
454 .  n, idxn - the number of columns and their global indices
455 -  addv - either ADD_VALUES or INSERT_VALUES, where
456    ADD_VALUES adds values to any existing entries, and
457    INSERT_VALUES replaces existing entries with new values
458 
459    Notes:
460    By default the values, v, are row-oriented and unsorted.
461    See MatSetOption() for other options.
462 
463    Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES
464    options cannot be mixed without intervening calls to the assembly
465    routines.
466 
467    MatSetValues() uses 0-based row and column numbers in Fortran
468    as well as in C.
469 
470    Negative indices may be passed in idxm and idxn, these rows and columns are
471    simply ignored. This allows easily inserting element stiffness matrices
472    with homogeneous Dirchlet boundary conditions that you don't want represented
473    in the matrix.
474 
475    Efficiency Alert:
476    The routine MatSetValuesBlocked() may offer much better efficiency
477    for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ).
478 
479    Level: beginner
480 
481    Concepts: matrices^putting entries in
482 
483 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
484 @*/
485 int MatSetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv)
486 {
487   int ierr;
488 
489   PetscFunctionBegin;
490   if (!m || !n) PetscFunctionReturn(0); /* no values to insert */
491   PetscValidHeaderSpecific(mat,MAT_COOKIE);
492   PetscValidType(mat);
493   MatPreallocated(mat);
494   PetscValidIntPointer(idxm);
495   PetscValidIntPointer(idxn);
496   PetscValidScalarPointer(v);
497   if (mat->insertmode == NOT_SET_VALUES) {
498     mat->insertmode = addv;
499   }
500 #if defined(PETSC_USE_BOPT_g)
501   else if (mat->insertmode != addv) {
502     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
503   }
504   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
505 #endif
506 
507   if (mat->assembled) {
508     mat->was_assembled = PETSC_TRUE;
509     mat->assembled     = PETSC_FALSE;
510   }
511   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
512   if (!mat->ops->setvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
513   ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr);
514   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
515   PetscFunctionReturn(0);
516 }
517 
518 #undef __FUNCT__
519 #define __FUNCT__ "MatSetValuesStencil"
520 /*@C
521    MatSetValuesStencil - Inserts or adds a block of values into a matrix.
522      Using structured grid indexing
523 
524    Not Collective
525 
526    Input Parameters:
527 +  mat - the matrix
528 .  v - a logically two-dimensional array of values
529 .  m - number of rows being entered
530 .  idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered
531 .  n - number of columns being entered
532 .  idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered
533 -  addv - either ADD_VALUES or INSERT_VALUES, where
534    ADD_VALUES adds values to any existing entries, and
535    INSERT_VALUES replaces existing entries with new values
536 
537    Notes:
538    By default the values, v, are row-oriented and unsorted.
539    See MatSetOption() for other options.
540 
541    Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES
542    options cannot be mixed without intervening calls to the assembly
543    routines.
544 
545    MatSetValuesStencil() uses 0-based row and column numbers in Fortran
546    as well as in C.
547 
548    In Fortran idxm and idxn should be declared as
549 $     MatStencil idxm(4,m),idxn(4,n)
550    and the values inserted using
551 $    idxm(MatStencil_i,1) = i
552 $    idxm(MatStencil_j,1) = j
553 $    idxm(MatStencil_k,1) = k
554    etc
555 
556    Negative indices may be passed in idxm and idxn, these rows and columns are
557    simply ignored. This allows easily inserting element stiffness matrices
558    with homogeneous Dirchlet boundary conditions that you don't want represented
559    in the matrix.
560 
561    Inspired by the structured grid interface to the HYPRE package
562    (www.llnl.gov/CASC/hyper)
563 
564    Efficiency Alert:
565    The routine MatSetValuesBlockedStencil() may offer much better efficiency
566    for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ).
567 
568    Level: beginner
569 
570    Concepts: matrices^putting entries in
571 
572 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
573           MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil()
574 @*/
575 int MatSetValuesStencil(Mat mat,int m,MatStencil *idxm,int n,MatStencil *idxn,Scalar *v,InsertMode addv)
576 {
577   int j,i,ierr,jdxm[128],jdxn[128],dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp;
578   int *starts = mat->stencil.starts,*dxm = (int*)idxm,*dxn = (int*)idxn,sdim = dim - (1 - (int)mat->stencil.noc);
579 
580   PetscFunctionBegin;
581   if (!m || !n) PetscFunctionReturn(0); /* no values to insert */
582   PetscValidHeaderSpecific(mat,MAT_COOKIE);
583   PetscValidType(mat);
584   PetscValidIntPointer(idxm);
585   PetscValidIntPointer(idxn);
586   PetscValidScalarPointer(v);
587 
588   for (i=0; i<m; i++) {
589     for (j=0; j<3-sdim; j++) dxm++;
590     tmp = *dxm++ - starts[0];
591     for (j=0; j<dim-1; j++) {
592       tmp = tmp*dims[j] + *dxm++ - starts[j+1];
593     }
594     if (mat->stencil.noc) dxm++;
595     jdxm[i] = tmp;
596   }
597   for (i=0; i<n; i++) {
598     for (j=0; j<3-sdim; j++) dxn++;
599     tmp = *dxn++ - starts[0];
600     for (j=0; j<dim-1; j++) {
601       tmp = tmp*dims[j] + *dxn++ - starts[j+1];
602     }
603     if (mat->stencil.noc) dxn++;
604     jdxn[i] = tmp;
605   }
606   ierr = MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr);
607   PetscFunctionReturn(0);
608 }
609 
610 #undef __FUNCT__
611 #define __FUNCT__ "MatSetStencil"
612 /*@
613    MatSetStencil - Sets the grid information for setting values into a matrix via
614         MatSetStencil()
615 
616    Not Collective
617 
618    Input Parameters:
619 +  mat - the matrix
620 .  dim - dimension of the grid 1,2, or 3
621 .  dims - number of grid points in x, y, and z direction, including ghost points on your processor
622 .  starts - starting point of ghost nodes on your processor in x, y, and z direction
623 -  dof - number of degrees of freedom per node
624 
625 
626    Inspired by the structured grid interface to the HYPRE package
627    (www.llnl.gov/CASC/hyper)
628 
629    Level: beginner
630 
631    Concepts: matrices^putting entries in
632 
633 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
634           MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil()
635 @*/
636 int MatSetStencil(Mat mat,int dim,int *dims,int *starts,int dof)
637 {
638   int i;
639 
640   PetscFunctionBegin;
641   PetscValidHeaderSpecific(mat,MAT_COOKIE);
642   PetscValidIntPointer(dims);
643   PetscValidIntPointer(starts);
644 
645   mat->stencil.dim = dim + (dof > 1);
646   for (i=0; i<dim; i++) {
647     mat->stencil.dims[i]   = dims[dim-i-1];      /* copy the values in backwards */
648     mat->stencil.starts[i] = starts[dim-i-1];
649   }
650   mat->stencil.dims[dim]   = dof;
651   mat->stencil.starts[dim] = 0;
652   mat->stencil.noc         = (PetscTruth)(dof == 1);
653   PetscFunctionReturn(0);
654 }
655 
656 #undef __FUNCT__
657 #define __FUNCT__ "MatSetValuesBlocked"
658 /*@
659    MatSetValuesBlocked - Inserts or adds a block of values into a matrix.
660 
661    Not Collective
662 
663    Input Parameters:
664 +  mat - the matrix
665 .  v - a logically two-dimensional array of values
666 .  m, idxm - the number of block rows and their global block indices
667 .  n, idxn - the number of block columns and their global block indices
668 -  addv - either ADD_VALUES or INSERT_VALUES, where
669    ADD_VALUES adds values to any existing entries, and
670    INSERT_VALUES replaces existing entries with new values
671 
672    Notes:
673    By default the values, v, are row-oriented and unsorted. So the layout of
674    v is the same as for MatSetValues(). See MatSetOption() for other options.
675 
676    Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES
677    options cannot be mixed without intervening calls to the assembly
678    routines.
679 
680    MatSetValuesBlocked() uses 0-based row and column numbers in Fortran
681    as well as in C.
682 
683    Negative indices may be passed in idxm and idxn, these rows and columns are
684    simply ignored. This allows easily inserting element stiffness matrices
685    with homogeneous Dirchlet boundary conditions that you don't want represented
686    in the matrix.
687 
688    Each time an entry is set within a sparse matrix via MatSetValues(),
689    internal searching must be done to determine where to place the the
690    data in the matrix storage space.  By instead inserting blocks of
691    entries via MatSetValuesBlocked(), the overhead of matrix assembly is
692    reduced.
693 
694    Restrictions:
695    MatSetValuesBlocked() is currently supported only for the block AIJ
696    matrix format (MATSEQBAIJ and MATMPIBAIJ, which are created via
697    MatCreateSeqBAIJ() and MatCreateMPIBAIJ()).
698 
699    Level: intermediate
700 
701    Concepts: matrices^putting entries in blocked
702 
703 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal()
704 @*/
705 int MatSetValuesBlocked(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv)
706 {
707   int ierr;
708 
709   PetscFunctionBegin;
710   if (!m || !n) PetscFunctionReturn(0); /* no values to insert */
711   PetscValidHeaderSpecific(mat,MAT_COOKIE);
712   PetscValidType(mat);
713   MatPreallocated(mat);
714   PetscValidIntPointer(idxm);
715   PetscValidIntPointer(idxn);
716   PetscValidScalarPointer(v);
717   if (mat->insertmode == NOT_SET_VALUES) {
718     mat->insertmode = addv;
719   }
720 #if defined(PETSC_USE_BOPT_g)
721   else if (mat->insertmode != addv) {
722     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
723   }
724   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
725 #endif
726 
727   if (mat->assembled) {
728     mat->was_assembled = PETSC_TRUE;
729     mat->assembled     = PETSC_FALSE;
730   }
731   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
732   if (!mat->ops->setvaluesblocked) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
733   ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr);
734   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
735   PetscFunctionReturn(0);
736 }
737 
738 /*MC
739    MatSetValue - Set a single entry into a matrix.
740 
741    Synopsis:
742    int MatSetValue(Mat m,int row,int col,Scalar value,InsertMode mode);
743 
744    Not collective
745 
746    Input Parameters:
747 +  m - the matrix
748 .  row - the row location of the entry
749 .  col - the column location of the entry
750 .  value - the value to insert
751 -  mode - either INSERT_VALUES or ADD_VALUES
752 
753    Notes:
754    For efficiency one should use MatSetValues() and set several or many
755    values simultaneously if possible.
756 
757    Note that VecSetValue() does NOT return an error code (since this
758    is checked internally).
759 
760    Level: beginner
761 
762 .seealso: MatSetValues()
763 M*/
764 
765 #undef __FUNCT__
766 #define __FUNCT__ "MatGetValues"
767 /*@
768    MatGetValues - Gets a block of values from a matrix.
769 
770    Not Collective; currently only returns a local block
771 
772    Input Parameters:
773 +  mat - the matrix
774 .  v - a logically two-dimensional array for storing the values
775 .  m, idxm - the number of rows and their global indices
776 -  n, idxn - the number of columns and their global indices
777 
778    Notes:
779    The user must allocate space (m*n Scalars) for the values, v.
780    The values, v, are then returned in a row-oriented format,
781    analogous to that used by default in MatSetValues().
782 
783    MatGetValues() uses 0-based row and column numbers in
784    Fortran as well as in C.
785 
786    MatGetValues() requires that the matrix has been assembled
787    with MatAssemblyBegin()/MatAssemblyEnd().  Thus, calls to
788    MatSetValues() and MatGetValues() CANNOT be made in succession
789    without intermediate matrix assembly.
790 
791    Level: advanced
792 
793    Concepts: matrices^accessing values
794 
795 .seealso: MatGetRow(), MatGetSubmatrices(), MatSetValues()
796 @*/
797 int MatGetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v)
798 {
799   int ierr;
800 
801   PetscFunctionBegin;
802   PetscValidHeaderSpecific(mat,MAT_COOKIE);
803   PetscValidType(mat);
804   MatPreallocated(mat);
805   PetscValidIntPointer(idxm);
806   PetscValidIntPointer(idxn);
807   PetscValidScalarPointer(v);
808   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
809   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
810   if (!mat->ops->getvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
811 
812   ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr);
813   ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr);
814   ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr);
815   PetscFunctionReturn(0);
816 }
817 
818 #undef __FUNCT__
819 #define __FUNCT__ "MatSetLocalToGlobalMapping"
820 /*@
821    MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by
822    the routine MatSetValuesLocal() to allow users to insert matrix entries
823    using a local (per-processor) numbering.
824 
825    Not Collective
826 
827    Input Parameters:
828 +  x - the matrix
829 -  mapping - mapping created with ISLocalToGlobalMappingCreate()
830              or ISLocalToGlobalMappingCreateIS()
831 
832    Level: intermediate
833 
834    Concepts: matrices^local to global mapping
835    Concepts: local to global mapping^for matrices
836 
837 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal()
838 @*/
839 int MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping mapping)
840 {
841   int ierr;
842   PetscFunctionBegin;
843   PetscValidHeaderSpecific(x,MAT_COOKIE);
844   PetscValidType(x);
845   MatPreallocated(x);
846   PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE);
847   if (x->mapping) {
848     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix");
849   }
850 
851   if (x->ops->setlocaltoglobalmapping) {
852     ierr = (*x->ops->setlocaltoglobalmapping)(x,mapping);CHKERRQ(ierr);
853   } else {
854     x->mapping = mapping;
855     ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr);
856   }
857   PetscFunctionReturn(0);
858 }
859 
860 #undef __FUNCT__
861 #define __FUNCT__ "MatSetLocalToGlobalMappingBlock"
862 /*@
863    MatSetLocalToGlobalMappingBlock - Sets a local-to-global numbering for use
864    by the routine MatSetValuesBlockedLocal() to allow users to insert matrix
865    entries using a local (per-processor) numbering.
866 
867    Not Collective
868 
869    Input Parameters:
870 +  x - the matrix
871 -  mapping - mapping created with ISLocalToGlobalMappingCreate() or
872              ISLocalToGlobalMappingCreateIS()
873 
874    Level: intermediate
875 
876    Concepts: matrices^local to global mapping blocked
877    Concepts: local to global mapping^for matrices, blocked
878 
879 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal(),
880            MatSetValuesBlocked(), MatSetValuesLocal()
881 @*/
882 int MatSetLocalToGlobalMappingBlock(Mat x,ISLocalToGlobalMapping mapping)
883 {
884   int ierr;
885   PetscFunctionBegin;
886   PetscValidHeaderSpecific(x,MAT_COOKIE);
887   PetscValidType(x);
888   MatPreallocated(x);
889   PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE);
890   if (x->bmapping) {
891     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix");
892   }
893 
894   x->bmapping = mapping;
895   ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr);
896   PetscFunctionReturn(0);
897 }
898 
899 #undef __FUNCT__
900 #define __FUNCT__ "MatSetValuesLocal"
901 /*@
902    MatSetValuesLocal - Inserts or adds values into certain locations of a matrix,
903    using a local ordering of the nodes.
904 
905    Not Collective
906 
907    Input Parameters:
908 +  x - the matrix
909 .  nrow, irow - number of rows and their local indices
910 .  ncol, icol - number of columns and their local indices
911 .  y -  a logically two-dimensional array of values
912 -  addv - either INSERT_VALUES or ADD_VALUES, where
913    ADD_VALUES adds values to any existing entries, and
914    INSERT_VALUES replaces existing entries with new values
915 
916    Notes:
917    Before calling MatSetValuesLocal(), the user must first set the
918    local-to-global mapping by calling MatSetLocalToGlobalMapping().
919 
920    Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES
921    options cannot be mixed without intervening calls to the assembly
922    routines.
923 
924    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
925    MUST be called after all calls to MatSetValuesLocal() have been completed.
926 
927    Level: intermediate
928 
929    Concepts: matrices^putting entries in with local numbering
930 
931 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(),
932            MatSetValueLocal()
933 @*/
934 int MatSetValuesLocal(Mat mat,int nrow,int *irow,int ncol,int *icol,Scalar *y,InsertMode addv)
935 {
936   int ierr,irowm[2048],icolm[2048];
937 
938   PetscFunctionBegin;
939   PetscValidHeaderSpecific(mat,MAT_COOKIE);
940   PetscValidType(mat);
941   MatPreallocated(mat);
942   PetscValidIntPointer(irow);
943   PetscValidIntPointer(icol);
944   PetscValidScalarPointer(y);
945 
946   if (mat->insertmode == NOT_SET_VALUES) {
947     mat->insertmode = addv;
948   }
949 #if defined(PETSC_USE_BOPT_g)
950   else if (mat->insertmode != addv) {
951     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
952   }
953   if (!mat->ops->setvalueslocal && (nrow > 2048 || ncol > 2048)) {
954     SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol);
955   }
956   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
957 #endif
958 
959   if (mat->assembled) {
960     mat->was_assembled = PETSC_TRUE;
961     mat->assembled     = PETSC_FALSE;
962   }
963   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
964   if (!mat->ops->setvalueslocal) {
965     ierr = ISLocalToGlobalMappingApply(mat->mapping,nrow,irow,irowm);CHKERRQ(ierr);
966     ierr = ISLocalToGlobalMappingApply(mat->mapping,ncol,icol,icolm);CHKERRQ(ierr);
967     ierr = (*mat->ops->setvalues)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr);
968   } else {
969     ierr = (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr);
970   }
971   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
972   PetscFunctionReturn(0);
973 }
974 
975 #undef __FUNCT__
976 #define __FUNCT__ "MatSetValuesBlockedLocal"
977 /*@
978    MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix,
979    using a local ordering of the nodes a block at a time.
980 
981    Not Collective
982 
983    Input Parameters:
984 +  x - the matrix
985 .  nrow, irow - number of rows and their local indices
986 .  ncol, icol - number of columns and their local indices
987 .  y -  a logically two-dimensional array of values
988 -  addv - either INSERT_VALUES or ADD_VALUES, where
989    ADD_VALUES adds values to any existing entries, and
990    INSERT_VALUES replaces existing entries with new values
991 
992    Notes:
993    Before calling MatSetValuesBlockedLocal(), the user must first set the
994    local-to-global mapping by calling MatSetLocalToGlobalMappingBlock(),
995    where the mapping MUST be set for matrix blocks, not for matrix elements.
996 
997    Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES
998    options cannot be mixed without intervening calls to the assembly
999    routines.
1000 
1001    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
1002    MUST be called after all calls to MatSetValuesBlockedLocal() have been completed.
1003 
1004    Level: intermediate
1005 
1006    Concepts: matrices^putting blocked values in with local numbering
1007 
1008 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesLocal(), MatSetLocalToGlobalMappingBlock(), MatSetValuesBlocked()
1009 @*/
1010 int MatSetValuesBlockedLocal(Mat mat,int nrow,int *irow,int ncol,int *icol,Scalar *y,InsertMode addv)
1011 {
1012   int ierr,irowm[2048],icolm[2048];
1013 
1014   PetscFunctionBegin;
1015   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1016   PetscValidType(mat);
1017   MatPreallocated(mat);
1018   PetscValidIntPointer(irow);
1019   PetscValidIntPointer(icol);
1020   PetscValidScalarPointer(y);
1021   if (mat->insertmode == NOT_SET_VALUES) {
1022     mat->insertmode = addv;
1023   }
1024 #if defined(PETSC_USE_BOPT_g)
1025   else if (mat->insertmode != addv) {
1026     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
1027   }
1028   if (!mat->bmapping) {
1029     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Local to global never set with MatSetLocalToGlobalMappingBlock()");
1030   }
1031   if (nrow > 2048 || ncol > 2048) {
1032     SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol);
1033   }
1034   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1035 #endif
1036 
1037   if (mat->assembled) {
1038     mat->was_assembled = PETSC_TRUE;
1039     mat->assembled     = PETSC_FALSE;
1040   }
1041   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
1042   ierr = ISLocalToGlobalMappingApply(mat->bmapping,nrow,irow,irowm);CHKERRQ(ierr);
1043   ierr = ISLocalToGlobalMappingApply(mat->bmapping,ncol,icol,icolm);CHKERRQ(ierr);
1044   ierr = (*mat->ops->setvaluesblocked)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr);
1045   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
1046   PetscFunctionReturn(0);
1047 }
1048 
1049 /* --------------------------------------------------------*/
1050 #undef __FUNCT__
1051 #define __FUNCT__ "MatMult"
1052 /*@
1053    MatMult - Computes the matrix-vector product, y = Ax.
1054 
1055    Collective on Mat and Vec
1056 
1057    Input Parameters:
1058 +  mat - the matrix
1059 -  x   - the vector to be multilplied
1060 
1061    Output Parameters:
1062 .  y - the result
1063 
1064    Notes:
1065    The vectors x and y cannot be the same.  I.e., one cannot
1066    call MatMult(A,y,y).
1067 
1068    Level: beginner
1069 
1070    Concepts: matrix-vector product
1071 
1072 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
1073 @*/
1074 int MatMult(Mat mat,Vec x,Vec y)
1075 {
1076   int ierr;
1077 
1078   PetscFunctionBegin;
1079   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1080   PetscValidType(mat);
1081   MatPreallocated(mat);
1082   PetscValidHeaderSpecific(x,VEC_COOKIE);
1083   PetscValidHeaderSpecific(y,VEC_COOKIE);
1084 
1085   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1086   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1087   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
1088   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1089   if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N);
1090   if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %d %d",mat->m,y->n);
1091 
1092   if (mat->nullsp) {
1093     ierr = MatNullSpaceRemove(mat->nullsp,x,&x);CHKERRQ(ierr);
1094   }
1095 
1096   ierr = PetscLogEventBegin(MAT_Mult,mat,x,y,0);CHKERRQ(ierr);
1097   ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr);
1098   ierr = PetscLogEventEnd(MAT_Mult,mat,x,y,0);CHKERRQ(ierr);
1099 
1100   if (mat->nullsp) {
1101     ierr = MatNullSpaceRemove(mat->nullsp,y,PETSC_NULL);CHKERRQ(ierr);
1102   }
1103   PetscFunctionReturn(0);
1104 }
1105 
1106 #undef __FUNCT__
1107 #define __FUNCT__ "MatMultTranspose"
1108 /*@
1109    MatMultTranspose - Computes matrix transpose times a vector.
1110 
1111    Collective on Mat and Vec
1112 
1113    Input Parameters:
1114 +  mat - the matrix
1115 -  x   - the vector to be multilplied
1116 
1117    Output Parameters:
1118 .  y - the result
1119 
1120    Notes:
1121    The vectors x and y cannot be the same.  I.e., one cannot
1122    call MatMultTranspose(A,y,y).
1123 
1124    Level: beginner
1125 
1126    Concepts: matrix vector product^transpose
1127 
1128 .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd()
1129 @*/
1130 int MatMultTranspose(Mat mat,Vec x,Vec y)
1131 {
1132   int ierr;
1133 
1134   PetscFunctionBegin;
1135   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1136   PetscValidType(mat);
1137   MatPreallocated(mat);
1138   PetscValidHeaderSpecific(x,VEC_COOKIE);
1139   PetscValidHeaderSpecific(y,VEC_COOKIE);
1140 
1141   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1142   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1143   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
1144   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N);
1145   if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->N,y->N);
1146 
1147   ierr = PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr);
1148   ierr = (*mat->ops->multtranspose)(mat,x,y);CHKERRQ(ierr);
1149   ierr = PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr);
1150   PetscFunctionReturn(0);
1151 }
1152 
1153 #undef __FUNCT__
1154 #define __FUNCT__ "MatMultAdd"
1155 /*@
1156     MatMultAdd -  Computes v3 = v2 + A * v1.
1157 
1158     Collective on Mat and Vec
1159 
1160     Input Parameters:
1161 +   mat - the matrix
1162 -   v1, v2 - the vectors
1163 
1164     Output Parameters:
1165 .   v3 - the result
1166 
1167     Notes:
1168     The vectors v1 and v3 cannot be the same.  I.e., one cannot
1169     call MatMultAdd(A,v1,v2,v1).
1170 
1171     Level: beginner
1172 
1173     Concepts: matrix vector product^addition
1174 
1175 .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd()
1176 @*/
1177 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3)
1178 {
1179   int ierr;
1180 
1181   PetscFunctionBegin;
1182   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1183   PetscValidType(mat);
1184   MatPreallocated(mat);
1185   PetscValidHeaderSpecific(v1,VEC_COOKIE);
1186   PetscValidHeaderSpecific(v2,VEC_COOKIE);
1187   PetscValidHeaderSpecific(v3,VEC_COOKIE);
1188 
1189   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1190   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1191   if (mat->N != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->N,v1->N);
1192   if (mat->M != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->M,v2->N);
1193   if (mat->M != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->M,v3->N);
1194   if (mat->m != v3->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: local dim %d %d",mat->m,v3->n);
1195   if (mat->m != v2->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: local dim %d %d",mat->m,v2->n);
1196   if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
1197 
1198   ierr = PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1199   ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr);
1200   ierr = PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1201   PetscFunctionReturn(0);
1202 }
1203 
1204 #undef __FUNCT__
1205 #define __FUNCT__ "MatMultTransposeAdd"
1206 /*@
1207    MatMultTransposeAdd - Computes v3 = v2 + A' * v1.
1208 
1209    Collective on Mat and Vec
1210 
1211    Input Parameters:
1212 +  mat - the matrix
1213 -  v1, v2 - the vectors
1214 
1215    Output Parameters:
1216 .  v3 - the result
1217 
1218    Notes:
1219    The vectors v1 and v3 cannot be the same.  I.e., one cannot
1220    call MatMultTransposeAdd(A,v1,v2,v1).
1221 
1222    Level: beginner
1223 
1224    Concepts: matrix vector product^transpose and addition
1225 
1226 .seealso: MatMultTranspose(), MatMultAdd(), MatMult()
1227 @*/
1228 int MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
1229 {
1230   int ierr;
1231 
1232   PetscFunctionBegin;
1233   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1234   PetscValidType(mat);
1235   MatPreallocated(mat);
1236   PetscValidHeaderSpecific(v1,VEC_COOKIE);
1237   PetscValidHeaderSpecific(v2,VEC_COOKIE);
1238   PetscValidHeaderSpecific(v3,VEC_COOKIE);
1239 
1240   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1241   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1242   if (!mat->ops->multtransposeadd) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1243   if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
1244   if (mat->M != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->M,v1->N);
1245   if (mat->N != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->N,v2->N);
1246   if (mat->N != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->N,v3->N);
1247 
1248   ierr = PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1249   ierr = (*mat->ops->multtransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr);
1250   ierr = PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1251   PetscFunctionReturn(0);
1252 }
1253 /* ------------------------------------------------------------*/
1254 #undef __FUNCT__
1255 #define __FUNCT__ "MatGetInfo"
1256 /*@C
1257    MatGetInfo - Returns information about matrix storage (number of
1258    nonzeros, memory, etc.).
1259 
1260    Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used
1261    as the flag
1262 
1263    Input Parameters:
1264 .  mat - the matrix
1265 
1266    Output Parameters:
1267 +  flag - flag indicating the type of parameters to be returned
1268    (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors,
1269    MAT_GLOBAL_SUM - sum over all processors)
1270 -  info - matrix information context
1271 
1272    Notes:
1273    The MatInfo context contains a variety of matrix data, including
1274    number of nonzeros allocated and used, number of mallocs during
1275    matrix assembly, etc.  Additional information for factored matrices
1276    is provided (such as the fill ratio, number of mallocs during
1277    factorization, etc.).  Much of this info is printed to STDOUT
1278    when using the runtime options
1279 $       -log_info -mat_view_info
1280 
1281    Example for C/C++ Users:
1282    See the file ${PETSC_DIR}/include/petscmat.h for a complete list of
1283    data within the MatInfo context.  For example,
1284 .vb
1285       MatInfo info;
1286       Mat     A;
1287       double  mal, nz_a, nz_u;
1288 
1289       MatGetInfo(A,MAT_LOCAL,&info);
1290       mal  = info.mallocs;
1291       nz_a = info.nz_allocated;
1292 .ve
1293 
1294    Example for Fortran Users:
1295    Fortran users should declare info as a double precision
1296    array of dimension MAT_INFO_SIZE, and then extract the parameters
1297    of interest.  See the file ${PETSC_DIR}/include/finclude/petscmat.h
1298    a complete list of parameter names.
1299 .vb
1300       double  precision info(MAT_INFO_SIZE)
1301       double  precision mal, nz_a
1302       Mat     A
1303       integer ierr
1304 
1305       call MatGetInfo(A,MAT_LOCAL,info,ierr)
1306       mal = info(MAT_INFO_MALLOCS)
1307       nz_a = info(MAT_INFO_NZ_ALLOCATED)
1308 .ve
1309 
1310     Level: intermediate
1311 
1312     Concepts: matrices^getting information on
1313 
1314 @*/
1315 int MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info)
1316 {
1317   int ierr;
1318 
1319   PetscFunctionBegin;
1320   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1321   PetscValidType(mat);
1322   MatPreallocated(mat);
1323   PetscValidPointer(info);
1324   if (!mat->ops->getinfo) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1325   ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr);
1326   PetscFunctionReturn(0);
1327 }
1328 
1329 /* ----------------------------------------------------------*/
1330 #undef __FUNCT__
1331 #define __FUNCT__ "MatILUDTFactor"
1332 /*@C
1333    MatILUDTFactor - Performs a drop tolerance ILU factorization.
1334 
1335    Collective on Mat
1336 
1337    Input Parameters:
1338 +  mat - the matrix
1339 .  info - information about the factorization to be done
1340 .  row - row permutation
1341 -  col - column permutation
1342 
1343    Output Parameters:
1344 .  fact - the factored matrix
1345 
1346    Level: developer
1347 
1348    Notes:
1349    Most users should employ the simplified SLES interface for linear solvers
1350    instead of working directly with matrix algebra routines such as this.
1351    See, e.g., SLESCreate().
1352 
1353    This is currently only supported for the SeqAIJ matrix format using code
1354    from Yousef Saad's SPARSEKIT2  package (translated to C with f2c) and/or
1355    Matlab. SPARSEKIT2 is copyrighted by Yousef Saad with the GNU copyright
1356    and thus can be distributed with PETSc.
1357 
1358     Concepts: matrices^ILUDT factorization
1359 
1360 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
1361 @*/
1362 int MatILUDTFactor(Mat mat,MatILUInfo *info,IS row,IS col,Mat *fact)
1363 {
1364   int ierr;
1365 
1366   PetscFunctionBegin;
1367   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1368   PetscValidType(mat);
1369   MatPreallocated(mat);
1370   PetscValidPointer(fact);
1371   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1372   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1373   if (!mat->ops->iludtfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1374 
1375   ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1376   ierr = (*mat->ops->iludtfactor)(mat,info,row,col,fact);CHKERRQ(ierr);
1377   ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1378 
1379   PetscFunctionReturn(0);
1380 }
1381 
1382 #undef __FUNCT__
1383 #define __FUNCT__ "MatLUFactor"
1384 /*@
1385    MatLUFactor - Performs in-place LU factorization of matrix.
1386 
1387    Collective on Mat
1388 
1389    Input Parameters:
1390 +  mat - the matrix
1391 .  row - row permutation
1392 .  col - column permutation
1393 -  info - options for factorization, includes
1394 $          fill - expected fill as ratio of original fill.
1395 $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
1396 $                   Run with the option -log_info to determine an optimal value to use
1397 
1398    Notes:
1399    Most users should employ the simplified SLES interface for linear solvers
1400    instead of working directly with matrix algebra routines such as this.
1401    See, e.g., SLESCreate().
1402 
1403    This changes the state of the matrix to a factored matrix; it cannot be used
1404    for example with MatSetValues() unless one first calls MatSetUnfactored().
1405 
1406    Level: developer
1407 
1408    Concepts: matrices^LU factorization
1409 
1410 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(),
1411           MatGetOrdering(), MatSetUnfactored()
1412 
1413 @*/
1414 int MatLUFactor(Mat mat,IS row,IS col,MatLUInfo *info)
1415 {
1416   int ierr;
1417 
1418   PetscFunctionBegin;
1419   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1420   PetscValidType(mat);
1421   MatPreallocated(mat);
1422   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1423   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1424   if (!mat->ops->lufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1425 
1426   ierr = PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr);
1427   ierr = (*mat->ops->lufactor)(mat,row,col,info);CHKERRQ(ierr);
1428   ierr = PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr);
1429   PetscFunctionReturn(0);
1430 }
1431 
1432 #undef __FUNCT__
1433 #define __FUNCT__ "MatILUFactor"
1434 /*@
1435    MatILUFactor - Performs in-place ILU factorization of matrix.
1436 
1437    Collective on Mat
1438 
1439    Input Parameters:
1440 +  mat - the matrix
1441 .  row - row permutation
1442 .  col - column permutation
1443 -  info - structure containing
1444 $      levels - number of levels of fill.
1445 $      expected fill - as ratio of original fill.
1446 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
1447                 missing diagonal entries)
1448 
1449    Notes:
1450    Probably really in-place only when level of fill is zero, otherwise allocates
1451    new space to store factored matrix and deletes previous memory.
1452 
1453    Most users should employ the simplified SLES interface for linear solvers
1454    instead of working directly with matrix algebra routines such as this.
1455    See, e.g., SLESCreate().
1456 
1457    Level: developer
1458 
1459    Concepts: matrices^ILU factorization
1460 
1461 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
1462 @*/
1463 int MatILUFactor(Mat mat,IS row,IS col,MatILUInfo *info)
1464 {
1465   int ierr;
1466 
1467   PetscFunctionBegin;
1468   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1469   PetscValidType(mat);
1470   MatPreallocated(mat);
1471   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
1472   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1473   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1474   if (!mat->ops->ilufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1475 
1476   ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1477   ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr);
1478   ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1479   PetscFunctionReturn(0);
1480 }
1481 
1482 #undef __FUNCT__
1483 #define __FUNCT__ "MatLUFactorSymbolic"
1484 /*@
1485    MatLUFactorSymbolic - Performs symbolic LU factorization of matrix.
1486    Call this routine before calling MatLUFactorNumeric().
1487 
1488    Collective on Mat
1489 
1490    Input Parameters:
1491 +  mat - the matrix
1492 .  row, col - row and column permutations
1493 -  info - options for factorization, includes
1494 $          fill - expected fill as ratio of original fill.
1495 $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
1496 $                   Run with the option -log_info to determine an optimal value to use
1497 
1498    Output Parameter:
1499 .  fact - new matrix that has been symbolically factored
1500 
1501    Notes:
1502    See the users manual for additional information about
1503    choosing the fill factor for better efficiency.
1504 
1505    Most users should employ the simplified SLES interface for linear solvers
1506    instead of working directly with matrix algebra routines such as this.
1507    See, e.g., SLESCreate().
1508 
1509    Level: developer
1510 
1511    Concepts: matrices^LU symbolic factorization
1512 
1513 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor()
1514 @*/
1515 int MatLUFactorSymbolic(Mat mat,IS row,IS col,MatLUInfo *info,Mat *fact)
1516 {
1517   int ierr;
1518 
1519   PetscFunctionBegin;
1520   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1521   PetscValidType(mat);
1522   MatPreallocated(mat);
1523   PetscValidPointer(fact);
1524   PetscValidHeaderSpecific(row,IS_COOKIE);
1525   PetscValidHeaderSpecific(col,IS_COOKIE);
1526   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1527   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1528   if (!mat->ops->lufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic LU",mat->type_name);
1529 
1530   ierr = PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
1531   ierr = (*mat->ops->lufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr);
1532   ierr = PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
1533   PetscFunctionReturn(0);
1534 }
1535 
1536 #undef __FUNCT__
1537 #define __FUNCT__ "MatLUFactorNumeric"
1538 /*@
1539    MatLUFactorNumeric - Performs numeric LU factorization of a matrix.
1540    Call this routine after first calling MatLUFactorSymbolic().
1541 
1542    Collective on Mat
1543 
1544    Input Parameters:
1545 +  mat - the matrix
1546 -  fact - the matrix generated for the factor, from MatLUFactorSymbolic()
1547 
1548    Notes:
1549    See MatLUFactor() for in-place factorization.  See
1550    MatCholeskyFactorNumeric() for the symmetric, positive definite case.
1551 
1552    Most users should employ the simplified SLES interface for linear solvers
1553    instead of working directly with matrix algebra routines such as this.
1554    See, e.g., SLESCreate().
1555 
1556    Level: developer
1557 
1558    Concepts: matrices^LU numeric factorization
1559 
1560 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor()
1561 @*/
1562 int MatLUFactorNumeric(Mat mat,Mat *fact)
1563 {
1564   int        ierr;
1565   PetscTruth flg;
1566 
1567   PetscFunctionBegin;
1568   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1569   PetscValidType(mat);
1570   MatPreallocated(mat);
1571   PetscValidPointer(fact);
1572   PetscValidHeaderSpecific(*fact,MAT_COOKIE);
1573   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1574   if (mat->M != (*fact)->M || mat->N != (*fact)->N) {
1575     SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dimensions are different %d should = %d %d should = %d",
1576             mat->M,(*fact)->M,mat->N,(*fact)->N);
1577   }
1578   if (!(*fact)->ops->lufactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1579 
1580   ierr = PetscLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1581   ierr = (*(*fact)->ops->lufactornumeric)(mat,fact);CHKERRQ(ierr);
1582   ierr = PetscLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1583   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_view_draw",&flg);CHKERRQ(ierr);
1584   if (flg) {
1585     ierr = PetscOptionsHasName(PETSC_NULL,"-mat_view_contour",&flg);CHKERRQ(ierr);
1586     if (flg) {
1587       ierr = PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
1588     }
1589     ierr = MatView(*fact,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1590     ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1591     if (flg) {
1592       ierr = PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1593     }
1594   }
1595   PetscFunctionReturn(0);
1596 }
1597 
1598 #undef __FUNCT__
1599 #define __FUNCT__ "MatCholeskyFactor"
1600 /*@
1601    MatCholeskyFactor - Performs in-place Cholesky factorization of a
1602    symmetric matrix.
1603 
1604    Collective on Mat
1605 
1606    Input Parameters:
1607 +  mat - the matrix
1608 .  perm - row and column permutations
1609 -  f - expected fill as ratio of original fill
1610 
1611    Notes:
1612    See MatLUFactor() for the nonsymmetric case.  See also
1613    MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric().
1614 
1615    Most users should employ the simplified SLES interface for linear solvers
1616    instead of working directly with matrix algebra routines such as this.
1617    See, e.g., SLESCreate().
1618 
1619    Level: developer
1620 
1621    Concepts: matrices^Cholesky factorization
1622 
1623 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric()
1624           MatGetOrdering()
1625 
1626 @*/
1627 int MatCholeskyFactor(Mat mat,IS perm,PetscReal f)
1628 {
1629   int ierr;
1630 
1631   PetscFunctionBegin;
1632   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1633   PetscValidType(mat);
1634   MatPreallocated(mat);
1635   PetscValidHeaderSpecific(perm,IS_COOKIE);
1636   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square");
1637   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1638   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1639   if (!mat->ops->choleskyfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1640 
1641   ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr);
1642   ierr = (*mat->ops->choleskyfactor)(mat,perm,f);CHKERRQ(ierr);
1643   ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr);
1644   PetscFunctionReturn(0);
1645 }
1646 
1647 #undef __FUNCT__
1648 #define __FUNCT__ "MatCholeskyFactorSymbolic"
1649 /*@
1650    MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization
1651    of a symmetric matrix.
1652 
1653    Collective on Mat
1654 
1655    Input Parameters:
1656 +  mat - the matrix
1657 .  perm - row and column permutations
1658 -  f - expected fill as ratio of original
1659 
1660    Output Parameter:
1661 .  fact - the factored matrix
1662 
1663    Notes:
1664    See MatLUFactorSymbolic() for the nonsymmetric case.  See also
1665    MatCholeskyFactor() and MatCholeskyFactorNumeric().
1666 
1667    Most users should employ the simplified SLES interface for linear solvers
1668    instead of working directly with matrix algebra routines such as this.
1669    See, e.g., SLESCreate().
1670 
1671    Level: developer
1672 
1673    Concepts: matrices^Cholesky symbolic factorization
1674 
1675 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric()
1676           MatGetOrdering()
1677 
1678 @*/
1679 int MatCholeskyFactorSymbolic(Mat mat,IS perm,PetscReal f,Mat *fact)
1680 {
1681   int ierr;
1682 
1683   PetscFunctionBegin;
1684   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1685   PetscValidType(mat);
1686   MatPreallocated(mat);
1687   PetscValidPointer(fact);
1688   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square");
1689   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1690   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1691   if (!mat->ops->choleskyfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1692 
1693   ierr = PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
1694   ierr = (*mat->ops->choleskyfactorsymbolic)(mat,perm,f,fact);CHKERRQ(ierr);
1695   ierr = PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
1696   PetscFunctionReturn(0);
1697 }
1698 
1699 #undef __FUNCT__
1700 #define __FUNCT__ "MatCholeskyFactorNumeric"
1701 /*@
1702    MatCholeskyFactorNumeric - Performs numeric Cholesky factorization
1703    of a symmetric matrix. Call this routine after first calling
1704    MatCholeskyFactorSymbolic().
1705 
1706    Collective on Mat
1707 
1708    Input Parameter:
1709 .  mat - the initial matrix
1710 
1711    Output Parameter:
1712 .  fact - the factored matrix
1713 
1714    Notes:
1715    Most users should employ the simplified SLES interface for linear solvers
1716    instead of working directly with matrix algebra routines such as this.
1717    See, e.g., SLESCreate().
1718 
1719    Level: developer
1720 
1721    Concepts: matrices^Cholesky numeric factorization
1722 
1723 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric()
1724 @*/
1725 int MatCholeskyFactorNumeric(Mat mat,Mat *fact)
1726 {
1727   int ierr;
1728 
1729   PetscFunctionBegin;
1730   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1731   PetscValidType(mat);
1732   MatPreallocated(mat);
1733   PetscValidPointer(fact);
1734   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1735   if (!(*fact)->ops->choleskyfactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1736   if (mat->M != (*fact)->M || mat->N != (*fact)->N) {
1737     SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dim %d should = %d %d should = %d",
1738             mat->M,(*fact)->M,mat->N,(*fact)->N);
1739   }
1740 
1741   ierr = PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1742   ierr = (*(*fact)->ops->choleskyfactornumeric)(mat,fact);CHKERRQ(ierr);
1743   ierr = PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1744   PetscFunctionReturn(0);
1745 }
1746 
1747 /* ----------------------------------------------------------------*/
1748 #undef __FUNCT__
1749 #define __FUNCT__ "MatSolve"
1750 /*@
1751    MatSolve - Solves A x = b, given a factored matrix.
1752 
1753    Collective on Mat and Vec
1754 
1755    Input Parameters:
1756 +  mat - the factored matrix
1757 -  b - the right-hand-side vector
1758 
1759    Output Parameter:
1760 .  x - the result vector
1761 
1762    Notes:
1763    The vectors b and x cannot be the same.  I.e., one cannot
1764    call MatSolve(A,x,x).
1765 
1766    Notes:
1767    Most users should employ the simplified SLES interface for linear solvers
1768    instead of working directly with matrix algebra routines such as this.
1769    See, e.g., SLESCreate().
1770 
1771    Level: developer
1772 
1773    Concepts: matrices^triangular solves
1774 
1775 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd()
1776 @*/
1777 int MatSolve(Mat mat,Vec b,Vec x)
1778 {
1779   int ierr;
1780 
1781   PetscFunctionBegin;
1782   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1783   PetscValidType(mat);
1784   MatPreallocated(mat);
1785   PetscValidHeaderSpecific(b,VEC_COOKIE);
1786   PetscValidHeaderSpecific(x,VEC_COOKIE);
1787   PetscCheckSameComm(mat,b);
1788   PetscCheckSameComm(mat,x);
1789   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1790   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1791   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1792   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1793   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1794   if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0);
1795 
1796   if (!mat->ops->solve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1797   ierr = PetscLogEventBegin(MAT_Solve,mat,b,x,0);CHKERRQ(ierr);
1798   ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr);
1799   ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr);
1800   PetscFunctionReturn(0);
1801 }
1802 
1803 #undef __FUNCT__
1804 #define __FUNCT__ "MatForwardSolve"
1805 /* @
1806    MatForwardSolve - Solves L x = b, given a factored matrix, A = LU.
1807 
1808    Collective on Mat and Vec
1809 
1810    Input Parameters:
1811 +  mat - the factored matrix
1812 -  b - the right-hand-side vector
1813 
1814    Output Parameter:
1815 .  x - the result vector
1816 
1817    Notes:
1818    MatSolve() should be used for most applications, as it performs
1819    a forward solve followed by a backward solve.
1820 
1821    The vectors b and x cannot be the same.  I.e., one cannot
1822    call MatForwardSolve(A,x,x).
1823 
1824    Most users should employ the simplified SLES interface for linear solvers
1825    instead of working directly with matrix algebra routines such as this.
1826    See, e.g., SLESCreate().
1827 
1828    Level: developer
1829 
1830    Concepts: matrices^forward solves
1831 
1832 .seealso: MatSolve(), MatBackwardSolve()
1833 @ */
1834 int MatForwardSolve(Mat mat,Vec b,Vec x)
1835 {
1836   int ierr;
1837 
1838   PetscFunctionBegin;
1839   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1840   PetscValidType(mat);
1841   MatPreallocated(mat);
1842   PetscValidHeaderSpecific(b,VEC_COOKIE);
1843   PetscValidHeaderSpecific(x,VEC_COOKIE);
1844   PetscCheckSameComm(mat,b);
1845   PetscCheckSameComm(mat,x);
1846   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1847   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1848   if (!mat->ops->forwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1849   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1850   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1851   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1852 
1853   ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr);
1854   ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr);
1855   ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr);
1856   PetscFunctionReturn(0);
1857 }
1858 
1859 #undef __FUNCT__
1860 #define __FUNCT__ "MatBackwardSolve"
1861 /* @
1862    MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU.
1863 
1864    Collective on Mat and Vec
1865 
1866    Input Parameters:
1867 +  mat - the factored matrix
1868 -  b - the right-hand-side vector
1869 
1870    Output Parameter:
1871 .  x - the result vector
1872 
1873    Notes:
1874    MatSolve() should be used for most applications, as it performs
1875    a forward solve followed by a backward solve.
1876 
1877    The vectors b and x cannot be the same.  I.e., one cannot
1878    call MatBackwardSolve(A,x,x).
1879 
1880    Most users should employ the simplified SLES interface for linear solvers
1881    instead of working directly with matrix algebra routines such as this.
1882    See, e.g., SLESCreate().
1883 
1884    Level: developer
1885 
1886    Concepts: matrices^backward solves
1887 
1888 .seealso: MatSolve(), MatForwardSolve()
1889 @ */
1890 int MatBackwardSolve(Mat mat,Vec b,Vec x)
1891 {
1892   int ierr;
1893 
1894   PetscFunctionBegin;
1895   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1896   PetscValidType(mat);
1897   MatPreallocated(mat);
1898   PetscValidHeaderSpecific(b,VEC_COOKIE);
1899   PetscValidHeaderSpecific(x,VEC_COOKIE);
1900   PetscCheckSameComm(mat,b);
1901   PetscCheckSameComm(mat,x);
1902   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1903   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1904   if (!mat->ops->backwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1905   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1906   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1907   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1908 
1909   ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr);
1910   ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr);
1911   ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr);
1912   PetscFunctionReturn(0);
1913 }
1914 
1915 #undef __FUNCT__
1916 #define __FUNCT__ "MatSolveAdd"
1917 /*@
1918    MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix.
1919 
1920    Collective on Mat and Vec
1921 
1922    Input Parameters:
1923 +  mat - the factored matrix
1924 .  b - the right-hand-side vector
1925 -  y - the vector to be added to
1926 
1927    Output Parameter:
1928 .  x - the result vector
1929 
1930    Notes:
1931    The vectors b and x cannot be the same.  I.e., one cannot
1932    call MatSolveAdd(A,x,y,x).
1933 
1934    Most users should employ the simplified SLES interface for linear solvers
1935    instead of working directly with matrix algebra routines such as this.
1936    See, e.g., SLESCreate().
1937 
1938    Level: developer
1939 
1940    Concepts: matrices^triangular solves
1941 
1942 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd()
1943 @*/
1944 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x)
1945 {
1946   Scalar one = 1.0;
1947   Vec    tmp;
1948   int    ierr;
1949 
1950   PetscFunctionBegin;
1951   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1952   PetscValidType(mat);
1953   MatPreallocated(mat);
1954   PetscValidHeaderSpecific(y,VEC_COOKIE);
1955   PetscValidHeaderSpecific(b,VEC_COOKIE);
1956   PetscValidHeaderSpecific(x,VEC_COOKIE);
1957   PetscCheckSameComm(mat,b);
1958   PetscCheckSameComm(mat,y);
1959   PetscCheckSameComm(mat,x);
1960   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1961   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1962   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1963   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1964   if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N);
1965   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1966   if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n);
1967 
1968   ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr);
1969   if (mat->ops->solveadd)  {
1970     ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr);
1971   } else {
1972     /* do the solve then the add manually */
1973     if (x != y) {
1974       ierr = MatSolve(mat,b,x);CHKERRQ(ierr);
1975       ierr = VecAXPY(&one,y,x);CHKERRQ(ierr);
1976     } else {
1977       ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr);
1978       PetscLogObjectParent(mat,tmp);
1979       ierr = VecCopy(x,tmp);CHKERRQ(ierr);
1980       ierr = MatSolve(mat,b,x);CHKERRQ(ierr);
1981       ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr);
1982       ierr = VecDestroy(tmp);CHKERRQ(ierr);
1983     }
1984   }
1985   ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr);
1986   PetscFunctionReturn(0);
1987 }
1988 
1989 #undef __FUNCT__
1990 #define __FUNCT__ "MatSolveTranspose"
1991 /*@
1992    MatSolveTranspose - Solves A' x = b, given a factored matrix.
1993 
1994    Collective on Mat and Vec
1995 
1996    Input Parameters:
1997 +  mat - the factored matrix
1998 -  b - the right-hand-side vector
1999 
2000    Output Parameter:
2001 .  x - the result vector
2002 
2003    Notes:
2004    The vectors b and x cannot be the same.  I.e., one cannot
2005    call MatSolveTranspose(A,x,x).
2006 
2007    Most users should employ the simplified SLES interface for linear solvers
2008    instead of working directly with matrix algebra routines such as this.
2009    See, e.g., SLESCreate().
2010 
2011    Level: developer
2012 
2013    Concepts: matrices^triangular solves
2014 
2015 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd()
2016 @*/
2017 int MatSolveTranspose(Mat mat,Vec b,Vec x)
2018 {
2019   int ierr;
2020 
2021   PetscFunctionBegin;
2022   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2023   PetscValidType(mat);
2024   MatPreallocated(mat);
2025   PetscValidHeaderSpecific(b,VEC_COOKIE);
2026   PetscValidHeaderSpecific(x,VEC_COOKIE);
2027   PetscCheckSameComm(mat,b);
2028   PetscCheckSameComm(mat,x);
2029   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2030   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2031   if (!mat->ops->solvetranspose) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s",mat->type_name);
2032   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N);
2033   if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N);
2034 
2035   ierr = PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr);
2036   ierr = (*mat->ops->solvetranspose)(mat,b,x);CHKERRQ(ierr);
2037   ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr);
2038   PetscFunctionReturn(0);
2039 }
2040 
2041 #undef __FUNCT__
2042 #define __FUNCT__ "MatSolveTransposeAdd"
2043 /*@
2044    MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a
2045                       factored matrix.
2046 
2047    Collective on Mat and Vec
2048 
2049    Input Parameters:
2050 +  mat - the factored matrix
2051 .  b - the right-hand-side vector
2052 -  y - the vector to be added to
2053 
2054    Output Parameter:
2055 .  x - the result vector
2056 
2057    Notes:
2058    The vectors b and x cannot be the same.  I.e., one cannot
2059    call MatSolveTransposeAdd(A,x,y,x).
2060 
2061    Most users should employ the simplified SLES interface for linear solvers
2062    instead of working directly with matrix algebra routines such as this.
2063    See, e.g., SLESCreate().
2064 
2065    Level: developer
2066 
2067    Concepts: matrices^triangular solves
2068 
2069 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose()
2070 @*/
2071 int MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x)
2072 {
2073   Scalar one = 1.0;
2074   int    ierr;
2075   Vec    tmp;
2076 
2077   PetscFunctionBegin;
2078   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2079   PetscValidType(mat);
2080   MatPreallocated(mat);
2081   PetscValidHeaderSpecific(y,VEC_COOKIE);
2082   PetscValidHeaderSpecific(b,VEC_COOKIE);
2083   PetscValidHeaderSpecific(x,VEC_COOKIE);
2084   PetscCheckSameComm(mat,b);
2085   PetscCheckSameComm(mat,y);
2086   PetscCheckSameComm(mat,x);
2087   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2088   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2089   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N);
2090   if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N);
2091   if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->N,y->N);
2092   if (x->n != y->n)   SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n);
2093 
2094   ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr);
2095   if (mat->ops->solvetransposeadd) {
2096     ierr = (*mat->ops->solvetransposeadd)(mat,b,y,x);CHKERRQ(ierr);
2097   } else {
2098     /* do the solve then the add manually */
2099     if (x != y) {
2100       ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr);
2101       ierr = VecAXPY(&one,y,x);CHKERRQ(ierr);
2102     } else {
2103       ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr);
2104       PetscLogObjectParent(mat,tmp);
2105       ierr = VecCopy(x,tmp);CHKERRQ(ierr);
2106       ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr);
2107       ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr);
2108       ierr = VecDestroy(tmp);CHKERRQ(ierr);
2109     }
2110   }
2111   ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr);
2112   PetscFunctionReturn(0);
2113 }
2114 /* ----------------------------------------------------------------*/
2115 
2116 #undef __FUNCT__
2117 #define __FUNCT__ "MatRelax"
2118 /*@
2119    MatRelax - Computes one relaxation sweep.
2120 
2121    Collective on Mat and Vec
2122 
2123    Input Parameters:
2124 +  mat - the matrix
2125 .  b - the right hand side
2126 .  omega - the relaxation factor
2127 .  flag - flag indicating the type of SOR (see below)
2128 .  shift -  diagonal shift
2129 -  its - the number of iterations
2130 
2131    Output Parameters:
2132 .  x - the solution (can contain an initial guess)
2133 
2134    SOR Flags:
2135 .     SOR_FORWARD_SWEEP - forward SOR
2136 .     SOR_BACKWARD_SWEEP - backward SOR
2137 .     SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR)
2138 .     SOR_LOCAL_FORWARD_SWEEP - local forward SOR
2139 .     SOR_LOCAL_BACKWARD_SWEEP - local forward SOR
2140 .     SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR
2141 .     SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies
2142          upper/lower triangular part of matrix to
2143          vector (with omega)
2144 .     SOR_ZERO_INITIAL_GUESS - zero initial guess
2145 
2146    Notes:
2147    SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and
2148    SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings
2149    on each processor.
2150 
2151    Application programmers will not generally use MatRelax() directly,
2152    but instead will employ the SLES/PC interface.
2153 
2154    Notes for Advanced Users:
2155    The flags are implemented as bitwise inclusive or operations.
2156    For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP)
2157    to specify a zero initial guess for SSOR.
2158 
2159    Most users should employ the simplified SLES interface for linear solvers
2160    instead of working directly with matrix algebra routines such as this.
2161    See, e.g., SLESCreate().
2162 
2163    Level: developer
2164 
2165    Concepts: matrices^relaxation
2166    Concepts: matrices^SOR
2167    Concepts: matrices^Gauss-Seidel
2168 
2169 @*/
2170 int MatRelax(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,int its,Vec x)
2171 {
2172   int ierr;
2173 
2174   PetscFunctionBegin;
2175   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2176   PetscValidType(mat);
2177   MatPreallocated(mat);
2178   PetscValidHeaderSpecific(b,VEC_COOKIE);
2179   PetscValidHeaderSpecific(x,VEC_COOKIE);
2180   PetscCheckSameComm(mat,b);
2181   PetscCheckSameComm(mat,x);
2182   if (!mat->ops->relax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2183   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2184   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2185   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
2186   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
2187   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
2188 
2189   ierr = PetscLogEventBegin(MAT_Relax,mat,b,x,0);CHKERRQ(ierr);
2190   ierr =(*mat->ops->relax)(mat,b,omega,flag,shift,its,x);CHKERRQ(ierr);
2191   ierr = PetscLogEventEnd(MAT_Relax,mat,b,x,0);CHKERRQ(ierr);
2192   PetscFunctionReturn(0);
2193 }
2194 
2195 #undef __FUNCT__
2196 #define __FUNCT__ "MatCopy_Basic"
2197 /*
2198       Default matrix copy routine.
2199 */
2200 int MatCopy_Basic(Mat A,Mat B,MatStructure str)
2201 {
2202   int    ierr,i,rstart,rend,nz,*cwork;
2203   Scalar *vwork;
2204 
2205   PetscFunctionBegin;
2206   ierr = MatZeroEntries(B);CHKERRQ(ierr);
2207   ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr);
2208   for (i=rstart; i<rend; i++) {
2209     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
2210     ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
2211     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
2212   }
2213   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2214   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2215   PetscFunctionReturn(0);
2216 }
2217 
2218 #undef __FUNCT__
2219 #define __FUNCT__ "MatCopy"
2220 /*@C
2221    MatCopy - Copys a matrix to another matrix.
2222 
2223    Collective on Mat
2224 
2225    Input Parameters:
2226 +  A - the matrix
2227 -  str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN
2228 
2229    Output Parameter:
2230 .  B - where the copy is put
2231 
2232    Notes:
2233    If you use SAME_NONZERO_PATTERN then the zero matrices had better have the
2234    same nonzero pattern or the routine will crash.
2235 
2236    MatCopy() copies the matrix entries of a matrix to another existing
2237    matrix (after first zeroing the second matrix).  A related routine is
2238    MatConvert(), which first creates a new matrix and then copies the data.
2239 
2240    Level: intermediate
2241 
2242    Concepts: matrices^copying
2243 
2244 .seealso: MatConvert()
2245 @*/
2246 int MatCopy(Mat A,Mat B,MatStructure str)
2247 {
2248   int ierr;
2249 
2250   PetscFunctionBegin;
2251   PetscValidHeaderSpecific(A,MAT_COOKIE);
2252   PetscValidHeaderSpecific(B,MAT_COOKIE);
2253   PetscValidType(A);
2254   MatPreallocated(A);
2255   PetscValidType(B);
2256   MatPreallocated(B);
2257   PetscCheckSameComm(A,B);
2258   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2259   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2260   if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %d %d",A->M,B->M,
2261                                              A->N,B->N);
2262 
2263   ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr);
2264   if (A->ops->copy) {
2265     ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr);
2266   } else { /* generic conversion */
2267     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2268   }
2269   ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr);
2270   PetscFunctionReturn(0);
2271 }
2272 
2273 #include "petscsys.h"
2274 PetscTruth MatConvertRegisterAllCalled = PETSC_FALSE;
2275 PetscFList      MatConvertList              = 0;
2276 
2277 #undef __FUNCT__
2278 #define __FUNCT__ "MatConvertRegister"
2279 /*@C
2280     MatConvertRegister - Allows one to register a routine that reads matrices
2281         from a binary file for a particular matrix type.
2282 
2283   Not Collective
2284 
2285   Input Parameters:
2286 +   type - the type of matrix (defined in include/petscmat.h), for example, MATSEQAIJ.
2287 -   Converter - the function that reads the matrix from the binary file.
2288 
2289   Level: developer
2290 
2291 .seealso: MatConvertRegisterAll(), MatConvert()
2292 
2293 @*/
2294 int MatConvertRegister(char *sname,char *path,char *name,int (*function)(Mat,MatType,Mat*))
2295 {
2296   int  ierr;
2297   char fullname[256];
2298 
2299   PetscFunctionBegin;
2300   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2301   ierr = PetscFListAdd(&MatConvertList,sname,fullname,(void (*)())function);CHKERRQ(ierr);
2302   PetscFunctionReturn(0);
2303 }
2304 
2305 #undef __FUNCT__
2306 #define __FUNCT__ "MatConvert"
2307 /*@C
2308    MatConvert - Converts a matrix to another matrix, either of the same
2309    or different type.
2310 
2311    Collective on Mat
2312 
2313    Input Parameters:
2314 +  mat - the matrix
2315 -  newtype - new matrix type.  Use MATSAME to create a new matrix of the
2316    same type as the original matrix.
2317 
2318    Output Parameter:
2319 .  M - pointer to place new matrix
2320 
2321    Notes:
2322    MatConvert() first creates a new matrix and then copies the data from
2323    the first matrix.  A related routine is MatCopy(), which copies the matrix
2324    entries of one matrix to another already existing matrix context.
2325 
2326    Level: intermediate
2327 
2328    Concepts: matrices^converting between storage formats
2329 
2330 .seealso: MatCopy(), MatDuplicate()
2331 @*/
2332 int MatConvert(Mat mat,MatType newtype,Mat *M)
2333 {
2334   int        ierr;
2335   PetscTruth sametype,issame,flg;
2336   char       convname[256],mtype[256];
2337 
2338   PetscFunctionBegin;
2339   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2340   PetscValidType(mat);
2341   MatPreallocated(mat);
2342   PetscValidPointer(M);
2343   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2344   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2345 
2346   ierr = PetscOptionsGetString(PETSC_NULL,"-matconvert_type",mtype,256,&flg);CHKERRQ(ierr);
2347   if (flg) {
2348     newtype = mtype;
2349   }
2350   ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2351 
2352   ierr = PetscTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr);
2353   ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr);
2354   if ((sametype || issame) && mat->ops->duplicate) {
2355     ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr);
2356   } else {
2357     int (*conv)(Mat,MatType,Mat*);
2358     ierr = PetscStrcpy(convname,"MatConvertTo_");CHKERRQ(ierr);
2359     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
2360     ierr =  PetscFListFind(mat->comm,MatConvertList,convname,(void(**)())&conv);CHKERRQ(ierr);
2361     if (conv) {
2362       ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr);
2363     } else {
2364       ierr = PetscStrcpy(convname,"MatConvert_");CHKERRQ(ierr);
2365       ierr = PetscStrcat(convname,mat->type_name);CHKERRQ(ierr);
2366       ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
2367       ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
2368       ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
2369       ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)())&conv);CHKERRQ(ierr);
2370       if (conv) {
2371         ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr);
2372       } else {
2373         if (mat->ops->convert) {
2374           ierr = (*mat->ops->convert)(mat,newtype,M);CHKERRQ(ierr);
2375         } else {
2376           ierr = MatConvert_Basic(mat,newtype,M);CHKERRQ(ierr);
2377         }
2378       }
2379     }
2380   }
2381   ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2382   PetscFunctionReturn(0);
2383 }
2384 
2385 
2386 #undef __FUNCT__
2387 #define __FUNCT__ "MatDuplicate"
2388 /*@C
2389    MatDuplicate - Duplicates a matrix including the non-zero structure.
2390 
2391    Collective on Mat
2392 
2393    Input Parameters:
2394 +  mat - the matrix
2395 -  op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy nonzero
2396         values as well or not
2397 
2398    Output Parameter:
2399 .  M - pointer to place new matrix
2400 
2401    Level: intermediate
2402 
2403    Concepts: matrices^duplicating
2404 
2405 .seealso: MatCopy(), MatConvert()
2406 @*/
2407 int MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M)
2408 {
2409   int ierr;
2410 
2411   PetscFunctionBegin;
2412   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2413   PetscValidType(mat);
2414   MatPreallocated(mat);
2415   PetscValidPointer(M);
2416   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2417   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2418 
2419   *M  = 0;
2420   ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2421   if (!mat->ops->duplicate) {
2422     SETERRQ(PETSC_ERR_SUP,"Not written for this matrix type");
2423   }
2424   ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr);
2425   ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2426   PetscFunctionReturn(0);
2427 }
2428 
2429 #undef __FUNCT__
2430 #define __FUNCT__ "MatGetDiagonal"
2431 /*@
2432    MatGetDiagonal - Gets the diagonal of a matrix.
2433 
2434    Collective on Mat and Vec
2435 
2436    Input Parameters:
2437 +  mat - the matrix
2438 -  v - the vector for storing the diagonal
2439 
2440    Output Parameter:
2441 .  v - the diagonal of the matrix
2442 
2443    Notes:
2444    For the SeqAIJ matrix format, this routine may also be called
2445    on a LU factored matrix; in that case it routines the reciprocal of
2446    the diagonal entries in U. It returns the entries permuted by the
2447    row and column permutation used during the symbolic factorization.
2448 
2449    Level: intermediate
2450 
2451    Concepts: matrices^accessing diagonals
2452 
2453 .seealso: MatGetRow(), MatGetSubmatrices(), MatGetSubmatrix(), MatGetRowMax()
2454 @*/
2455 int MatGetDiagonal(Mat mat,Vec v)
2456 {
2457   int ierr;
2458 
2459   PetscFunctionBegin;
2460   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2461   PetscValidType(mat);
2462   MatPreallocated(mat);
2463   PetscValidHeaderSpecific(v,VEC_COOKIE);
2464   /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */
2465   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2466   if (!mat->ops->getdiagonal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2467 
2468   ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr);
2469   PetscFunctionReturn(0);
2470 }
2471 
2472 #undef __FUNCT__
2473 #define __FUNCT__ "MatGetRowMax"
2474 /*@
2475    MatGetRowMax - Gets the maximum value (in absolute value) of each
2476         row of the matrix
2477 
2478    Collective on Mat and Vec
2479 
2480    Input Parameters:
2481 .  mat - the matrix
2482 
2483    Output Parameter:
2484 .  v - the vector for storing the maximums
2485 
2486    Level: intermediate
2487 
2488    Concepts: matrices^getting row maximums
2489 
2490 .seealso: MatGetDiagonal(), MatGetSubmatrices(), MatGetSubmatrix()
2491 @*/
2492 int MatGetRowMax(Mat mat,Vec v)
2493 {
2494   int ierr;
2495 
2496   PetscFunctionBegin;
2497   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2498   PetscValidType(mat);
2499   MatPreallocated(mat);
2500   PetscValidHeaderSpecific(v,VEC_COOKIE);
2501   /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */
2502   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2503   if (!mat->ops->getrowmax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2504 
2505   ierr = (*mat->ops->getrowmax)(mat,v);CHKERRQ(ierr);
2506   PetscFunctionReturn(0);
2507 }
2508 
2509 #undef __FUNCT__
2510 #define __FUNCT__ "MatTranspose"
2511 /*@C
2512    MatTranspose - Computes an in-place or out-of-place transpose of a matrix.
2513 
2514    Collective on Mat
2515 
2516    Input Parameter:
2517 .  mat - the matrix to transpose
2518 
2519    Output Parameters:
2520 .  B - the transpose (or pass in PETSC_NULL for an in-place transpose)
2521 
2522    Level: intermediate
2523 
2524    Concepts: matrices^transposing
2525 
2526 .seealso: MatMultTranspose(), MatMultTransposeAdd()
2527 @*/
2528 int MatTranspose(Mat mat,Mat *B)
2529 {
2530   int ierr;
2531 
2532   PetscFunctionBegin;
2533   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2534   PetscValidType(mat);
2535   MatPreallocated(mat);
2536   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2537   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2538   if (!mat->ops->transpose) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2539   ierr = (*mat->ops->transpose)(mat,B);CHKERRQ(ierr);
2540   PetscFunctionReturn(0);
2541 }
2542 
2543 #undef __FUNCT__
2544 #define __FUNCT__ "MatPermute"
2545 /*@C
2546    MatPermute - Creates a new matrix with rows and columns permuted from the
2547    original.
2548 
2549    Collective on Mat
2550 
2551    Input Parameters:
2552 +  mat - the matrix to permute
2553 .  row - row permutation, each processor supplies only the permutation for its rows
2554 -  col - column permutation, each processor needs the entire column permutation, that is
2555          this is the same size as the total number of columns in the matrix
2556 
2557    Output Parameters:
2558 .  B - the permuted matrix
2559 
2560    Level: advanced
2561 
2562    Concepts: matrices^permuting
2563 
2564 .seealso: MatGetOrdering()
2565 @*/
2566 int MatPermute(Mat mat,IS row,IS col,Mat *B)
2567 {
2568   int ierr;
2569 
2570   PetscFunctionBegin;
2571   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2572   PetscValidType(mat);
2573   MatPreallocated(mat);
2574   PetscValidHeaderSpecific(row,IS_COOKIE);
2575   PetscValidHeaderSpecific(col,IS_COOKIE);
2576   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2577   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2578   if (!mat->ops->permute) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2579   ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr);
2580   PetscFunctionReturn(0);
2581 }
2582 
2583 #undef __FUNCT__
2584 #define __FUNCT__ "MatEqual"
2585 /*@
2586    MatEqual - Compares two matrices.
2587 
2588    Collective on Mat
2589 
2590    Input Parameters:
2591 +  A - the first matrix
2592 -  B - the second matrix
2593 
2594    Output Parameter:
2595 .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.
2596 
2597    Level: intermediate
2598 
2599    Concepts: matrices^equality between
2600 @*/
2601 int MatEqual(Mat A,Mat B,PetscTruth *flg)
2602 {
2603   int ierr;
2604 
2605   PetscFunctionBegin;
2606   PetscValidHeaderSpecific(A,MAT_COOKIE);
2607   PetscValidHeaderSpecific(B,MAT_COOKIE);
2608   PetscValidType(A);
2609   MatPreallocated(A);
2610   PetscValidType(B);
2611   MatPreallocated(B);
2612   PetscValidIntPointer(flg);
2613   PetscCheckSameComm(A,B);
2614   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2615   if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2616   if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %d %d %d %d",A->M,B->M,A->N,B->N);
2617   if (!A->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",A->type_name);
2618   ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr);
2619   PetscFunctionReturn(0);
2620 }
2621 
2622 #undef __FUNCT__
2623 #define __FUNCT__ "MatDiagonalScale"
2624 /*@
2625    MatDiagonalScale - Scales a matrix on the left and right by diagonal
2626    matrices that are stored as vectors.  Either of the two scaling
2627    matrices can be PETSC_NULL.
2628 
2629    Collective on Mat
2630 
2631    Input Parameters:
2632 +  mat - the matrix to be scaled
2633 .  l - the left scaling vector (or PETSC_NULL)
2634 -  r - the right scaling vector (or PETSC_NULL)
2635 
2636    Notes:
2637    MatDiagonalScale() computes A = LAR, where
2638    L = a diagonal matrix, R = a diagonal matrix
2639 
2640    Level: intermediate
2641 
2642    Concepts: matrices^diagonal scaling
2643    Concepts: diagonal scaling of matrices
2644 
2645 .seealso: MatScale()
2646 @*/
2647 int MatDiagonalScale(Mat mat,Vec l,Vec r)
2648 {
2649   int ierr;
2650 
2651   PetscFunctionBegin;
2652   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2653   PetscValidType(mat);
2654   MatPreallocated(mat);
2655   if (!mat->ops->diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2656   if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE);PetscCheckSameComm(mat,l);}
2657   if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE);PetscCheckSameComm(mat,r);}
2658   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2659   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2660 
2661   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2662   ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr);
2663   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2664   PetscFunctionReturn(0);
2665 }
2666 
2667 #undef __FUNCT__
2668 #define __FUNCT__ "MatScale"
2669 /*@
2670     MatScale - Scales all elements of a matrix by a given number.
2671 
2672     Collective on Mat
2673 
2674     Input Parameters:
2675 +   mat - the matrix to be scaled
2676 -   a  - the scaling value
2677 
2678     Output Parameter:
2679 .   mat - the scaled matrix
2680 
2681     Level: intermediate
2682 
2683     Concepts: matrices^scaling all entries
2684 
2685 .seealso: MatDiagonalScale()
2686 @*/
2687 int MatScale(Scalar *a,Mat mat)
2688 {
2689   int ierr;
2690 
2691   PetscFunctionBegin;
2692   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2693   PetscValidType(mat);
2694   MatPreallocated(mat);
2695   PetscValidScalarPointer(a);
2696   if (!mat->ops->scale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2697   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2698   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2699 
2700   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2701   ierr = (*mat->ops->scale)(a,mat);CHKERRQ(ierr);
2702   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2703   PetscFunctionReturn(0);
2704 }
2705 
2706 #undef __FUNCT__
2707 #define __FUNCT__ "MatNorm"
2708 /*@
2709    MatNorm - Calculates various norms of a matrix.
2710 
2711    Collective on Mat
2712 
2713    Input Parameters:
2714 +  mat - the matrix
2715 -  type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY
2716 
2717    Output Parameters:
2718 .  norm - the resulting norm
2719 
2720    Level: intermediate
2721 
2722    Concepts: matrices^norm
2723    Concepts: norm^of matrix
2724 @*/
2725 int MatNorm(Mat mat,NormType type,PetscReal *norm)
2726 {
2727   int ierr;
2728 
2729   PetscFunctionBegin;
2730   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2731   PetscValidType(mat);
2732   MatPreallocated(mat);
2733   PetscValidScalarPointer(norm);
2734 
2735   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2736   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2737   if (!mat->ops->norm) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2738   ierr = (*mat->ops->norm)(mat,type,norm);CHKERRQ(ierr);
2739   PetscFunctionReturn(0);
2740 }
2741 
2742 /*
2743      This variable is used to prevent counting of MatAssemblyBegin() that
2744    are called from within a MatAssemblyEnd().
2745 */
2746 static int MatAssemblyEnd_InUse = 0;
2747 #undef __FUNCT__
2748 #define __FUNCT__ "MatAssemblyBegin"
2749 /*@
2750    MatAssemblyBegin - Begins assembling the matrix.  This routine should
2751    be called after completing all calls to MatSetValues().
2752 
2753    Collective on Mat
2754 
2755    Input Parameters:
2756 +  mat - the matrix
2757 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
2758 
2759    Notes:
2760    MatSetValues() generally caches the values.  The matrix is ready to
2761    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
2762    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
2763    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
2764    using the matrix.
2765 
2766    Level: beginner
2767 
2768    Concepts: matrices^assembling
2769 
2770 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
2771 @*/
2772 int MatAssemblyBegin(Mat mat,MatAssemblyType type)
2773 {
2774   int ierr;
2775 
2776   PetscFunctionBegin;
2777   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2778   PetscValidType(mat);
2779   MatPreallocated(mat);
2780   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?");
2781   if (mat->assembled) {
2782     mat->was_assembled = PETSC_TRUE;
2783     mat->assembled     = PETSC_FALSE;
2784   }
2785   if (!MatAssemblyEnd_InUse) {
2786     ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
2787     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
2788     ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
2789   } else {
2790     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
2791   }
2792   PetscFunctionReturn(0);
2793 }
2794 
2795 #undef __FUNCT__
2796 #define __FUNCT__ "MatAssembed"
2797 /*@
2798    MatAssembled - Indicates if a matrix has been assembled and is ready for
2799      use; for example, in matrix-vector product.
2800 
2801    Collective on Mat
2802 
2803    Input Parameter:
2804 .  mat - the matrix
2805 
2806    Output Parameter:
2807 .  assembled - PETSC_TRUE or PETSC_FALSE
2808 
2809    Level: advanced
2810 
2811    Concepts: matrices^assembled?
2812 
2813 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
2814 @*/
2815 int MatAssembled(Mat mat,PetscTruth *assembled)
2816 {
2817   PetscFunctionBegin;
2818   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2819   PetscValidType(mat);
2820   MatPreallocated(mat);
2821   *assembled = mat->assembled;
2822   PetscFunctionReturn(0);
2823 }
2824 
2825 #undef __FUNCT__
2826 #define __FUNCT__ "MatView_Private"
2827 /*
2828     Processes command line options to determine if/how a matrix
2829   is to be viewed. Called by MatAssemblyEnd() and MatLoad().
2830 */
2831 int MatView_Private(Mat mat)
2832 {
2833   int        ierr;
2834   PetscTruth flg;
2835 
2836   PetscFunctionBegin;
2837   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_info",&flg);CHKERRQ(ierr);
2838   if (flg) {
2839     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr);
2840     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2841     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2842   }
2843   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_info_detailed",&flg);CHKERRQ(ierr);
2844   if (flg) {
2845     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO_LONG);CHKERRQ(ierr);
2846     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2847     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2848   }
2849   ierr = PetscOptionsHasName(mat->prefix,"-mat_view",&flg);CHKERRQ(ierr);
2850   if (flg) {
2851     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2852   }
2853   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_matlab",&flg);CHKERRQ(ierr);
2854   if (flg) {
2855     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr);
2856     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2857     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2858   }
2859   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_draw",&flg);CHKERRQ(ierr);
2860   if (flg) {
2861     ierr = PetscOptionsHasName(mat->prefix,"-mat_view_contour",&flg);CHKERRQ(ierr);
2862     if (flg) {
2863       PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
2864     }
2865     ierr = MatView(mat,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2866     ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2867     if (flg) {
2868       PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2869     }
2870   }
2871   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_socket",&flg);CHKERRQ(ierr);
2872   if (flg) {
2873     ierr = MatView(mat,PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
2874     ierr = PetscViewerFlush(PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
2875   }
2876   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_binary",&flg);CHKERRQ(ierr);
2877   if (flg) {
2878     ierr = MatView(mat,PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr);
2879     ierr = PetscViewerFlush(PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr);
2880   }
2881   PetscFunctionReturn(0);
2882 }
2883 
2884 #undef __FUNCT__
2885 #define __FUNCT__ "MatAssemblyEnd"
2886 /*@
2887    MatAssemblyEnd - Completes assembling the matrix.  This routine should
2888    be called after MatAssemblyBegin().
2889 
2890    Collective on Mat
2891 
2892    Input Parameters:
2893 +  mat - the matrix
2894 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
2895 
2896    Options Database Keys:
2897 +  -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly()
2898 .  -mat_view_info_detailed - Prints more detailed info
2899 .  -mat_view - Prints matrix in ASCII format
2900 .  -mat_view_matlab - Prints matrix in Matlab format
2901 .  -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
2902 .  -display <name> - Sets display name (default is host)
2903 -  -draw_pause <sec> - Sets number of seconds to pause after display
2904 
2905    Notes:
2906    MatSetValues() generally caches the values.  The matrix is ready to
2907    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
2908    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
2909    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
2910    using the matrix.
2911 
2912    Level: beginner
2913 
2914 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), MatView(), MatAssembled()
2915 @*/
2916 int MatAssemblyEnd(Mat mat,MatAssemblyType type)
2917 {
2918   int        ierr;
2919   static int inassm = 0;
2920 
2921   PetscFunctionBegin;
2922   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2923   PetscValidType(mat);
2924   MatPreallocated(mat);
2925 
2926   inassm++;
2927   MatAssemblyEnd_InUse++;
2928   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
2929     ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
2930     if (mat->ops->assemblyend) {
2931       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
2932     }
2933     ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
2934   } else {
2935     if (mat->ops->assemblyend) {
2936       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
2937     }
2938   }
2939 
2940   /* Flush assembly is not a true assembly */
2941   if (type != MAT_FLUSH_ASSEMBLY) {
2942     mat->assembled  = PETSC_TRUE; mat->num_ass++;
2943   }
2944   mat->insertmode = NOT_SET_VALUES;
2945   MatAssemblyEnd_InUse--;
2946 
2947   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
2948     ierr = MatView_Private(mat);CHKERRQ(ierr);
2949   }
2950   inassm--;
2951   PetscFunctionReturn(0);
2952 }
2953 
2954 
2955 #undef __FUNCT__
2956 #define __FUNCT__ "MatCompress"
2957 /*@
2958    MatCompress - Tries to store the matrix in as little space as
2959    possible.  May fail if memory is already fully used, since it
2960    tries to allocate new space.
2961 
2962    Collective on Mat
2963 
2964    Input Parameters:
2965 .  mat - the matrix
2966 
2967    Level: advanced
2968 
2969 @*/
2970 int MatCompress(Mat mat)
2971 {
2972   int ierr;
2973 
2974   PetscFunctionBegin;
2975   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2976   PetscValidType(mat);
2977   MatPreallocated(mat);
2978   if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);}
2979   PetscFunctionReturn(0);
2980 }
2981 
2982 #undef __FUNCT__
2983 #define __FUNCT__ "MatSetOption"
2984 /*@
2985    MatSetOption - Sets a parameter option for a matrix. Some options
2986    may be specific to certain storage formats.  Some options
2987    determine how values will be inserted (or added). Sorted,
2988    row-oriented input will generally assemble the fastest. The default
2989    is row-oriented, nonsorted input.
2990 
2991    Collective on Mat
2992 
2993    Input Parameters:
2994 +  mat - the matrix
2995 -  option - the option, one of those listed below (and possibly others),
2996              e.g., MAT_ROWS_SORTED, MAT_NEW_NONZERO_LOCATION_ERR
2997 
2998    Options Describing Matrix Structure:
2999 +    MAT_SYMMETRIC - symmetric in terms of both structure and value
3000 -    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
3001 
3002    Options For Use with MatSetValues():
3003    Insert a logically dense subblock, which can be
3004 +    MAT_ROW_ORIENTED - row-oriented
3005 .    MAT_COLUMN_ORIENTED - column-oriented
3006 .    MAT_ROWS_SORTED - sorted by row
3007 .    MAT_ROWS_UNSORTED - not sorted by row
3008 .    MAT_COLUMNS_SORTED - sorted by column
3009 -    MAT_COLUMNS_UNSORTED - not sorted by column
3010 
3011    Not these options reflect the data you pass in with MatSetValues(); it has
3012    nothing to do with how the data is stored internally in the matrix
3013    data structure.
3014 
3015    When (re)assembling a matrix, we can restrict the input for
3016    efficiency/debugging purposes.  These options include
3017 +    MAT_NO_NEW_NONZERO_LOCATIONS - additional insertions will not be
3018         allowed if they generate a new nonzero
3019 .    MAT_YES_NEW_NONZERO_LOCATIONS - additional insertions will be allowed
3020 .    MAT_NO_NEW_DIAGONALS - additional insertions will not be allowed if
3021          they generate a nonzero in a new diagonal (for block diagonal format only)
3022 .    MAT_YES_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only)
3023 .    MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries
3024 .    MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry
3025 -    MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly
3026 
3027    Notes:
3028    Some options are relevant only for particular matrix types and
3029    are thus ignored by others.  Other options are not supported by
3030    certain matrix types and will generate an error message if set.
3031 
3032    If using a Fortran 77 module to compute a matrix, one may need to
3033    use the column-oriented option (or convert to the row-oriented
3034    format).
3035 
3036    MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion
3037    that would generate a new entry in the nonzero structure is instead
3038    ignored.  Thus, if memory has not alredy been allocated for this particular
3039    data, then the insertion is ignored. For dense matrices, in which
3040    the entire array is allocated, no entries are ever ignored.
3041 
3042    MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion
3043    that would generate a new entry in the nonzero structure instead produces
3044    an error. (Currently supported for AIJ and BAIJ formats only.)
3045    This is a useful flag when using SAME_NONZERO_PATTERN in calling
3046    SLESSetOperators() to ensure that the nonzero pattern truely does
3047    remain unchanged.
3048 
3049    MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion
3050    that would generate a new entry that has not been preallocated will
3051    instead produce an error. (Currently supported for AIJ and BAIJ formats
3052    only.) This is a useful flag when debugging matrix memory preallocation.
3053 
3054    MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for
3055    other processors should be dropped, rather than stashed.
3056    This is useful if you know that the "owning" processor is also
3057    always generating the correct matrix entries, so that PETSc need
3058    not transfer duplicate entries generated on another processor.
3059 
3060    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
3061    searches during matrix assembly. When this flag is set, the hash table
3062    is created during the first Matrix Assembly. This hash table is
3063    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
3064    to improve the searching of indices. MAT_NO_NEW_NONZERO_LOCATIONS flag
3065    should be used with MAT_USE_HASH_TABLE flag. This option is currently
3066    supported by MATMPIBAIJ format only.
3067 
3068    MAT_KEEP_ZEROED_ROWS indicates when MatZeroRows() is called the zeroed entries
3069    are kept in the nonzero structure
3070 
3071    MAT_IGNORE_ZERO_ENTRIES - when using ADD_VALUES for AIJ matrices this will stop
3072    zero values from creating a zero location in the matrix
3073 
3074    MAT_USE_INODES - indicates using inode version of the code - works with AIJ and
3075    ROWBS matrix types
3076 
3077    MAT_DO_NOT_USE_INODES - indicates not using inode version of the code - works
3078    with AIJ and ROWBS matrix types
3079 
3080    Level: intermediate
3081 
3082    Concepts: matrices^setting options
3083 
3084 @*/
3085 int MatSetOption(Mat mat,MatOption op)
3086 {
3087   int ierr;
3088 
3089   PetscFunctionBegin;
3090   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3091   PetscValidType(mat);
3092   MatPreallocated(mat);
3093   switch (op) {
3094   case MAT_SYMMETRIC:
3095     mat->symmetric              = PETSC_TRUE;
3096     mat->structurally_symmetric = PETSC_TRUE;
3097     break;
3098   case MAT_STRUCTURALLY_SYMMETRIC:
3099     mat->structurally_symmetric = PETSC_TRUE;
3100     break;
3101   default:
3102     if (mat->ops->setoption) {ierr = (*mat->ops->setoption)(mat,op);CHKERRQ(ierr);}
3103     break;
3104   }
3105   PetscFunctionReturn(0);
3106 }
3107 
3108 #undef __FUNCT__
3109 #define __FUNCT__ "MatZeroEntries"
3110 /*@
3111    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
3112    this routine retains the old nonzero structure.
3113 
3114    Collective on Mat
3115 
3116    Input Parameters:
3117 .  mat - the matrix
3118 
3119    Level: intermediate
3120 
3121    Concepts: matrices^zeroing
3122 
3123 .seealso: MatZeroRows()
3124 @*/
3125 int MatZeroEntries(Mat mat)
3126 {
3127   int ierr;
3128 
3129   PetscFunctionBegin;
3130   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3131   PetscValidType(mat);
3132   MatPreallocated(mat);
3133   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3134   if (!mat->ops->zeroentries) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3135 
3136   ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
3137   ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr);
3138   ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
3139   PetscFunctionReturn(0);
3140 }
3141 
3142 #undef __FUNCT__
3143 #define __FUNCT__ "MatZeroRows"
3144 /*@C
3145    MatZeroRows - Zeros all entries (except possibly the main diagonal)
3146    of a set of rows of a matrix.
3147 
3148    Collective on Mat
3149 
3150    Input Parameters:
3151 +  mat - the matrix
3152 .  is - index set of rows to remove
3153 -  diag - pointer to value put in all diagonals of eliminated rows.
3154           Note that diag is not a pointer to an array, but merely a
3155           pointer to a single value.
3156 
3157    Notes:
3158    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
3159    but does not release memory.  For the dense and block diagonal
3160    formats this does not alter the nonzero structure.
3161 
3162    If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure
3163    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
3164    merely zeroed.
3165 
3166    The user can set a value in the diagonal entry (or for the AIJ and
3167    row formats can optionally remove the main diagonal entry from the
3168    nonzero structure as well, by passing a null pointer (PETSC_NULL
3169    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
3170 
3171    For the parallel case, all processes that share the matrix (i.e.,
3172    those in the communicator used for matrix creation) MUST call this
3173    routine, regardless of whether any rows being zeroed are owned by
3174    them.
3175 
3176 
3177    Level: intermediate
3178 
3179    Concepts: matrices^zeroing rows
3180 
3181 .seealso: MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
3182 @*/
3183 int MatZeroRows(Mat mat,IS is,Scalar *diag)
3184 {
3185   int ierr;
3186 
3187   PetscFunctionBegin;
3188   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3189   PetscValidType(mat);
3190   MatPreallocated(mat);
3191   PetscValidHeaderSpecific(is,IS_COOKIE);
3192   if (diag) PetscValidScalarPointer(diag);
3193   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3194   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3195   if (!mat->ops->zerorows) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3196 
3197   ierr = (*mat->ops->zerorows)(mat,is,diag);CHKERRQ(ierr);
3198   ierr = MatView_Private(mat);CHKERRQ(ierr);
3199   PetscFunctionReturn(0);
3200 }
3201 
3202 #undef __FUNCT__
3203 #define __FUNCT__ "MatZeroRowsLocal"
3204 /*@C
3205    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
3206    of a set of rows of a matrix; using local numbering of rows.
3207 
3208    Collective on Mat
3209 
3210    Input Parameters:
3211 +  mat - the matrix
3212 .  is - index set of rows to remove
3213 -  diag - pointer to value put in all diagonals of eliminated rows.
3214           Note that diag is not a pointer to an array, but merely a
3215           pointer to a single value.
3216 
3217    Notes:
3218    Before calling MatZeroRowsLocal(), the user must first set the
3219    local-to-global mapping by calling MatSetLocalToGlobalMapping().
3220 
3221    For the AIJ matrix formats this removes the old nonzero structure,
3222    but does not release memory.  For the dense and block diagonal
3223    formats this does not alter the nonzero structure.
3224 
3225    The user can set a value in the diagonal entry (or for the AIJ and
3226    row formats can optionally remove the main diagonal entry from the
3227    nonzero structure as well, by passing a null pointer (PETSC_NULL
3228    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
3229 
3230    Level: intermediate
3231 
3232    Concepts: matrices^zeroing
3233 
3234 .seealso: MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
3235 @*/
3236 int MatZeroRowsLocal(Mat mat,IS is,Scalar *diag)
3237 {
3238   int ierr;
3239   IS  newis;
3240 
3241   PetscFunctionBegin;
3242   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3243   PetscValidType(mat);
3244   MatPreallocated(mat);
3245   PetscValidHeaderSpecific(is,IS_COOKIE);
3246   if (diag) PetscValidScalarPointer(diag);
3247   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3248   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3249 
3250   if (mat->ops->zerorowslocal) {
3251     ierr = (*mat->ops->zerorowslocal)(mat,is,diag);CHKERRQ(ierr);
3252   } else {
3253     if (!mat->mapping) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
3254     ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr);
3255     ierr = (*mat->ops->zerorows)(mat,newis,diag);CHKERRQ(ierr);
3256     ierr = ISDestroy(newis);CHKERRQ(ierr);
3257   }
3258   PetscFunctionReturn(0);
3259 }
3260 
3261 #undef __FUNCT__
3262 #define __FUNCT__ "MatGetSize"
3263 /*@
3264    MatGetSize - Returns the numbers of rows and columns in a matrix.
3265 
3266    Not Collective
3267 
3268    Input Parameter:
3269 .  mat - the matrix
3270 
3271    Output Parameters:
3272 +  m - the number of global rows
3273 -  n - the number of global columns
3274 
3275    Level: beginner
3276 
3277    Concepts: matrices^size
3278 
3279 .seealso: MatGetLocalSize()
3280 @*/
3281 int MatGetSize(Mat mat,int *m,int* n)
3282 {
3283   PetscFunctionBegin;
3284   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3285   if (m) *m = mat->M;
3286   if (n) *n = mat->N;
3287   PetscFunctionReturn(0);
3288 }
3289 
3290 #undef __FUNCT__
3291 #define __FUNCT__ "MatGetLocalSize"
3292 /*@
3293    MatGetLocalSize - Returns the number of rows and columns in a matrix
3294    stored locally.  This information may be implementation dependent, so
3295    use with care.
3296 
3297    Not Collective
3298 
3299    Input Parameters:
3300 .  mat - the matrix
3301 
3302    Output Parameters:
3303 +  m - the number of local rows
3304 -  n - the number of local columns
3305 
3306    Level: beginner
3307 
3308    Concepts: matrices^local size
3309 
3310 .seealso: MatGetSize()
3311 @*/
3312 int MatGetLocalSize(Mat mat,int *m,int* n)
3313 {
3314   PetscFunctionBegin;
3315   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3316   if (m) *m = mat->m;
3317   if (n) *n = mat->n;
3318   PetscFunctionReturn(0);
3319 }
3320 
3321 #undef __FUNCT__
3322 #define __FUNCT__ "MatGetOwnershipRange"
3323 /*@
3324    MatGetOwnershipRange - Returns the range of matrix rows owned by
3325    this processor, assuming that the matrix is laid out with the first
3326    n1 rows on the first processor, the next n2 rows on the second, etc.
3327    For certain parallel layouts this range may not be well defined.
3328 
3329    Not Collective
3330 
3331    Input Parameters:
3332 .  mat - the matrix
3333 
3334    Output Parameters:
3335 +  m - the global index of the first local row
3336 -  n - one more than the global index of the last local row
3337 
3338    Level: beginner
3339 
3340    Concepts: matrices^row ownership
3341 @*/
3342 int MatGetOwnershipRange(Mat mat,int *m,int* n)
3343 {
3344   int ierr;
3345 
3346   PetscFunctionBegin;
3347   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3348   PetscValidType(mat);
3349   MatPreallocated(mat);
3350   if (m) PetscValidIntPointer(m);
3351   if (n) PetscValidIntPointer(n);
3352   if (!mat->ops->getownershiprange) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3353   ierr = (*mat->ops->getownershiprange)(mat,m,n);CHKERRQ(ierr);
3354   PetscFunctionReturn(0);
3355 }
3356 
3357 #undef __FUNCT__
3358 #define __FUNCT__ "MatILUFactorSymbolic"
3359 /*@
3360    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
3361    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
3362    to complete the factorization.
3363 
3364    Collective on Mat
3365 
3366    Input Parameters:
3367 +  mat - the matrix
3368 .  row - row permutation
3369 .  column - column permutation
3370 -  info - structure containing
3371 $      levels - number of levels of fill.
3372 $      expected fill - as ratio of original fill.
3373 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
3374                 missing diagonal entries)
3375 
3376    Output Parameters:
3377 .  fact - new matrix that has been symbolically factored
3378 
3379    Notes:
3380    See the users manual for additional information about
3381    choosing the fill factor for better efficiency.
3382 
3383    Most users should employ the simplified SLES interface for linear solvers
3384    instead of working directly with matrix algebra routines such as this.
3385    See, e.g., SLESCreate().
3386 
3387    Level: developer
3388 
3389   Concepts: matrices^symbolic LU factorization
3390   Concepts: matrices^factorization
3391   Concepts: LU^symbolic factorization
3392 
3393 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
3394           MatGetOrdering()
3395 
3396 @*/
3397 int MatILUFactorSymbolic(Mat mat,IS row,IS col,MatILUInfo *info,Mat *fact)
3398 {
3399   int ierr;
3400 
3401   PetscFunctionBegin;
3402   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3403   PetscValidType(mat);
3404   MatPreallocated(mat);
3405   PetscValidPointer(fact);
3406   PetscValidHeaderSpecific(row,IS_COOKIE);
3407   PetscValidHeaderSpecific(col,IS_COOKIE);
3408   if (info && info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %d",info->levels);
3409   if (info && info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",info->fill);
3410   if (!mat->ops->ilufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic ILU",mat->type_name);
3411   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3412   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3413 
3414   ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
3415   ierr = (*mat->ops->ilufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr);
3416   ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
3417   PetscFunctionReturn(0);
3418 }
3419 
3420 #undef __FUNCT__
3421 #define __FUNCT__ "MatICCFactorSymbolic"
3422 /*@
3423    MatICCFactorSymbolic - Performs symbolic incomplete
3424    Cholesky factorization for a symmetric matrix.  Use
3425    MatCholeskyFactorNumeric() to complete the factorization.
3426 
3427    Collective on Mat
3428 
3429    Input Parameters:
3430 +  mat - the matrix
3431 .  perm - row and column permutation
3432 .  fill - levels of fill
3433 -  f - expected fill as ratio of original fill
3434 
3435    Output Parameter:
3436 .  fact - the factored matrix
3437 
3438    Notes:
3439    Currently only no-fill factorization is supported.
3440 
3441    Most users should employ the simplified SLES interface for linear solvers
3442    instead of working directly with matrix algebra routines such as this.
3443    See, e.g., SLESCreate().
3444 
3445    Level: developer
3446 
3447   Concepts: matrices^symbolic incomplete Cholesky factorization
3448   Concepts: matrices^factorization
3449   Concepts: Cholsky^symbolic factorization
3450 
3451 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor()
3452 @*/
3453 int MatICCFactorSymbolic(Mat mat,IS perm,PetscReal f,int fill,Mat *fact)
3454 {
3455   int ierr;
3456 
3457   PetscFunctionBegin;
3458   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3459   PetscValidType(mat);
3460   MatPreallocated(mat);
3461   PetscValidPointer(fact);
3462   PetscValidHeaderSpecific(perm,IS_COOKIE);
3463   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3464   if (fill < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Fill negative %d",fill);
3465   if (f < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",f);
3466   if (!mat->ops->iccfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic ICC",mat->type_name);
3467   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3468 
3469   ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
3470   ierr = (*mat->ops->iccfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr);
3471   ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
3472   PetscFunctionReturn(0);
3473 }
3474 
3475 #undef __FUNCT__
3476 #define __FUNCT__ "MatGetArray"
3477 /*@C
3478    MatGetArray - Returns a pointer to the element values in the matrix.
3479    The result of this routine is dependent on the underlying matrix data
3480    structure, and may not even work for certain matrix types.  You MUST
3481    call MatRestoreArray() when you no longer need to access the array.
3482 
3483    Not Collective
3484 
3485    Input Parameter:
3486 .  mat - the matrix
3487 
3488    Output Parameter:
3489 .  v - the location of the values
3490 
3491 
3492    Fortran Note:
3493    This routine is used differently from Fortran, e.g.,
3494 .vb
3495         Mat         mat
3496         Scalar      mat_array(1)
3497         PetscOffset i_mat
3498         int         ierr
3499         call MatGetArray(mat,mat_array,i_mat,ierr)
3500 
3501   C  Access first local entry in matrix; note that array is
3502   C  treated as one dimensional
3503         value = mat_array(i_mat + 1)
3504 
3505         [... other code ...]
3506         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3507 .ve
3508 
3509    See the Fortran chapter of the users manual and
3510    petsc/src/mat/examples/tests for details.
3511 
3512    Level: advanced
3513 
3514    Concepts: matrices^access array
3515 
3516 .seealso: MatRestoreArray(), MatGetArrayF90()
3517 @*/
3518 int MatGetArray(Mat mat,Scalar **v)
3519 {
3520   int ierr;
3521 
3522   PetscFunctionBegin;
3523   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3524   PetscValidType(mat);
3525   MatPreallocated(mat);
3526   PetscValidPointer(v);
3527   if (!mat->ops->getarray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3528   ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr);
3529   PetscFunctionReturn(0);
3530 }
3531 
3532 #undef __FUNCT__
3533 #define __FUNCT__ "MatRestoreArray"
3534 /*@C
3535    MatRestoreArray - Restores the matrix after MatGetArray() has been called.
3536 
3537    Not Collective
3538 
3539    Input Parameter:
3540 +  mat - the matrix
3541 -  v - the location of the values
3542 
3543    Fortran Note:
3544    This routine is used differently from Fortran, e.g.,
3545 .vb
3546         Mat         mat
3547         Scalar      mat_array(1)
3548         PetscOffset i_mat
3549         int         ierr
3550         call MatGetArray(mat,mat_array,i_mat,ierr)
3551 
3552   C  Access first local entry in matrix; note that array is
3553   C  treated as one dimensional
3554         value = mat_array(i_mat + 1)
3555 
3556         [... other code ...]
3557         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3558 .ve
3559 
3560    See the Fortran chapter of the users manual and
3561    petsc/src/mat/examples/tests for details
3562 
3563    Level: advanced
3564 
3565 .seealso: MatGetArray(), MatRestoreArrayF90()
3566 @*/
3567 int MatRestoreArray(Mat mat,Scalar **v)
3568 {
3569   int ierr;
3570 
3571   PetscFunctionBegin;
3572   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3573   PetscValidType(mat);
3574   MatPreallocated(mat);
3575   PetscValidPointer(v);
3576   if (!mat->ops->restorearray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3577   ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr);
3578   PetscFunctionReturn(0);
3579 }
3580 
3581 #undef __FUNCT__
3582 #define __FUNCT__ "MatGetSubMatrices"
3583 /*@C
3584    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
3585    points to an array of valid matrices, they may be reused to store the new
3586    submatrices.
3587 
3588    Collective on Mat
3589 
3590    Input Parameters:
3591 +  mat - the matrix
3592 .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
3593 .  irow, icol - index sets of rows and columns to extract
3594 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3595 
3596    Output Parameter:
3597 .  submat - the array of submatrices
3598 
3599    Notes:
3600    MatGetSubMatrices() can extract only sequential submatrices
3601    (from both sequential and parallel matrices). Use MatGetSubMatrix()
3602    to extract a parallel submatrix.
3603 
3604    When extracting submatrices from a parallel matrix, each processor can
3605    form a different submatrix by setting the rows and columns of its
3606    individual index sets according to the local submatrix desired.
3607 
3608    When finished using the submatrices, the user should destroy
3609    them with MatDestroySubMatrices().
3610 
3611    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
3612    original matrix has not changed from that last call to MatGetSubMatrices().
3613 
3614    This routine creates the matrices submat; you should NOT create them before
3615    calling it.
3616 
3617    Fortran Note:
3618    The Fortran interface is slightly different from that given below; it
3619    requires one to pass in  as submat a Mat (integer) array of size at least m.
3620 
3621    Level: advanced
3622 
3623    Concepts: matrices^accessing submatrices
3624    Concepts: submatrices
3625 
3626 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal()
3627 @*/
3628 int MatGetSubMatrices(Mat mat,int n,IS *irow,IS *icol,MatReuse scall,Mat **submat)
3629 {
3630   int        ierr;
3631 
3632   PetscFunctionBegin;
3633   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3634   PetscValidType(mat);
3635   MatPreallocated(mat);
3636   if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3637   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3638 
3639   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
3640   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
3641   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
3642   PetscFunctionReturn(0);
3643 }
3644 
3645 #undef __FUNCT__
3646 #define __FUNCT__ "MatDestroyMatrices"
3647 /*@C
3648    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
3649 
3650    Collective on Mat
3651 
3652    Input Parameters:
3653 +  n - the number of local matrices
3654 -  mat - the matrices
3655 
3656    Level: advanced
3657 
3658 .seealso: MatGetSubMatrices()
3659 @*/
3660 int MatDestroyMatrices(int n,Mat **mat)
3661 {
3662   int ierr,i;
3663 
3664   PetscFunctionBegin;
3665   if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %d",n);
3666   PetscValidPointer(mat);
3667   for (i=0; i<n; i++) {
3668     ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr);
3669   }
3670   /* memory is allocated even if n = 0 */
3671   ierr = PetscFree(*mat);CHKERRQ(ierr);
3672   PetscFunctionReturn(0);
3673 }
3674 
3675 #undef __FUNCT__
3676 #define __FUNCT__ "MatIncreaseOverlap"
3677 /*@
3678    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
3679    replaces the index sets by larger ones that represent submatrices with
3680    additional overlap.
3681 
3682    Collective on Mat
3683 
3684    Input Parameters:
3685 +  mat - the matrix
3686 .  n   - the number of index sets
3687 .  is  - the array of pointers to index sets
3688 -  ov  - the additional overlap requested
3689 
3690    Level: developer
3691 
3692    Concepts: overlap
3693    Concepts: ASM^computing overlap
3694 
3695 .seealso: MatGetSubMatrices()
3696 @*/
3697 int MatIncreaseOverlap(Mat mat,int n,IS *is,int ov)
3698 {
3699   int ierr;
3700 
3701   PetscFunctionBegin;
3702   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3703   PetscValidType(mat);
3704   MatPreallocated(mat);
3705   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3706   if (mat->factor)     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3707 
3708   if (!ov) PetscFunctionReturn(0);
3709   if (!mat->ops->increaseoverlap) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3710   ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
3711   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
3712   ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
3713   PetscFunctionReturn(0);
3714 }
3715 
3716 #undef __FUNCT__
3717 #define __FUNCT__ "MatPrintHelp"
3718 /*@
3719    MatPrintHelp - Prints all the options for the matrix.
3720 
3721    Collective on Mat
3722 
3723    Input Parameter:
3724 .  mat - the matrix
3725 
3726    Options Database Keys:
3727 +  -help - Prints matrix options
3728 -  -h - Prints matrix options
3729 
3730    Level: developer
3731 
3732 .seealso: MatCreate(), MatCreateXXX()
3733 @*/
3734 int MatPrintHelp(Mat mat)
3735 {
3736   static PetscTruth called = PETSC_FALSE;
3737   int               ierr;
3738   MPI_Comm          comm;
3739 
3740   PetscFunctionBegin;
3741   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3742   PetscValidType(mat);
3743   MatPreallocated(mat);
3744 
3745   comm = mat->comm;
3746   if (!called) {
3747     ierr = (*PetscHelpPrintf)(comm,"General matrix options:\n");CHKERRQ(ierr);
3748     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_info: view basic matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr);
3749     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_info_detailed: view detailed matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr);
3750     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_draw: draw nonzero matrix structure during MatAssemblyEnd()\n");CHKERRQ(ierr);
3751     ierr = (*PetscHelpPrintf)(comm,"      -draw_pause <sec>: set seconds of display pause\n");CHKERRQ(ierr);
3752     ierr = (*PetscHelpPrintf)(comm,"      -display <name>: set alternate display\n");CHKERRQ(ierr);
3753     called = PETSC_TRUE;
3754   }
3755   if (mat->ops->printhelp) {
3756     ierr = (*mat->ops->printhelp)(mat);CHKERRQ(ierr);
3757   }
3758   PetscFunctionReturn(0);
3759 }
3760 
3761 #undef __FUNCT__
3762 #define __FUNCT__ "MatGetBlockSize"
3763 /*@
3764    MatGetBlockSize - Returns the matrix block size; useful especially for the
3765    block row and block diagonal formats.
3766 
3767    Not Collective
3768 
3769    Input Parameter:
3770 .  mat - the matrix
3771 
3772    Output Parameter:
3773 .  bs - block size
3774 
3775    Notes:
3776    Block diagonal formats are MATSEQBDIAG, MATMPIBDIAG.
3777    Block row formats are MATSEQBAIJ, MATMPIBAIJ
3778 
3779    Level: intermediate
3780 
3781    Concepts: matrices^block size
3782 
3783 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag()
3784 @*/
3785 int MatGetBlockSize(Mat mat,int *bs)
3786 {
3787   int ierr;
3788 
3789   PetscFunctionBegin;
3790   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3791   PetscValidType(mat);
3792   MatPreallocated(mat);
3793   PetscValidIntPointer(bs);
3794   if (!mat->ops->getblocksize) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3795   ierr = (*mat->ops->getblocksize)(mat,bs);CHKERRQ(ierr);
3796   PetscFunctionReturn(0);
3797 }
3798 
3799 #undef __FUNCT__
3800 #define __FUNCT__ "MatGetRowIJ"
3801 /*@C
3802     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
3803 
3804    Collective on Mat
3805 
3806     Input Parameters:
3807 +   mat - the matrix
3808 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
3809 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3810                 symmetrized
3811 
3812     Output Parameters:
3813 +   n - number of rows in the (possibly compressed) matrix
3814 .   ia - the row pointers
3815 .   ja - the column indices
3816 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
3817 
3818     Level: developer
3819 
3820 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
3821 @*/
3822 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3823 {
3824   int ierr;
3825 
3826   PetscFunctionBegin;
3827   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3828   PetscValidType(mat);
3829   MatPreallocated(mat);
3830   if (ia) PetscValidIntPointer(ia);
3831   if (ja) PetscValidIntPointer(ja);
3832   PetscValidIntPointer(done);
3833   if (!mat->ops->getrowij) *done = PETSC_FALSE;
3834   else {
3835     *done = PETSC_TRUE;
3836     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3837   }
3838   PetscFunctionReturn(0);
3839 }
3840 
3841 #undef __FUNCT__
3842 #define __FUNCT__ "MatGetColumnIJ"
3843 /*@C
3844     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
3845 
3846     Collective on Mat
3847 
3848     Input Parameters:
3849 +   mat - the matrix
3850 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3851 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3852                 symmetrized
3853 
3854     Output Parameters:
3855 +   n - number of columns in the (possibly compressed) matrix
3856 .   ia - the column pointers
3857 .   ja - the row indices
3858 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
3859 
3860     Level: developer
3861 
3862 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
3863 @*/
3864 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3865 {
3866   int ierr;
3867 
3868   PetscFunctionBegin;
3869   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3870   PetscValidType(mat);
3871   MatPreallocated(mat);
3872   if (ia) PetscValidIntPointer(ia);
3873   if (ja) PetscValidIntPointer(ja);
3874   PetscValidIntPointer(done);
3875 
3876   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
3877   else {
3878     *done = PETSC_TRUE;
3879     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3880   }
3881   PetscFunctionReturn(0);
3882 }
3883 
3884 #undef __FUNCT__
3885 #define __FUNCT__ "MatRestoreRowIJ"
3886 /*@C
3887     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
3888     MatGetRowIJ().
3889 
3890     Collective on Mat
3891 
3892     Input Parameters:
3893 +   mat - the matrix
3894 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3895 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3896                 symmetrized
3897 
3898     Output Parameters:
3899 +   n - size of (possibly compressed) matrix
3900 .   ia - the row pointers
3901 .   ja - the column indices
3902 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
3903 
3904     Level: developer
3905 
3906 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
3907 @*/
3908 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3909 {
3910   int ierr;
3911 
3912   PetscFunctionBegin;
3913   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3914   PetscValidType(mat);
3915   MatPreallocated(mat);
3916   if (ia) PetscValidIntPointer(ia);
3917   if (ja) PetscValidIntPointer(ja);
3918   PetscValidIntPointer(done);
3919 
3920   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
3921   else {
3922     *done = PETSC_TRUE;
3923     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3924   }
3925   PetscFunctionReturn(0);
3926 }
3927 
3928 #undef __FUNCT__
3929 #define __FUNCT__ "MatRestoreColumnIJ"
3930 /*@C
3931     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
3932     MatGetColumnIJ().
3933 
3934     Collective on Mat
3935 
3936     Input Parameters:
3937 +   mat - the matrix
3938 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3939 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3940                 symmetrized
3941 
3942     Output Parameters:
3943 +   n - size of (possibly compressed) matrix
3944 .   ia - the column pointers
3945 .   ja - the row indices
3946 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
3947 
3948     Level: developer
3949 
3950 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
3951 @*/
3952 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3953 {
3954   int ierr;
3955 
3956   PetscFunctionBegin;
3957   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3958   PetscValidType(mat);
3959   MatPreallocated(mat);
3960   if (ia) PetscValidIntPointer(ia);
3961   if (ja) PetscValidIntPointer(ja);
3962   PetscValidIntPointer(done);
3963 
3964   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
3965   else {
3966     *done = PETSC_TRUE;
3967     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3968   }
3969   PetscFunctionReturn(0);
3970 }
3971 
3972 #undef __FUNCT__
3973 #define __FUNCT__ "MatColoringPatch"
3974 /*@C
3975     MatColoringPatch -Used inside matrix coloring routines that
3976     use MatGetRowIJ() and/or MatGetColumnIJ().
3977 
3978     Collective on Mat
3979 
3980     Input Parameters:
3981 +   mat - the matrix
3982 .   n   - number of colors
3983 -   colorarray - array indicating color for each column
3984 
3985     Output Parameters:
3986 .   iscoloring - coloring generated using colorarray information
3987 
3988     Level: developer
3989 
3990 .seealso: MatGetRowIJ(), MatGetColumnIJ()
3991 
3992 @*/
3993 int MatColoringPatch(Mat mat,int n,int ncolors,int *colorarray,ISColoring *iscoloring)
3994 {
3995   int ierr;
3996 
3997   PetscFunctionBegin;
3998   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3999   PetscValidType(mat);
4000   MatPreallocated(mat);
4001   PetscValidIntPointer(colorarray);
4002 
4003   if (!mat->ops->coloringpatch){
4004     ierr = ISColoringCreate(mat->comm,n,colorarray,iscoloring);CHKERRQ(ierr);
4005   } else {
4006     ierr = (*mat->ops->coloringpatch)(mat,n,ncolors,colorarray,iscoloring);CHKERRQ(ierr);
4007   }
4008   PetscFunctionReturn(0);
4009 }
4010 
4011 
4012 #undef __FUNCT__
4013 #define __FUNCT__ "MatSetUnfactored"
4014 /*@
4015    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
4016 
4017    Collective on Mat
4018 
4019    Input Parameter:
4020 .  mat - the factored matrix to be reset
4021 
4022    Notes:
4023    This routine should be used only with factored matrices formed by in-place
4024    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
4025    format).  This option can save memory, for example, when solving nonlinear
4026    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
4027    ILU(0) preconditioner.
4028 
4029    Note that one can specify in-place ILU(0) factorization by calling
4030 .vb
4031      PCType(pc,PCILU);
4032      PCILUSeUseInPlace(pc);
4033 .ve
4034    or by using the options -pc_type ilu -pc_ilu_in_place
4035 
4036    In-place factorization ILU(0) can also be used as a local
4037    solver for the blocks within the block Jacobi or additive Schwarz
4038    methods (runtime option: -sub_pc_ilu_in_place).  See the discussion
4039    of these preconditioners in the users manual for details on setting
4040    local solver options.
4041 
4042    Most users should employ the simplified SLES interface for linear solvers
4043    instead of working directly with matrix algebra routines such as this.
4044    See, e.g., SLESCreate().
4045 
4046    Level: developer
4047 
4048 .seealso: PCILUSetUseInPlace(), PCLUSetUseInPlace()
4049 
4050    Concepts: matrices^unfactored
4051 
4052 @*/
4053 int MatSetUnfactored(Mat mat)
4054 {
4055   int ierr;
4056 
4057   PetscFunctionBegin;
4058   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4059   PetscValidType(mat);
4060   MatPreallocated(mat);
4061   mat->factor = 0;
4062   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
4063   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
4064   PetscFunctionReturn(0);
4065 }
4066 
4067 /*MC
4068     MatGetArrayF90 - Accesses a matrix array from Fortran90.
4069 
4070     Synopsis:
4071     MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
4072 
4073     Not collective
4074 
4075     Input Parameter:
4076 .   x - matrix
4077 
4078     Output Parameters:
4079 +   xx_v - the Fortran90 pointer to the array
4080 -   ierr - error code
4081 
4082     Example of Usage:
4083 .vb
4084       Scalar, pointer xx_v(:)
4085       ....
4086       call MatGetArrayF90(x,xx_v,ierr)
4087       a = xx_v(3)
4088       call MatRestoreArrayF90(x,xx_v,ierr)
4089 .ve
4090 
4091     Notes:
4092     Not yet supported for all F90 compilers
4093 
4094     Level: advanced
4095 
4096 .seealso:  MatRestoreArrayF90(), MatGetArray(), MatRestoreArray()
4097 
4098     Concepts: matrices^accessing array
4099 
4100 M*/
4101 
4102 /*MC
4103     MatRestoreArrayF90 - Restores a matrix array that has been
4104     accessed with MatGetArrayF90().
4105 
4106     Synopsis:
4107     MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
4108 
4109     Not collective
4110 
4111     Input Parameters:
4112 +   x - matrix
4113 -   xx_v - the Fortran90 pointer to the array
4114 
4115     Output Parameter:
4116 .   ierr - error code
4117 
4118     Example of Usage:
4119 .vb
4120        Scalar, pointer xx_v(:)
4121        ....
4122        call MatGetArrayF90(x,xx_v,ierr)
4123        a = xx_v(3)
4124        call MatRestoreArrayF90(x,xx_v,ierr)
4125 .ve
4126 
4127     Notes:
4128     Not yet supported for all F90 compilers
4129 
4130     Level: advanced
4131 
4132 .seealso:  MatGetArrayF90(), MatGetArray(), MatRestoreArray()
4133 
4134 M*/
4135 
4136 
4137 #undef __FUNCT__
4138 #define __FUNCT__ "MatGetSubMatrix"
4139 /*@
4140     MatGetSubMatrix - Gets a single submatrix on the same number of processors
4141                       as the original matrix.
4142 
4143     Collective on Mat
4144 
4145     Input Parameters:
4146 +   mat - the original matrix
4147 .   isrow - rows this processor should obtain
4148 .   iscol - columns for all processors you wish to keep
4149 .   csize - number of columns "local" to this processor (does nothing for sequential
4150             matrices). This should match the result from VecGetLocalSize(x,...) if you
4151             plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE
4152 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
4153 
4154     Output Parameter:
4155 .   newmat - the new submatrix, of the same type as the old
4156 
4157     Level: advanced
4158 
4159     Notes: the iscol argument MUST be the same on each processor.
4160 
4161       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
4162    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
4163    to this routine with a mat of the same nonzero structure will reuse the matrix
4164    generated the first time.
4165 
4166     Concepts: matrices^submatrices
4167 
4168 .seealso: MatGetSubMatrices()
4169 @*/
4170 int MatGetSubMatrix(Mat mat,IS isrow,IS iscol,int csize,MatReuse cll,Mat *newmat)
4171 {
4172   int     ierr, size;
4173   Mat     *local;
4174 
4175   PetscFunctionBegin;
4176   PetscValidType(mat);
4177   MatPreallocated(mat);
4178   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
4179 
4180   /* if original matrix is on just one processor then use submatrix generated */
4181   if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
4182     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
4183     PetscFunctionReturn(0);
4184   } else if (!mat->ops->getsubmatrix && size == 1) {
4185     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
4186     *newmat = *local;
4187     ierr = PetscFree(local);CHKERRQ(ierr);
4188     PetscFunctionReturn(0);
4189   }
4190 
4191   if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4192   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscol,csize,cll,newmat);CHKERRQ(ierr);
4193   PetscFunctionReturn(0);
4194 }
4195 
4196 #undef __FUNCT__
4197 #define __FUNCT__ "MatGetMaps"
4198 /*@C
4199    MatGetMaps - Returns the maps associated with the matrix.
4200 
4201    Not Collective
4202 
4203    Input Parameter:
4204 .  mat - the matrix
4205 
4206    Output Parameters:
4207 +  rmap - the row (right) map
4208 -  cmap - the column (left) map
4209 
4210    Level: developer
4211 
4212    Concepts: maps^getting from matrix
4213 
4214 @*/
4215 int MatGetMaps(Mat mat,Map *rmap,Map *cmap)
4216 {
4217   int ierr;
4218 
4219   PetscFunctionBegin;
4220   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4221   PetscValidType(mat);
4222   MatPreallocated(mat);
4223   ierr = (*mat->ops->getmaps)(mat,rmap,cmap);CHKERRQ(ierr);
4224   PetscFunctionReturn(0);
4225 }
4226 
4227 /*
4228       Version that works for all PETSc matrices
4229 */
4230 #undef __FUNCT__
4231 #define __FUNCT__ "MatGetMaps_Petsc"
4232 int MatGetMaps_Petsc(Mat mat,Map *rmap,Map *cmap)
4233 {
4234   PetscFunctionBegin;
4235   if (rmap) *rmap = mat->rmap;
4236   if (cmap) *cmap = mat->cmap;
4237   PetscFunctionReturn(0);
4238 }
4239 
4240 #undef __FUNCT__
4241 #define __FUNCT__ "MatSetStashInitialSize"
4242 /*@
4243    MatSetStashInitialSize - sets the sizes of the matrix stash, that is
4244    used during the assembly process to store values that belong to
4245    other processors.
4246 
4247    Not Collective
4248 
4249    Input Parameters:
4250 +  mat   - the matrix
4251 .  size  - the initial size of the stash.
4252 -  bsize - the initial size of the block-stash(if used).
4253 
4254    Options Database Keys:
4255 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
4256 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
4257 
4258    Level: intermediate
4259 
4260    Notes:
4261      The block-stash is used for values set with VecSetValuesBlocked() while
4262      the stash is used for values set with VecSetValues()
4263 
4264      Run with the option -log_info and look for output of the form
4265      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
4266      to determine the appropriate value, MM, to use for size and
4267      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
4268      to determine the value, BMM to use for bsize
4269 
4270    Concepts: stash^setting matrix size
4271    Concepts: matrices^stash
4272 
4273 @*/
4274 int MatSetStashInitialSize(Mat mat,int size, int bsize)
4275 {
4276   int ierr;
4277 
4278   PetscFunctionBegin;
4279   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4280   PetscValidType(mat);
4281   MatPreallocated(mat);
4282   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
4283   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
4284   PetscFunctionReturn(0);
4285 }
4286 
4287 #undef __FUNCT__
4288 #define __FUNCT__ "MatInterpolateAdd"
4289 /*@
4290    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
4291      the matrix
4292 
4293    Collective on Mat
4294 
4295    Input Parameters:
4296 +  mat   - the matrix
4297 .  x,y - the vectors
4298 -  w - where the result is stored
4299 
4300    Level: intermediate
4301 
4302    Notes:
4303     w may be the same vector as y.
4304 
4305     This allows one to use either the restriction or interpolation (its transpose)
4306     matrix to do the interpolation
4307 
4308     Concepts: interpolation
4309 
4310 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
4311 
4312 @*/
4313 int MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
4314 {
4315   int M,N,ierr;
4316 
4317   PetscFunctionBegin;
4318   PetscValidType(A);
4319   MatPreallocated(A);
4320   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4321   if (N > M) {
4322     ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr);
4323   } else {
4324     ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr);
4325   }
4326   PetscFunctionReturn(0);
4327 }
4328 
4329 #undef __FUNCT__
4330 #define __FUNCT__ "MatInterpolate"
4331 /*@
4332    MatInterpolate - y = A*x or A'*x depending on the shape of
4333      the matrix
4334 
4335    Collective on Mat
4336 
4337    Input Parameters:
4338 +  mat   - the matrix
4339 -  x,y - the vectors
4340 
4341    Level: intermediate
4342 
4343    Notes:
4344     This allows one to use either the restriction or interpolation (its transpose)
4345     matrix to do the interpolation
4346 
4347    Concepts: matrices^interpolation
4348 
4349 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
4350 
4351 @*/
4352 int MatInterpolate(Mat A,Vec x,Vec y)
4353 {
4354   int M,N,ierr;
4355 
4356   PetscFunctionBegin;
4357   PetscValidType(A);
4358   MatPreallocated(A);
4359   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4360   if (N > M) {
4361     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
4362   } else {
4363     ierr = MatMult(A,x,y);CHKERRQ(ierr);
4364   }
4365   PetscFunctionReturn(0);
4366 }
4367 
4368 #undef __FUNCT__
4369 #define __FUNCT__ "MatRestrict"
4370 /*@
4371    MatRestrict - y = A*x or A'*x
4372 
4373    Collective on Mat
4374 
4375    Input Parameters:
4376 +  mat   - the matrix
4377 -  x,y - the vectors
4378 
4379    Level: intermediate
4380 
4381    Notes:
4382     This allows one to use either the restriction or interpolation (its transpose)
4383     matrix to do the restriction
4384 
4385    Concepts: matrices^restriction
4386 
4387 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()
4388 
4389 @*/
4390 int MatRestrict(Mat A,Vec x,Vec y)
4391 {
4392   int M,N,ierr;
4393 
4394   PetscFunctionBegin;
4395   PetscValidType(A);
4396   MatPreallocated(A);
4397   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4398   if (N > M) {
4399     ierr = MatMult(A,x,y);CHKERRQ(ierr);
4400   } else {
4401     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
4402   }
4403   PetscFunctionReturn(0);
4404 }
4405 
4406 #undef __FUNCT__
4407 #define __FUNCT__ "MatNullSpaceAttach"
4408 /*@C
4409    MatNullSpaceAttach - attaches a null space to a matrix.
4410         This null space will be removed from the resulting vector whenever
4411         MatMult() is called
4412 
4413    Collective on Mat
4414 
4415    Input Parameters:
4416 +  mat - the matrix
4417 -  nullsp - the null space object
4418 
4419    Level: developer
4420 
4421    Notes:
4422       Overwrites any previous null space that may have been attached
4423 
4424    Concepts: null space^attaching to matrix
4425 
4426 .seealso: MatCreate(), MatNullSpaceCreate()
4427 @*/
4428 int MatNullSpaceAttach(Mat mat,MatNullSpace nullsp)
4429 {
4430   int ierr;
4431 
4432   PetscFunctionBegin;
4433   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4434   PetscValidType(mat);
4435   MatPreallocated(mat);
4436   PetscValidHeaderSpecific(nullsp,MATNULLSPACE_COOKIE);
4437 
4438   if (mat->nullsp) {
4439     ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr);
4440   }
4441   mat->nullsp = nullsp;
4442   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
4443   PetscFunctionReturn(0);
4444 }
4445 
4446 #undef __FUNCT__
4447 #define __FUNCT__ "MatICCFactor"
4448 /*@
4449    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.
4450 
4451    Collective on Mat
4452 
4453    Input Parameters:
4454 +  mat - the matrix
4455 .  row - row/column permutation
4456 .  fill - expected fill factor >= 1.0
4457 -  level - level of fill, for ICC(k)
4458 
4459    Notes:
4460    Probably really in-place only when level of fill is zero, otherwise allocates
4461    new space to store factored matrix and deletes previous memory.
4462 
4463    Most users should employ the simplified SLES interface for linear solvers
4464    instead of working directly with matrix algebra routines such as this.
4465    See, e.g., SLESCreate().
4466 
4467    Level: developer
4468 
4469    Concepts: matrices^incomplete Cholesky factorization
4470    Concepts: Cholesky factorization
4471 
4472 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
4473 @*/
4474 int MatICCFactor(Mat mat,IS row,PetscReal fill,int level)
4475 {
4476   int ierr;
4477 
4478   PetscFunctionBegin;
4479   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4480   PetscValidType(mat);
4481   MatPreallocated(mat);
4482   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
4483   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4484   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4485   if (!mat->ops->iccfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4486   ierr = (*mat->ops->iccfactor)(mat,row,fill,level);CHKERRQ(ierr);
4487   PetscFunctionReturn(0);
4488 }
4489 
4490 #undef __FUNCT__
4491 #define __FUNCT__ "MatSetValuesAdic"
4492 /*@
4493    MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix.
4494 
4495    Not Collective
4496 
4497    Input Parameters:
4498 +  mat - the matrix
4499 -  v - the values compute with ADIC
4500 
4501    Level: developer
4502 
4503    Notes:
4504      Must call MatSetColoring() before using this routine. Also this matrix must already
4505      have its nonzero pattern determined.
4506 
4507 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4508           MatSetValues(), MatSetColoring(), MatSetValuesAdifor()
4509 @*/
4510 int MatSetValuesAdic(Mat mat,void *v)
4511 {
4512   int ierr;
4513 
4514   PetscFunctionBegin;
4515   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4516   PetscValidType(mat);
4517 
4518   if (!mat->assembled) {
4519     SETERRQ(1,"Matrix must be already assembled");
4520   }
4521   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4522   if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4523   ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr);
4524   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4525   PetscFunctionReturn(0);
4526 }
4527 
4528 
4529 #undef __FUNCT__
4530 #define __FUNCT__ "MatSetColoring"
4531 /*@
4532    MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic()
4533 
4534    Not Collective
4535 
4536    Input Parameters:
4537 +  mat - the matrix
4538 -  coloring - the coloring
4539 
4540    Level: developer
4541 
4542 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4543           MatSetValues(), MatSetValuesAdic()
4544 @*/
4545 int MatSetColoring(Mat mat,ISColoring coloring)
4546 {
4547   int ierr;
4548 
4549   PetscFunctionBegin;
4550   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4551   PetscValidType(mat);
4552 
4553   if (!mat->assembled) {
4554     SETERRQ(1,"Matrix must be already assembled");
4555   }
4556   if (!mat->ops->setcoloring) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4557   ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr);
4558   PetscFunctionReturn(0);
4559 }
4560 
4561 #undef __FUNCT__
4562 #define __FUNCT__ "MatSetValuesAdifor"
4563 /*@
4564    MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix.
4565 
4566    Not Collective
4567 
4568    Input Parameters:
4569 +  mat - the matrix
4570 .  nl - leading dimension of v
4571 -  v - the values compute with ADIFOR
4572 
4573    Level: developer
4574 
4575    Notes:
4576      Must call MatSetColoring() before using this routine. Also this matrix must already
4577      have its nonzero pattern determined.
4578 
4579 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4580           MatSetValues(), MatSetColoring()
4581 @*/
4582 int MatSetValuesAdifor(Mat mat,int nl,void *v)
4583 {
4584   int ierr;
4585 
4586   PetscFunctionBegin;
4587   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4588   PetscValidType(mat);
4589 
4590   if (!mat->assembled) {
4591     SETERRQ(1,"Matrix must be already assembled");
4592   }
4593   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4594   if (!mat->ops->setvaluesadifor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4595   ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr);
4596   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4597   PetscFunctionReturn(0);
4598 }
4599