Author: tdunning
Date: Thu Aug 12 02:51:28 2010
New Revision: 984634
URL: http://svn.apache.org/viewvc?rev=984634&view=rev
Log:
Fixed initialization error in vector aggregation. Also test for empty vectors.
Modified:
mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java
mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java
Modified:
mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java
URL:
http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java?rev=984634&r1=984633&r2=984634&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java
(original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java
Thu Aug 12 02:51:28 2010
@@ -36,16 +36,22 @@ public abstract class AbstractVector imp
}
public double aggregate(BinaryFunction aggregator, UnaryFunction map) {
- double result = 0.0;
- for (int i=0; i < size; i++) {
- result = aggregator.apply(result, map.apply(getQuick(i)) );
+ if (size < 1) {
+ throw new IllegalArgumentException("Cannot aggregate empty vector");
+ }
+ double result = map.apply(getQuick(0));
+ for (int i = 1; i < size; i++) {
+ result = aggregator.apply(result, map.apply(getQuick(i)));
}
return result;
}
public double aggregate(Vector other, BinaryFunction aggregator,
BinaryFunction combiner) {
- double result = 0.0;
- for (int i=0; i < size; i++) {
+ if (size < 1) {
+ throw new IllegalArgumentException("Cannot aggregate empty vector");
+ }
+ double result = combiner.apply(getQuick(0), other.getQuick(0));
+ for (int i = 1; i < size; i++) {
result = aggregator.apply(result, combiner.apply(getQuick(i),
other.getQuick(i)));
}
return result;
Modified: mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java
URL:
http://svn.apache.org/viewvc/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java?rev=984634&r1=984633&r2=984634&view=diff
==============================================================================
--- mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java
(original)
+++ mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java Thu
Aug 12 02:51:28 2010
@@ -637,6 +637,33 @@ public class VectorTest extends MahoutTe
v.aggregate(w, Functions.plus, Functions.chain(Functions.pow(2),
Functions.minus)));
}
+ public static void testEmptyAggregate() {
+ assertEquals(1.0, new DenseVector(new
double[]{1}).aggregate(Functions.min, Functions.identity));
+ assertEquals(1.0, new DenseVector(new double[]{2,
1}).aggregate(Functions.min, Functions.identity));
+
+ try {
+ new DenseVector(new double[]{}).aggregate(Functions.min,
Functions.identity);
+ fail("Should have thrown exception with empty vector");
+ } catch (IllegalArgumentException e) {
+ // as it should be
+ }
+
+ assertEquals(3.0,
+ new DenseVector(new double[]{1}).aggregate(
+ new DenseVector(new double[]{2}),
+ Functions.min, Functions.plus));
+
+ try {
+ new DenseVector(new double[]{}).aggregate(
+ new DenseVector(new double[]{}),
+ Functions.min, Functions.plus);
+
+ fail("Should have thrown exception with empty vector");
+ } catch (IllegalArgumentException e) {
+ // as it should be
+ }
+ }
+
private static void setUpFirstVector(Vector v) {
v.setQuick(1, 2);
v.setQuick(2, 0.5);