Java Code Examples for org.apache.commons.math3.util.MathArrays#buildArray()

The following examples show how to use org.apache.commons.math3.util.MathArrays#buildArray() . 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: BlockFieldMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public FieldVector<T> getColumnVector(final int column)
    throws OutOfRangeException {
    checkColumnIndex(column);
    final T[] outData = MathArrays.buildArray(getField(), rows);

    // 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) {
            outData[outIndex++] = block[i * jWidth + jColumn];
        }
    }

    return new ArrayFieldVector<T>(getField(), outData, false);
}
 
Example 2
Source File: BlockFieldMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public T[] getColumn(final int column) throws OutOfRangeException {
    checkColumnIndex(column);
    final T[] out = MathArrays.buildArray(getField(), rows);

    // 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) {
            out[outIndex++] = block[i * jWidth + jColumn];
        }
    }

    return out;
}
 
Example 3
Source File: AbstractFieldMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public T[] operate(final T[] v) throws DimensionMismatchException {

    final int nRows = getRowDimension();
    final int nCols = getColumnDimension();
    if (v.length != nCols) {
        throw new DimensionMismatchException(v.length, nCols);
    }

    final T[] out = MathArrays.buildArray(field, nRows);
    for (int row = 0; row < nRows; ++row) {
        T sum = field.getZero();
        for (int i = 0; i < nCols; ++i) {
            sum = sum.add(getEntry(row, i).multiply(v[i]));
        }
        out[row] = sum;
    }

    return out;
}
 
Example 4
Source File: AbstractFieldMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public FieldVector<T> operate(final FieldVector<T> v)
    throws DimensionMismatchException {
    try {
        return new ArrayFieldVector<T>(field, operate(((ArrayFieldVector<T>) v).getDataRef()), false);
    } catch (ClassCastException cce) {
        final int nRows = getRowDimension();
        final int nCols = getColumnDimension();
        if (v.getDimension() != nCols) {
            throw new DimensionMismatchException(v.getDimension(), nCols);
        }

        final T[] out = MathArrays.buildArray(field, nRows);
        for (int row = 0; row < nRows; ++row) {
            T sum = field.getZero();
            for (int i = 0; i < nCols; ++i) {
                sum = sum.add(getEntry(row, i).multiply(v.getEntry(i)));
            }
            out[row] = sum;
        }

        return new ArrayFieldVector<T>(field, out, false);
    }
}
 
Example 5
Source File: BlockFieldMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public FieldVector<T> getColumnVector(final int column)
    throws OutOfRangeException {
    checkColumnIndex(column);
    final T[] outData = MathArrays.buildArray(getField(), rows);

    // 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) {
            outData[outIndex++] = block[i * jWidth + jColumn];
        }
    }

    return new ArrayFieldVector<T>(getField(), outData, false);
}
 
Example 6
Source File: BlockFieldMatrix.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public FieldVector<T> getColumnVector(final int column)
    throws OutOfRangeException {
    checkColumnIndex(column);
    final T[] outData = MathArrays.buildArray(getField(), rows);

    // 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) {
            outData[outIndex++] = block[i * jWidth + jColumn];
        }
    }

    return new ArrayFieldVector<T>(getField(), outData, false);
}
 
Example 7
Source File: ArrayFieldVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a vector by appending one vector to another vector.
 *
 * @param field Field to which the elements belong.
 * @param v1 First vector (will be put in front of the new vector).
 * @param v2 Second vector (will be put at back of the new vector).
 * @throws NullArgumentException if {@code v1} or {@code v2} is
 * {@code null}.
 * @throws ZeroException if both arrays are empty.
 * @see #ArrayFieldVector(FieldElement[], FieldElement[])
 */
public ArrayFieldVector(Field<T> field, T[] v1, T[] v2)
        throws NullArgumentException, ZeroException {
    MathUtils.checkNotNull(v1);
    MathUtils.checkNotNull(v2);
    if (v1.length + v2.length == 0) {
        throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
    }
    data = MathArrays.buildArray(field, v1.length + v2.length);
    System.arraycopy(v1, 0, data, 0, v1.length);
    System.arraycopy(v2, 0, data, v1.length, v2.length);
    this.field = field;
}
 
Example 8
Source File: ArrayFieldVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public FieldVector<T> ebeMultiply(FieldVector<T> v)
    throws DimensionMismatchException {
    try {
        return ebeMultiply((ArrayFieldVector<T>) v);
    } catch (ClassCastException cce) {
        checkVectorDimensions(v);
        T[] out = MathArrays.buildArray(field, data.length);
        for (int i = 0; i < data.length; i++) {
            out[i] = data[i].multiply(v.getEntry(i));
        }
        return new ArrayFieldVector<T>(field, out, false);
    }
}
 
