Example usage for org.apache.commons.math.linear EigenDecompositionImpl EigenDecompositionImpl

List of usage examples for org.apache.commons.math.linear EigenDecompositionImpl EigenDecompositionImpl

Introduction

In this page you can find the example usage for org.apache.commons.math.linear EigenDecompositionImpl EigenDecompositionImpl.

Prototype

public EigenDecompositionImpl(final RealMatrix matrix, final double splitTolerance)
        throws InvalidMatrixException 

Source Link

Document

Calculates the eigen decomposition of the given symmetric matrix.

Usage

From source file:com.opengamma.analytics.math.matrix.CommonsMatrixAlgebra.java

/**
 * Returns a real matrix raised to some real power 
 * Currently this method is limited to symmetric matrices only as Commons Math does not support the diagonalization of asymmetric matrices  
 * @param m The <strong>symmetric</strong> matrix to take the power of. 
 * @param p The power to raise to matrix to
 * @return The result//from   w  w w . ja  v a2  s  .c o m
 */
@Override
public DoubleMatrix2D getPower(final Matrix<?> m, final double p) {
    if (m instanceof DoubleMatrix2D) {
        final RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix2D) m);
        final EigenDecomposition eigen = new EigenDecompositionImpl(temp, 0.0);
        final double[] rEigenValues = eigen.getRealEigenvalues();
        final double[] iEigenValues = eigen.getImagEigenvalues();
        final int n = rEigenValues.length;
        final double[][] d = new double[n][n];
        for (int i = n - 1; i >= 0; --i) {
            d[i][i] = Math.pow(rEigenValues[i], p);
            if (iEigenValues[i] != 0.0) {
                throw new NotImplementedException("Cannot handle complex eigenvalues in getPower");
            }
        }
        final RealMatrix res = eigen.getV().multiply((new Array2DRowRealMatrix(d)).multiply(eigen.getVT()));
        return CommonsMathWrapper.unwrap(res);
    }
    throw new IllegalArgumentException("Can only find pow of DoubleMatrix2D; have " + m.getClass());
}

From source file:juicebox.data.MatrixZoomData.java

/**
 * Computes eigenvector from Pearson's.//www .  j a v  a 2  s.c o m
 *
 * @param df    Expected values, needed to get Pearson's
 * @param which Which eigenvector; 0 is principal.
 * @return Eigenvector
 */
public double[] computeEigenvector(ExpectedValueFunction df, int which) {
    BasicMatrix pearsons = getPearsons(df);
    if (pearsons == null) {
        return null;
    }

    int dim = pearsons.getRowDimension();
    double[][] data = new double[dim][dim];
    BitSet bitSet = new BitSet(dim);
    for (int i = 0; i < dim; i++) {
        for (int j = 0; j < dim; j++) {
            float tmp = pearsons.getEntry(i, j);
            data[i][j] = tmp;
            if (data[i][j] != 0 && !Float.isNaN(tmp)) {
                bitSet.set(i);
            }
        }
    }

    int[] nonCentromereColumns = new int[bitSet.cardinality()];
    int count = 0;
    for (int i = 0; i < dim; i++) {
        if (bitSet.get(i))
            nonCentromereColumns[count++] = i;
    }

    RealMatrix subMatrix = new Array2DRowRealMatrix(data).getSubMatrix(nonCentromereColumns,
            nonCentromereColumns);
    RealVector rv = (new EigenDecompositionImpl(subMatrix, 0)).getEigenvector(which);

    double[] ev = rv.toArray();

    int size = pearsons.getColumnDimension();
    double[] eigenvector = new double[size];
    int num = 0;
    for (int i = 0; i < size; i++) {
        if (num < nonCentromereColumns.length && i == nonCentromereColumns[num]) {
            eigenvector[i] = ev[num];
            num++;
        } else {
            eigenvector[i] = Double.NaN;
        }
    }
    return eigenvector;

}

From source file:org.apache.mahout.math.ssvd.EigenSolverWrapper.java

public EigenSolverWrapper(double[][] bbt) {
    int dim = bbt.length;
    EigenDecomposition evd2 = new EigenDecompositionImpl(new Array2DRowRealMatrix(bbt), 0);
    eigenvalues = evd2.getRealEigenvalues();
    RealMatrix uHatrm = evd2.getV();/* w ww .  j a v a 2 s.com*/
    uHat = new double[dim][];
    for (int i = 0; i < dim; i++) {
        uHat[i] = uHatrm.getRow(i);
    }
}

From source file:org.caleydo.view.bicluster.elem.layout.MDSLayout.java

@Override
public boolean doLayout(List<? extends IGLLayoutElement> children, float w_p, float h_p,
        IGLLayoutElement parent, int deltaTimeMs) {
    final int n = children.size();
    RealMatrix a = MatrixUtils.createRealMatrix(n, n);

    // Sind als Startwerte anstatt Distanzen hnlichkeitsmae c_{ij} zwischen Objekten gegeben, so lassen sich diese
    // durch die Transformation
    ////from w  ww . j  av  a 2  s.  c  o m
    // d_{ij} = \sqrt{c_{ii}+c_{jj}-2c_{ij}}
    //
    // in Distanzen berfhren.

    for (int i = 0; i < n; ++i) {
        ClusterElement ci = (ClusterElement) children.get(i).asElement();
        int c_ii = ci.getDimSize() + ci.getRecSize();

        for (int j = i + 1; j < n; ++j) {
            ClusterElement cj = (ClusterElement) children.get(j).asElement();
            int recOverlap = ci.getRecOverlap(cj);
            int dimOverlap = ci.getDimOverlap(cj);

            int c_jj = cj.getDimSize() + cj.getRecSize();
            int c_ij = recOverlap + dimOverlap;

            double d_ij = Math.sqrt(c_ii + c_jj - 2 * c_ij);

            a.setEntry(i, j, d_ij);
            a.setEntry(j, i, d_ij);
        }
    }

    //#1. negative squared dissimilarity matrix Q
    //q = as.matrix( -0.5 * d ** 2 )
    RealMatrix q = a.copy();
    for (int i = 0; i < n; ++i) {
        q.getRowVector(i).mapPowToSelf(2);
    }
    q = q.scalarMultiply(-0.5);

    //#2. centering matrix H
    //h = diag(n) - 1/n * 1
    RealMatrix h = MatrixUtils.createRealMatrix(n, n);
    for (int i = 0; i < n; ++i)
        h.setEntry(i, i, n - 1. / n * 1);
    //#3. double-center matrix B
    //b = h %*% q %*% h
    RealMatrix b = h.copy().multiply(q).multiply(h);
    // #4. eigen decomposition of B
    // eig = eigen(b)
    EigenDecompositionImpl eig = new EigenDecompositionImpl(b, 0);
    // #5. use top k values/vectors to compute projected points
    // points = eig$vectors[,1:k] %*% diag(sqrt(eig$values[1:k]))
    for (int i = 0; i < n; ++i) {
        RealVector v = eig.getEigenvector(i).getSubVector(0, 2);
        double x = v.getEntry(0) * eig.getRealEigenvalue(0);
        double y = v.getEntry(1) * eig.getRealEigenvalue(1);
        IGLLayoutElement child = children.get(i);
        child.setLocation((float) x, (float) y);
    }

    return false;
}