11 #ifndef TBCI_SOLVER_SVD_SOLVER_H
12 #define TBCI_SOLVER_SVD_SOLVER_H
14 #include "tbci/matrix.h"
18 # define SVDFLOAT typename MatrixType::value_type
23 #ifdef NEED_BCPLX_WORKAROUND
24 # define SVDREAL(x) CPLX__ real(x)
26 # define SVDREAL(x) (x)
30 template <
typename ARG>
36 return absa *
MATH__ sqrt(1.0 + (absb/absa)*(absb/absa));
38 return (absb==0.0? 0.0: absb *
MATH__ sqrt(1.0 + (absa/absb)*(absa/absb)));
55 const int m = A.rows();
56 const int n = A.columns();
61 for (
i = 0;
i < n; ++
i) {
67 for (k =
i; k < m; ++k)
70 for (k =
i; k < m; ++k) {
78 for (
int j = l; j < n; ++j) {
80 for (k =
i; k < m; ++k)
83 for (k =
i; k < m; ++k)
86 for (k =
i; k < m; ++k)
92 if (
i < m &&
i != (n-1)) {
93 for (k = l; k < n; ++k)
96 for (k = l; k < n; ++k) {
104 for (k=l; k < n; ++k)
106 for (
int j = l; j < m; ++j) {
108 for (k = l; k < n; ++k)
110 for (k = l; k < n; ++k)
113 for(k = l; k < n; ++k)
119 for (
i = n-1;
i >= 0; --
i) {
123 for (j = l; j < n; ++j )
124 V(j,
i) = (A(
i,j)/A(
i,l))/g;
125 for (j = l; j < n; ++j) {
127 for (k = l; k < n; ++k )
129 for (k = l; k < n; ++k )
133 for (
int j = l; j < n; ++j)
134 V(
i,j) = V(j,
i) = 0.0;
140 for (
i =
MIN(m,n)-1;
i >= 0; --
i) {
144 for (j = l; j < n; ++j)
148 for (j = l; j < n; ++j) {
150 for (k = l; k < m; ++k)
153 for (k =
i; k < m; ++k)
156 for (j =
i; j < m; ++j)
159 for (j =
i; j < m; ++j)
163 for (k = n-1; k >= 0; --k) {
164 for (
int its = 0; its < 30; ++its) {
167 for (l = k; l >= 0; --l) {
179 for (
i = l;
i < k; ++
i) {
190 for (
int j = 0; j < m; ++j) {
202 for (
int j = 0; j < n; ++j)
208 STD__ cerr <<
"SVD: No convergence in 30 iterations.\n";
214 SVDFLOAT f = ((y-
z)*(y+z) + (g-
h)*(g+h)) / (2.0*h*y);
220 for (
int j = l; j <= nm; ++j) {
235 for (jj = 0; jj < n; ++jj) {
250 for (jj = 0; jj < m; ++jj) {
267 typename VectorType >
269 const VectorType & W,
const VectorType &
b,
272 const int n = W.size();
273 const int m = b.size();
276 for (j = 0; j < n; ++j) {
279 for (
int i = 0;
i < m; ++
i)
285 for (j = 0; j < n; ++j) {
287 for (
int i = 0;
i < n; ++
i)
295 template <typename VectorType >
298 int fixed = 0;
unsigned i;
300 const unsigned size = vec.size();
302 for (i = 0; i < size; ++
i)
304 double minsv = maxsv * cndno;
305 for (i = 0; i < size; ++
i)
308 vec[
i] = (
typename VectorType::value_type)0.0;
315 INST2(template <typename MAT, typename VEC >
class MAT friend VEC svd_solve (
const MAT& mat,
const VEC& b,
const double cndno);)
323 template <typename MatrixType, typename VectorType>
328 VectorType W(mat.columns());
329 sv_decomp <MatrixType, VectorType > (A, V, W);
332 VectorType
x (mat.columns());
340 template <typename MatrixType, typename T >
344 const int n = W.
size();
345 const int m = b.
size();
349 for (j = 0; j < n; ++j) {
352 for (
int i = 0;
i < m; ++
i)
358 for (j = 0; j < n; ++j) {
360 for (
int i = 0;
i < n; ++
i)
378 template <typename MatrixType, typename T >
384 sv_decomp <MatrixType, Vector<T> > (A, V, W);
const Vector< T > const Vector< T > & x
const Vector< T > const Vector< T > const Vector< T > int T h
void sv_decomp(MatrixType &A, MatrixType &V, VectorType &W)
Singular Value Decomposition Decomposes matrix A into U * diag(W) * V^T A is overwritten with U...
void sv_decomp_backsub(const MatrixType &U, const MatrixType &V, const VectorType &W, const VectorType &b, VectorType &x)
T & set(const T &val, const unsigned long i) const
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > & z
static int fix_condition(VectorType &vec, const double cndno=1e-12)
#define SVDFLOAT
svd_solver.h
double sv_decomp_pythag(ARG a, ARG b)
Calculate sqrt (a^2 + b^2) avoiding under/overflows.
unsigned long size() const
VectorType svd_solve(const MatrixType &mat, const VectorType &b, const double cndno=1e-12)
The interface to use the SVD solver: Solve the equation mat * x = b.
Temporary Base Class Idiom: Class TVector is used for temporary variables.
const Vector< T > Vector< T > Vector< T > Vector< T > & y
const unsigned TMatrix< T > const Matrix< T > * a