|
1 | 1 | #pragma once |
2 | 2 |
|
3 | 3 | #include "macros.hpp" |
| 4 | +#include "symmetricMatrix.hpp" |
4 | 5 | #include <cmath> |
5 | 6 |
|
6 | 7 | namespace hpcReact |
@@ -75,6 +76,47 @@ void solveNxN_Cholesky(REAL_TYPE const (&A)[N][N], REAL_TYPE const (&b)[N], REAL |
75 | 76 | } |
76 | 77 |
|
77 | 78 |
|
| 79 | + |
| 80 | +template< typename REAL_TYPE, int N > |
| 81 | +void solveNxN_Cholesky( symmetricMatrix<REAL_TYPE,int,N> const & A, |
| 82 | + REAL_TYPE const (&b)[N], |
| 83 | + REAL_TYPE (&x)[N]) |
| 84 | +{ |
| 85 | + symmetricMatrix<REAL_TYPE,int,N> L = {0}; |
| 86 | + |
| 87 | + // **Cholesky Decomposition** |
| 88 | + for (int i = 0; i < N; i++) { |
| 89 | + for (int j = 0; j <= i; j++) { |
| 90 | + REAL_TYPE sum = 0; |
| 91 | + for (int k = 0; k < j; k++) { |
| 92 | + sum += L(i,k) * L(j,k); |
| 93 | + } |
| 94 | + if (i == j) |
| 95 | + L(i,j) = sqrt(A(i,i) - sum); |
| 96 | + else |
| 97 | + L(i,j) = (A(i,j) - sum) / L[j][j]; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // **Forward Substitution: Solve L y = b** |
| 102 | + REAL_TYPE y[N]; |
| 103 | + for (int i = 0; i < N; i++) { |
| 104 | + y[i] = b[i]; |
| 105 | + for (int j = 0; j < i; j++) |
| 106 | + y[i] -= L(i,j) * y[j]; |
| 107 | + y[i] /= L(i,i); |
| 108 | + } |
| 109 | + |
| 110 | + // **Backward Substitution: Solve L^T x = y** |
| 111 | + for (int i = N - 1; i >= 0; i--) { |
| 112 | + x[i] = y[i]; |
| 113 | + for (int j = i + 1; j < N; j++) |
| 114 | + x[i] -= L(j,i) * x[j]; |
| 115 | + x[i] /= L(i,i); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | + |
78 | 120 | template< typename REAL_TYPE, int N > |
79 | 121 | HPCREACT_HOST_DEVICE |
80 | 122 | void solveNxN_pivoted( REAL_TYPE (&A)[N][N], REAL_TYPE (&b)[N], REAL_TYPE (&x)[N] ) |
|
0 commit comments