Skip to content

Instantly share code, notes, and snippets.

@mathemage
Created May 4, 2017 09:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathemage/f0bfcf9395574896ba6122c1ec8baaeb to your computer and use it in GitHub Desktop.
Save mathemage/f0bfcf9395574896ba6122c1ec8baaeb to your computer and use it in GitHub Desktop.
singular values and right eigenvectors out of eigenvalue decomposition (EVD)
package hex.pca;
import hex.util.LinearAlgebraUtils;
import no.uib.cipr.matrix.DenseMatrix;
import no.uib.cipr.matrix.NotConvergedException;
/**
* @author mathemage </ha@h2o.ai>
* @date 1.5.17
*/
public class EVD_MTJ_DenseMatrix implements SVDInterface {
private DenseMatrix gramMatrix;
private no.uib.cipr.matrix.EVD evd;
private double[][] eigenvectors;
EVD_MTJ_DenseMatrix(double[][] gramMatrix) {
this.gramMatrix = new DenseMatrix(gramMatrix);
runEVD();
}
@Override
public double[] getSingularValues() {
return evd.getRealEigenvalues();
}
@Override
public double[][] getEigenvectors() {
return eigenvectors;
}
private void runEVD() {
int gramDimension = gramMatrix.numRows();
try {
// Note: gramMatrix will be overwritten after this
evd = new no.uib.cipr.matrix.EVD(gramDimension).factor(gramMatrix);
} catch (NotConvergedException e) {
throw new RuntimeException(e);
}
double[] Vt_1D = evd.getRightEigenvectors().getData();
eigenvectors = LinearAlgebraUtils.reshape1DArray(Vt_1D, gramDimension, gramDimension);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment