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