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