Actual source code: ex65.c


  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;
  9:   PetscInt    m = 100, n = 11, js[11], i, j, cnt;
 10:   PetscScalar values[11];
 11:   PetscViewer view;

 14:   PetscInitialize(&argc, &args, (char *)0, help);
 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) js[cnt++] = j;
 23:     } else {
 24:       ;
 25:     }
 26:     MatSetValues(A, 1, &i, cnt, js, values, INSERT_VALUES);
 27:   }
 28:   MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
 29:   MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);

 31:   PetscViewerBinaryOpen(PETSC_COMM_WORLD, "rect", FILE_MODE_WRITE, &view);
 32:   MatView(A, view);
 33:   PetscViewerDestroy(&view);

 35:   MatDestroy(&A);

 37:   PetscFinalize();
 38:   return 0;
 39: }

 41: /*TEST

 43:    test:

 45: TEST*/