Actual source code: ex65.c
petsc-3.6.1 2015-08-06
2: static char help[] = "Saves a rectangular sparse matrix to disk.\n\n";
4: #include <petscmat.h>
8: int main(int argc,char **args)
9: {
10: Mat A;
12: PetscInt m = 100,n = 11,js[11],i,j,cnt;
13: PetscScalar values[11];
14: PetscViewer view;
16: PetscInitialize(&argc,&args,(char*)0,help);
18: MatCreateSeqAIJ(PETSC_COMM_WORLD,m,n,20,0,&A);
20: for (i=0; i<n; i++) values[i] = (PetscReal)i;
22: for (i=0; i<m; i++) {
23: cnt = 0;
24: if (i % 2) {
25: for (j=0; j<n; j += 2) {
26: js[cnt++] = j;
27: }
28: } else {
29: ;
30: }
31: MatSetValues(A,1,&i,cnt,js,values,INSERT_VALUES);
32: }
33: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
34: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
36: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"rect",FILE_MODE_WRITE,&view);
37: MatView(A,view);
38: PetscViewerDestroy(&view);
40: MatDestroy(&A);
42: PetscFinalize();
43: return 0;
44: }