Java Code Examples for org.apache.commons.math.exception.util.LocalizedFormats#DIMENSIONS_MISMATCH_2x2

The following examples show how to use org.apache.commons.math.exception.util.LocalizedFormats#DIMENSIONS_MISMATCH_2x2 . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractRealMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public void setColumnVector(final int column, final RealVector vector)
    throws MatrixIndexException, InvalidMatrixException {

    MatrixUtils.checkColumnIndex(this, column);
    final int nRows = getRowDimension();
    if (vector.getDimension() != nRows) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                vector.getDimension(), 1, nRows, 1);
    }
    for (int i = 0; i < nRows; ++i) {
        setEntry(i, column, vector.getEntry(i));
    }

}
 
Example 2
Source File: MatrixUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**Solve  a  system of composed of a Lower Triangular Matrix
 * {@link RealMatrix}.
 * <p>
 * This method is called to solve systems of equations which are
 * of the lower triangular form. The matrix {@link RealMatrix}
 * is assumed, though not checked, to be in lower triangular form.
 * The vector {@link RealVector} is overwritten with the solution.
 * The matrix is checked that it is square and its dimensions match
 * the length of the vector.
 * </p>
 * @param rm RealMatrix which is lower triangular
 * @param b  RealVector this is overwritten
 * @exception IllegalArgumentException if the matrix and vector are not conformable
 * @exception ArithmeticException there is a zero or near zero on the diagonal of rm
 */
public static void solveLowerTriangularSystem( RealMatrix rm, RealVector b){
    if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) {
        throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
                (rm == null) ? 0 : rm.getRowDimension(),
                (b == null) ? 0 : b.getDimension());
    }
    if( rm.getColumnDimension() != rm.getRowDimension() ){
        throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                rm.getRowDimension(),rm.getRowDimension(),
                rm.getRowDimension(),rm.getColumnDimension());
    }
    int rows = rm.getRowDimension();
    for( int i = 0 ; i < rows ; i++ ){
        double diag = rm.getEntry(i, i);
        if( FastMath.abs(diag) < MathUtils.SAFE_MIN ){
            throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
        }
        double bi = b.getEntry(i)/diag;
        b.setEntry(i,  bi );
        for( int j = i+1; j< rows; j++ ){
            b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i)  );
        }
    }
}
 
Example 3
Source File: BlockRealMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void setRow(final int row, final double[] array)
    throws MatrixIndexException, InvalidMatrixException {

    MatrixUtils.checkRowIndex(this, row);
    final int nCols = getColumnDimension();
    if (array.length != nCols) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                1, array.length, 1, nCols);
    }

    // perform copy block-wise, to ensure good cache behavior
    final int iBlock  = row / BLOCK_SIZE;
    final int iRow    = row - iBlock * BLOCK_SIZE;
    int outIndex      = 0;
    for (int jBlock = 0; jBlock < blockColumns; ++jBlock) {
        final int jWidth     = blockWidth(jBlock);
        final double[] block = blocks[iBlock * blockColumns + jBlock];
        System.arraycopy(array, outIndex, block, iRow * jWidth, jWidth);
        outIndex += jWidth;
    }

}
 
Example 4
Source File: MatrixUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Solver a  system composed  of an Upper Triangular Matrix
 * {@link RealMatrix}.
 * <p>
 * This method is called to solve systems of equations which are
 * of the lower triangular form. The matrix {@link RealMatrix}
 * is assumed, though not checked, to be in upper triangular form.
 * The vector {@link RealVector} is overwritten with the solution.
 * The matrix is checked that it is square and its dimensions match
 * the length of the vector.
 * </p>
 * @param rm RealMatrix which is upper triangular
 * @param b  RealVector this is overwritten
 * @exception IllegalArgumentException if the matrix and vector are not conformable
 * @exception ArithmeticException there is a zero or near zero on the diagonal of rm
 */
public static void solveUpperTriangularSystem( RealMatrix rm, RealVector b){
    if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) {
        throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
                (rm == null) ? 0 : rm.getRowDimension(),
                (b == null) ? 0 : b.getDimension());
    }
    if( rm.getColumnDimension() != rm.getRowDimension() ){
        throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                rm.getRowDimension(),rm.getRowDimension(),
                rm.getRowDimension(),rm.getColumnDimension());
    }
    int rows = rm.getRowDimension();
    for( int i = rows-1 ; i >-1 ; i-- ){
        double diag = rm.getEntry(i, i);
        if( FastMath.abs(diag) < MathUtils.SAFE_MIN ){
            throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
        }
        double bi = b.getEntry(i)/diag;
        b.setEntry(i,  bi );
        for( int j = i-1; j>-1; j-- ){
            b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i)  );
        }
    }
}
 
