Created
May 4, 2017 09:18
-
-
Save mathemage/f0bfcf9395574896ba6122c1ec8baaeb to your computer and use it in GitHub Desktop.
singular values and right eigenvectors out of eigenvalue decomposition (EVD)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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