xref: /libCEED/tests/t122-vector.c (revision 8aabebfe37a222101625224bfe211715824b5ec2)
1 /// @file
2 /// Test pointwise muliplication of a pair of vectors
3 /// \test Test pointwise muliplication of a pair of vectors
4 #include <ceed.h>
5 #include <math.h>
6 
7 int main(int argc, char **argv) {
8   Ceed ceed;
9   CeedVector x, y, w;
10   CeedInt n;
11   CeedScalar a[10];
12   const CeedScalar *b;
13 
14   CeedInit(argv[1], &ceed);
15 
16   n = 10;
17   CeedVectorCreate(ceed, n, &x);
18   CeedVectorCreate(ceed, n, &y);
19   CeedVectorCreate(ceed, n, &w);
20   for (CeedInt i=0; i<n; i++)
21     a[i] = i;
22   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, a);
23   CeedVectorSetArray(y, CEED_MEM_HOST, CEED_COPY_VALUES, a);
24 
25   // Test multiplying two vectors into third
26   CeedVectorPointwiseMult(w, x, y);
27   CeedVectorGetArrayRead(w, CEED_MEM_HOST, &b);
28   for (CeedInt i=0; i<n; i++)
29     if (fabs(b[i] - i*i ) > 1e-14)
30       // LCOV_EXCL_START
31       printf("Error in w = x .* y, computed: %f actual: %f\n", b[i], 1.0*i*i);
32   // LCOV_EXCL_STOP
33   CeedVectorRestoreArrayRead(w, &b);
34 
35   // Test multiplying two vectors into one of the two
36   CeedVectorPointwiseMult(w, w, y);
37   CeedVectorGetArrayRead(w, CEED_MEM_HOST, &b);
38   for (CeedInt i=0; i<n; i++)
39     if (fabs(b[i] - i*i*i ) > 1e-14)
40       // LCOV_EXCL_START
41       printf("Error in w = w .* y, computed: %f actual: %f\n", b[i], 1.0*i*i*i);
42   // LCOV_EXCL_STOP
43   CeedVectorRestoreArrayRead(w, &b);
44 
45   // Test multiplying two vectors into one of the two
46   CeedVectorPointwiseMult(w, x, w);
47   CeedVectorGetArrayRead(w, CEED_MEM_HOST, &b);
48   for (CeedInt i=0; i<n; i++)
49     if (fabs(b[i] - i*i*i*i ) > 1e-14)
50       // LCOV_EXCL_START
51       printf("Error in w = x .* w, computed: %f actual: %f\n", b[i], 1.0*i*i*i*i);
52   // LCOV_EXCL_STOP
53   CeedVectorRestoreArrayRead(w, &b);
54 
55   // Test multiplying vector by itself and putting product into self
56   CeedVectorPointwiseMult(y, y, y);
57   CeedVectorGetArrayRead(y, CEED_MEM_HOST, &b);
58   for (CeedInt i=0; i<n; i++)
59     if (fabs(b[i] - i*i ) > 1e-14)
60       // LCOV_EXCL_START
61       printf("Error in y = y .* y, computed: %f actual: %f\n", b[i], 1.0*i*i);
62   // LCOV_EXCL_STOP
63   CeedVectorRestoreArrayRead(y, &b);
64 
65   CeedVectorDestroy(&x);
66   CeedVectorDestroy(&y);
67   CeedVectorDestroy(&w);
68   CeedDestroy(&ceed);
69   return 0;
70 }
71