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: {
  9:   PetscInt       n;
 10:   Mat            C;
 11:   Vec            x,y;
 12:   PetscReal      norm;
 13:   PetscMPIInt    size;

 15:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 16:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

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

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

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

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

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

 47: /*TEST

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

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

 59: TEST*/