32 template <
typename Real>
48 static bool Solve(
int N, Real
const* A, Real
const* B, Real* X);
51 static bool Solve(
int N,
int M, Real
const* A, Real
const* B, Real* X);
58 Real
const* diagonal, Real
const* superdiagonal, Real
const* B,
66 Real diagonal, Real superdiagonal, Real
const* B, Real* X);
73 Real* X,
unsigned int maxIterations, Real tolerance);
86 Real
const* B, Real* X,
unsigned int maxIterations, Real tolerance);
90 static Real
Dot(
int N, Real
const* U, Real
const* V);
91 static void Mul(
int N, Real
const* A, Real
const* X, Real* P);
92 static void Mul(
int N, SparseMatrix
const& A, Real
const* X, Real* P);
93 static void UpdateX(
int N, Real* X, Real
alpha, Real
const* P);
94 static void UpdateR(
int N, Real* R, Real
alpha, Real
const* W);
95 static void UpdateP(
int N, Real* P, Real beta, Real
const* R);
99 template <
typename Real>
116 template <
typename Real>
133 template <
typename Real>
150 template <
typename Real>
155 nullptr, 0,
nullptr);
158 template <
typename Real>
167 template <
typename Real>
169 Real
const* diagonal, Real
const* superdiagonal, Real
const* B,
172 if (diagonal[0] == (Real)0)
177 std::vector<Real> tmp(N - 1);
178 Real expr = diagonal[0];
179 Real invExpr = ((Real)1) / expr;
180 X[0] = B[0] * invExpr;
183 for (i0 = 0, i1 = 1; i1 < N; ++i0, ++i1)
185 tmp[i0] = superdiagonal[i0] * invExpr;
186 expr = diagonal[i1] - subdiagonal[i0] * tmp[i0];
191 invExpr = ((Real)1) / expr;
192 X[i1] = (B[i1] - subdiagonal[i0] * X[i0]) * invExpr;
195 for (i0 = N - 1, i1 = N - 2; i1 >= 0; --i0, --i1)
197 X[i1] -= tmp[i1] * X[i0];
202 template <
typename Real>
204 Real diagonal, Real superdiagonal, Real
const* B, Real* X)
206 if (diagonal == (Real)0)
211 std::vector<Real> tmp(N - 1);
212 Real expr = diagonal;
213 Real invExpr = ((Real)1) / expr;
214 X[0] = B[0] * invExpr;
217 for (i0 = 0, i1 = 1; i1 < N; ++i0, ++i1)
219 tmp[i0] = superdiagonal * invExpr;
220 expr = diagonal - subdiagonal * tmp[i0];
225 invExpr = ((Real)1) / expr;
226 X[i1] = (B[i1] - subdiagonal * X[i0]) * invExpr;
229 for (i0 = N - 1, i1 = N - 2; i1 >= 0; --i0, --i1)
231 X[i1] -= tmp[i1] * X[i0];
236 template <
typename Real>
238 Real
const* B, Real* X,
unsigned int maxIterations, Real tolerance)
241 std::vector<Real> tmpR(N), tmpP(N), tmpW(N);
245 size_t numBytes = N *
sizeof(Real);
246 memset(X, 0, numBytes);
248 Real rho0 =
Dot(N, R, R);
254 Real rho1 =
Dot(N, R, R);
257 unsigned int iteration;
258 for (iteration = 1; iteration <= maxIterations; ++iteration)
260 Real root0 = sqrt(rho1);
261 Real norm =
Dot(N, B, B);
262 Real root1 = sqrt(norm);
263 if (root0 <= tolerance*root1)
268 Real beta = rho1 / rho0;
271 alpha = rho1 /
Dot(N, P, W);
280 template <
typename Real>
282 SparseMatrix const& A, Real
const* B, Real* X,
unsigned int maxIterations,
286 std::vector<Real> tmpR(N), tmpP(N), tmpW(N);
290 size_t numBytes = N *
sizeof(Real);
291 memset(X, 0, numBytes);
293 Real rho0 =
Dot(N, R, R);
299 Real rho1 =
Dot(N, R, R);
302 unsigned int iteration;
303 for (iteration = 1; iteration <= maxIterations; ++iteration)
305 Real root0 = sqrt(rho1);
306 Real norm =
Dot(N, B, B);
307 Real root1 = sqrt(norm);
308 if (root0 <= tolerance*root1)
313 Real beta = rho1 / rho0;
316 alpha = rho1 /
Dot(N, P, W);
325 template <
typename Real>
329 for (
int i = 0; i < N; ++i)
336 template <
typename Real>
339 #if defined(GTE_USE_ROW_MAJOR) 345 memset(P, 0, N *
sizeof(Real));
348 for (
int col = 0; col < N; ++col)
350 P[
row] += matA(
row, col) * X[col];
355 template <
typename Real>
359 memset(P, 0, N *
sizeof(Real));
360 for (
auto const& element : A)
362 int i = element.first[0];
363 int j = element.first[1];
364 Real
value = element.second;
365 P[i] += value * X[j];
368 P[j] += value * X[i];
373 template <
typename Real>
376 for (
int i = 0; i < N; ++i)
382 template <
typename Real>
385 for (
int i = 0; i < N; ++i)
391 template <
typename Real>
394 for (
int i = 0; i < N; ++i)
396 P[i] = R[i] + beta*P[i];
GLfloat GLfloat GLfloat alpha
static bool SolveTridiagonal(int N, Real const *subdiagonal, Real const *diagonal, Real const *superdiagonal, Real const *B, Real *X)
static void UpdateX(int N, Real *X, Real alpha, Real const *P)
static unsigned int SolveSymmetricCG(int N, Real const *A, Real const *B, Real *X, unsigned int maxIterations, Real tolerance)
static Real Dot(int N, Real const *U, Real const *V)
GLsizei const GLfloat * value
static void Mul(int N, Real const *A, Real const *X, Real *P)
static bool Solve(Matrix2x2< Real > const &A, Vector2< Real > const &B, Vector2< Real > &X)
GLenum GLenum GLsizei void * row
static void UpdateR(int N, Real *R, Real alpha, Real const *W)
static bool SolveConstantTridiagonal(int N, Real subdiagonal, Real diagonal, Real superdiagonal, Real const *B, Real *X)
void Memcpy(void *target, void const *source, size_t count)
std::map< std::array< int, 2 >, Real > SparseMatrix
Quaternion< Real > Inverse(Quaternion< Real > const &d)
static void UpdateP(int N, Real *P, Real beta, Real const *R)