1 /// @file
2 /// Test vector norms
3 /// \test Test vector norms
4
5 //TESTARGS(name="length 10") {ceed_resource} 10
6 //TESTARGS(name="length 0") {ceed_resource} 0
7 #include <ceed.h>
8 #include <math.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
main(int argc,char ** argv)12 int main(int argc, char **argv) {
13 Ceed ceed;
14 CeedVector x;
15 CeedInt len = 10;
16 CeedScalar array[len];
17
18 CeedInit(argv[1], &ceed);
19 len = argc > 2 ? atoi(argv[2]) : len;
20
21 CeedVectorCreate(ceed, len, &x);
22 for (CeedInt i = 0; i < len; i++) array[i] = i * (i % 2 ? 1 : -1);
23 CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, array);
24 {
25 // Sync memtype to device for GPU backends
26 CeedMemType type = CEED_MEM_HOST;
27 CeedGetPreferredMemType(ceed, &type);
28 CeedVectorSyncArray(x, type);
29 }
30
31 CeedScalar norm;
32 CeedVectorNorm(x, CEED_NORM_1, &norm);
33 if (len > 0 && fabs(norm - 45.) > 100. * CEED_EPSILON) printf("Error: L1 norm %f != 45.\n", norm);
34 else if (len == 0 && fabs(norm) > CEED_EPSILON) printf("Error: L1 norm %f != 0.\n", norm);
35
36 CeedVectorNorm(x, CEED_NORM_2, &norm);
37 if (len > 0 && fabs(norm - sqrt(285.)) > 100. * CEED_EPSILON) printf("Error: L2 norm %f != sqrt(285.)\n", norm);
38 else if (len == 0 && fabs(norm) > CEED_EPSILON) printf("Error: L2 norm %f != 0.\n", norm);
39
40 CeedVectorNorm(x, CEED_NORM_MAX, &norm);
41 if (len > 0 && fabs(norm - 9.) > 100. * CEED_EPSILON) printf("Error: Max norm %f != 9.\n", norm);
42 else if (len == 0 && fabs(norm) > CEED_EPSILON) printf("Error: Max norm %f != 0.\n", norm);
43
44 CeedVectorDestroy(&x);
45 CeedDestroy(&ceed);
46 return 0;
47 }
48