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