Example usage for org.apache.commons.math.linear RealMatrixChangingVisitor visit

List of usage examples for org.apache.commons.math.linear RealMatrixChangingVisitor visit

Introduction

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

Prototype

double visit(int row, int column, double value) throws MatrixVisitorException;

Source Link

Document

Visit one matrix entry.

Usage

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

/** {@inheritDoc} */
@Override//from   www.  jav  a 2 s. c o  m
public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor) throws MatrixVisitorException {
    visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
    for (int iBlock = 0, blockIndex = 0; iBlock < blockRows; ++iBlock) {
        final int pStart = iBlock * BLOCK_SIZE;
        final int pEnd = Math.min(pStart + BLOCK_SIZE, rows);

        for (int jBlock = 0; jBlock < blockColumns; ++jBlock, ++blockIndex) {
            try {
                final int qStart = jBlock * BLOCK_SIZE;
                final int qEnd = Math.min(qStart + BLOCK_SIZE, columns);

                final long blockOffset = this.getBlockOffset(blockIndex);
                LOG.debug(String.format("BlockIndex=%d (offset=%d) pStart=%d pEnd=%d qStart=%d qEnd=%d",
                        blockIndex, blockOffset, pStart, pEnd, qStart, qEnd));

                final DoubleBuffer block = this.dataFileChannel
                        .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
                block.clear();

                for (int p = pStart, k = 0; p < pEnd; ++p) {
                    // jump to end of row incase we are not there
                    k = (p - pStart) * BLOCK_SIZE;
                    for (int q = qStart; q < qEnd; ++q, ++k) {
                        block.put(k, visitor.visit(p, q, block.get(k)));
                    }
                }
                this.dataFileChannel.force(false);
            } catch (IOException ioe) {
                throw new MathRuntimeException(
                        "IO Exception while visiting blockIndex {0} (iBlock={1}, jBlock={2})", blockIndex,
                        iBlock, jBlock);
            }
        }
    }
    return visitor.end();
}