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

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

Introduction

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

Prototype

int getRowDimension();

Source Link

Document

Returns the number of rows in the matrix.

Usage

From source file:lsafunctions.LSA.java

private static RealMatrix normalizeMatrix(RealMatrix M) {
    double sumColumn = 0;
    //        Matrix row = new Matrix(1, M.getColumnDimension());
    RealMatrix row = MatrixUtils.createRealMatrix(1, M.getColumnDimension());
    for (int j = 0; j < M.getColumnDimension(); j++) {
        sumColumn = 0;//from  w  w  w  . j  a v  a2 s . co m
        for (int k = 0; k < M.getRowDimension(); k++) {
            sumColumn += Math.pow(M.getEntry(k, j), 2);
        }
        sumColumn = Math.sqrt(sumColumn);
        row.setEntry(0, j, sumColumn);
    }
    for (int j = 0; j < M.getColumnDimension(); j++) {
        for (int k = 0; k < M.getRowDimension(); k++) {
            M.setEntry(k, j, M.getEntry(k, j) / row.getEntry(0, j));
        }
    }
    return M;
}

From source file:cooccurrence.Omer_Levy.java

/**
 * Method that will extract top D singular values from CoVariance Matrix 
 * It will then identify the corresponding columns from U and V and add it to new matrices 
 * @param U//from  w ww. j av  a2  s .c om
 * @param V
 * @param coVariance
 * @param matrixUd
 * @param matrixVd
 * @param coVarD
 * @param indicesD 
 */
private static void getTopD(RealMatrix U, RealMatrix V, RealMatrix coVariance, RealMatrix matrixUd,
        RealMatrix matrixVd, RealMatrix coVarD, ArrayList<Integer> indicesD) {
    TreeMap<Double, Set<Integer>> tmap = new TreeMap<>();
    for (int i = 0; i < coVariance.getRowDimension(); i++) {
        double val = coVariance.getEntry(i, i);
        if (tmap.containsKey(val)) {
            Set<Integer> temp = tmap.get(val);
            temp.add(i);
        } else {
            Set<Integer> temp = new HashSet<>();
            temp.add(i);
            tmap.put(val, temp);
        }
    }
    Iterator iter = tmap.keySet().iterator();
    while (iter.hasNext()) {
        Double val = (Double) iter.next();
        Set<Integer> indices = tmap.get(val);
        for (int i = 0; i < indices.size(); i++) {
            Iterator iterIndices = indices.iterator();
            while (iterIndices.hasNext()) {
                int index = (Integer) iterIndices.next();
                indicesD.add(index);
                coVarD.addToEntry(index, index, val);
                matrixUd.setColumnVector(index, U.getColumnVector(index));
                matrixVd.setColumnVector(index, V.getColumnVector(index));
            }
        }
    }

}

From source file:edu.byu.nlp.stats.DirichletDistribution.java

public static double[][] sample(RealMatrix alphas, RandomGenerator rnd) {
    double[][] theta = GammaDistribution.sample(alphas, rnd);
    for (int i = 0; i < alphas.getRowDimension(); i++) {
        DoubleArrays.normalizeToSelf(theta[i]);
    }//from  w ww .  java  2s .  co m
    return theta;
}

From source file:edu.oregonstate.eecs.mcplan.ml.WekaGlue.java

public static SequentialProjectionHashLearner createSequentialProjectionHashLearner(final RandomGenerator rng,
        final Instances labeled, final Instances unlabeled, final int K, final double eta, final double alpha) {
    assert (labeled.classIndex() >= 0);
    final int Nfeatures = labeled.numAttributes() - 1;

    final RealMatrix X = new Array2DRowRealMatrix(Nfeatures, labeled.size() + unlabeled.size());
    final RealMatrix XL = new Array2DRowRealMatrix(Nfeatures, labeled.size() * 2);
    final RealMatrix S = new Array2DRowRealMatrix(XL.getColumnDimension(), XL.getColumnDimension());

    for (int j = 0; j < labeled.size(); ++j) {
        final Instance inst = labeled.get(j);
        for (int i = 0; i < XL.getRowDimension(); ++i) {
            X.setEntry(i, j, inst.value(i));
            XL.setEntry(i, j, inst.value(i));
        }/*from  w ww .  j  av  a2s  .  c  o m*/

        int sj = -1;
        Instance s = null;
        do {
            sj = rng.nextInt(labeled.size());
            s = labeled.get(sj);
        } while (s == inst || s.classValue() != inst.classValue());
        S.setEntry(j, sj, 1);

        int dj = -1;
        Instance d = null;
        do {
            dj = rng.nextInt(labeled.size());
            d = labeled.get(dj);
        } while (d == inst || d.classValue() == inst.classValue());
        S.setEntry(j, dj, -1);
    }

    for (int j = 0; j < unlabeled.size(); ++j) {
        final Instance inst = unlabeled.get(j);
        for (int i = 0; i < X.getRowDimension(); ++i) {
            X.setEntry(i, labeled.size() + j, inst.value(i));
        }
    }

    return new SequentialProjectionHashLearner(X, XL, S, K, eta, alpha);
}

