00001 #ifndef __VMML__SINGULAR_VALUE_DECOMPOSITION__HPP__
00002 #define __VMML__SINGULAR_VALUE_DECOMPOSITION__HPP__
00003
00004 #include <vmmlib/matrix.hpp>
00005 #include <vmmlib/vector.hpp>
00006
00007
00008 #include <vmmlib/helperFunctions.h>
00009 #include <vmmlib/details.hpp>
00010 #include <cmath>
00011
00012 namespace vmml
00013 {
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 template < size_t M, size_t N, typename T >
00024 void svdecompose(
00025 matrix< M, N, T >& a,
00026 vector< N, T >& w,
00027 matrix< N, N, T >& v
00028 )
00029 {
00030 int m = M;
00031 int n = N;
00032 int flag, i, its, j, jj, k, l, nm;
00033 T anorm, c, f, g, h, s, scale, x, y, z;
00034
00035
00036 vector< N, T > rv1;
00037
00038 g = scale = anorm = 0.0;
00039 for ( i = 0; i < n; ++i )
00040 {
00041 l = i + 1;
00042 rv1[i] = scale * g;
00043 g = s = scale = 0.0;
00044 if ( i < m )
00045 {
00046 for ( k = i; k < m; ++k )
00047 scale += fabs(a[k][i]);
00048 if ( scale )
00049 {
00050 for ( k = i; k < m; ++k )
00051 {
00052 a[k][i] /= scale;
00053 s += a[k][i] * a[k][i];
00054 }
00055 f = a[i][i];
00056 g = -sign( vmml::details::getSquareRoot(s), f);
00057 h = f * g - s;
00058 a[i][i] = f - g;
00059 for ( j = l; j < n; ++j )
00060 {
00061 for ( s = 0.0, k = i; k < m; ++k )
00062 s += a[k][i] * a[k][j];
00063 f = s / h;
00064 for ( k = i; k < m; ++k )
00065 a[k][j] += f * a[k][i];
00066 }
00067 for ( k = i; k < m; ++k )
00068 a[k][i] *= scale;
00069 }
00070 }
00071 w[i] = scale * g;
00072 g = s = scale = 0.0;
00073 if ( i < m && i != n-1 )
00074 {
00075 for ( k = l; k < n; ++k )
00076 scale += fabs(a[i][k]);
00077 if ( scale )
00078 {
00079 for ( k = l; k < n; ++k )
00080 {
00081 a[i][k] /= scale;
00082 s += a[i][k] * a[i][k];
00083 }
00084 f = a[i][l];
00085 g = -sign(vmml::details::getSquareRoot(s), f);
00086 h = f * g - s;
00087 a[i][l] = f - g;
00088 for ( k = l; k < n; ++k )
00089 rv1[k] = a[i][k] / h;
00090 for ( j = l; j < m; ++j )
00091 {
00092 for ( s = 0.0, k = l; k < n; ++k )
00093 s += a[j][k] * a[i][k];
00094 for ( k = l; k < n; ++k )
00095 a[j][k] += s * rv1[k];
00096 }
00097 for ( k = l; k < n; ++k )
00098 a[i][k] *= scale;
00099 }
00100 }
00101 anorm = max( anorm, static_cast< T >( ( fabs( w[i] ) + fabs( rv1[i] ) ) ) );
00102 }
00103 for ( i = n-1; i >= 0; --i )
00104 {
00105 if ( i < n )
00106 {
00107 if ( g )
00108 {
00109 for ( j = l; j < n; ++j )
00110 v[j][i] = (a[i][j] / a[i][l]) / g;
00111 for ( j = l; j < n; ++j )
00112 {
00113 for ( s = 0.0, k = l; k < n; ++k )
00114 s += a[i][k] * v[k][j];
00115 for ( k = l; k < n; ++k )
00116 v[k][j] += s * v[k][i];
00117 }
00118 }
00119 for ( j = l; j < n; ++j )
00120 v[i][j] = v[j][i] = 0.0;
00121 }
00122 v[i][i] = 1.0;
00123 g = rv1[i];
00124 l = i;
00125 }
00126 i = ( m < n ) ? m - 1 : n - 1;
00127 for ( ; i >= 0; --i )
00128 {
00129 l = i + 1;
00130 g = w[i];
00131 for ( j = l; j < n; ++j )
00132 a[i][j] = 0.0;
00133 if ( g )
00134 {
00135 g = 1.0 / g;
00136 for ( j = l; j < n; ++j )
00137 {
00138 for ( s = 0.0, k = l; k < m; ++k )
00139 s += a[k][i] * a[k][j];
00140 f = (s / a[i][i]) * g;
00141 for ( k = i; k < m; ++k )
00142 a[k][j] += f * a[k][i];
00143 }
00144 for ( j = i; j < m; ++j )
00145 a[j][i] *= g;
00146 }
00147 else
00148 for ( j = i; j < m; ++j )
00149 a[j][i] = 0.0;
00150 ++a[i][i];
00151 }
00152 for ( k = n-1; k >= 0; --k )
00153 {
00154
00155 for ( its = 0; its < 30; ++its )
00156 {
00157 flag = 1;
00158 for ( l = k; l >= 0; --l )
00159 {
00160 nm = l - 1;
00161 if ( ( fabs( rv1[l] ) + anorm ) == anorm )
00162 {
00163 flag = 0;
00164 break;
00165 }
00166 if ( ( fabs( w[ nm ] ) + anorm ) == anorm )
00167 break;
00168 }
00169 if ( flag )
00170 {
00171 c = 0.0;
00172 s = 1.0;
00173 for ( i = l; i <= k; ++i )
00174 {
00175 f = s * rv1[i];
00176 rv1[i] = c * rv1[i];
00177 if ( ( fabs(f) + anorm ) == anorm )
00178 break;
00179 g = w[i];
00180 h = pythag(f, g);
00181 w[i] = h;
00182 h = 1.0 / h;
00183 c = g * h;
00184 s = -f * h;
00185 for ( j = 0; j < m; ++j )
00186 {
00187 y = a[j][nm];
00188 z = a[j][i];
00189 a[j][nm] = y * c + z * s;
00190 a[j][i] = z * c - y * s;
00191 }
00192 }
00193 }
00194 z = w[k];
00195 if ( l == k )
00196 {
00197 if ( z < 0.0 )
00198 {
00199 w[k] = -z;
00200 for ( j = 0; j < n; ++j )
00201 v[j][k] = -v[j][k];
00202 }
00203 break;
00204 }
00205 if ( its == 30 )
00206 {
00207
00208 std::cerr << "SingularValueDecomposition - Warning: no convergence in 30 iterations." << std::endl;
00209 }
00210 x = w[l];
00211 nm = k - 1;
00212 y = w[nm];
00213 g = rv1[nm];
00214 h = rv1[k];
00215 f = ( (y-z) * (y+z) + (g-h) * (g+h) ) / (2.0 * h * y );
00216 g = pythag( f, static_cast< T >( 1.0 ) );
00217 f = ( ( x - z ) * ( x + z ) + h * ( ( y / ( f + sign( g, f ) ) ) - h ) ) / x;
00218 c = s = 1.0;
00219
00220 for ( j = l; j <= nm; ++j )
00221 {
00222 i = j + 1;
00223 g = rv1[i];
00224 y = w[i];
00225 h = s * g;
00226 g = c * g;
00227 z = pythag( f, h );
00228 rv1[j] = z;
00229 c = f / z;
00230 s = h / z;
00231 f = x * c + g * s;
00232 g = g * c - x * s;
00233 h = y * s;
00234 y *= c;
00235 for ( jj = 0; jj < n; ++jj )
00236 {
00237 x = v[jj][j];
00238 z = v[jj][i];
00239 v[jj][j] = x * c + z * s;
00240 v[jj][i] = z * c - x * s;
00241 }
00242 z = pythag( f, h );
00243 w[j] = z;
00244 if ( z )
00245 {
00246 z = 1.0 / z;
00247 c = f * z;
00248 s = h * z;
00249 }
00250 f = c * g + s * y;
00251 x = c * y - s * g;
00252 for ( jj = 0; jj < m; ++jj )
00253 {
00254 y = a[jj][j];
00255 z = a[jj][i];
00256 a[jj][j] = y * c + z * s;
00257 a[jj][i] = z * c - y * s;
00258 }
00259 }
00260 rv1[l] = 0.0;
00261 rv1[k] = f;
00262 w[k] = x;
00263 }
00264
00265 }
00266
00267 for ( i = 0; i < 3; ++i )
00268 for ( j = 0; j < 3; ++j )
00269 if ( i < j && w[i] < w[j] )
00270 {
00271 double t = w[i];
00272 double u = w[j];
00273 w[i] = u;
00274 w[j] = t;
00275
00276 for ( k = 0; k < 3; ++k )
00277 {
00278 t = v[i][k];
00279 u = v[j][k];
00280 v[i][k] = u * pow( -1, i );
00281 v[j][k] = t * pow( -1, j );
00282 t = a[k][i];
00283 u = a[k][j];
00284 a[k][i] = u * pow( -1, i );
00285 a[k][j] = t * pow( -1, j );
00286 }
00287 }
00288
00289 }
00290
00291 }
00292
00293 #endif
00294
00295