xref: /petsc/src/mat/tests/ex247.c (revision d5b43468fb8780a8feea140ccd6fa3e6a50411cc)
1 
2 static char help[] = "Tests MATCENTERING matrix type.\n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc, char **argv)
7 {
8   PetscInt    n;
9   Mat         C;
10   Vec         x, y;
11   PetscReal   norm;
12   PetscMPIInt size;
13 
14   PetscFunctionBeginUser;
15   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
16   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
17 
18   /* Create a parallel vector with 10*size total entries, and fill it with 1s. */
19   n = 10 * size;
20   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
21   PetscCall(VecSetSizes(x, PETSC_DECIDE, n));
22   PetscCall(VecSetFromOptions(x));
23   PetscCall(VecSet(x, 1.0));
24 
25   /* Create a corresponding n x n centering matrix and use it to create a mean-centered y = C * x. */
26   PetscCall(VecDuplicate(x, &y));
27   PetscCall(MatCreateCentering(PETSC_COMM_WORLD, PETSC_DECIDE, n, &C));
28   PetscCall(MatMult(C, x, y));
29 
30   /* Verify that the centered vector y has norm 0. */
31   PetscCall(VecNorm(y, NORM_2, &norm));
32   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Vector norm after MatMult() with centering matrix applied to vector of ones is %f.\n", (double)norm));
33 
34   /* Now repeat, but using MatMultTranspose(). */
35   PetscCall(MatMultTranspose(C, x, y));
36   PetscCall(VecNorm(y, NORM_2, &norm));
37   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Vector norm after MatMultTranspose() with centering matrix applied to vector of ones is %f.\n", (double)norm));
38 
39   /* Clean up. */
40   PetscCall(VecDestroy(&x));
41   PetscCall(VecDestroy(&y));
42   PetscCall(MatDestroy(&C));
43   PetscCall(PetscFinalize());
44   return 0;
45 }
46 
47 /*TEST
48 
49     test:
50       suffix: 1
51       nsize: 1
52       output_file: output/ex247.out
53 
54     test:
55       suffix: 2
56       nsize: 2
57       output_file: output/ex247.out
58 
59 TEST*/
60