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:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java

/**
 * Shuffles rows of a matrix using the provided random number generator.
 *
 * @param matrix The matrix of which the rows will be shuffled.
 * @param randomGenerator The random number generator to be used.
 * @return The new shuffled matrix.//from w ww  .  j a va  2s .c o m
 */
public static RealMatrix shuffleRows(RealMatrix matrix, RandomGenerator randomGenerator) {
    // Create an index vector to be shuffled:
    int[] index = MathArrays.sequence(matrix.getRowDimension(), 0, 1);
    MathArrays.shuffle(index, randomGenerator);

    // Create a new matrix:
    RealMatrix retval = MatrixUtils.createRealMatrix(matrix.getRowDimension(), matrix.getColumnDimension());

    // Populate:
    for (int row = 0; row < index.length; row++) {
        retval.setRowVector(row, matrix.getRowVector(index[row]));
    }

    // Done, return:
    return retval;
}

From source file:jmbench.impl.runtime.CommonsMathAlgorithmFactory.java

public static DenseMatrix64F realToEjml(RealMatrix orig) {
    if (orig == null)
        return null;

    DenseMatrix64F ret = new DenseMatrix64F(orig.getRowDimension(), orig.getColumnDimension());

    for (int i = 0; i < ret.numRows; i++) {
        for (int j = 0; j < ret.numCols; j++) {
            ret.set(i, j, orig.getEntry(i, j));
        }/*from  w  w  w.j a  v a  2s.  co m*/
    }

    return ret;
}

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

/**
 * Creates a matrix of equal dimension but with all non-diagonal
 * elements set to 0//from   w w w . j ava 2 s  .c o  m
 *
 * @param matrix the matrix from which to extract the diagonal
 * @return diagonal matrix
 */
public static RealMatrix getDiagonalMatrix(RealMatrix matrix) throws IllegalArgumentException {
    if (matrix == null)
        throw new IllegalArgumentException("null input matrix");

    double[][] zData = new double[matrix.getRowDimension()][matrix.getColumnDimension()];
    for (int row = 0; row < matrix.getRowDimension(); row++) {
        for (int col = 0; col < matrix.getColumnDimension(); col++) {
            if (row == col)
                zData[row][col] = matrix.getEntry(row, col);
            else
                zData[row][col] = 0;
        }
    }
    return new Array2DRowRealMatrix(zData);
}

From source file:jmatbench.commonsmath.CommonsMathAlgorithmFactory.java

public static RowMajorMatrix realToEjml(RealMatrix orig) {
    if (orig == null)
        return null;

    RowMajorMatrix ret = new RowMajorMatrix(orig.getRowDimension(), orig.getColumnDimension());

    for (int i = 0; i < ret.numRows; i++) {
        for (int j = 0; j < ret.numCols; j++) {
            ret.set(i, j, orig.getEntry(i, j));
        }/*from  ww w  .  j  a  v a2 s .c om*/
    }

    return ret;
}

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

/**
 * Returns the vec(matrix)./*w w w.j  ava  2s.com*/
 * @param RealMatrix matrix
 * @return RealMatrix representing vec(matrix).
 */
public static RealMatrix getVecMatrix(RealMatrix matrix) {
    if (matrix == null) {
        throw new IllegalArgumentException("Null matrix not allowed for " + "getVecMatrix()");
    }
    int numRows = matrix.getRowDimension();
    int numCols = matrix.getColumnDimension();
    RealMatrix vec = new Array2DRowRealMatrix(numRows * numCols, 1);
    int newRowNum = 0;

    //loop through each column
    for (int c = 0; c < numCols; c++) {
        //insert column values into new r x 1 matrix
        for (int r = 0; r < numRows; r++, newRowNum++) {
            vec.setEntry(newRowNum, 0, matrix.getEntry(r, c));
        }
    }
    return vec;
}

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

/**
 * Calculate the Kronecker product of two matrices
 *
 * @param matrix1 first matrix/*from  w w w  .  j a va2  s  .  c  om*/
 * @param matrix2 second matrix
 * @return Kronecker product of matrix 1 and matrix 2
 */
