protected double dotSelf() { return aggregate(Functions.PLUS, Functions.pow(2)); }
@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); } }
private static void doTestAggregation(Vector v, Vector w) { assertEquals("aggregate(plus, pow(2)) not equal to " + v.getLengthSquared(), v.getLengthSquared(), v.aggregate(Functions.PLUS, Functions.pow(2)), EPSILON); assertEquals("aggregate(plus, abs) not equal to " + v.norm(1), v.norm(1), v.aggregate(Functions.PLUS, Functions.ABS), EPSILON); assertEquals("aggregate(max, abs) not equal to " + v.norm(Double.POSITIVE_INFINITY), v.norm(Double.POSITIVE_INFINITY), v.aggregate(Functions.MAX, Functions.ABS), EPSILON); assertEquals("v.dot(w) != v.aggregate(w, plus, mult)", v.dot(w), v.aggregate(w, Functions.PLUS, Functions.MULT), EPSILON); assertEquals("|(v-w)|^2 != v.aggregate(w, plus, chain(pow(2), minus))", v.minus(w).dot(v.minus(w)), v.aggregate(w, Functions.PLUS, Functions.chain(Functions.pow(2), Functions.MINUS)), EPSILON); }
v1.assign(dv1); assertEquals(dv1.norm(2), Math.sqrt(v1.aggregate(Functions.PLUS, Functions.pow(2))), FUZZ); assertEquals(dv1.dot(dv2), v1.aggregate(v2, Functions.PLUS, Functions.MULT), FUZZ);
protected double dotSelf() { return aggregate(Functions.PLUS, Functions.pow(2)); }
@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); } }
private static void doTestAggregation(Vector v, Vector w) { assertEquals("aggregate(plus, pow(2)) not equal to " + v.getLengthSquared(), v.getLengthSquared(), v.aggregate(Functions.PLUS, Functions.pow(2)), EPSILON); assertEquals("aggregate(plus, abs) not equal to " + v.norm(1), v.norm(1), v.aggregate(Functions.PLUS, Functions.ABS), EPSILON); assertEquals("aggregate(max, abs) not equal to " + v.norm(Double.POSITIVE_INFINITY), v.norm(Double.POSITIVE_INFINITY), v.aggregate(Functions.MAX, Functions.ABS), EPSILON); assertEquals("v.dot(w) != v.aggregate(w, plus, mult)", v.dot(w), v.aggregate(w, Functions.PLUS, Functions.MULT), EPSILON); assertEquals("|(v-w)|^2 != v.aggregate(w, plus, chain(pow(2), minus))", v.minus(w).dot(v.minus(w)), v.aggregate(w, Functions.PLUS, Functions.chain(Functions.pow(2), Functions.MINUS)), EPSILON); }