@Override public Vector logNormalize() { return logNormalize(2.0, Math.sqrt(getLengthSquared())); }
@Override public Vector normalize() { return divide(Math.sqrt(getLengthSquared())); }
@Override public double norm(double power) { if (power < 0.0) { throw new IllegalArgumentException("Power must be >= 0"); } // We can special case certain powers. if (Double.isInfinite(power)) { return aggregate(Functions.MAX, Functions.ABS); } else if (power == 2.0) { return Math.sqrt(getLengthSquared()); } else if (power == 1.0) { double result = 0.0; Iterator<Element> iterator = this.iterateNonZero(); while (iterator.hasNext()) { result += Math.abs(iterator.next().get()); } return result; // TODO: this should ideally be used, but it's slower. // return aggregate(Functions.PLUS, Functions.ABS); } else if (power == 0.0) { return getNumNonZeroElements(); } else { return Math.pow(aggregate(Functions.PLUS, Functions.pow(power)), 1.0 / power); } }
@Override public double dot(Vector x) { if (size != x.size()) { throw new CardinalityException(size, x.size()); } if (this == x) { return getLengthSquared(); } return aggregate(x, Functions.PLUS, Functions.MULT); }
@Override public double getDistanceSquared(Vector that) { if (size != that.size()) { throw new CardinalityException(size, that.size()); } double thisLength = getLengthSquared(); double thatLength = that.getLengthSquared(); double dot = dot(that); double distanceEstimate = thisLength + thatLength - 2 * dot; if (distanceEstimate > 1.0e-3 * (thisLength + thatLength)) { // The vectors are far enough from each other that the formula is accurate. return Math.max(distanceEstimate, 0); } else { return aggregate(that, Functions.PLUS, Functions.MINUS_SQUARED); } }
@Override public Vector logNormalize() { return logNormalize(2.0, Math.sqrt(getLengthSquared())); }
@Override public Vector normalize() { return divide(Math.sqrt(getLengthSquared())); }
@Override public double norm(double power) { if (power < 0.0) { throw new IllegalArgumentException("Power must be >= 0"); } // We can special case certain powers. if (Double.isInfinite(power)) { return aggregate(Functions.MAX, Functions.ABS); } else if (power == 2.0) { return Math.sqrt(getLengthSquared()); } else if (power == 1.0) { double result = 0.0; Iterator<Element> iterator = this.iterateNonZero(); while (iterator.hasNext()) { result += Math.abs(iterator.next().get()); } return result; // TODO: this should ideally be used, but it's slower. // return aggregate(Functions.PLUS, Functions.ABS); } else if (power == 0.0) { return getNumNonZeroElements(); } else { return Math.pow(aggregate(Functions.PLUS, Functions.pow(power)), 1.0 / power); } }
@Override public double dot(Vector x) { if (size != x.size()) { throw new CardinalityException(size, x.size()); } if (this == x) { return getLengthSquared(); } return aggregate(x, Functions.PLUS, Functions.MULT); }
@Override public double getDistanceSquared(Vector that) { if (size != that.size()) { throw new CardinalityException(size, that.size()); } double thisLength = getLengthSquared(); double thatLength = that.getLengthSquared(); double dot = dot(that); double distanceEstimate = thisLength + thatLength - 2 * dot; if (distanceEstimate > 1.0e-3 * (thisLength + thatLength)) { // The vectors are far enough from each other that the formula is accurate. return Math.max(distanceEstimate, 0); } else { return aggregate(that, Functions.PLUS, Functions.MINUS_SQUARED); } }