public static RealMatrix getKroneckerProduct(RealMatrix matrix1, RealMatrix matrix2) {
    if (matrix1 == null || matrix2 == null)
        throw new IllegalArgumentException("null input matrix");

    int m1Rows = matrix1.getRowDimension();
    int m1Cols = matrix1.getColumnDimension();
    int m2Rows = matrix2.getRowDimension();
    int m2Cols = matrix2.getColumnDimension();

    double[][] productData = new double[m1Rows * m2Rows][m1Cols * m2Cols];
    RealMatrix productMatrix = new Array2DRowRealMatrix(productData);
    for (int col = 0; col < m1Cols; col++) {
        for (int row = 0; row < m1Rows; row++) {
            productMatrix.setSubMatrix((matrix2.scalarMultiply(matrix1.getEntry(row, col))).getData(),
                    row * m2Rows, col * m2Cols);
        }
    }

    return productMatrix;
}

From source file:com.clust4j.algo.preprocess.PCA.java

/**
 * Flip Eigenvectors' sign to enforce deterministic output
 * @param U//w ww  .  ja  va 2  s . c  o  m
 * @param V
 * @return
 */
static EntryPair<RealMatrix, RealMatrix> eigenSignFlip(RealMatrix U, RealMatrix V) {
    // need to get column arg maxes of abs vals of U
    double[][] u = U.getData();
    double[][] v = V.getData();
    int[] max_abs_cols = MatUtils.argMax(MatUtils.abs(u), Axis.COL);

    // Get the signs of the diagonals in the rows corresponding to max_abs_cols
    int col_idx = 0;
    double val;
    double[] row;
    int[] signs = new int[U.getColumnDimension()];
    for (int row_idx : max_abs_cols) {
        row = u[row_idx];
        val = row[col_idx];
        signs[col_idx++] = val == 0 ? 0 : val < 0 ? -1 : 1;
    }

    // Multiply U by the signs... column-wise
    for (int i = 0; i < u.length; i++) {
        for (int j = 0; j < U.getColumnDimension(); j++) {
            u[i][j] *= signs[j];
        }
    }

    // Perform same op for V row-wise
    for (int j = 0; j < signs.length; j++) {
        for (int k = 0; k < V.getColumnDimension(); k++) {
            v[j][k] *= signs[j];
        }
    }

    return new EntryPair<RealMatrix, RealMatrix>(new Array2DRowRealMatrix(u, false),
            new Array2DRowRealMatrix(v, false));
}

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

/**
 * Find the maximum value in the matrix// w  ww  . ja  va 2s. c  om
 * @param matrix
 */
public static double getMaxValue(RealMatrix matrix) {
    double max;
    if (matrix == null) {
        max = Double.NaN;
    } else {
        max = Double.NEGATIVE_INFINITY;
        for (int r = 0; r < matrix.getRowDimension(); r++) {
            for (int c = 0; c < matrix.getColumnDimension(); c++) {
                double value = matrix.getEntry(r, c);
                if (value > max) {
                    max = value;
                }
            }
        }
    }
    return max;
}

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

/**
 * Return the element wise sum of squares
 * @param matrix input matrix/*  www.j a v a  2 s .c o m*/
 * @return sum of squares
 */
public static double getSumOfSquares(RealMatrix matrix) {
    if (matrix == null) {
        throw new IllegalArgumentException("Null matrix not allowed for " + "getSumOfSquares()");
    }
    double sum = 0.0;
    for (int r = 0; r < matrix.getRowDimension(); r++) {
        for (int c = 0; c < matrix.getColumnDimension(); c++) {
            double value = matrix.getEntry(r, c);
            sum += value * value;
        }
    }
    return sum;
}

From source file:hivemall.utils.math.MatrixUtils.java

@Nonnull
public static double[] flatten(@Nonnull final RealMatrix[] grid) {
    Preconditions.checkArgument(grid.length >= 1, "The number of rows must be greater than 1");

    final int rows = grid.length;
    RealMatrix grid0 = grid[0];
    Preconditions.checkNotNull(grid0);/*w w w.  ja v  a2  s.  c  o m*/
    int cellRows = grid0.getRowDimension();
    int cellCols = grid0.getColumnDimension();

    final DoubleArrayList list = new DoubleArrayList(rows * cellRows * cellCols);
    final RealMatrixPreservingVisitor visitor = new DefaultRealMatrixPreservingVisitor() {
        @Override
        public void visit(int row, int column, double value) {
            list.add(value);
        }
    };

    for (int row = 0; row < rows; row++) {
        RealMatrix cell = grid[row];
        cell.walkInRowOrder(visitor);
    }

    return list.toArray();
}