From source file:edu.cudenver.bios.matrix.MatrixUtilities.java

/**
 * Create a string representing the content of a RealMatrix,
 * suitable for logging.// www  . ja  v a 2s .  c o m
 *
 * @param label An optional label.
 * @param rm    The RealMatrix.
 *
 * @return The string.
 */
public static String logMessage(String label, RealMatrix rm) {
    StringBuilder sb = new StringBuilder();

    sb.append(EOL);

    if (label != null) {
        sb.append("  ").append(label).append(EOL);
    }

    if (rm == null) {
        sb.append("   (null)");
        sb.append(EOL);
    } else {
        int m = rm.getRowDimension();
        int n = rm.getColumnDimension();

        sb.append("   (").append(m).append(" x ").append(n).append(")").append(EOL);

        for (int i = 0; i < m; ++i) {
            sb.append("    ");

            for (int j = 0; j < n; ++j) {
                sb.append(rm.getEntry(i, j)).append(' ');
            }

            sb.append(EOL);
        }
    }

    return sb.toString();
}

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

/**
 * Returns a new matrix by subtracting elements column by column.
 *
 * @param matrix The input matrix/*from  ww  w.j  a v  a 2  s .c o m*/
 * @param vector The vector to be subtracted from columns
 * @return A new matrix of which the vector is subtracted column by column
 */
public static RealMatrix colSubtract(RealMatrix matrix, double[] vector) {
    // Declare and initialize the new matrix:
    double[][] retval = new double[matrix.getRowDimension()][matrix.getColumnDimension()];

    // Iterate over rows:
    for (int col = 0; col < retval.length; col++) {
        // Iterate over columns:
        for (int row = 0; row < retval[0].length; row++) {
            retval[row][col] = matrix.getEntry(row, col) - vector[row];
        }
    }

    // Done, return a new matrix:
    return MatrixUtils.createRealMatrix(retval);
}

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

/**
 * Returns a new matrix by subtracting elements row by row.
 *
 * @param matrix The input matrix//from   w w w . j  av a  2s .  com
 * @param vector The vector to be subtracted from rows
 * @return A new matrix of which the vector is subtracted row by row
 */
public static RealMatrix rowSubtract(RealMatrix matrix, double[] vector) {
    // Declare and initialize the new matrix:
    double[][] retval = new double[matrix.getRowDimension()][matrix.getColumnDimension()];

    // Iterate over rows:
    for (int row = 0; row < retval.length; row++) {
        // Iterate over columns:
        for (int col = 0; col < retval[0].length; col++) {
            retval[row][col] = matrix.getEntry(row, col) - vector[col];
        }
    }

    // Done, return a new matrix:
    return MatrixUtils.createRealMatrix(retval);
}

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

/**
 * Returns a new matrix by adding elements row by row.
 *
 * @param matrix The input matrix//w  w  w.  j a v a  2  s  .  com
 * @param vector The vector to be added to rows
 * @return A new matrix of which the vector is added row by row
 */
public static RealMatrix columnAdd(RealMatrix matrix, double[] vector) {
    // Declare and initialize the new matrix:
    double[][] retval = new double[matrix.getRowDimension()][matrix.getColumnDimension()];

    // Iterate over rows:
    for (int col = 0; col < retval.length; col++) {
        // Iterate over columns:
        for (int row = 0; row < retval[0].length; row++) {
            retval[row][col] = matrix.getEntry(row, col) + vector[row];
        }
    }

    // Done, return a new matrix:
    return MatrixUtils.createRealMatrix(retval);
}

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

/**
 * Returns a new matrix by adding elements row by row.
 *
 * @param matrix The input matrix//w  ww  . java 2s .  c  o  m
 * @param vector The vector to be added to rows
 * @return A new matrix of which the vector is added row by row
 */
public static RealMatrix rowAdd(RealMatrix matrix, double[] vector) {
    // Declare and initialize the new matrix:
    double[][] retval = new double[matrix.getRowDimension()][matrix.getColumnDimension()];

    // Iterate over rows:
    for (int row = 0; row < retval.length; row++) {
        // Iterate over columns:
        for (int col = 0; col < retval[0].length; col++) {
            retval[row][col] = matrix.getEntry(row, col) + vector[col];
        }
    }

    // Done, return a new matrix:
    return MatrixUtils.createRealMatrix(retval);
}

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

/**
 * Multiplies the matrix' rows using the vector element-by-element.
 *
 * @param matrix The input matrix.//  w w w. j  a v a 2s .c o m
 * @param vector The vector which will be used to multiply rows of the matrix element-by-element.
 * @return The new matrix of which rows are multiplied with the vector element-by-element.
 */
public static RealMatrix rbrMultiply(RealMatrix matrix, RealVector vector) {
    // Define the return value:
    RealMatrix retval = MatrixUtils.createRealMatrix(matrix.getRowDimension(), matrix.getColumnDimension());

    // Iterate over rows:
    for (int i = 0; i < retval.getRowDimension(); i++) {
        retval.setRowVector(i, matrix.getRowVector(i).ebeMultiply(vector));
    }

    // Done, return:
    return retval;
}