Hai,
Thanx. I downloaded two packages from this site JAMA and Matrix Toolkit
for Java(MTJ). I calculated Singular Vector Decomposition using these
packages. On decomposing the input matrix(X) we will get three matrices
namely,
U - Left Singular Matrix
S - Singular values
V - Right singular Matrix
then X=U*S*(VTranspose).
This works fine for JAMA. But in MTJ I not able to get back the input
matrix on multiplying these three matrices.
I downloaded MTJ from following site
http://www.math.uib.no/~bjornoh/mtj/
I like to know whether the problem is on package or in my code.
Regards,
ackumar
CODE:
import mt.*;
import mt.fact.*;
import java.io.*;
import java.lang.*;
class mtjSVD{
public static void main(String args[])
{
String val=new String();
double[][] array={{1.0,2.0},{1.0,1.0},{1.0,3.0}};
DenseMatrix X=new DenseMatrix(array);
System.out.println("Rows="+X.numRows()+"\nCols="+X.numColumns());
System.out.println("\n********************\tX\t*******************\n");
printMatrix(X);
SingularvalueComputer svc=new
SingularvalueComputer(X.numRows(),X.numColumns(),true);
SVD svd=new SVD(X.numRows(),X.numColumns());
try{
svd=svc.factor(X.copy());
}catch(Exception ee){ee.printStackTrace();}
DenseMatrix U=svd.getU();
System.out.println("********************\tU\t*******************\n");
printMatrix(U,5);
DenseMatrix V=svd.getVt();
System.out.println("********************\tV\t*******************\n");
printMatrix(V,5);
DenseMatrix S = new DenseMatrix(X.numRows(),X.numColumns());
double singVal[]=svd.getS();
for(int i=0;i<2;i++)
S.set(i,i,singVal
);
System.out.println("********************\tS\t*******************\n");
printMatrix(S);
System.out.println("********************\tRetained -
X\t*******************\n");
DenseMatrix retainedX=new DenseMatrix(X.numRows(),X.numColumns());
retainedX=(DenseMatrix)U.mult(S,retainedX);
retainedX=(DenseMatrix)retainedX.mult(V.transpose(),retainedX);
printMatrix(retainedX);
}
static void printMatrix(Matrix M)
{
for(int i=0;i<M.numRows();i++)
{
for(int j=0;j<M.numColumns();j++)
{
System.out.print(M.get(i,j)+"\t");
}
System.out.println("\n");
}
}
static void printMatrix(Matrix M,int val)
{
for(int i=0;i<M.numRows();i++)
{
for(int j=0;j<M.numColumns();j++)
{
System.out.print((Double.toString(M.get(i,j))).substring(0,val)+" ");
}
System.out.println("\n");
}
}
}