/** * Construct a vector by appending a vector to this vector. * @param v vector to append to this one. * @return a new vector */ public ArrayFieldVector<T> append(ArrayFieldVector<T> v) { return new ArrayFieldVector<T>(this, v); }
/** {@inheritDoc} */ public FieldVector<T> add(FieldVector<T> v) throws DimensionMismatchException { try { return add((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].add(v.getEntry(i)); } return new ArrayFieldVector<T>(field, out, false); } }
/** {@inheritDoc} */ public FieldVector<T> append(FieldVector<T> v) { try { return append((ArrayFieldVector<T>) v); } catch (ClassCastException cce) { return new ArrayFieldVector<T>(this,new ArrayFieldVector<T>(v)); } }
/** Find the orthogonal projection of this vector onto another vector. * @param v vector onto which {@code this} must be projected * @return projection of {@code this} onto {@code v} * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} * @throws MathArithmeticException if {@code v} is the null vector. */ public ArrayFieldVector<T> projection(ArrayFieldVector<T> v) throws DimensionMismatchException, MathArithmeticException { return (ArrayFieldVector<T>) v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); }
/** {@inheritDoc} */ public FieldVector<T> getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n); } ArrayFieldVector<T> out = new ArrayFieldVector<T>(field, n); try { System.arraycopy(data, index, out.data, 0, n); } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + n - 1); } return out; }
/** * 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); }
/** * Visits (and possibly alters) some entries of this vector in default order * (increasing index). * * @param visitor visitor to be used to process the entries of this vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) * @return the value returned by {@link FieldVectorChangingVisitor#end()} * at the end of the walk * @throws NumberIsTooSmallException if {@code end < start}. * @throws OutOfRangeException if the indices are not valid. * @since 3.3 */ public T walkInDefaultOrder(final FieldVectorChangingVisitor<T> visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { checkIndices(start, end); visitor.start(getDimension(), start, end); for (int i = start; i <= end; i++) { setEntry(i, visitor.visit(i, getEntry(i))); } return visitor.end(); }
/** {@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> 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); } }
/** {@inheritDoc} */ public FieldVector<T> subtract(FieldVector<T> v) throws DimensionMismatchException { try { return subtract((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].subtract(v.getEntry(i)); } return new ArrayFieldVector<T>(field, out, false); } }
/** {@inheritDoc} */ public FieldVector<T> ebeDivide(FieldVector<T> v) throws DimensionMismatchException, MathArithmeticException { try { return ebeDivide((ArrayFieldVector<T>) v); } catch (ClassCastException cce) { checkVectorDimensions(v); T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { try { out[i] = data[i].divide(v.getEntry(i)); } catch (final MathArithmeticException e) { throw new MathArithmeticException(LocalizedFormats.INDEX, i); } } return new ArrayFieldVector<T>(field, out, false); } }
final int length = b.getDimension(); if (length != m) { throw new DimensionMismatchException(length, m); bp[row] = b.getEntry(pivot[row]); return new ArrayFieldVector<T>(bp, false);
/** * Visits (but does not alter) some entries of this vector in default order * (increasing index). * * @param visitor visitor to be used to process the entries of this vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) * @return the value returned by {@link FieldVectorPreservingVisitor#end()} * at the end of the walk * @throws NumberIsTooSmallException if {@code end < start}. * @throws OutOfRangeException if the indices are not valid. * @since 3.3 */ public T walkInDefaultOrder(final FieldVectorPreservingVisitor<T> visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { checkIndices(start, end); visitor.start(getDimension(), start, end); for (int i = start; i <= end; i++) { visitor.visit(i, getEntry(i)); } return visitor.end(); }
/** {@inheritDoc} */ public T dotProduct(FieldVector<T> v) throws DimensionMismatchException { try { return dotProduct((ArrayFieldVector<T>) v); } catch (ClassCastException cce) { checkVectorDimensions(v); T dot = field.getZero(); for (int i = 0; i < data.length; i++) { dot = dot.add(data[i].multiply(v.getEntry(i))); } return dot; } }
/** {@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} */ @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); } }
/** * 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()); }
/** {@inheritDoc} */ public void setEntry(int index, T value) { try { data[index] = value; } catch (IndexOutOfBoundsException e) { checkIndex(index); } }
/** {@inheritDoc} */ public FieldVector<T> projection(FieldVector<T> v) throws DimensionMismatchException, MathArithmeticException { return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); }
/** * 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); }