Example 5
Source File: AbstractRealMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public void setColumn(final int column, final double[] array)
    throws MatrixIndexException, InvalidMatrixException {

    MatrixUtils.checkColumnIndex(this, column);
    final int nRows = getRowDimension();
    if (array.length != nRows) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                array.length, 1, nRows, 1);
    }
    for (int i = 0; i < nRows; ++i) {
        setEntry(i, column, array[i]);
    }

}
 
Example 6
Source File: AbstractRealMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public void setRow(final int row, final double[] array)
    throws MatrixIndexException, InvalidMatrixException {

    MatrixUtils.checkRowIndex(this, row);
    final int nCols = getColumnDimension();
    if (array.length != nCols) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                1, array.length, 1, nCols);
    }
    for (int i = 0; i < nCols; ++i) {
        setEntry(row, i, array[i]);
    }

}
 
Example 7
Source File: AbstractRealMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public void setRowVector(final int row, final RealVector vector)
    throws MatrixIndexException, InvalidMatrixException {

    MatrixUtils.checkRowIndex(this, row);
    final int nCols = getColumnDimension();
    if (vector.getDimension() != nCols) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                1, vector.getDimension(), 1, nCols);
    }
    for (int i = 0; i < nCols; ++i) {
        setEntry(row, i, vector.getEntry(i));
    }

}
 
Example 8
Source File: MatrixUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**Solve  a  system of composed of a Lower Triangular Matrix
 * {@link RealMatrix}.
 * <p>
 * This method is called to solve systems of equations which are
 * of the lower triangular form. The matrix {@link RealMatrix}
 * is assumed, though not checked, to be in lower triangular form.
 * The vector {@link RealVector} is overwritten with the solution.
 * The matrix is checked that it is square and its dimensions match
 * the length of the vector.
 * </p>
 * @param rm RealMatrix which is lower triangular
 * @param b  RealVector this is overwritten
 * @exception IllegalArgumentException if the matrix and vector are not conformable
 * @exception ArithmeticException there is a zero or near zero on the diagonal of rm
 */
public static void solveLowerTriangularSystem( RealMatrix rm, RealVector b){
    if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) {
        throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
                (rm == null) ? 0 : rm.getRowDimension(),
                (b == null) ? 0 : b.getDimension());
    }
    if( rm.getColumnDimension() != rm.getRowDimension() ){
        throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                rm.getRowDimension(),rm.getRowDimension(),
                rm.getRowDimension(),rm.getColumnDimension());
    }
    int rows = rm.getRowDimension();
    for( int i = 0 ; i < rows ; i++ ){
        double diag = rm.getEntry(i, i);
        if( FastMath.abs(diag) < MathUtils.SAFE_MIN ){
            throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
        }
        double bi = b.getEntry(i)/diag;
        b.setEntry(i,  bi );
        for( int j = i+1; j< rows; j++ ){
            b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i)  );
        }
    }
}
 
Example 9
Source File: AbstractRealMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public void setRowMatrix(final int row, final RealMatrix matrix)
    throws MatrixIndexException, InvalidMatrixException {

    MatrixUtils.checkRowIndex(this, row);
    final int nCols = getColumnDimension();
    if ((matrix.getRowDimension() != 1) ||
        (matrix.getColumnDimension() != nCols)) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols);
    }
    for (int i = 0; i < nCols; ++i) {
        setEntry(row, i, matrix.getEntry(0, i));
    }

}
 
Example 10
Source File: MatrixUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Solver a  system composed  of an Upper Triangular Matrix
 * {@link RealMatrix}.
 * <p>
 * This method is called to solve systems of equations which are
 * of the lower triangular form. The matrix {@link RealMatrix}
 * is assumed, though not checked, to be in upper triangular form.
 * The vector {@link RealVector} is overwritten with the solution.
 * The matrix is checked that it is square and its dimensions match
 * the length of the vector.
 * </p>
 * @param rm RealMatrix which is upper triangular
 * @param b  RealVector this is overwritten
 * @exception IllegalArgumentException if the matrix and vector are not conformable
 * @exception ArithmeticException there is a zero or near zero on the diagonal of rm
 */
public static void solveUpperTriangularSystem( RealMatrix rm, RealVector b){
    if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) {
        throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
                (rm == null) ? 0 : rm.getRowDimension(),
                (b == null) ? 0 : b.getDimension());
    }
    if( rm.getColumnDimension() != rm.getRowDimension() ){
        throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                rm.getRowDimension(),rm.getRowDimension(),
                rm.getRowDimension(),rm.getColumnDimension());
    }
    int rows = rm.getRowDimension();
    for( int i = rows-1 ; i >-1 ; i-- ){
        double diag = rm.getEntry(i, i);
        if( FastMath.abs(diag) < MathUtils.SAFE_MIN ){
            throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
        }
        double bi = b.getEntry(i)/diag;
        b.setEntry(i,  bi );
        for( int j = i-1; j>-1; j-- ){
            b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i)  );
        }
    }
}
 
