Actual source code: ex247.c


  2: static char help[] = "Tests MATCENTERING matrix type.\n\n";

  4: #include <petscmat.h>

  6: int main(int argc,char **argv)
  7: {
  8:   PetscInt       n;
  9:   Mat            C;
 10:   Vec            x,y;
 11:   PetscReal      norm;
 12:   PetscMPIInt    size;

 14:   PetscInitialize(&argc,&argv,(char*)0,help);
 15:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

 17:   /* Create a parallel vector with 10*size total entries, and fill it with 1s. */
 18:   n = 10*size;
 19:   VecCreate(PETSC_COMM_WORLD,&x);
 20:   VecSetSizes(x,PETSC_DECIDE,n);
 21:   VecSetFromOptions(x);
 22:   VecSet(x,1.0);

 24:   /* Create a corresponding n x n centering matrix and use it to create a mean-centered y = C * x. */
 25:   VecDuplicate(x,&y);
 26:   MatCreateCentering(PETSC_COMM_WORLD,PETSC_DECIDE,n,&C);
 27:   MatMult(C,x,y);

 29:   /* Verify that the centered vector y has norm 0. */
 30:   VecNorm(y,NORM_2,&norm);
 31:   PetscPrintf(PETSC_COMM_WORLD,"Vector norm after MatMult() with centering matrix applied to vector of ones is %f.\n",(double)norm);

 33:   /* Now repeat, but using MatMultTranspose(). */
 34:   MatMultTranspose(C,x,y);
 35:   VecNorm(y,NORM_2,&norm);
 36:   PetscPrintf(PETSC_COMM_WORLD,"Vector norm after MatMultTranspose() with centering matrix applied to vector of ones is %f.\n",(double)norm);

 38:   /* Clean up. */
 39:   VecDestroy(&x);
 40:   VecDestroy(&y);
 41:   MatDestroy(&C);
 42:   PetscFinalize();
 43:   return 0;
 44: }

 46: /*TEST

 48:     test:
 49:       suffix: 1
 50:       nsize: 1
 51:       output_file: output/ex247.out

 53:     test:
 54:       suffix: 2
 55:       nsize: 2
 56:       output_file: output/ex247.out

 58: TEST*/