/** {@inheritDoc} */ public T getEntry(int index) throws OutOfRangeException { checkIndex(index); return entries.get(index); }
/** {@inheritDoc} */ @Override public T getEntry(int row, int column) { checkRowIndex(row); checkColumnIndex(column); return entries.get(computeKey(row, column)); }
/** {@inheritDoc} */ @Override public void addToEntry(int row, int column, T increment) { checkRowIndex(row); checkColumnIndex(column); final int key = computeKey(row, column); final T value = entries.get(key).add(increment); if (getField().getZero().equals(value)) { entries.remove(key); } else { entries.put(key, value); } }
/** {@inheritDoc} */ @Override public void multiplyEntry(int row, int column, T factor) { checkRowIndex(row); checkColumnIndex(column); final int key = computeKey(row, column); final T value = entries.get(key).multiply(factor); if (getField().getZero().equals(value)) { entries.remove(key); } else { entries.put(key, value); } }
/** {@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; } }
/** * Optimized method to add sparse vectors. * * @param v Vector to add. * @return {@code this + v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this}. */ public FieldVector<T> add(SparseFieldVector<T> v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); SparseFieldVector<T> res = (SparseFieldVector<T>)copy(); OpenIntToFieldHashMap<T>.Iterator iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); T value = iter.value(); if (entries.containsKey(key)) { res.setEntry(key, entries.get(key).add(value)); } else { res.setEntry(key, value); } } return res; }
/** * Optimized method to 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 SparseFieldVector<T> subtract(SparseFieldVector<T> v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); SparseFieldVector<T> res = (SparseFieldVector<T>)copy(); OpenIntToFieldHashMap<T>.Iterator iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (entries.containsKey(key)) { res.setEntry(key, entries.get(key).subtract(iter.value())); } else { res.setEntry(key, field.getZero().subtract(iter.value())); } } return res; }
/** {@inheritDoc} */ public T getEntry(int index) throws OutOfRangeException { checkIndex(index); return entries.get(index); }
/** {@inheritDoc} */ @Override public T getEntry(int row, int column) { checkRowIndex(row); checkColumnIndex(column); return entries.get(computeKey(row, column)); }
/** {@inheritDoc} */ @Override public void addToEntry(int row, int column, T increment) { checkRowIndex(row); checkColumnIndex(column); final int key = computeKey(row, column); final T value = entries.get(key).add(increment); if (getField().getZero().equals(value)) { entries.remove(key); } else { entries.put(key, value); } }
/** {@inheritDoc} */ @Override public void multiplyEntry(int row, int column, T factor) { checkRowIndex(row); checkColumnIndex(column); final int key = computeKey(row, column); final T value = entries.get(key).multiply(factor); if (getField().getZero().equals(value)) { entries.remove(key); } else { entries.put(key, value); } }
/** {@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; } }
/** * Optimized method to add sparse vectors. * * @param v Vector to add. * @return {@code this + v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this}. */ public FieldVector<T> add(SparseFieldVector<T> v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); SparseFieldVector<T> res = (SparseFieldVector<T>)copy(); OpenIntToFieldHashMap<T>.Iterator iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); T value = iter.value(); if (entries.containsKey(key)) { res.setEntry(key, entries.get(key).add(value)); } else { res.setEntry(key, value); } } return res; }
/** * Optimized method to 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 SparseFieldVector<T> subtract(SparseFieldVector<T> v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); SparseFieldVector<T> res = (SparseFieldVector<T>)copy(); OpenIntToFieldHashMap<T>.Iterator iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (entries.containsKey(key)) { res.setEntry(key, entries.get(key).subtract(iter.value())); } else { res.setEntry(key, field.getZero().subtract(iter.value())); } } return res; }