/** * Check if instance and specified vectors have the same dimension. * @param v vector to compare instance with * @exception DimensionMismatchException if the vectors do not * have the same dimensions */ protected void checkVectorDimensions(FieldVector<T> v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); }
@SuppressWarnings("unchecked") // May fail, but we ignore ClassCastException FieldVector<T> rhs = (FieldVector<T>) other; if (data.length != rhs.getDimension()) { return false;
/** {@inheritDoc} */ public void setSubVector(int index, FieldVector<T> v) throws OutOfRangeException { checkIndex(index); checkIndex(index + v.getDimension() - 1); final int n = v.getDimension(); for (int i = 0; i < n; i++) { setEntry(i + index, v.getEntry(i)); } }
/** {@inheritDoc} */ public void setSubVector(int index, FieldVector<T> v) throws OutOfRangeException { try { try { set(index, (ArrayFieldVector<T>) v); } catch (ClassCastException cce) { for (int i = index; i < index + v.getDimension(); ++i) { data[i] = v.getEntry(i-index); } } } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + v.getDimension() - 1); } }
/** {@inheritDoc} */ public void setColumnVector(final int column, final FieldVector<T> vector) throws OutOfRangeException, MatrixDimensionMismatchException { checkColumnIndex(column); final int nRows = getRowDimension(); if (vector.getDimension() != nRows) { throw new MatrixDimensionMismatchException(vector.getDimension(), 1, nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, vector.getEntry(i)); } }
/** {@inheritDoc} */ public void setRowVector(final int row, final FieldVector<T> vector) throws OutOfRangeException, MatrixDimensionMismatchException { checkRowIndex(row); final int nCols = getColumnDimension(); if (vector.getDimension() != nCols) { throw new MatrixDimensionMismatchException(1, vector.getDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, vector.getEntry(i)); } }
/** {@inheritDoc} */ public FieldVector<T> append(FieldVector<T> v) { if (v instanceof SparseFieldVector<?>) { return append((SparseFieldVector<T>) v); } else { final int n = v.getDimension(); FieldVector<T> res = new SparseFieldVector<T>(this, n); for (int i = 0; i < n; i++) { res.setEntry(i + virtualSize, v.getEntry(i)); } return res; } }
/** {@inheritDoc} */ public FieldVector<T> projection(FieldVector<T> v) throws DimensionMismatchException, MathArithmeticException { checkVectorDimensions(v.getDimension()); return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); }
/** * 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); } }
/** {@inheritDoc} */ public FieldMatrix<T> outerProduct(FieldVector<T> v) { try { return outerProduct((ArrayFieldVector<T>) v); } catch (ClassCastException cce) { final int m = data.length; final int n = v.getDimension(); final FieldMatrix<T> out = new Array2DRowFieldMatrix<T>(field, m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { out.setEntry(i, j, data[i].multiply(v.getEntry(j))); } } return out; } }
/** {@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); } }
/** {@inheritDoc} */ public FieldVector<T> preMultiply(final FieldVector<T> v) throws DimensionMismatchException { try { return new ArrayFieldVector<T>(field, preMultiply(((ArrayFieldVector<T>) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nRows) { throw new DimensionMismatchException(v.getDimension(), nRows); } final T[] out = MathArrays.buildArray(field, nCols); for (int col = 0; col < nCols; ++col) { T sum = field.getZero(); for (int i = 0; i < nRows; ++i) { sum = sum.add(getEntry(i, col).multiply(v.getEntry(i))); } out[col] = sum; } return new ArrayFieldVector<T>(field, out, false); } }
if (b.getDimension() != m) { throw new DimensionMismatchException(b.getDimension(), m);
/** {@inheritDoc} */ public FieldVector<T> add(FieldVector<T> v) throws DimensionMismatchException { if (v instanceof SparseFieldVector<?>) { return add((SparseFieldVector<T>) v); } else { final int n = v.getDimension(); checkVectorDimensions(n); SparseFieldVector<T> res = new SparseFieldVector<T>(field, getDimension()); for (int i = 0; i < n; i++) { res.setEntry(i, v.getEntry(i).add(getEntry(i))); } return res; } }
/** {@inheritDoc} */ public T dotProduct(FieldVector<T> v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); T res = field.getZero(); OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res = res.add(v.getEntry(iter.key()).multiply(iter.value())); } return res; }
/** {@inheritDoc} */ public FieldVector<T> ebeMultiply(FieldVector<T> v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); SparseFieldVector<T> res = new SparseFieldVector<T>(this); OpenIntToFieldHashMap<T>.Iterator iter = res.entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value().multiply(v.getEntry(iter.key()))); } return res; }
/** {@inheritDoc} */ public FieldVector<T> ebeDivide(FieldVector<T> v) throws DimensionMismatchException, MathArithmeticException { checkVectorDimensions(v.getDimension()); SparseFieldVector<T> res = new SparseFieldVector<T>(this); OpenIntToFieldHashMap<T>.Iterator iter = res.entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value().divide(v.getEntry(iter.key()))); } return res; }
/** {@inheritDoc} */ public FieldMatrix<T> outerProduct(FieldVector<T> v) { if (v instanceof SparseFieldVector<?>) { return outerProduct((SparseFieldVector<T>)v); } else { final int n = v.getDimension(); FieldMatrix<T> res = new SparseFieldMatrix<T>(field, virtualSize, n); OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int row = iter.key(); FieldElement<T>value = iter.value(); for (int col = 0; col < n; col++) { res.setEntry(row, col, value.multiply(v.getEntry(col))); } } return res; } }
/** {@inheritDoc} */ public FieldVector<T> subtract(FieldVector<T> v) throws DimensionMismatchException { if (v instanceof SparseFieldVector<?>) { return subtract((SparseFieldVector<T>)v); } else { final int n = v.getDimension(); checkVectorDimensions(n); SparseFieldVector<T> res = new SparseFieldVector<T>(this); for (int i = 0; i < n; i++) { if (entries.containsKey(i)) { res.setEntry(i, entries.get(i).subtract(v.getEntry(i))); } else { res.setEntry(i, field.getZero().subtract(v.getEntry(i))); } } return res; } }
/** * Check if instance and specified vectors have the same dimension. * @param v vector to compare instance with * @exception DimensionMismatchException if the vectors do not * have the same dimensions */ protected void checkVectorDimensions(FieldVector<T> v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); }