Example 11
Source File: AbstractFieldMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public void setRow(final int row, final T[] array)
    throws MatrixIndexException, InvalidMatrixException {

    checkRowIndex(row);
    final int nCols = getColumnDimension();
    if (array.length != nCols) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                1, array.length, 1, nCols);
    }
    for (int i = 0; i < nCols; ++i) {
        setEntry(row, i, array[i]);
    }

}
 
Example 12
Source File: AbstractFieldMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public void setColumnVector(final int column, final FieldVector<T> vector)
    throws MatrixIndexException, InvalidMatrixException {

    checkColumnIndex(column);
    final int nRows = getRowDimension();
    if (vector.getDimension() != nRows) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                vector.getDimension(), 1, nRows, 1);
    }
    for (int i = 0; i < nRows; ++i) {
        setEntry(i, column, vector.getEntry(i));
    }

}
 
Example 13
Source File: AbstractFieldMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public void setRowVector(final int row, final FieldVector<T> vector)
    throws MatrixIndexException, InvalidMatrixException {

    checkRowIndex(row);
    final int nCols = getColumnDimension();
    if (vector.getDimension() != nCols) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                1, vector.getDimension(), 1, nCols);
    }
    for (int i = 0; i < nCols; ++i) {
        setEntry(row, i, vector.getEntry(i));
    }

}
 
Example 14
Source File: AbstractFieldMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public void setRowMatrix(final int row, final FieldMatrix<T> matrix)
    throws MatrixIndexException, InvalidMatrixException {

    checkRowIndex(row);
    final int nCols = getColumnDimension();
    if ((matrix.getRowDimension() != 1) ||
        (matrix.getColumnDimension() != nCols)) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols);
    }
    for (int i = 0; i < nCols; ++i) {
        setEntry(row, i, matrix.getEntry(0, i));
    }

}
 
Example 15
Source File: MatrixDimensionMismatchException.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct an exception from the mismatched dimensions.
 *
 * @param wrongRowDim Wrong row dimension.
 * @param wrongColDim Wrong column dimension.
 * @param expectedRowDim Expected row dimension.
 * @param expectedColDim Expected column dimension.
 */
public MatrixDimensionMismatchException(int wrongRowDim,
                                        int wrongColDim,
                                        int expectedRowDim,
                                        int expectedColDim) {
    super(LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
          new Integer[] { wrongRowDim, wrongColDim },
          new Integer[] { expectedRowDim, expectedColDim });
}
 
Example 16
Source File: BlockRealMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the entries in column number <code>column</code>
 * as a column matrix.  Column indices start at 0.
 *
 * @param column the column to be set
 * @param matrix column matrix (must have one column and the same number of rows
 * as the instance)
 * @throws MatrixIndexException if the specified column index is invalid
 * @throws InvalidMatrixException if the matrix dimensions do not match one
 * instance column
 */
void setColumnMatrix(final int column, final BlockRealMatrix matrix)
    throws MatrixIndexException, InvalidMatrixException {

    MatrixUtils.checkColumnIndex(this, column);
    final int nRows = getRowDimension();
    if ((matrix.getRowDimension() != nRows) ||
        (matrix.getColumnDimension() != 1)) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                matrix.getRowDimension(), matrix.getColumnDimension(),
                nRows, 1);
    }

    // perform copy block-wise, to ensure good cache behavior
    final int jBlock  = column / BLOCK_SIZE;
    final int jColumn = column - jBlock * BLOCK_SIZE;
    final int jWidth  = blockWidth(jBlock);
    int mBlockIndex = 0;
    int mIndex      = 0;
    double[] mBlock = matrix.blocks[mBlockIndex];
    for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
        final int iHeight = blockHeight(iBlock);
        final double[] block = blocks[iBlock * blockColumns + jBlock];
        for (int i = 0; i < iHeight; ++i) {
            if (mIndex >= mBlock.length) {
                mBlock = matrix.blocks[++mBlockIndex];
                mIndex = 0;
            }
            block[i * jWidth + jColumn] = mBlock[mIndex++];
        }
    }

}
 
Example 17
Source File: MatrixDimensionMismatchException.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct an exception from the mismatched dimensions.
 *
 * @param wrongRowDim Wrong row dimension.
 * @param wrongColDim Wrong column dimension.
 * @param expectedRowDim Expected row dimension.
 * @param expectedColDim Expected column dimension.
 */
