List of usage examples for org.apache.commons.math.linear MatrixUtils checkSubMatrixIndex
public static void checkSubMatrixIndex(final AnyMatrix m, final int startRow, final int endRow, final int startColumn, final int endColumn)
From source file:org.mitre.math.linear.BufferRealMatrix.java
/** * Gets a submatrix. Rows and columns are indicated * counting from 0 to n-1.// ww w . ja v a2s. co m * * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) * @return The subMatrix containing the data of the * specified rows and columns * @exception MatrixIndexException if the indices are not valid */ public double[][] getSubMatrixData(final int startRow, final int endRow, final int startColumn, final int endColumn) throws MatrixIndexException { // safety checks MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); // create the output matrix final double[][] out = new double[endRow - startRow + 1][endColumn - startColumn + 1]; // compute blocks shifts final int blockStartRow = startRow / BLOCK_SIZE; final int blockEndRow = endRow / BLOCK_SIZE; final int blockStartColumn = startColumn / BLOCK_SIZE; final int blockEndColumn = endColumn / BLOCK_SIZE; // perform extraction block-wise, to ensure good cache behavior for (int iBlock = blockStartRow; iBlock <= blockEndRow; ++iBlock) { for (int jBlock = blockStartColumn; jBlock <= blockEndColumn; ++jBlock) { // blocks are rows major order final int blockIndex = iBlock * this.blockColumns + jBlock; try { final long blockOffset = this.getBlockOffset(blockIndex); final DoubleBuffer block = this.dataFileChannel .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer(); block.clear(); final int iStart = Math.max(startRow, iBlock * BLOCK_SIZE); final int iEnd = Math.min(endRow, (iBlock * BLOCK_SIZE) + BLOCK_SIZE); final int jStart = Math.max(startColumn, jBlock * BLOCK_SIZE); final int jEnd = Math.min(endColumn, (jBlock * BLOCK_SIZE) + BLOCK_SIZE); LOG.info(String.format("blockIndex=%d (%d, %d), iStart=%d, iEnd=%d, jStart=%d, jEnd=%d", blockIndex, jBlock, iBlock, iStart, iEnd, jStart, jEnd)); for (int i = iStart; i < iEnd; ++i) { for (int j = jStart; j < jEnd; ++j) { // data in blocks are rows major, get our index in the block final int index = (i - iBlock * BLOCK_SIZE) * BLOCK_SIZE + (j - jBlock * BLOCK_SIZE); out[i - startRow][j - startColumn] = block.get(index); } } } catch (IOException ioe) { throw new MathRuntimeException( "IO Exception while visiting blockIndex {0} (iBlock={1}, jBlock={2})", blockIndex, iBlock, jBlock); } } } return out; }