I want to benchmark the best 2 or 3 libraries to compute a truncated singular value decomposition (SVD), i.e. an SVD where only the k largest singular values are kept. Moreover, I have those constraints :
I've encountered quite a large range of libraries, but for instance, with Colt, I don't even know if the SVD algorithm takes into account the fact that my matrix is sparse. Also, I did not find a single library that can directly compute the truncated solution (which is supposed to be much faster). Actually, I'm mostly interested in the approximate matrix obtained from the truncated SVD.
Thanks by advance for your help,
Romain Laroche
I had the exact same problem, and my solution is to:
What you obtain is the truncated SVD of your original matrix.
Below is the full solution, tested with matrices having a few thousands rows/columns.
public static double[][] getTruncatedSVD(double[][] matrix, final int k) {
SingularValueDecomposition svd = new SingularValueDecomposition(new Array2DRowRealMatrix(matrix));
double[][] truncatedU = new double[svd.getU().getRowDimension()][k];
svd.getU().copySubMatrix(0, truncatedU.length - 1, 0, k - 1, truncatedU);
double[][] truncatedS = new double[k][k];
svd.getS().copySubMatrix(0, k - 1, 0, k - 1, truncatedS);
double[][] truncatedVT = new double[k][svd.getVT().getColumnDimension()];
svd.getVT().copySubMatrix(0, k - 1, 0, truncatedVT[0].length - 1, truncatedVT);
RealMatrix approximatedSvdMatrix = (new Array2DRowRealMatrix(truncatedU)).multiply(new Array2DRowRealMatrix(truncatedS)).multiply(new Array2DRowRealMatrix(truncatedVT));
return approximatedSvdMatrix.getData();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With