public MatrixDimensionMismatchException(int wrongRowDim,
                                        int wrongColDim,
                                        int expectedRowDim,
                                        int expectedColDim) {
    super(LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
          new Integer[] { wrongRowDim, wrongColDim },
          new Integer[] { expectedRowDim, expectedColDim });
}
 
Example 18
Source File: BlockFieldMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void setColumn(final int column, final T[] array)
    throws MatrixIndexException, InvalidMatrixException {

    checkColumnIndex(column);
    final int nRows = getRowDimension();
    if (array.length != nRows) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                array.length, 1, nRows, 1);
    }

    // perform copy block-wise, to ensure good cache behavior
    final int jBlock  = column / BLOCK_SIZE;
    final int jColumn = column - jBlock * BLOCK_SIZE;
    final int jWidth  = blockWidth(jBlock);
    int outIndex      = 0;
    for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
        final int iHeight = blockHeight(iBlock);
        final T[] block = blocks[iBlock * blockColumns + jBlock];
        for (int i = 0; i < iHeight; ++i) {
            block[i * jWidth + jColumn] = array[outIndex++];
        }
    }

}
 
Example 19
Source File: BlockFieldMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the entries in row number <code>row</code>
 * as a row matrix.  Row indices start at 0.
 *
 * @param row the row to be set
 * @param matrix row matrix (must have one row and the same number of columns
 * as the instance)
 * @throws MatrixIndexException if the specified row index is invalid
 * @throws InvalidMatrixException if the matrix dimensions do not match one
 * instance row
 */
public void setRowMatrix(final int row, final BlockFieldMatrix<T> matrix)
    throws MatrixIndexException, InvalidMatrixException {

    checkRowIndex(row);
    final int nCols = getColumnDimension();
    if ((matrix.getRowDimension() != 1) ||
        (matrix.getColumnDimension() != nCols)) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                matrix.getRowDimension(), matrix.getColumnDimension(),
                1, nCols);
    }

    // perform copy block-wise, to ensure good cache behavior
    final int iBlock = row / BLOCK_SIZE;
    final int iRow   = row - iBlock * BLOCK_SIZE;
    int mBlockIndex  = 0;
    int mIndex       = 0;
    T[] mBlock  = matrix.blocks[mBlockIndex];
    for (int jBlock = 0; jBlock < blockColumns; ++jBlock) {
        final int jWidth     = blockWidth(jBlock);
        final T[] block = blocks[iBlock * blockColumns + jBlock];
        final int available  = mBlock.length - mIndex;
        if (jWidth > available) {
            System.arraycopy(mBlock, mIndex, block, iRow * jWidth, available);
            mBlock = matrix.blocks[++mBlockIndex];
            System.arraycopy(mBlock, 0, block, iRow * jWidth, jWidth - available);
            mIndex = jWidth - available;
        } else {
            System.arraycopy(mBlock, mIndex, block, iRow * jWidth, jWidth);
            mIndex += jWidth;
       }
    }

}
 
Example 20
Source File: BlockFieldMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the entries in column number <code>column</code>
 * as a column matrix.  Column indices start at 0.
 *
 * @param column the column to be set
 * @param matrix column matrix (must have one column and the same number of rows
 * as the instance)
 * @throws MatrixIndexException if the specified column index is invalid
 * @throws InvalidMatrixException if the matrix dimensions do not match one
 * instance column
 */
void setColumnMatrix(final int column, final BlockFieldMatrix<T> matrix)
    throws MatrixIndexException, InvalidMatrixException {

    checkColumnIndex(column);
    final int nRows = getRowDimension();
    if ((matrix.getRowDimension() != nRows) ||
        (matrix.getColumnDimension() != 1)) {
        throw new InvalidMatrixException(
                LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
                matrix.getRowDimension(), matrix.getColumnDimension(),
                nRows, 1);
    }

    // perform copy block-wise, to ensure good cache behavior
    final int jBlock  = column / BLOCK_SIZE;
    final int jColumn = column - jBlock * BLOCK_SIZE;
    final int jWidth  = blockWidth(jBlock);
    int mBlockIndex = 0;
    int mIndex      = 0;
    T[] mBlock = matrix.blocks[mBlockIndex];
    for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
        final int iHeight = blockHeight(iBlock);
        final T[] block = blocks[iBlock * blockColumns + jBlock];
        for (int i = 0; i < iHeight; ++i) {
            if (mIndex >= mBlock.length) {
                mBlock = matrix.blocks[++mBlockIndex];
                mIndex = 0;
            }
            block[i * jWidth + jColumn] = mBlock[mIndex++];
        }
    }

}