xref: /libCEED/tests/t301-basis.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
14411cf47Sjeremylt /// @file
252bfb9bbSJeremy L Thompson /// Test QR Factorization
352bfb9bbSJeremy L Thompson /// \test Test QR Factorization
457c64913Sjeremylt #include <ceed.h>
59ac7b42eSJeremy L Thompson #include <ceed/backend.h>
69ac7b42eSJeremy L Thompson #include <math.h>
757c64913Sjeremylt 
857c64913Sjeremylt int main(int argc, char **argv) {
957c64913Sjeremylt   Ceed       ceed;
109ac7b42eSJeremy L Thompson   CeedScalar A[12]    = {1, -1, 4, 1, 4, -2, 1, 4, 2, 1, -1, 0};
1152bfb9bbSJeremy L Thompson   CeedScalar qr[12]   = {1, -1, 4, 1, 4, -2, 1, 4, 2, 1, -1, 0};
129ac7b42eSJeremy L Thompson   CeedScalar A_qr[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
139ac7b42eSJeremy L Thompson   CeedScalar tau[4];
1457c64913Sjeremylt 
1557c64913Sjeremylt   CeedInit(argv[1], &ceed);
16aedaa0e5Sjeremylt 
1752bfb9bbSJeremy L Thompson   CeedQRFactorization(ceed, qr, tau, 4, 3);
18*2b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) {
19*2b730f8bSJeremy L Thompson     for (CeedInt j = i; j < 3; j++) A_qr[i * 3 + j] = qr[i * 3 + j];
20*2b730f8bSJeremy L Thompson   }
219ac7b42eSJeremy L Thompson   CeedHouseholderApplyQ(A_qr, qr, tau, CEED_NOTRANSPOSE, 4, 3, 3, 3, 1);
229ac7b42eSJeremy L Thompson 
23*2b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 12; i++) {
24*2b730f8bSJeremy L Thompson     if (fabs(A_qr[i] - A[i]) > 100. * CEED_EPSILON) {
259ac7b42eSJeremy L Thompson       // LCOV_EXCL_START
26*2b730f8bSJeremy L Thompson       printf("Error in QR factorization A_qr[%" CeedInt_FMT "] = %f != A[%" CeedInt_FMT "] = %f\n", i, A_qr[i], i, A[i]);
279ac7b42eSJeremy L Thompson       // LCOV_EXCL_STOP
28*2b730f8bSJeremy L Thompson     }
29*2b730f8bSJeremy L Thompson   }
309ac7b42eSJeremy L Thompson 
3157c64913Sjeremylt   CeedDestroy(&ceed);
3257c64913Sjeremylt   return 0;
3357c64913Sjeremylt }
34