xref: /petsc/src/mat/interface/matrix.c (revision 516d1546314df30606f63bb2b355587cdc58f501)
1 /*$Id: matrix.c,v 1.411 2001/08/07 03:02:41 balay Exp bsmith $*/
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    (www.llnl.gov/CASC/hyper)
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     ierr = PetscStrcpy(convname,"MatConvertTo_");CHKERRQ(ierr);
2368     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
2369     ierr =  PetscFListFind(mat->comm,MatConvertList,convname,(void(**)())&conv);CHKERRQ(ierr);
2370     if (conv) {
2371       ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr);
2372     } else {
2373       ierr = PetscStrcpy(convname,"MatConvert_");CHKERRQ(ierr);
2374       ierr = PetscStrcat(convname,mat->type_name);CHKERRQ(ierr);
2375       ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
2376       ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
2377       ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
2378       ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)())&conv);CHKERRQ(ierr);
2379       if (conv) {
2380         ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr);
2381       } else {
2382         if (mat->ops->convert) {
2383           ierr = (*mat->ops->convert)(mat,newtype,M);CHKERRQ(ierr);
2384         } else {
2385           ierr = MatConvert_Basic(mat,newtype,M);CHKERRQ(ierr);
2386         }
2387       }
2388     }
2389   }
2390   ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2391   PetscFunctionReturn(0);
2392 }
2393 
2394 
2395 #undef __FUNCT__
2396 #define __FUNCT__ "MatDuplicate"
2397 /*@C
2398    MatDuplicate - Duplicates a matrix including the non-zero structure.
2399 
2400    Collective on Mat
2401 
2402    Input Parameters:
2403 +  mat - the matrix
2404 -  op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy nonzero
2405         values as well or not
2406 
2407    Output Parameter:
2408 .  M - pointer to place new matrix
2409 
2410    Level: intermediate
2411 
2412    Concepts: matrices^duplicating
2413 
2414 .seealso: MatCopy(), MatConvert()
2415 @*/
2416 int MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M)
2417 {
2418   int ierr;
2419 
2420   PetscFunctionBegin;
2421   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2422   PetscValidType(mat);
2423   MatPreallocated(mat);
2424   PetscValidPointer(M);
2425   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2426   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2427 
2428   *M  = 0;
2429   ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2430   if (!mat->ops->duplicate) {
2431     SETERRQ(PETSC_ERR_SUP,"Not written for this matrix type");
2432   }
2433   ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr);
2434   ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2435   PetscFunctionReturn(0);
2436 }
2437 
2438 #undef __FUNCT__
2439 #define __FUNCT__ "MatGetDiagonal"
2440 /*@
2441    MatGetDiagonal - Gets the diagonal of a matrix.
2442 
2443    Collective on Mat and Vec
2444 
2445    Input Parameters:
2446 +  mat - the matrix
2447 -  v - the vector for storing the diagonal
2448 
2449    Output Parameter:
2450 .  v - the diagonal of the matrix
2451 
2452    Notes:
2453    For the SeqAIJ matrix format, this routine may also be called
2454    on a LU factored matrix; in that case it routines the reciprocal of
2455    the diagonal entries in U. It returns the entries permuted by the
2456    row and column permutation used during the symbolic factorization.
2457 
2458    Level: intermediate
2459 
2460    Concepts: matrices^accessing diagonals
2461 
2462 .seealso: MatGetRow(), MatGetSubmatrices(), MatGetSubmatrix(), MatGetRowMax()
2463 @*/
2464 int MatGetDiagonal(Mat mat,Vec v)
2465 {
2466   int ierr;
2467 
2468   PetscFunctionBegin;
2469   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2470   PetscValidType(mat);
2471   MatPreallocated(mat);
2472   PetscValidHeaderSpecific(v,VEC_COOKIE);
2473   /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */
2474   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2475   if (!mat->ops->getdiagonal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2476 
2477   ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr);
2478   PetscFunctionReturn(0);
2479 }
2480 
2481 #undef __FUNCT__
2482 #define __FUNCT__ "MatGetRowMax"
2483 /*@
2484    MatGetRowMax - Gets the maximum value (in absolute value) of each
2485         row of the matrix
2486 
2487    Collective on Mat and Vec
2488 
2489    Input Parameters:
2490 .  mat - the matrix
2491 
2492    Output Parameter:
2493 .  v - the vector for storing the maximums
2494 
2495    Level: intermediate
2496 
2497    Concepts: matrices^getting row maximums
2498 
2499 .seealso: MatGetDiagonal(), MatGetSubmatrices(), MatGetSubmatrix()
2500 @*/
2501 int MatGetRowMax(Mat mat,Vec v)
2502 {
2503   int ierr;
2504 
2505   PetscFunctionBegin;
2506   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2507   PetscValidType(mat);
2508   MatPreallocated(mat);
2509   PetscValidHeaderSpecific(v,VEC_COOKIE);
2510   /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */
2511   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2512   if (!mat->ops->getrowmax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2513 
2514   ierr = (*mat->ops->getrowmax)(mat,v);CHKERRQ(ierr);
2515   PetscFunctionReturn(0);
2516 }
2517 
2518 #undef __FUNCT__
2519 #define __FUNCT__ "MatTranspose"
2520 /*@C
2521    MatTranspose - Computes an in-place or out-of-place transpose of a matrix.
2522 
2523    Collective on Mat
2524 
2525    Input Parameter:
2526 .  mat - the matrix to transpose
2527 
2528    Output Parameters:
2529 .  B - the transpose (or pass in PETSC_NULL for an in-place transpose)
2530 
2531    Level: intermediate
2532 
2533    Concepts: matrices^transposing
2534 
2535 .seealso: MatMultTranspose(), MatMultTransposeAdd()
2536 @*/
2537 int MatTranspose(Mat mat,Mat *B)
2538 {
2539   int ierr;
2540 
2541   PetscFunctionBegin;
2542   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2543   PetscValidType(mat);
2544   MatPreallocated(mat);
2545   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2546   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2547   if (!mat->ops->transpose) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2548   ierr = (*mat->ops->transpose)(mat,B);CHKERRQ(ierr);
2549   PetscFunctionReturn(0);
2550 }
2551 
2552 #undef __FUNCT__
2553 #define __FUNCT__ "MatPermute"
2554 /*@C
2555    MatPermute - Creates a new matrix with rows and columns permuted from the
2556    original.
2557 
2558    Collective on Mat
2559 
2560    Input Parameters:
2561 +  mat - the matrix to permute
2562 .  row - row permutation, each processor supplies only the permutation for its rows
2563 -  col - column permutation, each processor needs the entire column permutation, that is
2564          this is the same size as the total number of columns in the matrix
2565 
2566    Output Parameters:
2567 .  B - the permuted matrix
2568 
2569    Level: advanced
2570 
2571    Concepts: matrices^permuting
2572 
2573 .seealso: MatGetOrdering()
2574 @*/
2575 int MatPermute(Mat mat,IS row,IS col,Mat *B)
2576 {
2577   int ierr;
2578 
2579   PetscFunctionBegin;
2580   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2581   PetscValidType(mat);
2582   MatPreallocated(mat);
2583   PetscValidHeaderSpecific(row,IS_COOKIE);
2584   PetscValidHeaderSpecific(col,IS_COOKIE);
2585   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2586   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2587   if (!mat->ops->permute) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2588   ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr);
2589   PetscFunctionReturn(0);
2590 }
2591 
2592 #undef __FUNCT__
2593 #define __FUNCT__ "MatEqual"
2594 /*@
2595    MatEqual - Compares two matrices.
2596 
2597    Collective on Mat
2598 
2599    Input Parameters:
2600 +  A - the first matrix
2601 -  B - the second matrix
2602 
2603    Output Parameter:
2604 .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.
2605 
2606    Level: intermediate
2607 
2608    Concepts: matrices^equality between
2609 @*/
2610 int MatEqual(Mat A,Mat B,PetscTruth *flg)
2611 {
2612   int ierr;
2613 
2614   PetscFunctionBegin;
2615   PetscValidHeaderSpecific(A,MAT_COOKIE);
2616   PetscValidHeaderSpecific(B,MAT_COOKIE);
2617   PetscValidType(A);
2618   MatPreallocated(A);
2619   PetscValidType(B);
2620   MatPreallocated(B);
2621   PetscValidIntPointer(flg);
2622   PetscCheckSameComm(A,B);
2623   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2624   if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2625   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);
2626   if (!A->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",A->type_name);
2627   ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr);
2628   PetscFunctionReturn(0);
2629 }
2630 
2631 #undef __FUNCT__
2632 #define __FUNCT__ "MatDiagonalScale"
2633 /*@
2634    MatDiagonalScale - Scales a matrix on the left and right by diagonal
2635    matrices that are stored as vectors.  Either of the two scaling
2636    matrices can be PETSC_NULL.
2637 
2638    Collective on Mat
2639 
2640    Input Parameters:
2641 +  mat - the matrix to be scaled
2642 .  l - the left scaling vector (or PETSC_NULL)
2643 -  r - the right scaling vector (or PETSC_NULL)
2644 
2645    Notes:
2646    MatDiagonalScale() computes A = LAR, where
2647    L = a diagonal matrix, R = a diagonal matrix
2648 
2649    Level: intermediate
2650 
2651    Concepts: matrices^diagonal scaling
2652    Concepts: diagonal scaling of matrices
2653 
2654 .seealso: MatScale()
2655 @*/
2656 int MatDiagonalScale(Mat mat,Vec l,Vec r)
2657 {
2658   int ierr;
2659 
2660   PetscFunctionBegin;
2661   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2662   PetscValidType(mat);
2663   MatPreallocated(mat);
2664   if (!mat->ops->diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2665   if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE);PetscCheckSameComm(mat,l);}
2666   if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE);PetscCheckSameComm(mat,r);}
2667   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2668   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2669 
2670   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2671   ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr);
2672   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2673   PetscFunctionReturn(0);
2674 }
2675 
2676 #undef __FUNCT__
2677 #define __FUNCT__ "MatScale"
2678 /*@
2679     MatScale - Scales all elements of a matrix by a given number.
2680 
2681     Collective on Mat
2682 
2683     Input Parameters:
2684 +   mat - the matrix to be scaled
2685 -   a  - the scaling value
2686 
2687     Output Parameter:
2688 .   mat - the scaled matrix
2689 
2690     Level: intermediate
2691 
2692     Concepts: matrices^scaling all entries
2693 
2694 .seealso: MatDiagonalScale()
2695 @*/
2696 int MatScale(PetscScalar *a,Mat mat)
2697 {
2698   int ierr;
2699 
2700   PetscFunctionBegin;
2701   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2702   PetscValidType(mat);
2703   MatPreallocated(mat);
2704   PetscValidScalarPointer(a);
2705   if (!mat->ops->scale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2706   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2707   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2708 
2709   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2710   ierr = (*mat->ops->scale)(a,mat);CHKERRQ(ierr);
2711   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2712   PetscFunctionReturn(0);
2713 }
2714 
2715 #undef __FUNCT__
2716 #define __FUNCT__ "MatNorm"
2717 /*@
2718    MatNorm - Calculates various norms of a matrix.
2719 
2720    Collective on Mat
2721 
2722    Input Parameters:
2723 +  mat - the matrix
2724 -  type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY
2725 
2726    Output Parameters:
2727 .  norm - the resulting norm
2728 
2729    Level: intermediate
2730 
2731    Concepts: matrices^norm
2732    Concepts: norm^of matrix
2733 @*/
2734 int MatNorm(Mat mat,NormType type,PetscReal *norm)
2735 {
2736   int ierr;
2737 
2738   PetscFunctionBegin;
2739   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2740   PetscValidType(mat);
2741   MatPreallocated(mat);
2742   PetscValidScalarPointer(norm);
2743 
2744   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2745   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2746   if (!mat->ops->norm) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2747   ierr = (*mat->ops->norm)(mat,type,norm);CHKERRQ(ierr);
2748   PetscFunctionReturn(0);
2749 }
2750 
2751 /*
2752      This variable is used to prevent counting of MatAssemblyBegin() that
2753    are called from within a MatAssemblyEnd().
2754 */
2755 static int MatAssemblyEnd_InUse = 0;
2756 #undef __FUNCT__
2757 #define __FUNCT__ "MatAssemblyBegin"
2758 /*@
2759    MatAssemblyBegin - Begins assembling the matrix.  This routine should
2760    be called after completing all calls to MatSetValues().
2761 
2762    Collective on Mat
2763 
2764    Input Parameters:
2765 +  mat - the matrix
2766 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
2767 
2768    Notes:
2769    MatSetValues() generally caches the values.  The matrix is ready to
2770    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
2771    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
2772    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
2773    using the matrix.
2774 
2775    Level: beginner
2776 
2777    Concepts: matrices^assembling
2778 
2779 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
2780 @*/
2781 int MatAssemblyBegin(Mat mat,MatAssemblyType type)
2782 {
2783   int ierr;
2784 
2785   PetscFunctionBegin;
2786   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2787   PetscValidType(mat);
2788   MatPreallocated(mat);
2789   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?");
2790   if (mat->assembled) {
2791     mat->was_assembled = PETSC_TRUE;
2792     mat->assembled     = PETSC_FALSE;
2793   }
2794   if (!MatAssemblyEnd_InUse) {
2795     ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
2796     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
2797     ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
2798   } else {
2799     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
2800   }
2801   PetscFunctionReturn(0);
2802 }
2803 
2804 #undef __FUNCT__
2805 #define __FUNCT__ "MatAssembed"
2806 /*@
2807    MatAssembled - Indicates if a matrix has been assembled and is ready for
2808      use; for example, in matrix-vector product.
2809 
2810    Collective on Mat
2811 
2812    Input Parameter:
2813 .  mat - the matrix
2814 
2815    Output Parameter:
2816 .  assembled - PETSC_TRUE or PETSC_FALSE
2817 
2818    Level: advanced
2819 
2820    Concepts: matrices^assembled?
2821 
2822 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
2823 @*/
2824 int MatAssembled(Mat mat,PetscTruth *assembled)
2825 {
2826   PetscFunctionBegin;
2827   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2828   PetscValidType(mat);
2829   MatPreallocated(mat);
2830   *assembled = mat->assembled;
2831   PetscFunctionReturn(0);
2832 }
2833 
2834 #undef __FUNCT__
2835 #define __FUNCT__ "MatView_Private"
2836 /*
2837     Processes command line options to determine if/how a matrix
2838   is to be viewed. Called by MatAssemblyEnd() and MatLoad().
2839 */
2840 int MatView_Private(Mat mat)
2841 {
2842   int        ierr;
2843   PetscTruth flg;
2844 
2845   PetscFunctionBegin;
2846   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_info",&flg);CHKERRQ(ierr);
2847   if (flg) {
2848     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr);
2849     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2850     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2851   }
2852   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_info_detailed",&flg);CHKERRQ(ierr);
2853   if (flg) {
2854     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO_LONG);CHKERRQ(ierr);
2855     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2856     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2857   }
2858   ierr = PetscOptionsHasName(mat->prefix,"-mat_view",&flg);CHKERRQ(ierr);
2859   if (flg) {
2860     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2861   }
2862   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_matlab",&flg);CHKERRQ(ierr);
2863   if (flg) {
2864     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr);
2865     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2866     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2867   }
2868   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_draw",&flg);CHKERRQ(ierr);
2869   if (flg) {
2870     ierr = PetscOptionsHasName(mat->prefix,"-mat_view_contour",&flg);CHKERRQ(ierr);
2871     if (flg) {
2872       PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
2873     }
2874     ierr = MatView(mat,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2875     ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2876     if (flg) {
2877       PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2878     }
2879   }
2880   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_socket",&flg);CHKERRQ(ierr);
2881   if (flg) {
2882     ierr = MatView(mat,PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
2883     ierr = PetscViewerFlush(PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
2884   }
2885   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_binary",&flg);CHKERRQ(ierr);
2886   if (flg) {
2887     ierr = MatView(mat,PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr);
2888     ierr = PetscViewerFlush(PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr);
2889   }
2890   PetscFunctionReturn(0);
2891 }
2892 
2893 #undef __FUNCT__
2894 #define __FUNCT__ "MatAssemblyEnd"
2895 /*@
2896    MatAssemblyEnd - Completes assembling the matrix.  This routine should
2897    be called after MatAssemblyBegin().
2898 
2899    Collective on Mat
2900 
2901    Input Parameters:
2902 +  mat - the matrix
2903 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
2904 
2905    Options Database Keys:
2906 +  -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly()
2907 .  -mat_view_info_detailed - Prints more detailed info
2908 .  -mat_view - Prints matrix in ASCII format
2909 .  -mat_view_matlab - Prints matrix in Matlab format
2910 .  -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
2911 .  -display <name> - Sets display name (default is host)
2912 -  -draw_pause <sec> - Sets number of seconds to pause after display
2913 
2914    Notes:
2915    MatSetValues() generally caches the values.  The matrix is ready to
2916    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
2917    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
2918    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
2919    using the matrix.
2920 
2921    Level: beginner
2922 
2923 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), MatView(), MatAssembled()
2924 @*/
2925 int MatAssemblyEnd(Mat mat,MatAssemblyType type)
2926 {
2927   int        ierr;
2928   static int inassm = 0;
2929 
2930   PetscFunctionBegin;
2931   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2932   PetscValidType(mat);
2933   MatPreallocated(mat);
2934 
2935   inassm++;
2936   MatAssemblyEnd_InUse++;
2937   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
2938     ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
2939     if (mat->ops->assemblyend) {
2940       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
2941     }
2942     ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
2943   } else {
2944     if (mat->ops->assemblyend) {
2945       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
2946     }
2947   }
2948 
2949   /* Flush assembly is not a true assembly */
2950   if (type != MAT_FLUSH_ASSEMBLY) {
2951     mat->assembled  = PETSC_TRUE; mat->num_ass++;
2952   }
2953   mat->insertmode = NOT_SET_VALUES;
2954   MatAssemblyEnd_InUse--;
2955 
2956   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
2957     ierr = MatView_Private(mat);CHKERRQ(ierr);
2958   }
2959   inassm--;
2960   PetscFunctionReturn(0);
2961 }
2962 
2963 
2964 #undef __FUNCT__
2965 #define __FUNCT__ "MatCompress"
2966 /*@
2967    MatCompress - Tries to store the matrix in as little space as
2968    possible.  May fail if memory is already fully used, since it
2969    tries to allocate new space.
2970 
2971    Collective on Mat
2972 
2973    Input Parameters:
2974 .  mat - the matrix
2975 
2976    Level: advanced
2977 
2978 @*/
2979 int MatCompress(Mat mat)
2980 {
2981   int ierr;
2982 
2983   PetscFunctionBegin;
2984   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2985   PetscValidType(mat);
2986   MatPreallocated(mat);
2987   if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);}
2988   PetscFunctionReturn(0);
2989 }
2990 
2991 #undef __FUNCT__
2992 #define __FUNCT__ "MatSetOption"
2993 /*@
2994    MatSetOption - Sets a parameter option for a matrix. Some options
2995    may be specific to certain storage formats.  Some options
2996    determine how values will be inserted (or added). Sorted,
2997    row-oriented input will generally assemble the fastest. The default
2998    is row-oriented, nonsorted input.
2999 
3000    Collective on Mat
3001 
3002    Input Parameters:
3003 +  mat - the matrix
3004 -  option - the option, one of those listed below (and possibly others),
3005              e.g., MAT_ROWS_SORTED, MAT_NEW_NONZERO_LOCATION_ERR
3006 
3007    Options Describing Matrix Structure:
3008 +    MAT_SYMMETRIC - symmetric in terms of both structure and value
3009 -    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
3010 
3011    Options For Use with MatSetValues():
3012    Insert a logically dense subblock, which can be
3013 +    MAT_ROW_ORIENTED - row-oriented
3014 .    MAT_COLUMN_ORIENTED - column-oriented
3015 .    MAT_ROWS_SORTED - sorted by row
3016 .    MAT_ROWS_UNSORTED - not sorted by row
3017 .    MAT_COLUMNS_SORTED - sorted by column
3018 -    MAT_COLUMNS_UNSORTED - not sorted by column
3019 
3020    Not these options reflect the data you pass in with MatSetValues(); it has
3021    nothing to do with how the data is stored internally in the matrix
3022    data structure.
3023 
3024    When (re)assembling a matrix, we can restrict the input for
3025    efficiency/debugging purposes.  These options include
3026 +    MAT_NO_NEW_NONZERO_LOCATIONS - additional insertions will not be
3027         allowed if they generate a new nonzero
3028 .    MAT_YES_NEW_NONZERO_LOCATIONS - additional insertions will be allowed
3029 .    MAT_NO_NEW_DIAGONALS - additional insertions will not be allowed if
3030          they generate a nonzero in a new diagonal (for block diagonal format only)
3031 .    MAT_YES_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only)
3032 .    MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries
3033 .    MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry
3034 -    MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly
3035 
3036    Notes:
3037    Some options are relevant only for particular matrix types and
3038    are thus ignored by others.  Other options are not supported by
3039    certain matrix types and will generate an error message if set.
3040 
3041    If using a Fortran 77 module to compute a matrix, one may need to
3042    use the column-oriented option (or convert to the row-oriented
3043    format).
3044 
3045    MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion
3046    that would generate a new entry in the nonzero structure is instead
3047    ignored.  Thus, if memory has not alredy been allocated for this particular
3048    data, then the insertion is ignored. For dense matrices, in which
3049    the entire array is allocated, no entries are ever ignored.
3050 
3051    MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion
3052    that would generate a new entry in the nonzero structure instead produces
3053    an error. (Currently supported for AIJ and BAIJ formats only.)
3054    This is a useful flag when using SAME_NONZERO_PATTERN in calling
3055    SLESSetOperators() to ensure that the nonzero pattern truely does
3056    remain unchanged.
3057 
3058    MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion
3059    that would generate a new entry that has not been preallocated will
3060    instead produce an error. (Currently supported for AIJ and BAIJ formats
3061    only.) This is a useful flag when debugging matrix memory preallocation.
3062 
3063    MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for
3064    other processors should be dropped, rather than stashed.
3065    This is useful if you know that the "owning" processor is also
3066    always generating the correct matrix entries, so that PETSc need
3067    not transfer duplicate entries generated on another processor.
3068 
3069    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
3070    searches during matrix assembly. When this flag is set, the hash table
3071    is created during the first Matrix Assembly. This hash table is
3072    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
3073    to improve the searching of indices. MAT_NO_NEW_NONZERO_LOCATIONS flag
3074    should be used with MAT_USE_HASH_TABLE flag. This option is currently
3075    supported by MATMPIBAIJ format only.
3076 
3077    MAT_KEEP_ZEROED_ROWS indicates when MatZeroRows() is called the zeroed entries
3078    are kept in the nonzero structure
3079 
3080    MAT_IGNORE_ZERO_ENTRIES - when using ADD_VALUES for AIJ matrices this will stop
3081    zero values from creating a zero location in the matrix
3082 
3083    MAT_USE_INODES - indicates using inode version of the code - works with AIJ and
3084    ROWBS matrix types
3085 
3086    MAT_DO_NOT_USE_INODES - indicates not using inode version of the code - works
3087    with AIJ and ROWBS matrix types
3088 
3089    Level: intermediate
3090 
3091    Concepts: matrices^setting options
3092 
3093 @*/
3094 int MatSetOption(Mat mat,MatOption op)
3095 {
3096   int ierr;
3097 
3098   PetscFunctionBegin;
3099   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3100   PetscValidType(mat);
3101   MatPreallocated(mat);
3102   switch (op) {
3103   case MAT_SYMMETRIC:
3104     mat->symmetric              = PETSC_TRUE;
3105     mat->structurally_symmetric = PETSC_TRUE;
3106     break;
3107   case MAT_STRUCTURALLY_SYMMETRIC:
3108     mat->structurally_symmetric = PETSC_TRUE;
3109     break;
3110   default:
3111     if (mat->ops->setoption) {ierr = (*mat->ops->setoption)(mat,op);CHKERRQ(ierr);}
3112     break;
3113   }
3114   PetscFunctionReturn(0);
3115 }
3116 
3117 #undef __FUNCT__
3118 #define __FUNCT__ "MatZeroEntries"
3119 /*@
3120    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
3121    this routine retains the old nonzero structure.
3122 
3123    Collective on Mat
3124 
3125    Input Parameters:
3126 .  mat - the matrix
3127 
3128    Level: intermediate
3129 
3130    Concepts: matrices^zeroing
3131 
3132 .seealso: MatZeroRows()
3133 @*/
3134 int MatZeroEntries(Mat mat)
3135 {
3136   int ierr;
3137 
3138   PetscFunctionBegin;
3139   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3140   PetscValidType(mat);
3141   MatPreallocated(mat);
3142   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3143   if (!mat->ops->zeroentries) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3144 
3145   ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
3146   ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr);
3147   ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
3148   PetscFunctionReturn(0);
3149 }
3150 
3151 #undef __FUNCT__
3152 #define __FUNCT__ "MatZeroRows"
3153 /*@C
3154    MatZeroRows - Zeros all entries (except possibly the main diagonal)
3155    of a set of rows of a matrix.
3156 
3157    Collective on Mat
3158 
3159    Input Parameters:
3160 +  mat - the matrix
3161 .  is - index set of rows to remove
3162 -  diag - pointer to value put in all diagonals of eliminated rows.
3163           Note that diag is not a pointer to an array, but merely a
3164           pointer to a single value.
3165 
3166    Notes:
3167    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
3168    but does not release memory.  For the dense and block diagonal
3169    formats this does not alter the nonzero structure.
3170 
3171    If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure
3172    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
3173    merely zeroed.
3174 
3175    The user can set a value in the diagonal entry (or for the AIJ and
3176    row formats can optionally remove the main diagonal entry from the
3177    nonzero structure as well, by passing a null pointer (PETSC_NULL
3178    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
3179 
3180    For the parallel case, all processes that share the matrix (i.e.,
3181    those in the communicator used for matrix creation) MUST call this
3182    routine, regardless of whether any rows being zeroed are owned by
3183    them.
3184 
3185 
3186    Level: intermediate
3187 
3188    Concepts: matrices^zeroing rows
3189 
3190 .seealso: MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
3191 @*/
3192 int MatZeroRows(Mat mat,IS is,PetscScalar *diag)
3193 {
3194   int ierr;
3195 
3196   PetscFunctionBegin;
3197   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3198   PetscValidType(mat);
3199   MatPreallocated(mat);
3200   PetscValidHeaderSpecific(is,IS_COOKIE);
3201   if (diag) PetscValidScalarPointer(diag);
3202   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3203   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3204   if (!mat->ops->zerorows) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3205 
3206   ierr = (*mat->ops->zerorows)(mat,is,diag);CHKERRQ(ierr);
3207   ierr = MatView_Private(mat);CHKERRQ(ierr);
3208   PetscFunctionReturn(0);
3209 }
3210 
3211 #undef __FUNCT__
3212 #define __FUNCT__ "MatZeroRowsLocal"
3213 /*@C
3214    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
3215    of a set of rows of a matrix; using local numbering of rows.
3216 
3217    Collective on Mat
3218 
3219    Input Parameters:
3220 +  mat - the matrix
3221 .  is - index set of rows to remove
3222 -  diag - pointer to value put in all diagonals of eliminated rows.
3223           Note that diag is not a pointer to an array, but merely a
3224           pointer to a single value.
3225 
3226    Notes:
3227    Before calling MatZeroRowsLocal(), the user must first set the
3228    local-to-global mapping by calling MatSetLocalToGlobalMapping().
3229 
3230    For the AIJ matrix formats this removes the old nonzero structure,
3231    but does not release memory.  For the dense and block diagonal
3232    formats this does not alter the nonzero structure.
3233 
3234    The user can set a value in the diagonal entry (or for the AIJ and
3235    row formats can optionally remove the main diagonal entry from the
3236    nonzero structure as well, by passing a null pointer (PETSC_NULL
3237    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
3238 
3239    Level: intermediate
3240 
3241    Concepts: matrices^zeroing
3242 
3243 .seealso: MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
3244 @*/
3245 int MatZeroRowsLocal(Mat mat,IS is,PetscScalar *diag)
3246 {
3247   int ierr;
3248   IS  newis;
3249 
3250   PetscFunctionBegin;
3251   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3252   PetscValidType(mat);
3253   MatPreallocated(mat);
3254   PetscValidHeaderSpecific(is,IS_COOKIE);
3255   if (diag) PetscValidScalarPointer(diag);
3256   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3257   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3258 
3259   if (mat->ops->zerorowslocal) {
3260     ierr = (*mat->ops->zerorowslocal)(mat,is,diag);CHKERRQ(ierr);
3261   } else {
3262     if (!mat->mapping) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
3263     ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr);
3264     ierr = (*mat->ops->zerorows)(mat,newis,diag);CHKERRQ(ierr);
3265     ierr = ISDestroy(newis);CHKERRQ(ierr);
3266   }
3267   PetscFunctionReturn(0);
3268 }
3269 
3270 #undef __FUNCT__
3271 #define __FUNCT__ "MatGetSize"
3272 /*@
3273    MatGetSize - Returns the numbers of rows and columns in a matrix.
3274 
3275    Not Collective
3276 
3277    Input Parameter:
3278 .  mat - the matrix
3279 
3280    Output Parameters:
3281 +  m - the number of global rows
3282 -  n - the number of global columns
3283 
3284    Level: beginner
3285 
3286    Concepts: matrices^size
3287 
3288 .seealso: MatGetLocalSize()
3289 @*/
3290 int MatGetSize(Mat mat,int *m,int* n)
3291 {
3292   PetscFunctionBegin;
3293   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3294   if (m) *m = mat->M;
3295   if (n) *n = mat->N;
3296   PetscFunctionReturn(0);
3297 }
3298 
3299 #undef __FUNCT__
3300 #define __FUNCT__ "MatGetLocalSize"
3301 /*@
3302    MatGetLocalSize - Returns the number of rows and columns in a matrix
3303    stored locally.  This information may be implementation dependent, so
3304    use with care.
3305 
3306    Not Collective
3307 
3308    Input Parameters:
3309 .  mat - the matrix
3310 
3311    Output Parameters:
3312 +  m - the number of local rows
3313 -  n - the number of local columns
3314 
3315    Level: beginner
3316 
3317    Concepts: matrices^local size
3318 
3319 .seealso: MatGetSize()
3320 @*/
3321 int MatGetLocalSize(Mat mat,int *m,int* n)
3322 {
3323   PetscFunctionBegin;
3324   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3325   if (m) *m = mat->m;
3326   if (n) *n = mat->n;
3327   PetscFunctionReturn(0);
3328 }
3329 
3330 #undef __FUNCT__
3331 #define __FUNCT__ "MatGetOwnershipRange"
3332 /*@
3333    MatGetOwnershipRange - Returns the range of matrix rows owned by
3334    this processor, assuming that the matrix is laid out with the first
3335    n1 rows on the first processor, the next n2 rows on the second, etc.
3336    For certain parallel layouts this range may not be well defined.
3337 
3338    Not Collective
3339 
3340    Input Parameters:
3341 .  mat - the matrix
3342 
3343    Output Parameters:
3344 +  m - the global index of the first local row
3345 -  n - one more than the global index of the last local row
3346 
3347    Level: beginner
3348 
3349    Concepts: matrices^row ownership
3350 @*/
3351 int MatGetOwnershipRange(Mat mat,int *m,int* n)
3352 {
3353   int ierr;
3354 
3355   PetscFunctionBegin;
3356   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3357   PetscValidType(mat);
3358   MatPreallocated(mat);
3359   if (m) PetscValidIntPointer(m);
3360   if (n) PetscValidIntPointer(n);
3361   if (!mat->ops->getownershiprange) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3362   ierr = (*mat->ops->getownershiprange)(mat,m,n);CHKERRQ(ierr);
3363   PetscFunctionReturn(0);
3364 }
3365 
3366 #undef __FUNCT__
3367 #define __FUNCT__ "MatILUFactorSymbolic"
3368 /*@
3369    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
3370    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
3371    to complete the factorization.
3372 
3373    Collective on Mat
3374 
3375    Input Parameters:
3376 +  mat - the matrix
3377 .  row - row permutation
3378 .  column - column permutation
3379 -  info - structure containing
3380 $      levels - number of levels of fill.
3381 $      expected fill - as ratio of original fill.
3382 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
3383                 missing diagonal entries)
3384 
3385    Output Parameters:
3386 .  fact - new matrix that has been symbolically factored
3387 
3388    Notes:
3389    See the users manual for additional information about
3390    choosing the fill factor for better efficiency.
3391 
3392    Most users should employ the simplified SLES interface for linear solvers
3393    instead of working directly with matrix algebra routines such as this.
3394    See, e.g., SLESCreate().
3395 
3396    Level: developer
3397 
3398   Concepts: matrices^symbolic LU factorization
3399   Concepts: matrices^factorization
3400   Concepts: LU^symbolic factorization
3401 
3402 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
3403           MatGetOrdering(), MatILUInfo
3404 
3405 @*/
3406 int MatILUFactorSymbolic(Mat mat,IS row,IS col,MatILUInfo *info,Mat *fact)
3407 {
3408   int ierr;
3409 
3410   PetscFunctionBegin;
3411   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3412   PetscValidType(mat);
3413   MatPreallocated(mat);
3414   PetscValidPointer(fact);
3415   PetscValidHeaderSpecific(row,IS_COOKIE);
3416   PetscValidHeaderSpecific(col,IS_COOKIE);
3417   if (info && info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %d",info->levels);
3418   if (info && info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",info->fill);
3419   if (!mat->ops->ilufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic ILU",mat->type_name);
3420   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3421   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3422 
3423   ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
3424   ierr = (*mat->ops->ilufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr);
3425   ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
3426   PetscFunctionReturn(0);
3427 }
3428 
3429 #undef __FUNCT__
3430 #define __FUNCT__ "MatICCFactorSymbolic"
3431 /*@
3432    MatICCFactorSymbolic - Performs symbolic incomplete
3433    Cholesky factorization for a symmetric matrix.  Use
3434    MatCholeskyFactorNumeric() to complete the factorization.
3435 
3436    Collective on Mat
3437 
3438    Input Parameters:
3439 +  mat - the matrix
3440 .  perm - row and column permutation
3441 .  fill - levels of fill
3442 -  f - expected fill as ratio of original fill
3443 
3444    Output Parameter:
3445 .  fact - the factored matrix
3446 
3447    Notes:
3448    Currently only no-fill factorization is supported.
3449 
3450    Most users should employ the simplified SLES interface for linear solvers
3451    instead of working directly with matrix algebra routines such as this.
3452    See, e.g., SLESCreate().
3453 
3454    Level: developer
3455 
3456   Concepts: matrices^symbolic incomplete Cholesky factorization
3457   Concepts: matrices^factorization
3458   Concepts: Cholsky^symbolic factorization
3459 
3460 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor()
3461 @*/
3462 int MatICCFactorSymbolic(Mat mat,IS perm,PetscReal f,int fill,Mat *fact)
3463 {
3464   int ierr;
3465 
3466   PetscFunctionBegin;
3467   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3468   PetscValidType(mat);
3469   MatPreallocated(mat);
3470   PetscValidPointer(fact);
3471   PetscValidHeaderSpecific(perm,IS_COOKIE);
3472   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3473   if (fill < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Fill negative %d",fill);
3474   if (f < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",f);
3475   if (!mat->ops->iccfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic ICC",mat->type_name);
3476   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3477 
3478   ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
3479   ierr = (*mat->ops->iccfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr);
3480   ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
3481   PetscFunctionReturn(0);
3482 }
3483 
3484 #undef __FUNCT__
3485 #define __FUNCT__ "MatGetArray"
3486 /*@C
3487    MatGetArray - Returns a pointer to the element values in the matrix.
3488    The result of this routine is dependent on the underlying matrix data
3489    structure, and may not even work for certain matrix types.  You MUST
3490    call MatRestoreArray() when you no longer need to access the array.
3491 
3492    Not Collective
3493 
3494    Input Parameter:
3495 .  mat - the matrix
3496 
3497    Output Parameter:
3498 .  v - the location of the values
3499 
3500 
3501    Fortran Note:
3502    This routine is used differently from Fortran, e.g.,
3503 .vb
3504         Mat         mat
3505         PetscScalar mat_array(1)
3506         PetscOffset i_mat
3507         int         ierr
3508         call MatGetArray(mat,mat_array,i_mat,ierr)
3509 
3510   C  Access first local entry in matrix; note that array is
3511   C  treated as one dimensional
3512         value = mat_array(i_mat + 1)
3513 
3514         [... other code ...]
3515         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3516 .ve
3517 
3518    See the Fortran chapter of the users manual and
3519    petsc/src/mat/examples/tests for details.
3520 
3521    Level: advanced
3522 
3523    Concepts: matrices^access array
3524 
3525 .seealso: MatRestoreArray(), MatGetArrayF90()
3526 @*/
3527 int MatGetArray(Mat mat,PetscScalar **v)
3528 {
3529   int ierr;
3530 
3531   PetscFunctionBegin;
3532   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3533   PetscValidType(mat);
3534   MatPreallocated(mat);
3535   PetscValidPointer(v);
3536   if (!mat->ops->getarray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3537   ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr);
3538   PetscFunctionReturn(0);
3539 }
3540 
3541 #undef __FUNCT__
3542 #define __FUNCT__ "MatRestoreArray"
3543 /*@C
3544    MatRestoreArray - Restores the matrix after MatGetArray() has been called.
3545 
3546    Not Collective
3547 
3548    Input Parameter:
3549 +  mat - the matrix
3550 -  v - the location of the values
3551 
3552    Fortran Note:
3553    This routine is used differently from Fortran, e.g.,
3554 .vb
3555         Mat         mat
3556         PetscScalar mat_array(1)
3557         PetscOffset i_mat
3558         int         ierr
3559         call MatGetArray(mat,mat_array,i_mat,ierr)
3560 
3561   C  Access first local entry in matrix; note that array is
3562   C  treated as one dimensional
3563         value = mat_array(i_mat + 1)
3564 
3565         [... other code ...]
3566         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3567 .ve
3568 
3569    See the Fortran chapter of the users manual and
3570    petsc/src/mat/examples/tests for details
3571 
3572    Level: advanced
3573 
3574 .seealso: MatGetArray(), MatRestoreArrayF90()
3575 @*/
3576 int MatRestoreArray(Mat mat,PetscScalar **v)
3577 {
3578   int ierr;
3579 
3580   PetscFunctionBegin;
3581   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3582   PetscValidType(mat);
3583   MatPreallocated(mat);
3584   PetscValidPointer(v);
3585   if (!mat->ops->restorearray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3586   ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr);
3587   PetscFunctionReturn(0);
3588 }
3589 
3590 #undef __FUNCT__
3591 #define __FUNCT__ "MatGetSubMatrices"
3592 /*@C
3593    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
3594    points to an array of valid matrices, they may be reused to store the new
3595    submatrices.
3596 
3597    Collective on Mat
3598 
3599    Input Parameters:
3600 +  mat - the matrix
3601 .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
3602 .  irow, icol - index sets of rows and columns to extract
3603 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3604 
3605    Output Parameter:
3606 .  submat - the array of submatrices
3607 
3608    Notes:
3609    MatGetSubMatrices() can extract only sequential submatrices
3610    (from both sequential and parallel matrices). Use MatGetSubMatrix()
3611    to extract a parallel submatrix.
3612 
3613    When extracting submatrices from a parallel matrix, each processor can
3614    form a different submatrix by setting the rows and columns of its
3615    individual index sets according to the local submatrix desired.
3616 
3617    When finished using the submatrices, the user should destroy
3618    them with MatDestroyMatrices().
3619 
3620    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
3621    original matrix has not changed from that last call to MatGetSubMatrices().
3622 
3623    This routine creates the matrices submat; you should NOT create them before
3624    calling it.
3625 
3626    Fortran Note:
3627    The Fortran interface is slightly different from that given below; it
3628    requires one to pass in  as submat a Mat (integer) array of size at least m.
3629 
3630    Level: advanced
3631 
3632    Concepts: matrices^accessing submatrices
3633    Concepts: submatrices
3634 
3635 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal()
3636 @*/
3637 int MatGetSubMatrices(Mat mat,int n,IS *irow,IS *icol,MatReuse scall,Mat **submat)
3638 {
3639   int        ierr;
3640 
3641   PetscFunctionBegin;
3642   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3643   PetscValidType(mat);
3644   MatPreallocated(mat);
3645   if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3646   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3647 
3648   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
3649   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
3650   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
3651   PetscFunctionReturn(0);
3652 }
3653 
3654 #undef __FUNCT__
3655 #define __FUNCT__ "MatDestroyMatrices"
3656 /*@C
3657    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
3658 
3659    Collective on Mat
3660 
3661    Input Parameters:
3662 +  n - the number of local matrices
3663 -  mat - the matrices
3664 
3665    Level: advanced
3666 
3667     Notes: Frees not only the matrices, but also the array that contains the matrices
3668 
3669 .seealso: MatGetSubMatrices()
3670 @*/
3671 int MatDestroyMatrices(int n,Mat **mat)
3672 {
3673   int ierr,i;
3674 
3675   PetscFunctionBegin;
3676   if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %d",n);
3677   PetscValidPointer(mat);
3678   for (i=0; i<n; i++) {
3679     ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr);
3680   }
3681   /* memory is allocated even if n = 0 */
3682   ierr = PetscFree(*mat);CHKERRQ(ierr);
3683   PetscFunctionReturn(0);
3684 }
3685 
3686 #undef __FUNCT__
3687 #define __FUNCT__ "MatIncreaseOverlap"
3688 /*@
3689    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
3690    replaces the index sets by larger ones that represent submatrices with
3691    additional overlap.
3692 
3693    Collective on Mat
3694 
3695    Input Parameters:
3696 +  mat - the matrix
3697 .  n   - the number of index sets
3698 .  is  - the array of pointers to index sets
3699 -  ov  - the additional overlap requested
3700 
3701    Level: developer
3702 
3703    Concepts: overlap
3704    Concepts: ASM^computing overlap
3705 
3706 .seealso: MatGetSubMatrices()
3707 @*/
3708 int MatIncreaseOverlap(Mat mat,int n,IS *is,int ov)
3709 {
3710   int ierr;
3711 
3712   PetscFunctionBegin;
3713   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3714   PetscValidType(mat);
3715   MatPreallocated(mat);
3716   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3717   if (mat->factor)     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3718 
3719   if (!ov) PetscFunctionReturn(0);
3720   if (!mat->ops->increaseoverlap) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3721   ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
3722   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
3723   ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
3724   PetscFunctionReturn(0);
3725 }
3726 
3727 #undef __FUNCT__
3728 #define __FUNCT__ "MatPrintHelp"
3729 /*@
3730    MatPrintHelp - Prints all the options for the matrix.
3731 
3732    Collective on Mat
3733 
3734    Input Parameter:
3735 .  mat - the matrix
3736 
3737    Options Database Keys:
3738 +  -help - Prints matrix options
3739 -  -h - Prints matrix options
3740 
3741    Level: developer
3742 
3743 .seealso: MatCreate(), MatCreateXXX()
3744 @*/
3745 int MatPrintHelp(Mat mat)
3746 {
3747   static PetscTruth called = PETSC_FALSE;
3748   int               ierr;
3749   MPI_Comm          comm;
3750 
3751   PetscFunctionBegin;
3752   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3753   PetscValidType(mat);
3754   MatPreallocated(mat);
3755 
3756   comm = mat->comm;
3757   if (!called) {
3758     ierr = (*PetscHelpPrintf)(comm,"General matrix options:\n");CHKERRQ(ierr);
3759     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_info: view basic matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr);
3760     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_info_detailed: view detailed matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr);
3761     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_draw: draw nonzero matrix structure during MatAssemblyEnd()\n");CHKERRQ(ierr);
3762     ierr = (*PetscHelpPrintf)(comm,"      -draw_pause <sec>: set seconds of display pause\n");CHKERRQ(ierr);
3763     ierr = (*PetscHelpPrintf)(comm,"      -display <name>: set alternate display\n");CHKERRQ(ierr);
3764     called = PETSC_TRUE;
3765   }
3766   if (mat->ops->printhelp) {
3767     ierr = (*mat->ops->printhelp)(mat);CHKERRQ(ierr);
3768   }
3769   PetscFunctionReturn(0);
3770 }
3771 
3772 #undef __FUNCT__
3773 #define __FUNCT__ "MatGetBlockSize"
3774 /*@
3775    MatGetBlockSize - Returns the matrix block size; useful especially for the
3776    block row and block diagonal formats.
3777 
3778    Not Collective
3779 
3780    Input Parameter:
3781 .  mat - the matrix
3782 
3783    Output Parameter:
3784 .  bs - block size
3785 
3786    Notes:
3787    Block diagonal formats are MATSEQBDIAG, MATMPIBDIAG.
3788    Block row formats are MATSEQBAIJ, MATMPIBAIJ
3789 
3790    Level: intermediate
3791 
3792    Concepts: matrices^block size
3793 
3794 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag()
3795 @*/
3796 int MatGetBlockSize(Mat mat,int *bs)
3797 {
3798   int ierr;
3799 
3800   PetscFunctionBegin;
3801   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3802   PetscValidType(mat);
3803   MatPreallocated(mat);
3804   PetscValidIntPointer(bs);
3805   if (!mat->ops->getblocksize) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3806   ierr = (*mat->ops->getblocksize)(mat,bs);CHKERRQ(ierr);
3807   PetscFunctionReturn(0);
3808 }
3809 
3810 #undef __FUNCT__
3811 #define __FUNCT__ "MatGetRowIJ"
3812 /*@C
3813     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
3814 
3815    Collective on Mat
3816 
3817     Input Parameters:
3818 +   mat - the matrix
3819 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
3820 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3821                 symmetrized
3822 
3823     Output Parameters:
3824 +   n - number of rows in the (possibly compressed) matrix
3825 .   ia - the row pointers
3826 .   ja - the column indices
3827 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
3828 
3829     Level: developer
3830 
3831 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
3832 @*/
3833 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3834 {
3835   int ierr;
3836 
3837   PetscFunctionBegin;
3838   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3839   PetscValidType(mat);
3840   MatPreallocated(mat);
3841   if (ia) PetscValidIntPointer(ia);
3842   if (ja) PetscValidIntPointer(ja);
3843   PetscValidIntPointer(done);
3844   if (!mat->ops->getrowij) *done = PETSC_FALSE;
3845   else {
3846     *done = PETSC_TRUE;
3847     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3848   }
3849   PetscFunctionReturn(0);
3850 }
3851 
3852 #undef __FUNCT__
3853 #define __FUNCT__ "MatGetColumnIJ"
3854 /*@C
3855     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
3856 
3857     Collective on Mat
3858 
3859     Input Parameters:
3860 +   mat - the matrix
3861 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3862 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3863                 symmetrized
3864 
3865     Output Parameters:
3866 +   n - number of columns in the (possibly compressed) matrix
3867 .   ia - the column pointers
3868 .   ja - the row indices
3869 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
3870 
3871     Level: developer
3872 
3873 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
3874 @*/
3875 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3876 {
3877   int ierr;
3878 
3879   PetscFunctionBegin;
3880   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3881   PetscValidType(mat);
3882   MatPreallocated(mat);
3883   if (ia) PetscValidIntPointer(ia);
3884   if (ja) PetscValidIntPointer(ja);
3885   PetscValidIntPointer(done);
3886 
3887   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
3888   else {
3889     *done = PETSC_TRUE;
3890     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3891   }
3892   PetscFunctionReturn(0);
3893 }
3894 
3895 #undef __FUNCT__
3896 #define __FUNCT__ "MatRestoreRowIJ"
3897 /*@C
3898     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
3899     MatGetRowIJ().
3900 
3901     Collective on Mat
3902 
3903     Input Parameters:
3904 +   mat - the matrix
3905 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3906 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3907                 symmetrized
3908 
3909     Output Parameters:
3910 +   n - size of (possibly compressed) matrix
3911 .   ia - the row pointers
3912 .   ja - the column indices
3913 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
3914 
3915     Level: developer
3916 
3917 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
3918 @*/
3919 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3920 {
3921   int ierr;
3922 
3923   PetscFunctionBegin;
3924   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3925   PetscValidType(mat);
3926   MatPreallocated(mat);
3927   if (ia) PetscValidIntPointer(ia);
3928   if (ja) PetscValidIntPointer(ja);
3929   PetscValidIntPointer(done);
3930 
3931   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
3932   else {
3933     *done = PETSC_TRUE;
3934     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3935   }
3936   PetscFunctionReturn(0);
3937 }
3938 
3939 #undef __FUNCT__
3940 #define __FUNCT__ "MatRestoreColumnIJ"
3941 /*@C
3942     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
3943     MatGetColumnIJ().
3944 
3945     Collective on Mat
3946 
3947     Input Parameters:
3948 +   mat - the matrix
3949 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3950 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3951                 symmetrized
3952 
3953     Output Parameters:
3954 +   n - size of (possibly compressed) matrix
3955 .   ia - the column pointers
3956 .   ja - the row indices
3957 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
3958 
3959     Level: developer
3960 
3961 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
3962 @*/
3963 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3964 {
3965   int ierr;
3966 
3967   PetscFunctionBegin;
3968   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3969   PetscValidType(mat);
3970   MatPreallocated(mat);
3971   if (ia) PetscValidIntPointer(ia);
3972   if (ja) PetscValidIntPointer(ja);
3973   PetscValidIntPointer(done);
3974 
3975   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
3976   else {
3977     *done = PETSC_TRUE;
3978     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3979   }
3980   PetscFunctionReturn(0);
3981 }
3982 
3983 #undef __FUNCT__
3984 #define __FUNCT__ "MatColoringPatch"
3985 /*@C
3986     MatColoringPatch -Used inside matrix coloring routines that
3987     use MatGetRowIJ() and/or MatGetColumnIJ().
3988 
3989     Collective on Mat
3990 
3991     Input Parameters:
3992 +   mat - the matrix
3993 .   n   - number of colors
3994 -   colorarray - array indicating color for each column
3995 
3996     Output Parameters:
3997 .   iscoloring - coloring generated using colorarray information
3998 
3999     Level: developer
4000 
4001 .seealso: MatGetRowIJ(), MatGetColumnIJ()
4002 
4003 @*/
4004 int MatColoringPatch(Mat mat,int n,int ncolors,int *colorarray,ISColoring *iscoloring)
4005 {
4006   int ierr;
4007 
4008   PetscFunctionBegin;
4009   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4010   PetscValidType(mat);
4011   MatPreallocated(mat);
4012   PetscValidIntPointer(colorarray);
4013 
4014   if (!mat->ops->coloringpatch){
4015     ierr = ISColoringCreate(mat->comm,n,colorarray,iscoloring);CHKERRQ(ierr);
4016   } else {
4017     ierr = (*mat->ops->coloringpatch)(mat,n,ncolors,colorarray,iscoloring);CHKERRQ(ierr);
4018   }
4019   PetscFunctionReturn(0);
4020 }
4021 
4022 
4023 #undef __FUNCT__
4024 #define __FUNCT__ "MatSetUnfactored"
4025 /*@
4026    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
4027 
4028    Collective on Mat
4029 
4030    Input Parameter:
4031 .  mat - the factored matrix to be reset
4032 
4033    Notes:
4034    This routine should be used only with factored matrices formed by in-place
4035    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
4036    format).  This option can save memory, for example, when solving nonlinear
4037    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
4038    ILU(0) preconditioner.
4039 
4040    Note that one can specify in-place ILU(0) factorization by calling
4041 .vb
4042      PCType(pc,PCILU);
4043      PCILUSeUseInPlace(pc);
4044 .ve
4045    or by using the options -pc_type ilu -pc_ilu_in_place
4046 
4047    In-place factorization ILU(0) can also be used as a local
4048    solver for the blocks within the block Jacobi or additive Schwarz
4049    methods (runtime option: -sub_pc_ilu_in_place).  See the discussion
4050    of these preconditioners in the users manual for details on setting
4051    local solver options.
4052 
4053    Most users should employ the simplified SLES interface for linear solvers
4054    instead of working directly with matrix algebra routines such as this.
4055    See, e.g., SLESCreate().
4056 
4057    Level: developer
4058 
4059 .seealso: PCILUSetUseInPlace(), PCLUSetUseInPlace()
4060 
4061    Concepts: matrices^unfactored
4062 
4063 @*/
4064 int MatSetUnfactored(Mat mat)
4065 {
4066   int ierr;
4067 
4068   PetscFunctionBegin;
4069   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4070   PetscValidType(mat);
4071   MatPreallocated(mat);
4072   mat->factor = 0;
4073   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
4074   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
4075   PetscFunctionReturn(0);
4076 }
4077 
4078 /*MC
4079     MatGetArrayF90 - Accesses a matrix array from Fortran90.
4080 
4081     Synopsis:
4082     MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
4083 
4084     Not collective
4085 
4086     Input Parameter:
4087 .   x - matrix
4088 
4089     Output Parameters:
4090 +   xx_v - the Fortran90 pointer to the array
4091 -   ierr - error code
4092 
4093     Example of Usage:
4094 .vb
4095       PetscScalar, pointer xx_v(:)
4096       ....
4097       call MatGetArrayF90(x,xx_v,ierr)
4098       a = xx_v(3)
4099       call MatRestoreArrayF90(x,xx_v,ierr)
4100 .ve
4101 
4102     Notes:
4103     Not yet supported for all F90 compilers
4104 
4105     Level: advanced
4106 
4107 .seealso:  MatRestoreArrayF90(), MatGetArray(), MatRestoreArray()
4108 
4109     Concepts: matrices^accessing array
4110 
4111 M*/
4112 
4113 /*MC
4114     MatRestoreArrayF90 - Restores a matrix array that has been
4115     accessed with MatGetArrayF90().
4116 
4117     Synopsis:
4118     MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
4119 
4120     Not collective
4121 
4122     Input Parameters:
4123 +   x - matrix
4124 -   xx_v - the Fortran90 pointer to the array
4125 
4126     Output Parameter:
4127 .   ierr - error code
4128 
4129     Example of Usage:
4130 .vb
4131        PetscScalar, pointer xx_v(:)
4132        ....
4133        call MatGetArrayF90(x,xx_v,ierr)
4134        a = xx_v(3)
4135        call MatRestoreArrayF90(x,xx_v,ierr)
4136 .ve
4137 
4138     Notes:
4139     Not yet supported for all F90 compilers
4140 
4141     Level: advanced
4142 
4143 .seealso:  MatGetArrayF90(), MatGetArray(), MatRestoreArray()
4144 
4145 M*/
4146 
4147 
4148 #undef __FUNCT__
4149 #define __FUNCT__ "MatGetSubMatrix"
4150 /*@
4151     MatGetSubMatrix - Gets a single submatrix on the same number of processors
4152                       as the original matrix.
4153 
4154     Collective on Mat
4155 
4156     Input Parameters:
4157 +   mat - the original matrix
4158 .   isrow - rows this processor should obtain
4159 .   iscol - columns for all processors you wish to keep
4160 .   csize - number of columns "local" to this processor (does nothing for sequential
4161             matrices). This should match the result from VecGetLocalSize(x,...) if you
4162             plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE
4163 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
4164 
4165     Output Parameter:
4166 .   newmat - the new submatrix, of the same type as the old
4167 
4168     Level: advanced
4169 
4170     Notes: the iscol argument MUST be the same on each processor.
4171 
4172       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
4173    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
4174    to this routine with a mat of the same nonzero structure will reuse the matrix
4175    generated the first time.
4176 
4177     Concepts: matrices^submatrices
4178 
4179 .seealso: MatGetSubMatrices()
4180 @*/
4181 int MatGetSubMatrix(Mat mat,IS isrow,IS iscol,int csize,MatReuse cll,Mat *newmat)
4182 {
4183   int     ierr, size;
4184   Mat     *local;
4185 
4186   PetscFunctionBegin;
4187   PetscValidType(mat);
4188   MatPreallocated(mat);
4189   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
4190 
4191   /* if original matrix is on just one processor then use submatrix generated */
4192   if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
4193     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
4194     PetscFunctionReturn(0);
4195   } else if (!mat->ops->getsubmatrix && size == 1) {
4196     ierr    = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
4197     *newmat = *local;
4198     ierr    = PetscFree(local);CHKERRQ(ierr);
4199     PetscFunctionReturn(0);
4200   }
4201 
4202   if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4203   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscol,csize,cll,newmat);CHKERRQ(ierr);
4204   PetscFunctionReturn(0);
4205 }
4206 
4207 #undef __FUNCT__
4208 #define __FUNCT__ "MatGetPetscMaps"
4209 /*@C
4210    MatGetPetscMaps - Returns the maps associated with the matrix.
4211 
4212    Not Collective
4213 
4214    Input Parameter:
4215 .  mat - the matrix
4216 
4217    Output Parameters:
4218 +  rmap - the row (right) map
4219 -  cmap - the column (left) map
4220 
4221    Level: developer
4222 
4223    Concepts: maps^getting from matrix
4224 
4225 @*/
4226 int MatGetPetscMaps(Mat mat,PetscMap *rmap,PetscMap *cmap)
4227 {
4228   int ierr;
4229 
4230   PetscFunctionBegin;
4231   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4232   PetscValidType(mat);
4233   MatPreallocated(mat);
4234   ierr = (*mat->ops->getmaps)(mat,rmap,cmap);CHKERRQ(ierr);
4235   PetscFunctionReturn(0);
4236 }
4237 
4238 /*
4239       Version that works for all PETSc matrices
4240 */
4241 #undef __FUNCT__
4242 #define __FUNCT__ "MatGetPetscMaps_Petsc"
4243 int MatGetPetscMaps_Petsc(Mat mat,PetscMap *rmap,PetscMap *cmap)
4244 {
4245   PetscFunctionBegin;
4246   if (rmap) *rmap = mat->rmap;
4247   if (cmap) *cmap = mat->cmap;
4248   PetscFunctionReturn(0);
4249 }
4250 
4251 #undef __FUNCT__
4252 #define __FUNCT__ "MatSetStashInitialSize"
4253 /*@
4254    MatSetStashInitialSize - sets the sizes of the matrix stash, that is
4255    used during the assembly process to store values that belong to
4256    other processors.
4257 
4258    Not Collective
4259 
4260    Input Parameters:
4261 +  mat   - the matrix
4262 .  size  - the initial size of the stash.
4263 -  bsize - the initial size of the block-stash(if used).
4264 
4265    Options Database Keys:
4266 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
4267 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
4268 
4269    Level: intermediate
4270 
4271    Notes:
4272      The block-stash is used for values set with VecSetValuesBlocked() while
4273      the stash is used for values set with VecSetValues()
4274 
4275      Run with the option -log_info and look for output of the form
4276      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
4277      to determine the appropriate value, MM, to use for size and
4278      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
4279      to determine the value, BMM to use for bsize
4280 
4281    Concepts: stash^setting matrix size
4282    Concepts: matrices^stash
4283 
4284 @*/
4285 int MatSetStashInitialSize(Mat mat,int size, int bsize)
4286 {
4287   int ierr;
4288 
4289   PetscFunctionBegin;
4290   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4291   PetscValidType(mat);
4292   MatPreallocated(mat);
4293   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
4294   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
4295   PetscFunctionReturn(0);
4296 }
4297 
4298 #undef __FUNCT__
4299 #define __FUNCT__ "MatInterpolateAdd"
4300 /*@
4301    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
4302      the matrix
4303 
4304    Collective on Mat
4305 
4306    Input Parameters:
4307 +  mat   - the matrix
4308 .  x,y - the vectors
4309 -  w - where the result is stored
4310 
4311    Level: intermediate
4312 
4313    Notes:
4314     w may be the same vector as y.
4315 
4316     This allows one to use either the restriction or interpolation (its transpose)
4317     matrix to do the interpolation
4318 
4319     Concepts: interpolation
4320 
4321 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
4322 
4323 @*/
4324 int MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
4325 {
4326   int M,N,ierr;
4327 
4328   PetscFunctionBegin;
4329   PetscValidType(A);
4330   MatPreallocated(A);
4331   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4332   if (N > M) {
4333     ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr);
4334   } else {
4335     ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr);
4336   }
4337   PetscFunctionReturn(0);
4338 }
4339 
4340 #undef __FUNCT__
4341 #define __FUNCT__ "MatInterpolate"
4342 /*@
4343    MatInterpolate - y = A*x or A'*x depending on the shape of
4344      the matrix
4345 
4346    Collective on Mat
4347 
4348    Input Parameters:
4349 +  mat   - the matrix
4350 -  x,y - the vectors
4351 
4352    Level: intermediate
4353 
4354    Notes:
4355     This allows one to use either the restriction or interpolation (its transpose)
4356     matrix to do the interpolation
4357 
4358    Concepts: matrices^interpolation
4359 
4360 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
4361 
4362 @*/
4363 int MatInterpolate(Mat A,Vec x,Vec y)
4364 {
4365   int M,N,ierr;
4366 
4367   PetscFunctionBegin;
4368   PetscValidType(A);
4369   MatPreallocated(A);
4370   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4371   if (N > M) {
4372     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
4373   } else {
4374     ierr = MatMult(A,x,y);CHKERRQ(ierr);
4375   }
4376   PetscFunctionReturn(0);
4377 }
4378 
4379 #undef __FUNCT__
4380 #define __FUNCT__ "MatRestrict"
4381 /*@
4382    MatRestrict - y = A*x or A'*x
4383 
4384    Collective on Mat
4385 
4386    Input Parameters:
4387 +  mat   - the matrix
4388 -  x,y - the vectors
4389 
4390    Level: intermediate
4391 
4392    Notes:
4393     This allows one to use either the restriction or interpolation (its transpose)
4394     matrix to do the restriction
4395 
4396    Concepts: matrices^restriction
4397 
4398 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()
4399 
4400 @*/
4401 int MatRestrict(Mat A,Vec x,Vec y)
4402 {
4403   int M,N,ierr;
4404 
4405   PetscFunctionBegin;
4406   PetscValidType(A);
4407   MatPreallocated(A);
4408   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4409   if (N > M) {
4410     ierr = MatMult(A,x,y);CHKERRQ(ierr);
4411   } else {
4412     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
4413   }
4414   PetscFunctionReturn(0);
4415 }
4416 
4417 #undef __FUNCT__
4418 #define __FUNCT__ "MatNullSpaceAttach"
4419 /*@C
4420    MatNullSpaceAttach - attaches a null space to a matrix.
4421         This null space will be removed from the resulting vector whenever
4422         MatMult() is called
4423 
4424    Collective on Mat
4425 
4426    Input Parameters:
4427 +  mat - the matrix
4428 -  nullsp - the null space object
4429 
4430    Level: developer
4431 
4432    Notes:
4433       Overwrites any previous null space that may have been attached
4434 
4435    Concepts: null space^attaching to matrix
4436 
4437 .seealso: MatCreate(), MatNullSpaceCreate()
4438 @*/
4439 int MatNullSpaceAttach(Mat mat,MatNullSpace nullsp)
4440 {
4441   int ierr;
4442 
4443   PetscFunctionBegin;
4444   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4445   PetscValidType(mat);
4446   MatPreallocated(mat);
4447   PetscValidHeaderSpecific(nullsp,MATNULLSPACE_COOKIE);
4448 
4449   if (mat->nullsp) {
4450     ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr);
4451   }
4452   mat->nullsp = nullsp;
4453   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
4454   PetscFunctionReturn(0);
4455 }
4456 
4457 #undef __FUNCT__
4458 #define __FUNCT__ "MatICCFactor"
4459 /*@
4460    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.
4461 
4462    Collective on Mat
4463 
4464    Input Parameters:
4465 +  mat - the matrix
4466 .  row - row/column permutation
4467 .  fill - expected fill factor >= 1.0
4468 -  level - level of fill, for ICC(k)
4469 
4470    Notes:
4471    Probably really in-place only when level of fill is zero, otherwise allocates
4472    new space to store factored matrix and deletes previous memory.
4473 
4474    Most users should employ the simplified SLES interface for linear solvers
4475    instead of working directly with matrix algebra routines such as this.
4476    See, e.g., SLESCreate().
4477 
4478    Level: developer
4479 
4480    Concepts: matrices^incomplete Cholesky factorization
4481    Concepts: Cholesky factorization
4482 
4483 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
4484 @*/
4485 int MatICCFactor(Mat mat,IS row,PetscReal fill,int level)
4486 {
4487   int ierr;
4488 
4489   PetscFunctionBegin;
4490   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4491   PetscValidType(mat);
4492   MatPreallocated(mat);
4493   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
4494   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4495   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4496   if (!mat->ops->iccfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4497   ierr = (*mat->ops->iccfactor)(mat,row,fill,level);CHKERRQ(ierr);
4498   PetscFunctionReturn(0);
4499 }
4500 
4501 #undef __FUNCT__
4502 #define __FUNCT__ "MatSetValuesAdic"
4503 /*@
4504    MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix.
4505 
4506    Not Collective
4507 
4508    Input Parameters:
4509 +  mat - the matrix
4510 -  v - the values compute with ADIC
4511 
4512    Level: developer
4513 
4514    Notes:
4515      Must call MatSetColoring() before using this routine. Also this matrix must already
4516      have its nonzero pattern determined.
4517 
4518 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4519           MatSetValues(), MatSetColoring(), MatSetValuesAdifor()
4520 @*/
4521 int MatSetValuesAdic(Mat mat,void *v)
4522 {
4523   int ierr;
4524 
4525   PetscFunctionBegin;
4526   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4527   PetscValidType(mat);
4528 
4529   if (!mat->assembled) {
4530     SETERRQ(1,"Matrix must be already assembled");
4531   }
4532   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4533   if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4534   ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr);
4535   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4536   ierr = MatView_Private(mat);CHKERRQ(ierr);
4537   PetscFunctionReturn(0);
4538 }
4539 
4540 
4541 #undef __FUNCT__
4542 #define __FUNCT__ "MatSetColoring"
4543 /*@
4544    MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic()
4545 
4546    Not Collective
4547 
4548    Input Parameters:
4549 +  mat - the matrix
4550 -  coloring - the coloring
4551 
4552    Level: developer
4553 
4554 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4555           MatSetValues(), MatSetValuesAdic()
4556 @*/
4557 int MatSetColoring(Mat mat,ISColoring coloring)
4558 {
4559   int ierr;
4560 
4561   PetscFunctionBegin;
4562   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4563   PetscValidType(mat);
4564 
4565   if (!mat->assembled) {
4566     SETERRQ(1,"Matrix must be already assembled");
4567   }
4568   if (!mat->ops->setcoloring) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4569   ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr);
4570   PetscFunctionReturn(0);
4571 }
4572 
4573 #undef __FUNCT__
4574 #define __FUNCT__ "MatSetValuesAdifor"
4575 /*@
4576    MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix.
4577 
4578    Not Collective
4579 
4580    Input Parameters:
4581 +  mat - the matrix
4582 .  nl - leading dimension of v
4583 -  v - the values compute with ADIFOR
4584 
4585    Level: developer
4586 
4587    Notes:
4588      Must call MatSetColoring() before using this routine. Also this matrix must already
4589      have its nonzero pattern determined.
4590 
4591 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4592           MatSetValues(), MatSetColoring()
4593 @*/
4594 int MatSetValuesAdifor(Mat mat,int nl,void *v)
4595 {
4596   int ierr;
4597 
4598   PetscFunctionBegin;
4599   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4600   PetscValidType(mat);
4601 
4602   if (!mat->assembled) {
4603     SETERRQ(1,"Matrix must be already assembled");
4604   }
4605   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4606   if (!mat->ops->setvaluesadifor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4607   ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr);
4608   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4609   PetscFunctionReturn(0);
4610 }
4611