/** Solve the linear equation A × X = B. * <p>The A matrix is implicit here. It is </p> * @param b right-hand side of the equation A × X = B * @return a vector X that minimizes the two norm of A × X - B * @throws IllegalArgumentException if matrices dimensions don't match * @throws InvalidMatrixException if decomposed matrix is singular */ public ArrayRealVector solve(ArrayRealVector b) throws IllegalArgumentException, InvalidMatrixException { return new ArrayRealVector(solve(b.getDataRef()), false); }
/** * Compute the sum of this and v. * @param v vector to be added * @return this + v * @throws IllegalArgumentException if v is not the same size as this */ public ArrayRealVector add(ArrayRealVector v) throws IllegalArgumentException { return (ArrayRealVector) add(v.data); }
/** {@inheritDoc} */ public RealVector getSubVector(int index, int n) { ArrayRealVector out = new ArrayRealVector(n); try { System.arraycopy(data, index, out.data, 0, n); } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + n - 1); } return out; }
/** Find the orthogonal projection of this vector onto another vector. * @param v vector onto which instance must be projected * @return projection of the instance onto v * @throws IllegalArgumentException if v is not the same size as this */ public ArrayRealVector projection(ArrayRealVector v) { return (ArrayRealVector) v.mapMultiply(dotProduct(v) / v.dotProduct(v)); }
/** {@inheritDoc} */ @Override public RealVector projection(double[] v) { return projection(new ArrayRealVector(v, false)); }
/** {@inheritDoc} */ @Override public RealVector ebeMultiply(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); double[] out = data.clone(); for (int i = 0; i < data.length; i++) { out[i] *= v[i]; } return new ArrayRealVector(out, false); }
/** * Construct a vector by appending a vector to this vector. * @param v vector to append to this one. * @return a new vector */ public ArrayRealVector append(ArrayRealVector v) { return new ArrayRealVector(this, v); }
/** {@inheritDoc} */ public RealVector ebeMultiply(RealVector v) throws IllegalArgumentException { if (v instanceof ArrayRealVector) { return ebeMultiply((ArrayRealVector) v); } else { checkVectorDimensions(v); double[] out = data.clone(); for (int i = 0; i < data.length; i++) { out[i] *= v.getEntry(i); } return new ArrayRealVector(out, false); } }
/** {@inheritDoc} */ public RealVector ebeDivide(RealVector v) throws IllegalArgumentException { if (v instanceof ArrayRealVector) { return ebeDivide((ArrayRealVector) v); } else { checkVectorDimensions(v); double[] out = data.clone(); for (int i = 0; i < data.length; i++) { out[i] /= v.getEntry(i); } return new ArrayRealVector(out, false); } }
/** {@inheritDoc} */ @Override public RealVector add(RealVector v) throws IllegalArgumentException { if (v instanceof ArrayRealVector) { return add((ArrayRealVector) v); } else { checkVectorDimensions(v); double[] out = data.clone(); Iterator<Entry> it = v.sparseIterator(); Entry e; while (it.hasNext() && (e = it.next()) != null) { out[e.getIndex()] += e.getValue(); } return new ArrayRealVector(out, false); } }
for (int i = 0; i < m; ++i) { final ArrayRealVector v = eigenvectors[i]; final double[] vData = v.getDataRef(); final double s = v.dotProduct(b) / realEigenvalues[i]; for (int j = 0; j < m; ++j) { bp[j] += s * vData[j]; return new ArrayRealVector(bp, false);
/** {@inheritDoc} */ @Override public RealVector subtract(RealVector v) throws IllegalArgumentException { if (v instanceof ArrayRealVector) { return subtract((ArrayRealVector) v); } else { checkVectorDimensions(v); double[] out = data.clone(); Iterator<Entry> it = v.sparseIterator(); Entry e; while(it.hasNext() && (e = it.next()) != null) { out[e.getIndex()] -= e.getValue(); } return new ArrayRealVector(out, false); } }
/** {@inheritDoc} */ @Override public double dotProduct(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); double dot = 0; for (int i = 0; i < data.length; i++) { dot += data[i] * v[i]; } return dot; }
/** {@inheritDoc} */ @Override public void setColumnVector(final int column, final RealVector vector) throws MatrixIndexException, InvalidMatrixException { try { setColumn(column, ((ArrayRealVector) vector).getDataRef()); } catch (ClassCastException cce) { super.setColumnVector(column, vector); } }
/** {@inheritDoc} */ @Override public void setSubVector(int index, RealVector v) { try { try { set(index, (ArrayRealVector) 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 double dotProduct(RealVector v) throws IllegalArgumentException { if (v instanceof ArrayRealVector) { return dotProduct((ArrayRealVector) v); } else { checkVectorDimensions(v); double dot = 0; Iterator<Entry> it = v.sparseIterator(); Entry e; while(it.hasNext() && (e = it.next()) != null) { dot += data[e.getIndex()] * e.getValue(); } return dot; } }
for (int i = 0; i < m; ++i) { final ArrayRealVector v = eigenvectors[i]; final double[] vData = v.getDataRef(); final double s = v.dotProduct(b) / realEigenvalues[i]; for (int j = 0; j < m; ++j) { bp[j] += s * vData[j];
for (int i = 0; i < m; ++i) { final ArrayRealVector v = eigenvectors[i]; final double[] vData = v.getDataRef(); double s = 0; for (int j = 0; j < m; ++j) { s += v.getEntry(j) * b.getEntry(j, k);
/** {@inheritDoc} */ @Override public double getL1Distance(RealVector v) throws IllegalArgumentException { if (v instanceof ArrayRealVector) { return getL1Distance((ArrayRealVector) v); } else { checkVectorDimensions(v); double sum = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - v.getEntry(i); sum += FastMath.abs(delta); } return sum; } }
/** {@inheritDoc} */ @Override public double getLInfDistance(RealVector v) throws IllegalArgumentException { if (v instanceof ArrayRealVector) { return getLInfDistance((ArrayRealVector) v); } else { checkVectorDimensions(v); double max = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - v.getEntry(i); max = FastMath.max(max, FastMath.abs(delta)); } return max; } }