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

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

Introduction

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

Prototype

void setSubMatrix(double[][] subMatrix, int row, int column)
        throws NoDataException, OutOfRangeException, DimensionMismatchException, NullArgumentException;

Source Link

Document

Replace the submatrix starting at row, column using data in the input subMatrix array.

Usage

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

/**
 * Horizontally append two matrices//from  w ww  .  j av  a  2 s. com
 * @param matrix
 * @param column
 * @return the combined matrix
 * @throws IllegalArgumentException
 */
public static RealMatrix getHorizontalAppend(RealMatrix m1, RealMatrix m2) throws IllegalArgumentException {
    if (m1 == null || m2 == null)
        throw new IllegalArgumentException("Missing required argument");
    if (m1.getRowDimension() != m2.getRowDimension())
        throw new IllegalArgumentException("Row dimensions must be equal");

    RealMatrix newMatrix = new Array2DRowRealMatrix(m1.getRowDimension(),
            m1.getColumnDimension() + m2.getColumnDimension());
    newMatrix.setSubMatrix(m1.getData(), 0, 0);
    newMatrix.setSubMatrix(m2.getData(), 0, m1.getColumnDimension());

    return newMatrix;
}

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

/**
 * Calculate the Kronecker product of two matrices
 *
 * @param matrix1 first matrix/*  w w  w.  java2 s .  c o  m*/
 * @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:edu.cudenver.bios.matrix.MatrixUtils.java

/**
 * Calculate the horizontal direct product of two matrices
 *
 * @param matrix1 first matrix/*w ww  .j ava 2  s  .  co m*/
 * @param matrix2 second matrix
 * @return horizontal direct product of matrix 1 and matrix 2
 */
public static RealMatrix getHorizontalDirectProduct(RealMatrix matrix1, RealMatrix matrix2)
        throws IllegalArgumentException {
    if (matrix1 == null || matrix2 == null)
        throw new IllegalArgumentException("null input matrix");
    if (matrix1.getRowDimension() != matrix2.getRowDimension())
        throw new IllegalArgumentException("input matrices must have equal row dimension");

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

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

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

@Nonnull
public static RealMatrix combinedMatrices(@Nonnull final RealMatrix[][] grid, final int dimensions) {
    Preconditions.checkArgument(grid.length >= 1, "The number of rows must be greater than 1");
    Preconditions.checkArgument(grid[0].length >= 1, "The number of cols must be greater than 1");
    Preconditions.checkArgument(dimensions > 0, "Dimension should be more than 0: ", dimensions);

    final int rows = grid.length;
    final int cols = grid[0].length;

    final RealMatrix combined = new BlockRealMatrix(rows * dimensions, cols * dimensions);
    for (int row = 0; row < grid.length; row++) {
        for (int col = 0; col < grid[row].length; col++) {
            combined.setSubMatrix(grid[row][col].getData(), row * dimensions, col * dimensions);
        }/*w  w  w .  j a v  a2s  .  c o  m*/
    }
    return combined;
}

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

/**
 * QR decomposition for a tridiagonal matrix T.
 *
 * @see https://gist.github.com/lightcatcher/8118181
 * @see http://www.ericmart.in/blog/optimizing_julia_tridiag_qr
 * @param T target tridiagonal matrix//from www  . j  av  a  2s.  c o m
 * @param R output matrix for R which is the same shape as T
 * @param Qt output matrix for Q.T which is the same shape an T
 */
public static void tridiagonalQR(@Nonnull final RealMatrix T, @Nonnull final RealMatrix R,
        @Nonnull final RealMatrix Qt) {
    int n = T.getRowDimension();
    Preconditions.checkArgument(n == R.getRowDimension() && n == R.getColumnDimension(),
            "T and R must be the same shape");
    Preconditions.checkArgument(n == Qt.getRowDimension() && n == Qt.getColumnDimension(),
            "T and Qt must be the same shape");

    // initial R = T
    R.setSubMatrix(T.getData(), 0, 0);

    // initial Qt = identity
    Qt.setSubMatrix(eye(n), 0, 0);

    for (int i = 0; i < n - 1; i++) {
        // Householder projection for a vector x
        // https://en.wikipedia.org/wiki/Householder_transformation
        RealVector x = T.getSubMatrix(i, i + 1, i, i).getColumnVector(0);
        x = unitL2norm(x);

        RealMatrix subR = R.getSubMatrix(i, i + 1, 0, n - 1);
        R.setSubMatrix(subR.subtract(x.outerProduct(subR.preMultiply(x)).scalarMultiply(2)).getData(), i, 0);

        RealMatrix subQt = Qt.getSubMatrix(i, i + 1, 0, n - 1);
        Qt.setSubMatrix(subQt.subtract(x.outerProduct(subQt.preMultiply(x)).scalarMultiply(2)).getData(), i, 0);
    }
}

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

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

    final int rows = grid.length;
    final int rowDims = grid[0].getRowDimension();
    final int colDims = grid[0].getColumnDimension();

    final RealMatrix combined = new BlockRealMatrix(rows * rowDims, colDims);
    for (int row = 0; row < grid.length; row++) {
        RealMatrix cell = grid[row];//from w  ww  .j av a 2  s.  c o  m
        Preconditions.checkArgument(cell.getRowDimension() == rowDims, "Mismatch in row dimensions at row ",
                row);
        Preconditions.checkArgument(cell.getColumnDimension() == colDims, "Mismatch in col dimensions at row ",
                row);
        combined.setSubMatrix(cell.getData(), row * rowDims, 0);
    }
    return combined;
}

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

