Example usage for org.apache.commons.math.linear MatrixUtils checkAdditionCompatible

List of usage examples for org.apache.commons.math.linear MatrixUtils checkAdditionCompatible

Introduction

In this page you can find the example usage for org.apache.commons.math.linear MatrixUtils checkAdditionCompatible.

Prototype

public static void checkAdditionCompatible(final AnyMatrix left, final AnyMatrix right)
        throws IllegalArgumentException 

Source Link

Document

Check if matrices are addition compatible

Usage

From source file:org.mitre.math.linear.BufferRealMatrix.java

/**
 * Compute the sum of this and <code>m</code>.
 *
 * @param m    matrix to be added/*from  w w  w .jav  a2 s .com*/
 * @return     this + m
 * @throws  IllegalArgumentException if m is not the same size as this
 */
public BufferRealMatrix add(final BufferRealMatrix b) throws IllegalArgumentException {

    // safety checks
    if (b == this) {
        return this.addSelf();
    }
    MatrixUtils.checkAdditionCompatible(this, b);

    try {
        final BufferRealMatrix c = new BufferRealMatrix(rows, columns, null);

        // perform addition block-wise, to ensure good cache behavior
        for (int blockIndex = 0; blockIndex < this.blockRows * this.blockColumns; ++blockIndex) {
            // all the same size, so should all be the same blockOffsets and layout
            final long blockOffset = this.getBlockOffset(blockIndex);
            DoubleBuffer adb = this.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            adb.clear();
            DoubleBuffer bdb = b.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            bdb.clear();
            DoubleBuffer cdb = c.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            cdb.clear();
            for (int k = 0; k < BLOCK_BYTE_SIZE / DOUBLE_BYTE_SIZE; k++) {
                try {
                    cdb.put(adb.get() + bdb.get());
                } catch (BufferUnderflowException e) {
                    LOG.fatal(String.format("BufferUnderflowException while adding elements at %d in block %d",
                            k, blockIndex));
                    throw e;
                }
            }
        }
        return c;
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}