Monday, September 3, 2007

[Work] SVD example



#include < tnt.h >
#include < jama_svd.h >

void svbksb(TNT::Array2D &u, TNT::Array1D &w, TNT::Array2D &v, int m, int n, TNT::Array1D &b, TNT::Array1D &x)
{
int jj,j,i;
float s;

TNT::Array1D< float > tmp(n, 0.0f);

for (j=0; j < n; j++) {
s = 0.0;
if ( w[j]) {
for (i = 0; i < m; i++) s += u[i][j]*b[i];
s /= w[j];
}
tmp[j]=s;
}

for (j=0;j < n;j++) {
s=0.0;
for (jj=0;jj < n;jj++) s += v[j][jj]*tmp[jj];
x[j]=s;
}
}

void svd_solve(TNT::Array2D A, TNT::Array1D b, TNT::Array1D &sol){
int m=A.dim1();
int n=A.dim2();

JAMA::SVD svd(A);
TNT::Array2D u;
svd.getU(u);
TNT::Array1D w;
svd.getSingularValues(w);
TNT::Array2D v;
svd.getV(v);
svbksb(u, w, v, m, n, b, sol);
}


int main(int argc, char* argv[]){

int n, m;
n=m=8;

TNT::Array2D A(m, n, 0.0);
TNT::Array1D b(m, 0.0);
TNT::Array1D sol(n, 0.0);

//here fill in A and b

svd_solve(A, b, sol){

}

No comments: