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