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) js[cnt++] = j; 23 } else { 24 ; 25 } 26 PetscCall(MatSetValues(A, 1, &i, cnt, js, values, INSERT_VALUES)); 27 } 28 PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 29 PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 30 31 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "rect", FILE_MODE_WRITE, &view)); 32 PetscCall(MatView(A, view)); 33 PetscCall(PetscViewerDestroy(&view)); 34 35 PetscCall(MatDestroy(&A)); 36 37 PetscCall(PetscFinalize()); 38 return 0; 39 } 40 41 /*TEST 42 43 test: 44 45 TEST*/ 46