Example usage for org.apache.commons.math.linear MatrixUtils createRealMatrix

List of usage examples for org.apache.commons.math.linear MatrixUtils createRealMatrix

Introduction

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

Prototype

public static RealMatrix createRealMatrix(final int rows, final int columns) 

Source Link

Document

Returns a RealMatrix with specified dimensions.

Usage

From source file:net.sf.maltcms.chromaui.features.spi.FeatureTable.java

public static RealMatrix normalize(RealMatrix sourceMatrix, boolean center, boolean normalize) {
    RealMatrix normalized = MatrixUtils.createRealMatrix(sourceMatrix.getRowDimension(),
            sourceMatrix.getColumnDimension());
    for (int col = 0; col < sourceMatrix.getColumnDimension(); col++) {
        double[] columnVector = sourceMatrix.getColumn(col);
        double mean = StatUtils.mean(columnVector);
        double stdev = Math.sqrt(StatUtils.variance(columnVector, mean));
        Logger.getLogger(FeatureTable.class.getName()).log(Level.INFO, "column {0}, mean={1} stdev={2}",
                new Object[] { col, mean, stdev });
        for (int j = 0; j < columnVector.length; j++) {
            normalized.setEntry(j, col, (sourceMatrix.getEntry(j, col) - mean) / stdev);
        }//from  w  w  w  . ja  v  a2 s  . c  om
    }
    return normalized;
}

From source file:net.sf.maltcms.chromaui.features.spi.FeatureTable.java

/**
 * factors-> number of rows variables-> number of columns
 *
 * @param factors//from  www  .  j  a  va2s  .  c  o m
 * @param variables
 *
 */
public FeatureTable(int factors, int variables) {
    this.featureMatrix = MatrixUtils.createRealMatrix(factors, variables);
    this.factorNames = new String[factors];
    this.variableNames = new String[variables];
}

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   ww  w.j ava  2s.com*/
    // 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;
}

From source file:org.mitre.math.linear.RealMatrixUtils.java

/**
 * Checks for "use.block.real.matrix" System property and if set (to anything), this will return
 * {@link BlockRealMatrix} instead of the default {@link Array2DRowRealMatrix}.
 *
 * @param rowDimension/* ww  w  . j a v  a 2 s .co m*/
 * @param columnDimension
 * @return a RealMatrix
 */
static public RealMatrix getNewRealMatrix(int rowDimension, int columnDimension) {
    // @todo: fix code in phylogeny-core
    /**
    if (System.getProperty("use.block.real.matrix") == null) {
    return new Array2DRowRealMatrix(rowDimension, columnDimension);
    } else {
    return new BlockRealMatrix(rowDimension, columnDimension);
    }*/
    return MatrixUtils.createRealMatrix(rowDimension, columnDimension);
}

From source file:org.mitre.math.linear.RealMatrixUtils.java

static public RealMatrix getNewRealMatrix(int rowDimension, int columnDimension, double initialValue) {
    RealMatrix matrix;//  w  w  w  . j  a  v  a2  s  . co m
    /**
    if (System.getProperty("use.block.real.matrix") == null) {
    matrix = new Array2DRowRealMatrix(rowDimension, columnDimension);
    } else {
    matrix = new BlockRealMatrix(rowDimension, columnDimension);
    }*/

    matrix = MatrixUtils.createRealMatrix(rowDimension, columnDimension);
    for (int i = 0; i < rowDimension; i++) {
        for (int j = 0; j < columnDimension; j++) {
            matrix.setEntry(i, j, initialValue);
        }
    }

    return matrix;
}