Example 9
Source File: ArrayFieldVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a vector from another vector, using a deep copy.
 *
 * @param v Vector to copy.
 * @throws NullArgumentException if {@code v} is {@code null}.
 */
public ArrayFieldVector(FieldVector<T> v)
        throws NullArgumentException {
    MathUtils.checkNotNull(v);
    field = v.getField();
    data = MathArrays.buildArray(field, v.getDimension());
    for (int i = 0; i < data.length; ++i) {
        data[i] = v.getEntry(i);
    }
}
 
Example 10
Source File: AbstractFieldMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public T[] getRow(final int row) throws OutOfRangeException {
    checkRowIndex(row);
    final int nCols = getColumnDimension();
    final T[] out = MathArrays.buildArray(field, nCols);
    for (int i = 0; i < nCols; ++i) {
        out[i] = getEntry(row, i);
    }

    return out;

}
 
Example 11
Source File: BlockFieldMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new dense matrix copying entries from block layout data.
 * <p>The input array <em>must</em> already be in blocks layout.</p>
 * @param rows  the number of rows in the new matrix
 * @param columns  the number of columns in the new matrix
 * @param blockData data for new matrix
 * @param copyArray if true, the input array will be copied, otherwise
 * it will be referenced
 *
 * @throws DimensionMismatchException if the {@code blockData} shape is
 * inconsistent with block layout.
 * @throws NotStrictlyPositiveException if row or column dimension is not
 * positive.
 * @see #createBlocksLayout(Field, int, int)
 * @see #toBlocksLayout(FieldElement[][])
 * @see #BlockFieldMatrix(FieldElement[][])
 */
public BlockFieldMatrix(final int rows, final int columns,
                        final T[][] blockData, final boolean copyArray)
    throws DimensionMismatchException, NotStrictlyPositiveException {
    super(extractField(blockData), rows, columns);
    this.rows    = rows;
    this.columns = columns;

    // number of blocks
    blockRows    = (rows    + BLOCK_SIZE - 1) / BLOCK_SIZE;
    blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE;

    if (copyArray) {
        // allocate storage blocks, taking care of smaller ones at right and bottom
        blocks = MathArrays.buildArray(getField(), blockRows * blockColumns, -1);
    } else {
        // reference existing array
        blocks = blockData;
    }

    int index = 0;
    for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
        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));
            }
            if (copyArray) {
                blocks[index] = blockData[index].clone();
            }
        }
    }
}
 
Example 12
Source File: ArrayFieldVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a vector from another vector, using a deep copy.
 *
 * @param v Vector to copy.
 * @throws NullArgumentException if {@code v} is {@code null}.
 */
public ArrayFieldVector(FieldVector<T> v)
        throws NullArgumentException {
    MathUtils.checkNotNull(v);
    field = v.getField();
    data = MathArrays.buildArray(field, v.getDimension());
    for (int i = 0; i < data.length; ++i) {
        data[i] = v.getEntry(i);
    }
}
 
Example 13
Source File: BlockFieldMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new dense matrix copying entries from block layout data.
 * <p>The input array <em>must</em> already be in blocks layout.</p>
 * @param rows  the number of rows in the new matrix
 * @param columns  the number of columns in the new matrix
 * @param blockData data for new matrix
 * @param copyArray if true, the input array will be copied, otherwise
 * it will be referenced
 *
 * @throws DimensionMismatchException if the {@code blockData} shape is
 * inconsistent with block layout.
 * @throws NotStrictlyPositiveException if row or column dimension is not
 * positive.
 * @see #createBlocksLayout(Field, int, int)
 * @see #toBlocksLayout(FieldElement[][])
 * @see #BlockFieldMatrix(FieldElement[][])
 */
public BlockFieldMatrix(final int rows, final int columns,
                        final T[][] blockData, final boolean copyArray)
    throws DimensionMismatchException, NotStrictlyPositiveException {
    super(extractField(blockData), rows, columns);
    this.rows    = rows;
    this.columns = columns;

    // number of blocks
    blockRows    = (rows    + BLOCK_SIZE - 1) / BLOCK_SIZE;
    blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE;

    if (copyArray) {
        // allocate storage blocks, taking care of smaller ones at right and bottom
        blocks = MathArrays.buildArray(getField(), blockRows * blockColumns, -1);
    } else {
        // reference existing array
        blocks = blockData;
    }

    int index = 0;
    for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
        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));
            }
            if (copyArray) {
                blocks[index] = blockData[index].clone();
            }
        }
    }
}
 
Example 14
Source File: ArrayFieldVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compute {@code this} minus {@code v}.
 * @param v vector to be subtracted
 * @return {@code this - v}
 * @throws DimensionMismatchException if {@code v} is not the same size as
 * {@code this}
 */
