@Override public int hashCode () { int len = size(); int hash = len; for (int i=0; i<len; ++i) { hash = hash*71+(int) Math.round(_data[i]*73); } return hash; }
@Override public int hashCode () { int len = size(); int hash = len; for (int i=0; i<len; ++i) { hash = hash*71+(int) Math.round(_data[i]*73); } return hash; }
/** * Calculate the dot product of two vectors. Both vectors must have the same * number of dimensions. Neither input vector is altered. * * @param that The other vector to multiply by this one * @return The dot product of this and that */ public double dot (Vector that) { int len = size(); if (that.size() != len) throw new IllegalArgumentException("Attempt to take the dot product of vectors of different lengths"); double res = 0.0; for (int i=0; i<size(); ++i) { res += _data[i]*that._data[i]; } return res; }
/** * Calculate the difference of this and another vector. Both vectors must * have the same number of dimensions. Neither input vector is altered. * * @param that The other vector * @return The difference between the two vectors (this - that) */ public Vector subtract (Vector that) { int len = size(); if (that.size() != len) throw new IllegalArgumentException("Attempt to subtract vectors of different lengths"); double[] result = new double[len]; for (int i=0; i<len; ++i) { result[i] = _data[i] - that._data[i]; } return new Vector(result); }
/** * Calculate the sum of this and another vector. Both vectors must have the * same number of dimensions. Neither input vector is altered. * * @param that The other vector * @return The sum of the two vectors. */ public Vector add (Vector that) { int len = size(); if (that.size() != len) throw new IllegalArgumentException("Attempt to add vectors of different lengths"); double[] result = new double[len]; for (int i=0; i<len; ++i) { result[i] = _data[i] + that._data[i]; } return new Vector(result); }
@Override public boolean equals (Object obj) { if (this == obj) return true; if (null == obj) return false; if (!(obj instanceof Vector)) return false; Vector v = (Vector) obj; int len = size(); if (v.size() != len) return false; for (int i=0; i<len; ++i) { double ours = _data[i]; double theirs = v._data[i]; if (Double.isNaN(ours) || Double.isNaN(theirs)) { if (!(Double.isNaN(ours) && Double.isNaN(theirs))) return false; } else if (Math.abs(ours-theirs)>=_epsilon) return false; } return true; }
@Override public boolean equals (Object obj) { if (this == obj) return true; if (null == this) return false; if (!(obj instanceof Vector)) return false; Vector v = (Vector) obj; int len = size(); if (v.size() != len) return false; for (int i=0; i<len; ++i) { double ours = _data[i]; double theirs = v._data[i]; if (Double.isNaN(ours) || Double.isNaN(theirs)) { if (!(Double.isNaN(ours) && Double.isNaN(theirs))) return false; } else if (Math.abs(ours-theirs)>=_epsilon) return false; } return true; }
public Vector scale (double scale) { int len = size(); double[] coords = new double[len]; for (int i=0; i<len; ++i) coords[i] = _data[i]*scale; return new Vector(coords); }
/** * Calculate a scaled version of this vector. * * @param scale The scale to apply. * @return A new vector whose value is a scaled version of this vector. */ public Vector scale (double scale) { int len = size(); double[] coords = new double[len]; for (int i=0; i<len; ++i) coords[i] = _data[i]*scale; return new Vector(coords); }
/** * Get the coordinate-wise mean of the given vectors. */ static public Vector mean (List<? extends Vector> data) { if (data.isEmpty()) throw new IllegalArgumentException("Attempt to take the mean of 0 vectors"); Vector mean = zeroVector(data.get(0).size()); for (Vector datum: data) { mean = mean.add(datum); } mean = mean.scale(1.0/data.size()); return mean; }
/** * {@inheritDoc} */ public Vector mean (List<? extends Vector> data) { if (data.isEmpty()) throw new IllegalArgumentException("Attempt to take the mean of 0 vectors"); Vector mean = zeroVector(data.get(0).size()); for (Vector datum: data) { mean = mean.add(datum); } mean = mean.scale(1.0/data.size()); return mean; }
/** * Find this*X * * @param X * The vector by which we are being multiplied * @return The result of multiplying us by <code>X</code> */ public Vector times (Vector X) { if (X.size() != _n) throw new IllegalArgumentException("Illegal result - matrix multiplication can't result in a vector of size " + X.size()); double[] r = new double[_n]; for (int i = 0; i < _n; ++i) { double entry = 0; if (i > 0) entry += _a[i] * X.coord(i - 1); entry += _b[i] * X.coord(i); if (i < _n - 1) entry += _c[i] * X.coord(i + 1); r[i] = entry; } return new Vector(r); } }
/** * Find this*X * * @param X * The vector by which we are being multiplied * @return The result of multiplying us by <code>X</code> */ public Vector times (Vector X) { if (X.size() != _n) throw new IllegalArgumentException("Illegal result - matrix multiplication can't result in a vector of size " + X.size()); double[] r = new double[_n]; for (int i = 0; i < _n; ++i) { double entry = 0; if (i > 0) entry += _a[i] * X.coord(i - 1); entry += _b[i] * X.coord(i); if (i < _n - 1) entry += _c[i] * X.coord(i + 1); r[i] = entry; } return new Vector(r); } }
public Vector cross (Vector v) { if (3 != size() || 3 != v.size()) throw new IllegalArgumentException("Attempt to take the cross product of non-3-vectors"); double xa = coord(0); double ya = coord(1); double za = coord(2); double xb = v.coord(0); double yb = v.coord(1); double zb = v.coord(2); return new Vector(ya*zb-za*yb, za*xb-xa*zb, xa*yb-ya*xb); }
/** * Find the X for which this*X=d * * Taken from @see <a href="http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm></a>, but modified to * use recursion instead of iteration, and thereby to handle degenerate * cases cleanly. * * @param d * The result (<code>d</code>) in the above equation * @return The <code>X</code> in the above equation */ public Vector solve (Vector d) { if (d.size() != _n) throw new IllegalArgumentException("Attempt to find tri-diagonal solution with improper-sized vector"); double[] x = new double[_n]; solve(d, x, 0, (_n > 0 ? _b[0] : 0), (_n > 1 ? _c[0] : 0), (_n > 0 ? d.coord(0) : 0)); //solve3(d, x, 1, _b[0], _c[0], 0, d.coord(0)); return new Vector(x); }
/** * Find the X for which this*X=d * * Taken from {@linkplain http * ://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm}, but modified to * use recursion instead of iteration, and thereby to handle degenerate * cases cleanly. * * @param d * The result (<code>d</code>) in the above equation * @return The <code>X</code> in the above equation */ public Vector solve (Vector d) { if (d.size() != _n) throw new IllegalArgumentException("Attempt to find tri-diagonal solution with improper-sized vector"); double[] x = new double[_n]; solve(d, x, 0, (_n > 0 ? _b[0] : 0), (_n > 1 ? _c[0] : 0), (_n > 0 ? d.coord(0) : 0)); //solve3(d, x, 1, _b[0], _c[0], 0, d.coord(0)); return new Vector(x); }
/** * Calculate the cross product of two vectors. Both vectors must be of 3 components. * @param v cross product operand * @return The cross product */ public Vector cross (Vector v) { if (3 != size() || 3 != v.size()) throw new IllegalArgumentException("Attempt to take the cross product of non-3-vectors"); double xa = coord(0); double ya = coord(1); double za = coord(2); double xb = v.coord(0); double yb = v.coord(1); double zb = v.coord(2); return new Vector(ya*zb-za*yb, za*xb-xa*zb, xa*yb-ya*xb); }