1 2 static char help[] = "Saves a rectangular sparse matrix to disk.\n\n"; 3 4 #include <petscmat.h> 5 6 int main(int argc,char **args) 7 { 8 Mat A; 9 PetscInt m = 100,n = 11,js[11],i,j,cnt; 10 PetscScalar values[11]; 11 PetscViewer view; 12 13 PetscFunctionBeginUser; 14 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 15 PetscCall(MatCreateSeqAIJ(PETSC_COMM_WORLD,m,n,20,0,&A)); 16 17 for (i=0; i<n; i++) values[i] = (PetscReal)i; 18 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 PetscCall(MatSetValues(A,1,&i,cnt,js,values,INSERT_VALUES)); 29 } 30 PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 31 PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 32 33 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"rect",FILE_MODE_WRITE,&view)); 34 PetscCall(MatView(A,view)); 35 PetscCall(PetscViewerDestroy(&view)); 36 37 PetscCall(MatDestroy(&A)); 38 39 PetscCall(PetscFinalize()); 40 return 0; 41 } 42 43 /*TEST 44 45 test: 46 47 TEST*/ 48