/**
 * Lanczos tridiagonalization for a symmetric matrix C to make s * s tridiagonal matrix T.
 *
 * @see http://www.cas.mcmaster.ca/~qiao/publications/spie05.pdf
 * @param C target symmetric matrix//from   www. j  a va  2s .c  om
 * @param a initial vector
 * @param T result is stored here
 */
public static void lanczosTridiagonalization(@Nonnull final RealMatrix C, @Nonnull final double[] a,
        @Nonnull final RealMatrix T) {
    Preconditions.checkArgument(Arrays.deepEquals(C.getData(), C.transpose().getData()),
            "Target matrix C must be a symmetric matrix");
    Preconditions.checkArgument(C.getColumnDimension() == a.length,
            "Column size of A and length of a should be same");
    Preconditions.checkArgument(T.getRowDimension() == T.getColumnDimension(), "T must be a square matrix");

    int s = T.getRowDimension();

    // initialize T with zeros
    T.setSubMatrix(new double[s][s], 0, 0);

    RealVector a0 = new ArrayRealVector(a.length);
    RealVector r = new ArrayRealVector(a);

    double beta0 = 1.d;

    for (int i = 0; i < s; i++) {
        RealVector a1 = r.mapDivide(beta0);
        RealVector Ca1 = C.operate(a1);

        double alpha1 = a1.dotProduct(Ca1);

        r = Ca1.add(a1.mapMultiply(-1.d * alpha1)).add(a0.mapMultiply(-1.d * beta0));

        double beta1 = r.getNorm();

        T.setEntry(i, i, alpha1);
        if (i - 1 >= 0) {
            T.setEntry(i, i - 1, beta0);
        }
        if (i + 1 < s) {
            T.setEntry(i, i + 1, beta1);
        }

        a0 = a1.copy();
        beta0 = beta1;
    }
}

From source file:fi.smaa.jsmaa.model.MultivariateGaussianCriterionMeasurement.java

@Override
public void addAlternative(Alternative alt) {
    alternatives.add(alt);/*from  ww  w.  j  a v  a2 s  .  c  o  m*/
    RealMatrix matrix = MatrixUtils.createRealIdentityMatrix(alternatives.size());
    final RealMatrix newCovarianceMatrix = matrix;
    newCovarianceMatrix.setSubMatrix(covarianceMatrix.getData(), 0, 0);
    final RealVector newMeanVector = meanVector.append(0);
    fireEvents(setMeanVectorInternal(newMeanVector), setCovarianceMatrixInternal(newCovarianceMatrix));
}

From source file:com.joptimizer.functions.SOCPLogarithmicBarrier.java

private RealMatrix buildJ(SOCPConstraintParameters param, RealVector X) {
    RealMatrix J = new Array2DRowRealMatrix(dim, param.getA().getRowDimension() + 1);
    J.setSubMatrix(param.getA().transpose().getData(), 0, 0);
    J.setColumnVector(param.getA().getRowDimension(), param.getC());
    return J;/*www. jav a 2 s . co  m*/
}

From source file:com.joptimizer.functions.SOCPLogarithmicBarrier.java

/**
 * Create the barrier function for the Phase I.
 * It is an instance of this class for the constraints: 
 * <br>||Ai.x+bi|| < ci.x+di+t, i=1,...,m
 * @see "S.Boyd and L.Vandenberghe, Convex Optimization, 11.6.2"
 *//*  w  w  w  .ja  v a  2 s .  c  o m*/
public BarrierFunction createPhase1BarrierFunction() {

    final int dimPh1 = dim + 1;
    List<SOCPConstraintParameters> socpConstraintParametersPh1List = new ArrayList<SOCPConstraintParameters>();
    SOCPLogarithmicBarrier bfPh1 = new SOCPLogarithmicBarrier(socpConstraintParametersPh1List, dimPh1);

    for (int i = 0; i < socpConstraintParametersList.size(); i++) {
        SOCPConstraintParameters param = socpConstraintParametersList.get(i);
        RealMatrix A = param.getA();
        RealVector b = param.getB();
        RealVector c = param.getC();
        double d = param.getD();

        RealMatrix APh1 = MatrixUtils.createRealMatrix(A.getRowDimension(), dimPh1);
        APh1.setSubMatrix(A.getData(), 0, 0);
        RealVector bPh1 = b;
        RealVector cPh1 = new ArrayRealVector(c.getDimension() + 1);
        cPh1.setSubVector(0, c);
        cPh1.setEntry(c.getDimension(), 1);
        double dPh1 = d;

        SOCPConstraintParameters paramsPh1 = new SOCPConstraintParameters(APh1.getData(), bPh1.toArray(),
                cPh1.toArray(), dPh1);
        socpConstraintParametersPh1List.add(socpConstraintParametersPh1List.size(), paramsPh1);
    }

    return bfPh1;
}