/** {@inheritDoc} */ @Override public FieldMatrix<T> getColumnMatrix(final int column) throws OutOfRangeException { checkColumnIndex(column); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, 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 outBlockIndex = 0; int outIndex = 0; T[] outBlock = out.blocks[outBlockIndex]; 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 (outIndex >= outBlock.length) { outBlock = out.blocks[++outBlockIndex]; outIndex = 0; } outBlock[outIndex++] = block[i * jWidth + jColumn]; } } return out; }
throws DimensionMismatchException { try { return multiply((BlockFieldMatrix<T>) m); } catch (ClassCastException cce) { checkMultiplicationCompatible(m); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, m.getColumnDimension()); final T zero = getField().getZero(); final int kWidth = blockWidth(kBlock); final T[] tBlock = blocks[iBlock * blockColumns + kBlock]; final int rStart = kBlock * BLOCK_SIZE;
/** {@inheritDoc} */ @Override public void setRowVector(final int row, final FieldVector<T> vector) throws MatrixDimensionMismatchException, OutOfRangeException { try { setRow(row, ((ArrayFieldVector<T>) vector).getDataRef()); } catch (ClassCastException cce) { super.setRowVector(row, vector); } }
/** {@inheritDoc} */ @Override public void setEntry(final int row, final int column, final T value) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); blocks[iBlock * blockColumns + jBlock][k] = value; }
/** {@inheritDoc} */ @Override public FieldMatrix<T> createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { return new BlockFieldMatrix<T>(getField(), rowDimension, columnDimension); }
/** {@inheritDoc} */ @Override public void setColumn(final int column, final T[] array) throws MatrixDimensionMismatchException, OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); if (array.length != nRows) { throw new MatrixDimensionMismatchException(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++]; } } }
checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); new BlockFieldMatrix<T>(getField(), endRow - startRow + 1, endColumn - startColumn + 1); final int iHeight = out.blockHeight(iBlock); int qBlock = blockStartColumn; for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { final int jWidth = out.blockWidth(jBlock); final T[] outBlock = out.blocks[outIndex]; final int index = pBlock * blockColumns + qBlock; final int width = blockWidth(qBlock); final int width2 = blockWidth(qBlock + 1); copyBlockPart(blocks[index], width, rowsShift, BLOCK_SIZE, columnsShift, BLOCK_SIZE, outBlock, jWidth, 0, 0); copyBlockPart(blocks[index + 1], width2, rowsShift, BLOCK_SIZE, 0, widthExcess, outBlock, jWidth, 0, jWidth - widthExcess); copyBlockPart(blocks[index + blockColumns], width, 0, heightExcess, columnsShift, BLOCK_SIZE, outBlock, jWidth, iHeight - heightExcess, 0); copyBlockPart(blocks[index + blockColumns + 1], width2, 0, heightExcess, 0, widthExcess,
/** {@inheritDoc} */ @Override public FieldMatrix<T> getRowMatrix(final int row) throws OutOfRangeException { checkRowIndex(row); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), 1, columns); // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int outBlockIndex = 0; int outIndex = 0; T[] outBlock = out.blocks[outBlockIndex]; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; final int available = outBlock.length - outIndex; if (jWidth > available) { System.arraycopy(block, iRow * jWidth, outBlock, outIndex, available); outBlock = out.blocks[++outBlockIndex]; System.arraycopy(block, iRow * jWidth, outBlock, 0, jWidth - available); outIndex = jWidth - available; } else { System.arraycopy(block, iRow * jWidth, outBlock, outIndex, jWidth); outIndex += jWidth; } } return out; }
throws MatrixDimensionMismatchException { try { return subtract((BlockFieldMatrix<T>) m); } catch (ClassCastException cce) { checkSubtractionCompatible(m); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns);
throws MatrixDimensionMismatchException { try { return add((BlockFieldMatrix<T>) m); } catch (ClassCastException cce) { checkAdditionCompatible(m); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns);
final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), nCols, nRows);
matrix1 = Convert.list2Matrix(list); FieldElement<ExprFieldElement>[] av = vector0.getData(); BlockFieldMatrix<ExprFieldElement> m = new BlockFieldMatrix<ExprFieldElement>(ExprField.CONST,1,av.length); m.setRow(0, vector0.getData()); return Convert.matrix2List(m.multiply( matrix1)); } else if (o1.isVector() != (-1)) {
/** {@inheritDoc} */ @Override public T[] getRow(final int row) throws OutOfRangeException { checkRowIndex(row); final T[] out = MathArrays.buildArray(getField(), columns); // 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 T[] block = blocks[iBlock * blockColumns + jBlock]; System.arraycopy(block, iRow * jWidth, out, outIndex, jWidth); outIndex += jWidth; } return out; }
/** {@inheritDoc} */ @Override public T walkInOptimizedOrder(final FieldMatrixPreservingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int p = pStart; p < pEnd; ++p) { int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); }
checkRowIndex(row); final int nCols = getColumnDimension(); if ((matrix.getRowDimension() != 1) || (matrix.getColumnDimension() != nCols)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols); 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;
final T[][] blockData, final boolean copyArray) throws DimensionMismatchException, NotStrictlyPositiveException { super(extractField(blockData), rows, columns); this.rows = rows; this.columns = columns; blocks = MathArrays.buildArray(getField(), blockRows * blockColumns, -1); } else { final int iHeight = blockHeight(iBlock); for (int jBlock = 0; jBlock < blockColumns; ++jBlock, ++index) { if (blockData[index].length != iHeight * blockWidth(jBlock)) { throw new DimensionMismatchException(blockData[index].length, iHeight * blockWidth(jBlock));
/** * Compute {@code this - m}. * * @param m matrix to be subtracted * @return {@code this - m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this} */ public BlockFieldMatrix<T> subtract(final BlockFieldMatrix<T> m) throws MatrixDimensionMismatchException { // safety check checkSubtractionCompatible(m); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; final T[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k].subtract(mBlock[k]); } } return out; }
/** * Compute the sum of {@code this} and {@code m}. * * @param m matrix to be added * @return {@code this + m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this} */ public BlockFieldMatrix<T> add(final BlockFieldMatrix<T> m) throws MatrixDimensionMismatchException { // safety check checkAdditionCompatible(m); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns); // perform addition block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; final T[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k].add(mBlock[k]); } } return out; }
/** {@inheritDoc} */ @Override public void setRow(final int row, final T[] array) throws OutOfRangeException, MatrixDimensionMismatchException { checkRowIndex(row); final int nCols = getColumnDimension(); if (array.length != nCols) { throw new MatrixDimensionMismatchException(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 T[] block = blocks[iBlock * blockColumns + jBlock]; System.arraycopy(array, outIndex, block, iRow * jWidth, jWidth); outIndex += jWidth; } }
checkSubMatrixIndex(row, endRow, column, endColumn); for (final T[] subRow : subMatrix) { if (subRow.length != refLength) { final int iHeight = blockHeight(iBlock); final int firstRow = iBlock * BLOCK_SIZE; final int iStart = FastMath.max(row, firstRow); final int jWidth = blockWidth(jBlock); final int firstColumn = jBlock * BLOCK_SIZE; final int jStart = FastMath.max(column, firstColumn);