Example usage for org.apache.commons.math3.linear RealMatrix getColumnDimension

List of usage examples for org.apache.commons.math3.linear RealMatrix getColumnDimension

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealMatrix getColumnDimension.

Prototype

int getColumnDimension();

Source Link

Document

Returns the number of columns in the matrix.

Usage

From source file:edu.washington.gs.skyline.model.quantification.DesignMatrix.java

private static RealMatrix matrixFromColumnVectors(double[][] columnVectors) {
    RealMatrix realMatrix = MatrixUtils.createRealMatrix(columnVectors[0].length, columnVectors.length);
    for (int iRow = 0; iRow < realMatrix.getRowDimension(); iRow++) {
        for (int iCol = 0; iCol < realMatrix.getColumnDimension(); iCol++) {
            realMatrix.setEntry(iRow, iCol, columnVectors[iCol][iRow]);
        }/*www.  j a v a  2  s  . c om*/
    }
    return realMatrix;
}

From source file:ch.zhaw.iamp.rct.weights.Weights.java

/**
 * Calculates a pseudo-inverse using the values in the given files. They are
 * first read, then converted to a matrix, and finally used for the
 * calculation of the inverse, using Singular value decomposition (SVD).
 *
 * @param pathToA The file which contains the matrix A, represented in
 * comma-separated-value format.//from w  w  w  .j  av a  2  s.c o m
 * @param targetTrajectoryFile The file which contains the target
 * trajectory, represented in comma-separated-value format.
 * @param weightsFile The file, to which the calculated weights should be
 * written to.
 * @param offset The numbers of first steps to ignore (to skip fading-memory
 * initialization steps).
 */
public static void calculateWeights(final String pathToA, final String targetTrajectoryFile,
        final String weightsFile, final int offset) {
    try {
        RealMatrix A = csvToMatrix(pathToA);
        // cut first n elements
        A = A.getSubMatrix(offset, A.getRowDimension() - 1, 0, A.getColumnDimension() - 1);
        A = addNoise(A);

        RealMatrix b = csvToMatrix(targetTrajectoryFile);

        // adjust b to cutting
        int n = offset % b.getRowDimension();

        if (n > 0) {
            RealMatrix tmp = b.getSubMatrix(n, b.getRowDimension() - 1, 0, b.getColumnDimension() - 1);
            b = b.getSubMatrix(0, n - 1, 0, b.getColumnDimension() - 1);
            double[][] tmpArray = tmp.getData();
            double[][] tmpArray2 = b.getData();
            b = MatrixUtils.createRealMatrix(concat(tmpArray, tmpArray2));
            tmpArray = b.getData();

            for (int i = 0; tmpArray.length < A.getRowDimension(); ++i) {
                tmpArray2 = new double[1][tmpArray[0].length];

                for (int j = 0; j < tmpArray[i].length; ++j) {
                    tmpArray2[0][j] = tmpArray[i][j];
                }

                tmpArray = concat(tmpArray, tmpArray2);
            }

            b = MatrixUtils.createRealMatrix(tmpArray);
        }

        DecompositionSolver solver = new SingularValueDecomposition(A).getSolver();
        RealMatrix x = solver.solve(b).transpose();
        matrixToCsv(x, weightsFile);

    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "Could not read a file: " + ex.getMessage(), "File Error",
                JOptionPane.ERROR_MESSAGE);
        Logger.getLogger(Weights.class.getName()).log(Level.WARNING, "Could not read a file: {0}", ex);
    } catch (DimensionMismatchException ex) {
        JOptionPane.showMessageDialog(null,
                "<html>Could not calculate the " + "pseudo-inverse since a dimension mismatch occurred.<br />"
                        + "Please make sure that all lines of the CSV file posses "
                        + "the same amount of entries.<br />Hint: Remove the last "
                        + "line and try it again.</html>",
                "Matrix Dimension Mismatch", JOptionPane.ERROR_MESSAGE);
        Logger.getLogger(Weights.class.getName()).log(Level.WARNING, "A dimension mismatch occurred: {0}", ex);
    }
}

From source file:lsafunctions.LSA.java

public static RealMatrix calculateTfIdf(RealMatrix M) {
    int tf;/* w w w  .jav a2 s .  com*/
    double idf;
    int df;
    double ndf;
    for (int j = 0; j < M.getRowDimension(); j++) {

        df = calcDf(j, M);
        // System.out.println("J:"+j+"  df:"+df);

        for (int k = 0; k < M.getColumnDimension(); k++) {
            tf = (int) M.getEntry(j, k);
            ndf = M.getColumnDimension() / df;
            idf = Math.log(ndf) / Math.log(2);
            M.setEntry(j, k, idf * tf);
        }
    }
    //M.print(NumberFormat.INTEGER_FIELD, M.getColumnDimension());
    M = normalizeMatrix(M);
    return M;
}

From source file:dataGen.DataGen.java

