Actual source code: ex65.c
petsc-3.14.6 2021-03-30
2: static char help[] = "Saves a rectangular sparse matrix to disk.\n\n";
4: #include <petscmat.h>
6: int main(int argc,char **args)
7: {
8: Mat A;
10: PetscInt m = 100,n = 11,js[11],i,j,cnt;
11: PetscScalar values[11];
12: PetscViewer view;
14: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
15: MatCreateSeqAIJ(PETSC_COMM_WORLD,m,n,20,0,&A);
17: for (i=0; i<n; i++) values[i] = (PetscReal)i;
19: for (i=0; i<m; i++) {
20: cnt = 0;
21: if (i % 2) {
22: for (j=0; j<n; j += 2) {
23: js[cnt++] = j;
24: }
25: } else {
26: ;
27: }
28: MatSetValues(A,1,&i,cnt,js,values,INSERT_VALUES);
29: }
30: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
31: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
33: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"rect",FILE_MODE_WRITE,&view);
34: MatView(A,view);
35: PetscViewerDestroy(&view);
37: MatDestroy(&A);
39: PetscFinalize();
40: return ierr;
41: }
45: /*TEST
47: test:
49: TEST*/