32 #ifndef __VMML__SINGULAR_VALUE_DECOMPOSITION__HPP__
33 #define __VMML__SINGULAR_VALUE_DECOMPOSITION__HPP__
35 #include <vmmlib/matrix.hpp>
36 #include <vmmlib/vector.hpp>
51 template <
size_t M,
size_t N,
typename T >
60 int flag, i, its, j, jj, k, l, nm = 0;
61 T anorm, c, f, g, h, s, scale, x, y, z;
66 g = scale = anorm = 0.0;
67 for ( i = 0; i < n; ++i )
74 for ( k = i; k < m; ++k )
75 scale += fabs(a[k][i]);
78 for ( k = i; k < m; ++k )
81 s += a[k][i] * a[k][i];
84 g = -math::sign( static_cast< T >(sqrt(s)), f);
87 for ( j = l; j < n; ++j )
89 for ( s = 0.0, k = i; k < m; ++k )
90 s += a[k][i] * a[k][j];
92 for ( k = i; k < m; ++k )
93 a[k][j] += f * a[k][i];
95 for ( k = i; k < m; ++k )
101 if ( i < m && i != n-1 )
103 for ( k = l; k < n; ++k )
104 scale += fabs(a[i][k]);
107 for ( k = l; k < n; ++k )
110 s += a[i][k] * a[i][k];
113 g = -math::sign( static_cast< T >( sqrt(s)), f);
116 for ( k = l; k < n; ++k )
117 rv1[k] = a[i][k] / h;
118 for ( j = l; j < m; ++j )
120 for ( s = 0.0, k = l; k < n; ++k )
121 s += a[j][k] * a[i][k];
122 for ( k = l; k < n; ++k )
123 a[j][k] += s * rv1[k];
125 for ( k = l; k < n; ++k )
129 anorm = (std::max)( anorm, static_cast< T >( ( fabs( w[i] ) + fabs( rv1[i] ) ) ) );
131 for ( i = n-1; i >= 0; --i )
137 for ( j = l; j < n; ++j )
138 v[j][i] = (a[i][j] / a[i][l]) / g;
139 for ( j = l; j < n; ++j )
141 for ( s = 0.0, k = l; k < n; ++k )
142 s += a[i][k] * v[k][j];
143 for ( k = l; k < n; ++k )
144 v[k][j] += s * v[k][i];
147 for ( j = l; j < n; ++j )
148 v[i][j] = v[j][i] = 0.0;
154 i = ( m < n ) ? m - 1 : n - 1;
155 for ( ; i >= 0; --i )
159 for ( j = l; j < n; ++j )
164 for ( j = l; j < n; ++j )
166 for ( s = 0.0, k = l; k < m; ++k )
167 s += a[k][i] * a[k][j];
168 f = (s / a[i][i]) * g;
169 for ( k = i; k < m; ++k )
170 a[k][j] += f * a[k][i];
172 for ( j = i; j < m; ++j )
176 for ( j = i; j < m; ++j )
180 for ( k = n-1; k >= 0; --k )
183 for ( its = 0; its < 30; ++its )
186 for ( l = k; l >= 0; --l )
189 if ( ( fabs( rv1[l] ) + anorm ) == anorm )
194 if ( ( fabs( w[ nm ] ) + anorm ) == anorm )
201 for ( i = l; i <= k; ++i )
205 if ( ( fabs(f) + anorm ) == anorm )
208 h = math::pythag(f, g);
213 for ( j = 0; j < m; ++j )
217 a[j][nm] = y * c + z * s;
218 a[j][i] = z * c - y * s;
228 for ( j = 0; j < n; ++j )
236 std::cerr <<
"SingularValueDecomposition - Warning: no convergence in 30 iterations." << std::endl;
243 f = ( (y-z) * (y+z) + (g-h) * (g+h) ) / (2.0 * h * y );
244 g = math::pythag( f, static_cast< T >( 1.0 ) );
245 f = ( ( x - z ) * ( x + z ) + h * ( ( y / ( f + math::sign( g, f ) ) ) - h ) ) / x;
248 for ( j = l; j <= nm; ++j )
255 z = math::pythag( f, h );
263 for ( jj = 0; jj < n; ++jj )
267 v[jj][j] = x * c + z * s;
268 v[jj][i] = z * c - x * s;
270 z = math::pythag( f, h );
280 for ( jj = 0; jj < m; ++jj )
284 a[jj][j] = y * c + z * s;
285 a[jj][i] = z * c - y * s;
295 for ( i = 0; i < 3; ++i )
296 for ( j = 0; j < 3; ++j )
297 if ( i < j && w[i] < w[j] )
304 for ( k = 0; k < 3; ++k )
308 v[i][k] = u * std::pow( -1., i );
309 v[j][k] = t * std::pow( -1., j );
312 a[k][i] = u * std::pow( -1., i );
313 a[k][j] = t * std::pow( -1., j );