public ArrayFieldVector<T> subtract(ArrayFieldVector<T> v)
    throws DimensionMismatchException {
    checkVectorDimensions(v.data.length);
    T[] out = MathArrays.buildArray(field, data.length);
    for (int i = 0; i < data.length; i++) {
        out[i] = data[i].subtract(v.data[i]);
    }
    return new ArrayFieldVector<T>(field, out, false);
}
 
Example 15
Source File: BlockFieldMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new dense matrix copying entries from block layout data.
 * <p>The input array <em>must</em> already be in blocks layout.</p>
 * @param rows  the number of rows in the new matrix
 * @param columns  the number of columns in the new matrix
 * @param blockData data for new matrix
 * @param copyArray if true, the input array will be copied, otherwise
 * it will be referenced
 *
 * @throws DimensionMismatchException if the {@code blockData} shape is
 * inconsistent with block layout.
 * @throws NotStrictlyPositiveException if row or column dimension is not
 * positive.
 * @see #createBlocksLayout(Field, int, int)
 * @see #toBlocksLayout(FieldElement[][])
 * @see #BlockFieldMatrix(FieldElement[][])
 */
public BlockFieldMatrix(final int rows, final int columns,
                        final T[][] blockData, final boolean copyArray)
    throws DimensionMismatchException, NotStrictlyPositiveException {
    super(extractField(blockData), rows, columns);
    this.rows    = rows;
    this.columns = columns;

    // number of blocks
    blockRows    = (rows    + BLOCK_SIZE - 1) / BLOCK_SIZE;
    blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE;

    if (copyArray) {
        // allocate storage blocks, taking care of smaller ones at right and bottom
        blocks = MathArrays.buildArray(getField(), blockRows * blockColumns, -1);
    } else {
        // reference existing array
        blocks = blockData;
    }

    int index = 0;
    for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
        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));
            }
            if (copyArray) {
                blocks[index] = blockData[index].clone();
            }
        }
    }
}
 
Example 16
Source File: ArrayFieldVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compute the sum of {@code this} and {@code v}.
 * @param v vector to be added
 * @return {@code this + v}
 * @throws DimensionMismatchException if {@code v} is not the same size as
 * {@code this}
 */
public ArrayFieldVector<T> add(ArrayFieldVector<T> v)
    throws DimensionMismatchException {
    checkVectorDimensions(v.data.length);
    T[] out = MathArrays.buildArray(field, data.length);
    for (int i = 0; i < data.length; i++) {
        out[i] = data[i].add(v.data[i]);
    }
    return new ArrayFieldVector<T>(field, out, false);
}
 
Example 17
Source File: Array2DRowFieldMatrix.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new {@code FieldMatrix<T>} with the supplied row and column dimensions.
 *
 * @param field Field to which the elements belong.
 * @param rowDimension Number of rows in the new matrix.
 * @param columnDimension Number of columns in the new matrix.
 * @throws NotStrictlyPositiveException if row or column dimension is not positive.
 */
public Array2DRowFieldMatrix(final Field<T> field, final int rowDimension,
                             final int columnDimension)
    throws NotStrictlyPositiveException {
    super(field, rowDimension, columnDimension);
    data = MathArrays.buildArray(field, rowDimension, columnDimension);
}
 
Example 18
Source File: AbstractFieldMatrix.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/** Build an array of elements.
 * <p>
 * Arrays are filled with field.getZero()
 * </p>
 * @param <T> the type of the field elements
 * @param field field to which array elements belong
 * @param length of the array
 * @return a new array
 * @deprecated as of 3.2, replaced by {@link MathArrays#buildArray(Field, int)}
 */
@Deprecated
protected static <T extends FieldElement<T>> T[] buildArray(final Field<T> field,
                                                            final int length) {
    return MathArrays.buildArray(field, length);
}
 
Example 19
Source File: AbstractFieldMatrix.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/** Build an array of elements.
 * <p>
 * Arrays are filled with field.getZero()
 * </p>
 * @param <T> the type of the field elements
 * @param field field to which array elements belong
 * @param length of the array
 * @return a new array
 * @deprecated as of 3.2, replaced by {@link MathArrays#buildArray(Field, int)}
 */
@Deprecated
protected static <T extends FieldElement<T>> T[] buildArray(final Field<T> field,
                                                            final int length) {
    return MathArrays.buildArray(field, length);
}
 
Example 20
Source File: ArrayFieldVector.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Construct a vector of zeroes.
 *
 * @param field Field to which the elements belong.
 * @param size Size of the vector.
 */
public ArrayFieldVector(Field<T> field, int size) {
    this.field = field;
    this.data  = MathArrays.buildArray(field, size);
}