/**
 * Do the yet created data stored in the matrix fulfill the Anti-Correlation condition?
 * Returns true if all correlation coefficients are below the Supremum correlation coefficient.
 * @param corrMatrix//from w w  w  .  j a  va 2  s.  co  m
 *       The Data-Matrix
 * @param corrCoeff
 *       the Supremum correlation coefficient
 * @param dimensions
 *       how many dimensions does the test file have?
 * @return
 *       true if all correlation coefficients are below the Supremum correlation coefficient.
 */
public static boolean isCorrMatrixCompleteValid(RealMatrix corrMatrix, double corrCoeff, int dimensions) {
    for (int i = 1; i < corrMatrix.getColumnDimension(); i++) {
        for (int j = 0; j < i; j++) {
            if (corrMatrix.getEntry(i, j) > corrCoeff) {
                return false;
            }
        }
    }
    return true;
}

From source file:edu.macalester.tagrelatedness.KendallsCorrelation.java

/**
 * This code is taken from a patch submitted for the Apache Commons 3.3
 * library by The Apache Software Foundation 
 * at: https://issues.apache.org/jira/browse/MATH-814
 * //from  ww w. j  a v a2  s  .  c  o m
 * 
 * Computes the Kendall's Tau rank correlation matrix for the columns of
 * the input matrix.
 *
 * @param matrix matrix with columns representing variables to correlate
 * @return correlation matrix
 * @author Matt Adereth
 */
static public RealMatrix computeCorrelationMatrix(final RealMatrix matrix) {
    int nVars = matrix.getColumnDimension();
    RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < i; j++) {
            double corr = correlation(matrix.getColumn(i), matrix.getColumn(j));
            outMatrix.setEntry(i, j, corr);
            outMatrix.setEntry(j, i, corr);
        }
        outMatrix.setEntry(i, i, 1d);
    }
    return outMatrix;
}

From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java

/**
 * Returns the standard deviations of columns.
 *
 * @param matrix The matrix of which the standard deviations of columns to be computed
 * @return A double array of column standard deviations.
 *//*w  w w. j  av a 2 s .com*/
public static double[] columnStdDevs(RealMatrix matrix) {
    double[] retval = new double[matrix.getColumnDimension()];
    for (int i = 0; i < retval.length; i++) {
        retval[i] = new DescriptiveStatistics(matrix.getColumn(i)).getStandardDeviation();
    }
    return retval;
}

From source file:ch.zhaw.iamp.rct.weights.Weights.java

private static void matrixToCsv(final RealMatrix matrix, final String targetFilePath) throws IOException {
    Writer out = new FileWriter(targetFilePath);

    for (int i = 0; i < matrix.getRowDimension(); ++i) {
        for (int j = 0; j < matrix.getColumnDimension(); ++j) {
            out.append("" + matrix.getEntry(i, j));
            if (j < matrix.getColumnDimension() - 1) {
                out.append(",");
            }/*w ww.ja v a 2 s.  co m*/
        }
        out.append("\n");
        out.flush();
    }

}

From source file:edu.mit.genecircuits.GcUtils.java

/** Convert dense Apache Commons to Colt matrix */
static public DoubleMatrix2D apache2colt(RealMatrix apache) {

    // Convert apache commons matrix to colt
    DoubleMatrix2D colt = new DenseDoubleMatrix2D(apache.getRowDimension(), apache.getColumnDimension());
    for (int i = 0; i < apache.getRowDimension(); i++)
        for (int j = 0; j < apache.getColumnDimension(); j++)
            colt.set(i, j, apache.getEntry(i, j));

    return colt;//from w ww .j  a  v  a2 s . c om
}

From source file:com.yahoo.egads.utilities.SpectralMethods.java

public static RealMatrix createHankelMatrix(RealMatrix data, int windowSize) {

    int n = data.getRowDimension();
    int m = data.getColumnDimension();
    int k = n - windowSize + 1;

    RealMatrix res = MatrixUtils.createRealMatrix(k, m * windowSize);
    double[] buffer = {};

    for (int i = 0; i < n; ++i) {
        double[] row = data.getRow(i);
        buffer = ArrayUtils.addAll(buffer, row);

        if (i >= windowSize - 1) {
            RealMatrix mat = MatrixUtils.createRowRealMatrix(buffer);
            res.setRowMatrix(i - windowSize + 1, mat);
            buffer = ArrayUtils.subarray(buffer, m, buffer.length);
        }//from  ww w.  j a v a2 s  .  co  m
    }

    return res;
}

From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java

/**
 * Returns the row range from the matrix as a new matrix.
 *
 * @param matrix The input matrix/*w  w  w. java2s.c  o  m*/
 * @param start The index of the row to start with (inclusive)
 * @param end The index of the row to end with (inclusive)
 * @return A new matrix with rows specified
 */
public static RealMatrix getRowRange(RealMatrix matrix, int start, int end) {
    return matrix.getSubMatrix(start, end, 0, matrix.getColumnDimension() - 1);
}