SingularValueDecomposition rrsvd = new SingularValueDecomposition(rrmul); // RS' = U \Sigma V' double[] sval = rrsvd.getSingularValues(); // get singular values as double array Matrix eigvec = yt_qr.getQ().times(rrsvd.getV()); model._output._singular_vals = rrsvd.getSingularValues(); assert model._output._names_expanded.length == model._output._eigenvectors_raw.length; model._output._eigenvectors = new TwoDimTable("Eigenvectors", null,
model._output._d = ArrayUtils.mult((Arrays.copyOfRange(ArrayUtils.sqrtArr(svdJ.getSingularValues()), 0, _parms._nv)), sqrt(tB.numRows())); model._output._d = Arrays.copyOfRange(svdJ.getSingularValues(), 0, _parms._nv); model._output._v = svdJ.getU().getMatrix(0, atqJ.getRowDimension() - 1, 0, _parms._nv - 1).getArray();
public PCAModel buildModel(DataInfo dinfo, GramTask tsk) { logStart(); Matrix myGram = new Matrix(tsk._gram.getXX()); // X'X/n where n = num rows SingularValueDecomposition mySVD = myGram.svd(); // Extract eigenvalues and eigenvectors // Note: Singular values ordered in weakly descending order by algorithm double[] Sval = mySVD.getSingularValues(); double[][] eigVec = mySVD.getV().getArray(); // rows = features, cols = principal components assert Sval.length == eigVec.length; // DKV.put(EigenvectorMatrix.makeKey(input("source"), destination_key), new EigenvectorMatrix(eigVec)); // Compute standard deviation double[] sdev = new double[Sval.length]; double totVar = 0; double dfcorr = dinfo._adaptedFrame.numRows()/(dinfo._adaptedFrame.numRows() - 1.0); for(int i = 0; i < Sval.length; i++) { // if(standardize) Sval[i] = dfcorr*Sval[i]; // Correct since degrees of freedom = n-1 sdev[i] = Math.sqrt(Sval[i]); totVar += Sval[i]; } double[] propVar = new double[Sval.length]; // Proportion of total variance double[] cumVar = new double[Sval.length]; // Cumulative proportion of total variance for(int i = 0; i < Sval.length; i++) { propVar[i] = Sval[i]/totVar; cumVar[i] = i == 0 ? propVar[0] : cumVar[i-1] + propVar[i]; } Key dataKey = input("source") == null ? null : Key.make(input("source")); int ncomp = Math.min(getNumPC(sdev, tolerance), max_pc); return new PCAModel(this, destination_key, dataKey, dinfo, tsk, sdev, propVar, cumVar, eigVec, mySVD.rank(), ncomp); }
Separable(SingularValueDecomposition svd) { final int nrows = svd.getU().getRowDimension(); this.row = new float[nrows]; this.col = new float[nrows]; final float factor = (float) Math.sqrt(svd.getS().get(0, 0)); for (int i = 0; i < nrows; i++) { this.row[i] = (float) svd.getU().get(i, 0) * factor; this.col[i] = (float) svd.getV().get(i, 0) * factor; } }
public Matrix[] svd() { if (getColumnCount() > getRowCount()) { SingularValueDecomposition svd = new SingularValueDecomposition(matrix.transpose()); Matrix u = new JamaDenseDoubleMatrix2D(svd.getV()); Matrix s = new JamaDenseDoubleMatrix2D(svd.getS().transpose()); Matrix v = new JamaDenseDoubleMatrix2D(svd.getU()); return new Matrix[] { u, s, v }; } else { SingularValueDecomposition svd = new SingularValueDecomposition(matrix); Matrix u = new JamaDenseDoubleMatrix2D(svd.getU()); Matrix s = new JamaDenseDoubleMatrix2D(svd.getS()); Matrix v = new JamaDenseDoubleMatrix2D(svd.getV()); return new Matrix[] { u, s, v }; } }
public static void computePrincipalMomentsOfInertia(Matrix inertiaForSVD, Matrix3d principalAxesRotationToPack, Vector3d principalMomentsOfInertiaToPack) { // Decompose Inertia Matrix: I = U*sigma*V SingularValueDecomposition inertiaSVD = new SingularValueDecomposition(inertiaForSVD); Matrix sigma = inertiaSVD.getS(); // Matrix U = inertiaSVD.getU(); Matrix V = inertiaSVD.getV(); // If determinant is -1.0, then multiply V by -1. Since I = U*sigma*U_Transpose, then I = (-U) * sigma * (-U_Transpose) double determinant = V.det(); if (determinant < 0.0) { V = V.times(-1.0); determinant = -determinant; } if (Math.abs(determinant - 1.0) > 1e-5) { throw new RuntimeException("Problem in Link.addEllipsoidFromMassProperties(). Determinant should be 1.0"); } principalMomentsOfInertiaToPack.set(sigma.get(0, 0), sigma.get(1, 1), sigma.get(2, 2)); principalAxesRotationToPack.set(V.getRowPackedCopy()); }
/** * Given an approximate rotation matrix Q (that doesn't necessarily conform * properly to a rotation matrix), return the best estimate of a rotation * matrix R such that the Frobenius norm is minimised: min||r-Q||^2_F. * <p> * Fundamentally, this works by performing an SVD of the matrix, setting all * the singular values to 1 and then reconstructing the input. * * @param approx * the initial guess * @return the rotation matrix */ public static Matrix approximateRotationMatrix(Matrix approx) { final SingularValueDecomposition svd = approx.svd(); return svd.getU().times(svd.getV().transpose()); }
double[] sval = svdJ.getSingularValues(); model._output._d = MemoryManager.malloc8d(_parms._nv); double[][] v = svdJ.getV().getArray(); assert v.length == _ncolExp && LinearAlgebraUtils.numColsExp(dinfo._adaptedFrame,_parms._use_all_factor_levels) == _ncolExp; model._output._v = MemoryManager.malloc8d(_ncolExp, _parms._nv);
check(A,SVD.getU().times(SVD.getS().times(SVD.getV().transpose()))); try_success("SingularValueDecomposition...",""); } catch ( java.lang.RuntimeException e ) { double [] singularvalues = SVD.getSingularValues(); try { check(B.cond(),singularvalues[0]/singularvalues[Math.min(B.getRowDimension(),B.getColumnDimension())-1]);
private void runSVD() { svd = gramMatrix.svd(); rightEigenvectors = svd.getV().getArray(); } }
/** Matrix rank @return effective numerical rank, obtained from SVD. */ public int rank () { return new SingularValueDecomposition(this).rank(); }
/** Singular Value Decomposition @return SingularValueDecomposition @see SingularValueDecomposition */ public SingularValueDecomposition svd () { return new SingularValueDecomposition(this); }
@Override public double[] getVariances() { return svd.getSingularValues(); }
public ArrayList<Vector3D> eigenVectors() { // Extract singular values Matrix mat = svd().getU(); ArrayList<Vector3D> res = new ArrayList<Vector3D>(3); for (int i = 0; i < 3; i++) { res.add(new Vector3D(mat.get(0, i), mat.get(1, i), mat.get(2, i))); } return res; }
/** Two norm @return maximum singular value. */ public double norm2 () { return (new SingularValueDecomposition(this).norm2()); }
/** Matrix condition (2 norm) @return ratio of largest to smallest singular value. */ public double cond () { return new SingularValueDecomposition(this).cond(); }
Separable(SingularValueDecomposition svd) { final int nrows = svd.getU().getRowDimension(); this.row = new float[nrows]; this.col = new float[nrows]; final float factor = (float) Math.sqrt(svd.getS().get(0, 0)); for (int i = 0; i < nrows; i++) { this.row[i] = (float) svd.getU().get(i, 0) * factor; this.col[i] = (float) svd.getV().get(i, 0) * factor; } }