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