You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2010/04/21 09:41:56 UTC
svn commit: r936183 [3/3] - in /lucene/mahout/trunk:
core/src/main/java/org/apache/mahout/cf/taste/hadoop/item/
core/src/main/java/org/apache/mahout/clustering/canopy/
core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/
core/src/main/java/org/...
Modified: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestDenseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestDenseVector.java?rev=936183&r1=936182&r2=936183&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestDenseVector.java (original)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestDenseVector.java Wed Apr 21 07:41:55 2010
@@ -17,407 +17,20 @@
package org.apache.mahout.math;
-import static org.apache.mahout.math.function.Functions.mult;
-import static org.apache.mahout.math.function.Functions.plus;
-
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-
-import junit.framework.TestCase;
-
-public class TestDenseVector extends TestCase {
-
- private final double[] values = {1.1, 2.2, 3.3};
-
- private final Vector test = new DenseVector(values);
+public class TestDenseVector extends AbstractTestVector {
public TestDenseVector(String name) {
super(name);
}
- public void testAsFormatString() {
- String formatString = test.asFormatString();
- Vector vec = AbstractVector.decodeVector(formatString);
- assertEquals(vec, test);
- }
-
- public void testCardinality() {
- assertEquals("cardinality", 3, test.size());
- }
-
- public void testCopy() throws Exception {
- Vector copy = test.clone();
- for (int i = 0; i < test.size(); i++) {
- assertEquals("copy [" + i + ']', test.get(i), copy.get(i));
- }
- }
-
- public void testGet() throws Exception {
- for (int i = 0; i < test.size(); i++) {
- assertEquals("get [" + i + ']', values[i], test.get(i));
- }
- }
-
- public void testGetOver() {
- try {
- test.get(test.size());
- fail("expected exception");
- } catch (IndexException e) {
- assertTrue(true);
- }
- }
-
- public void testGetUnder() {
- try {
- test.get(-1);
- fail("expected exception");
- } catch (IndexException e) {
- assertTrue(true);
- }
- }
-
- public void testSet() throws Exception {
- test.set(2, 4.5);
- for (int i = 0; i < test.size(); i++) {
- if (i == 2) {
- assertEquals("set [" + i + ']', 4.5, test.get(i));
- } else {
- assertEquals("set [" + i + ']', values[i], test.get(i));
- }
- }
- }
-
-
- public void testIterator() throws Exception {
- Iterator<Vector.Element> iterator = test.iterateNonZero();
- checkIterator(iterator, values, 3);
-
- iterator = test.iterateAll();
- checkIterator(iterator, values, 3);
-
- double[] doubles = {0.0, 5.0, 0, 3.0};
- DenseVector zeros = new DenseVector(doubles);
- iterator = zeros.iterateNonZero();
- checkIterator(iterator, doubles, 2);
- iterator = zeros.iterateAll();
- checkIterator(iterator, doubles, doubles.length);
-
- doubles = new double[]{0.0, 0.0, 0, 0.0};
- zeros = new DenseVector(doubles);
- iterator = zeros.iterateNonZero();
- checkIterator(iterator, doubles, 0);
- iterator = zeros.iterateAll();
- checkIterator(iterator, doubles, doubles.length);
-
- }
-
- private static void checkIterator(Iterator<Vector.Element> nzIter, double[] values, int expectedNum) {
- int i = 0;
- while (nzIter.hasNext()) {
- Vector.Element elt = nzIter.next();
- assertEquals((elt.index()) + " Value: " + values[elt.index()] + " does not equal: " + elt.get(),
- values[elt.index()], elt.get(), 0.0);
- i++;
- }
- assertEquals(i + " does not equal: " + expectedNum, i, expectedNum);
+ @Override
+ Vector generateTestVector(int cardinality) {
+ return new DenseVector(cardinality);
}
+ @Override
public void testSize() throws Exception {
- assertEquals("size", 3, test.getNumNondefaultElements());
- }
-
- public void testViewPart() throws Exception {
- Vector part = test.viewPart(1, 2);
- assertEquals("part size", 2, part.getNumNondefaultElements());
- for (int i = 0; i < part.size(); i++) {
- assertEquals("part[" + i + ']', values[i + 1], part.get(i));
- }
- }
-
- public void testViewPartUnder() {
- try {
- test.viewPart(-1, values.length);
- fail("no exception");
- } catch (CardinalityException e) {
- fail("wrong exception");
- } catch (IndexException e) {
- assertTrue(true);
- }
- }
-
- public void testViewPartOver() {
- try {
- test.viewPart(2, values.length);
- fail("no exception");
- } catch (CardinalityException e) {
- fail("wrong exception");
- } catch (IndexException e) {
- assertTrue(true);
- }
- }
-
- public void testViewPartCardinality() {
- try {
- test.viewPart(1, values.length + 1);
- fail("no exception");
- } catch (CardinalityException e) {
- assertTrue(true);
- } catch (IndexException e) {
- fail("wrong exception");
- }
- }
-
- public void testDecodeVector() throws Exception {
- Vector val = AbstractVector.decodeVector(test.asFormatString());
- for (int i = 0; i < test.size(); i++) {
- assertEquals("get [" + i + ']', test.get(i), val.get(i));
- }
+ assertEquals("size", 7, getTestVector().getNumNondefaultElements());
}
- public void testDenseVectorDoubleArray() throws Exception {
- for (int i = 0; i < test.size(); i++) {
- assertEquals("test[" + i + ']', values[i], test.get(i));
- }
- }
-
- public void testDenseVectorInt() throws Exception {
- Vector val = new DenseVector(4);
- assertEquals("cardinality", 4, val.size());
- for (int i = 0; i < test.size(); i++) {
- assertEquals("get [" + i + ']', 0.0, val.get(i));
- }
- }
-
- public void testDot() throws Exception {
- double res = test.dot(test);
- assertEquals("dot", 1.1 * 1.1 + 2.2 * 2.2 + 3.3 * 3.3, res);
- }
-
- public void testDotCardinality() {
- try {
- test.dot(new DenseVector(test.size() + 1));
- fail("expected exception");
- } catch (CardinalityException e) {
- assertTrue(true);
- }
- }
-
- public void testNormalize() throws Exception {
- Vector res = test.normalize();
- double mag = Math.sqrt(1.1 * 1.1 + 2.2 * 2.2 + 3.3 * 3.3);
- for (int i = 0; i < test.size(); i++) {
- assertEquals("dot", values[i] / mag, res.get(i));
- }
- }
-
- public void testMinus() throws Exception {
- Vector val = test.minus(test);
- assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++) {
- assertEquals("get [" + i + ']', 0.0, val.get(i));
- }
- Vector val1 = test.plus(1);
- val = val1.minus(test);
- for (int i = 0; i < test.size(); i++) {
- assertEquals("get [" + i + ']', 1.0, val.get(i));
- }
-
- val1 = test.plus(-1);
- val = val1.minus(test);
- for (int i = 0; i < test.size(); i++) {
- assertEquals("get [" + i + ']', -1.0, val.get(i));
- }
- }
-
- public void testPlusDouble() throws Exception {
- Vector val = test.plus(1);
- assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++) {
- assertEquals("get [" + i + ']', values[i] + 1, val.get(i));
- }
- }
-
- public void testPlusVector() throws Exception {
- Vector val = test.plus(test);
- assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++) {
- assertEquals("get [" + i + ']', values[i] * 2, val.get(i));
- }
- }
-
- public void testPlusVectorCardinality() {
- try {
- test.plus(new DenseVector(test.size() + 1));
- fail("expected exception");
- } catch (CardinalityException e) {
- assertTrue(true);
- }
- }
-
- public void testTimesDouble() throws Exception {
- Vector val = test.times(3);
- assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++) {
- assertEquals("get [" + i + ']', values[i] * 3, val.get(i));
- }
- }
-
- public void testDivideDouble() throws Exception {
- Vector val = test.divide(3);
- assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++) {
- assertEquals("get [" + i + ']', values[i] / 3, val.get(i));
- }
- }
-
- public void testTimesVector() throws Exception {
- Vector val = test.times(test);
- assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++) {
- assertEquals("get [" + i + ']', values[i] * values[i], val.get(i));
- }
- }
-
- public void testTimesVectorCardinality() {
- try {
- test.times(new DenseVector(test.size() + 1));
- fail("expected exception");
- } catch (CardinalityException e) {
- assertTrue(true);
- }
- }
-
- public void testZSum() {
- double expected = 0;
- for (double value : values) {
- expected += value;
- }
- assertEquals("wrong zSum", expected, test.zSum());
- }
-
- public void testGetDistanceSquared() {
- Vector other = new DenseVector(test.size());
- other.set(0, -2);
- other.set(1, -5);
- other.set(2, -9);
- double expected = test.minus(other).getLengthSquared();
- assertTrue("a.getDistanceSquared(b) != a.minus(b).getLengthSquared",
- Math.abs(expected - test.getDistanceSquared(other)) < 1.0e-6);
- }
-
- public void testAssignDouble() {
- test.assign(0);
- for (int i = 0; i < values.length; i++) {
- assertEquals("value[" + i + ']', 0.0, test.getQuick(i));
- }
- }
-
- public void testAssignDoubleArray() throws Exception {
- double[] array = new double[test.size()];
- test.assign(array);
- for (int i = 0; i < values.length; i++) {
- assertEquals("value[" + i + ']', 0.0, test.getQuick(i));
- }
- }
-
- public void testAssignDoubleArrayCardinality() {
- double[] array = new double[test.size() + 1];
- try {
- test.assign(array);
- fail("cardinality exception expected");
- } catch (CardinalityException e) {
- assertTrue(true);
- }
- }
-
- public void testAssignVector() throws Exception {
- Vector other = new DenseVector(test.size());
- test.assign(other);
- for (int i = 0; i < values.length; i++) {
- assertEquals("value[" + i + ']', 0.0, test.getQuick(i));
- }
- }
-
- public void testAssignVectorCardinality() {
- Vector other = new DenseVector(test.size() - 1);
- try {
- test.assign(other);
- fail("cardinality exception expected");
- } catch (CardinalityException e) {
- assertTrue(true);
- }
- }
-
- public void testAssignUnaryFunction() {
- test.assign(mult(-1));
- for (int i = 0; i < values.length; i++) {
- assertEquals("value[" + i + ']', -values[i], test.getQuick(i));
- }
- }
-
- public void testAssignBinaryFunction() throws Exception {
- test.assign(test, plus);
- for (int i = 0; i < values.length; i++) {
- assertEquals("value[" + i + ']', 2 * values[i], test.getQuick(i));
- }
- }
-
- public void testAssignBinaryFunction2() throws Exception {
- test.assign(plus(4));
- for (int i = 0; i < values.length; i++) {
- assertEquals("value[" + i + ']', values[i] + 4, test.getQuick(i));
- }
- }
-
- public void testAssignBinaryFunction3() throws Exception {
- test.assign(mult(4));
- for (int i = 0; i < values.length; i++) {
- assertEquals("value[" + i + ']', values[i] * 4, test.getQuick(i));
- }
- }
-
- public void testAssignBinaryFunctionCardinality() {
- try {
- test.assign(test.like(2), plus);
- fail("Cardinality exception expected");
- } catch (CardinalityException e) {
- assertTrue(true);
- }
- }
-
- public void testLike() {
- assertTrue("not like", test.like() instanceof DenseVector);
- }
-
- public void testLikeN() {
- Vector other = test.like(5);
- assertTrue("not like", other instanceof DenseVector);
- assertEquals("size", 5, other.size());
- }
-
- public void testCrossProduct() {
- Matrix result = test.cross(test);
- assertEquals("row size", test.size(), result.size()[0]);
- assertEquals("col size", test.size(), result.size()[1]);
- for (int row = 0; row < result.size()[0]; row++) {
- for (int col = 0; col < result.size()[1]; col++) {
- assertEquals("cross[" + row + "][" + col + ']', test.getQuick(row)
- * test.getQuick(col), result.getQuick(row, col));
- }
- }
- }
-
- public void testLabelIndexing() {
- Map<String, Integer> bindings = new HashMap<String, Integer>();
- bindings.put("Fee", 0);
- bindings.put("Fie", 1);
- bindings.put("Foe", 2);
- test.setLabelBindings(bindings);
- assertEquals("Fee", test.get(0), test.get("Fee"));
- assertEquals("Fie", test.get(1), test.get("Fie"));
- assertEquals("Foe", test.get(2), test.get("Foe"));
- test.set("Fie", 15.3);
- assertEquals("Fie", test.get(1), test.get("Fie"));
- }
}
Added: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestRandomAccessSparseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestRandomAccessSparseVector.java?rev=936183&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestRandomAccessSparseVector.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestRandomAccessSparseVector.java Wed Apr 21 07:41:55 2010
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mahout.math;
+
+public class TestRandomAccessSparseVector extends AbstractTestVector {
+
+ public TestRandomAccessSparseVector(String name) {
+ super(name);
+ }
+
+ @Override
+ Vector generateTestVector(int cardinality) {
+ return new RandomAccessSparseVector(cardinality);
+ }
+
+}
Added: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSequentialAccessSparseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSequentialAccessSparseVector.java?rev=936183&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSequentialAccessSparseVector.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSequentialAccessSparseVector.java Wed Apr 21 07:41:55 2010
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mahout.math;
+
+public class TestSequentialAccessSparseVector extends AbstractTestVector {
+
+ public TestSequentialAccessSparseVector(String name) {
+ super(name);
+ }
+
+ @Override
+ Vector generateTestVector(int cardinality) {
+ return new SequentialAccessSparseVector(cardinality);
+ }
+
+}
\ No newline at end of file
Modified: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java?rev=936183&r1=936182&r2=936183&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java (original)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java Wed Apr 21 07:41:55 2010
@@ -67,7 +67,6 @@ public class TestVectorView extends Test
test.get(test.size());
fail("expected exception");
} catch (IndexException e) {
- assertTrue(true);
}
}
@@ -75,14 +74,14 @@ public class TestVectorView extends Test
VectorView view = new VectorView(new DenseVector(values), offset, cardinality);
double[] gold = {1.1, 2.2, 3.3};
- Iterator<Vector.Element> iter = view.iterateAll();
+ Iterator<Vector.Element> iter = view.iterator();
checkIterator(iter, gold);
iter = view.iterateNonZero();
checkIterator(iter, gold);
view = new VectorView(new DenseVector(values), 0, cardinality);
gold = new double[]{0.0, 1.1, 2.2};
- iter = view.iterateAll();
+ iter = view.iterator();
checkIterator(iter, gold);
gold = new double[]{1.1, 2.2};
iter = view.iterateNonZero();
@@ -105,7 +104,6 @@ public class TestVectorView extends Test
test.get(-1);
fail("expected exception");
} catch (IndexException e) {
- assertTrue(true);
}
}
@@ -136,10 +134,7 @@ public class TestVectorView extends Test
try {
test.viewPart(-1, cardinality);
fail("no exception");
- } catch (CardinalityException e) {
- fail("expected index exception");
} catch (IndexException e) {
- assertTrue(true);
}
}
@@ -147,10 +142,7 @@ public class TestVectorView extends Test
try {
test.viewPart(2, cardinality);
fail("no exception");
- } catch (CardinalityException e) {
- fail("expected index exception");
} catch (IndexException e) {
- assertTrue(true);
}
}
@@ -158,10 +150,7 @@ public class TestVectorView extends Test
try {
test.viewPart(1, values.length + 1);
fail("no exception");
- } catch (CardinalityException e) {
- assertTrue(true);
} catch (IndexException e) {
- fail("expected cardinality exception");
}
}
@@ -175,7 +164,6 @@ public class TestVectorView extends Test
test.dot(new DenseVector(test.size() + 1));
fail("expected exception");
} catch (CardinalityException e) {
- assertTrue(true);
}
}
@@ -216,7 +204,6 @@ public class TestVectorView extends Test
test.plus(new DenseVector(test.size() + 1));
fail("expected exception");
} catch (CardinalityException e) {
- assertTrue(true);
}
}
@@ -250,7 +237,6 @@ public class TestVectorView extends Test
test.times(new DenseVector(test.size() + 1));
fail("expected exception");
} catch (CardinalityException e) {
- assertTrue(true);
}
}
@@ -283,7 +269,6 @@ public class TestVectorView extends Test
test.assign(array);
fail("cardinality exception expected");
} catch (CardinalityException e) {
- assertTrue(true);
}
}
@@ -301,7 +286,6 @@ public class TestVectorView extends Test
test.assign(other);
fail("cardinality exception expected");
} catch (CardinalityException e) {
- assertTrue(true);
}
}
@@ -338,7 +322,6 @@ public class TestVectorView extends Test
test.assign(test.like(2), plus);
fail("Cardinality exception expected");
} catch (CardinalityException e) {
- assertTrue(true);
}
}
Modified: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java?rev=936183&r1=936182&r2=936183&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java (original)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java Wed Apr 21 07:41:55 2010
@@ -23,10 +23,10 @@ import com.google.gson.reflect.TypeToken
import junit.framework.TestCase;
import org.apache.mahout.math.function.Functions;
-import static org.apache.mahout.math.function.Functions.*;
-
import java.lang.reflect.Type;
+import java.util.Collection;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
@@ -37,16 +37,16 @@ public class VectorTest extends TestCase
}
public void testSparseVector() throws Exception {
- RandomAccessSparseVector vec1 = new RandomAccessSparseVector(3);
- RandomAccessSparseVector vec2 = new RandomAccessSparseVector(3);
+ Vector vec1 = new RandomAccessSparseVector(3);
+ Vector vec2 = new RandomAccessSparseVector(3);
doTestVectors(vec1, vec2);
}
public void testEquivalent() throws Exception {
//names are not used for equivalent
- RandomAccessSparseVector randomAccessLeft = new RandomAccessSparseVector("foo", 3);
- SequentialAccessSparseVector sequentialAccessLeft = new SequentialAccessSparseVector("foo", 3);
- DenseVector right = new DenseVector("foo", 3);
+ RandomAccessSparseVector randomAccessLeft = new RandomAccessSparseVector(3);
+ Vector sequentialAccessLeft = new SequentialAccessSparseVector(3);
+ Vector right = new DenseVector(3);
randomAccessLeft.setQuick(0, 1);
randomAccessLeft.setQuick(1, 2);
randomAccessLeft.setQuick(2, 3);
@@ -56,50 +56,38 @@ public class VectorTest extends TestCase
right.setQuick(0, 1);
right.setQuick(1, 2);
right.setQuick(2, 3);
- assertTrue("equivalent didn't work", AbstractVector.equivalent(randomAccessLeft, right));
- assertTrue("equivalent didn't work", AbstractVector.equivalent(sequentialAccessLeft, right));
- assertTrue("equivalent didn't work", AbstractVector.equivalent(sequentialAccessLeft, randomAccessLeft));
- assertEquals("equals didn't work", randomAccessLeft, right);
- assertEquals("equals didn't work", sequentialAccessLeft, right);
- assertEquals("equals didn't work", sequentialAccessLeft, randomAccessLeft);
- assertFalse("equivalent didn't work", AbstractVector.strictEquivalence(randomAccessLeft, right));
- assertFalse("equivalent didn't work", AbstractVector.strictEquivalence(randomAccessLeft, sequentialAccessLeft));
- assertFalse("equivalent didn't work", AbstractVector.strictEquivalence(sequentialAccessLeft, right));
+ assertEquals(randomAccessLeft, right);
+ assertEquals(sequentialAccessLeft, right);
+ assertEquals(sequentialAccessLeft, randomAccessLeft);
- DenseVector leftBar = new DenseVector("bar", 3);
+ Vector leftBar = new DenseVector(3);
leftBar.setQuick(0, 1);
leftBar.setQuick(1, 2);
leftBar.setQuick(2, 3);
- assertTrue("equivalent didn't work", AbstractVector.equivalent(leftBar, right));
- assertFalse("equals didn't work", leftBar.equals(right));
- assertFalse("equivalent didn't work", AbstractVector.strictEquivalence(randomAccessLeft, right));
- assertFalse("equivalent didn't work", AbstractVector.strictEquivalence(sequentialAccessLeft, right));
+ assertEquals(leftBar, right);
+ assertEquals(randomAccessLeft, right);
+ assertEquals(sequentialAccessLeft, right);
- RandomAccessSparseVector rightBar = new RandomAccessSparseVector("bar", 3);
+ Vector rightBar = new RandomAccessSparseVector(3);
rightBar.setQuick(0, 1);
rightBar.setQuick(1, 2);
rightBar.setQuick(2, 3);
- assertTrue("equivalent didn't work", AbstractVector.equivalent(randomAccessLeft, rightBar));
- assertFalse("equals didn't work", randomAccessLeft.equals(rightBar));
- assertFalse("equivalent didn't work", AbstractVector.strictEquivalence(randomAccessLeft, rightBar));
+ assertEquals(randomAccessLeft, rightBar);
right.setQuick(2, 4);
- assertFalse("equivalent didn't work", AbstractVector.equivalent(randomAccessLeft, right));
- assertFalse("equals didn't work", randomAccessLeft.equals(right));
+ assertFalse(randomAccessLeft.equals(right));
right = new DenseVector(4);
right.setQuick(0, 1);
right.setQuick(1, 2);
right.setQuick(2, 3);
right.setQuick(3, 3);
- assertFalse("equivalent didn't work", AbstractVector.equivalent(randomAccessLeft, right));
- assertFalse("equals didn't work", randomAccessLeft.equals(right));
+ assertFalse(randomAccessLeft.equals(right));
randomAccessLeft = new RandomAccessSparseVector(2);
randomAccessLeft.setQuick(0, 1);
randomAccessLeft.setQuick(1, 2);
- assertFalse("equivalent didn't work", AbstractVector.equivalent(randomAccessLeft, right));
- assertFalse("equals didn't work", randomAccessLeft.equals(right));
+ assertFalse(randomAccessLeft.equals(right));
- DenseVector dense = new DenseVector(3);
+ Vector dense = new DenseVector(3);
right = new DenseVector(3);
right.setQuick(0, 1);
right.setQuick(1, 2);
@@ -107,8 +95,7 @@ public class VectorTest extends TestCase
dense.setQuick(0, 1);
dense.setQuick(1, 2);
dense.setQuick(2, 3);
- assertTrue("equivalent didn't work", AbstractVector.equivalent(dense, right));
- assertEquals("equals didn't work", dense, right);
+ assertEquals(dense, right);
RandomAccessSparseVector sparse = new RandomAccessSparseVector(3);
randomAccessLeft = new RandomAccessSparseVector(3);
@@ -118,19 +105,15 @@ public class VectorTest extends TestCase
randomAccessLeft.setQuick(0, 1);
randomAccessLeft.setQuick(1, 2);
randomAccessLeft.setQuick(2, 3);
- assertTrue("equivalent didn't work", AbstractVector.equivalent(sparse, randomAccessLeft));
- assertEquals("equals didn't work", randomAccessLeft, sparse);
+ assertEquals(randomAccessLeft, sparse);
- VectorView v1 = new VectorView(randomAccessLeft, 0, 2);
- VectorView v2 = new VectorView(right, 0, 2);
- assertTrue("equivalent didn't work", AbstractVector.equivalent(v1, v2));
- assertEquals("equals didn't work", v1, v2);
+ Vector v1 = new VectorView(randomAccessLeft, 0, 2);
+ Vector v2 = new VectorView(right, 0, 2);
+ assertEquals(v1, v2);
sparse = new RandomAccessSparseVector(2);
sparse.setQuick(0, 1);
sparse.setQuick(1, 2);
- assertTrue("equivalent didn't work", AbstractVector.equivalent(v1, sparse));
- assertEquals("equals didn't work", v1, sparse);
-
+ assertEquals(v1, sparse);
}
private static void doTestVectors(Vector left, Vector right) {
@@ -141,13 +124,12 @@ public class VectorTest extends TestCase
right.setQuick(1, 5);
right.setQuick(2, 6);
double result = left.dot(right);
- assertEquals(result + " does not equal: " + 32, 32.0, result);
+ assertEquals(32.0, result);
String formattedString = left.asFormatString();
- System.out.println("Vec: " + formattedString);
+ //System.out.println("Vec: " + formattedString);
Vector vec = AbstractVector.decodeVector(formattedString);
- assertNotNull("vec is null and it shouldn't be", vec);
- assertTrue("Vector could not be decoded from the formatString",
- AbstractVector.equivalent(vec, left));
+ assertNotNull(vec);
+ assertEquals(vec, left);
}
public void testGetDistanceSquared() throws Exception {
@@ -185,10 +167,9 @@ public class VectorTest extends TestCase
w.setQuick(4, 2.1);
}
- public void doTestGetDistanceSquared(Vector v, Vector w) throws Exception {
+ public void doTestGetDistanceSquared(Vector v, Vector w) {
double expected = v.minus(w).getLengthSquared();
- assertTrue("a.getDistanceSquared(b) != a.minus(b).getLengthSquared",
- Math.abs(expected - v.getDistanceSquared(w)) < 1.0e-6);
+ assertEquals(expected, v.getDistanceSquared(w), 1.0e-6);
}
public void testGetLengthSquared() throws Exception {
@@ -204,14 +185,15 @@ public class VectorTest extends TestCase
}
public static double lengthSquaredSlowly(Vector v) {
- double d = 0;
- for(int i=0; i<v.size(); i++) {
- d += (v.get(i) * v.get(i));
+ double d = 0.0;
+ for (int i = 0; i < v.size(); i++) {
+ double value = v.get(i);
+ d += value * value;
}
return d;
}
- public void doTestGetLengthSquared(Vector v) throws Exception {
+ public void doTestGetLengthSquared(Vector v) {
double expected = lengthSquaredSlowly(v);
assertEquals("v.getLengthSquared() != sum_of_squared_elements(v)", expected, v.getLengthSquared(), 0.0);
@@ -223,7 +205,7 @@ public class VectorTest extends TestCase
expected = lengthSquaredSlowly(v);
assertEquals("mutation via setQuick() fails to change lengthSquared", expected, v.getLengthSquared());
- Iterator<Vector.Element> it = v.iterateAll();
+ Iterator<Vector.Element> it = v.iterator();
while(it.hasNext()) {
Vector.Element e = it.next();
if(e.index() == v.size() - 2) {
@@ -234,11 +216,11 @@ public class VectorTest extends TestCase
assertEquals("mutation via dense iterator.set fails to change lengthSquared", expected, v.getLengthSquared());
it = v.iterateNonZero();
- int i=0;
- while(it.hasNext()) {
+ int i = 0;
+ while (it.hasNext()) {
i++;
Vector.Element e = it.next();
- if(i == v.getNumNondefaultElements() - 1) {
+ if (i == v.getNumNondefaultElements() - 1) {
e.set(e.get() - 5.0);
}
}
@@ -287,8 +269,62 @@ public class VectorTest extends TestCase
assertEquals("mutation via assign(v,plus) fails to change lengthSquared", expected, v.getLengthSquared());
}
+ public void testIterator() {
+
+ Collection<Integer> expectedIndices = new HashSet<Integer>();
+ int i = 1;
+ while (i <= 20) {
+ expectedIndices.add(i * (i + 1) / 2);
+ i++;
+ }
+
+ Vector denseVector = new DenseVector(i * i);
+ for (int index : expectedIndices) {
+ denseVector.set(index, (double) 2 * index);
+ }
+ doTestIterators(denseVector, expectedIndices);
+
+ Vector randomAccessVector = new RandomAccessSparseVector(i * i);
+ for (int index : expectedIndices) {
+ randomAccessVector.set(index, (double) 2 * index);
+ }
+ doTestIterators(randomAccessVector, expectedIndices);
+
+ Vector sequentialVector = new SequentialAccessSparseVector(i * i);
+ for (int index : expectedIndices) {
+ sequentialVector.set(index, (double) 2 * index);
+ }
+ doTestIterators(sequentialVector, expectedIndices);
+ }
+
+ private static void doTestIterators(Vector vector, Collection<Integer> expectedIndices) {
+ expectedIndices = new HashSet<Integer>(expectedIndices);
+ Iterator<Vector.Element> allIterator = vector.iterator();
+ int index = 0;
+ while (allIterator.hasNext()) {
+ Vector.Element element = allIterator.next();
+ assertEquals(index, element.index());
+ if (expectedIndices.contains(index)) {
+ assertEquals((double) index * 2, element.get());
+ } else {
+ assertEquals(0.0, element.get());
+ }
+ index++;
+ }
+
+ Iterator<Vector.Element> nonZeroIterator = vector.iterateNonZero();
+ while (nonZeroIterator.hasNext()) {
+ Vector.Element element = nonZeroIterator.next();
+ index = element.index();
+ assertTrue(expectedIndices.contains(index));
+ assertEquals((double) index * 2, element.get());
+ expectedIndices.remove(index);
+ }
+ assertTrue(expectedIndices.isEmpty());
+ }
+
public void testNormalize() throws Exception {
- RandomAccessSparseVector vec1 = new RandomAccessSparseVector(3);
+ Vector vec1 = new RandomAccessSparseVector(3);
vec1.setQuick(0, 1);
vec1.setQuick(1, 2);
@@ -296,7 +332,7 @@ public class VectorTest extends TestCase
Vector norm = vec1.normalize();
assertNotNull("norm1 is null and it shouldn't be", norm);
- SequentialAccessSparseVector vec2 = new SequentialAccessSparseVector(3);
+ Vector vec2 = new SequentialAccessSparseVector(3);
vec2.setQuick(0, 1);
vec2.setQuick(1, 2);
@@ -310,25 +346,23 @@ public class VectorTest extends TestCase
expected.setQuick(1, 0.5345224838248488);
expected.setQuick(2, 0.8017837257372732);
- assertEquals("norm is not equal to expected", norm, expected);
- assertTrue("norm is not equivalent to expected", AbstractVector.equivalent(norm2, expected));
+ assertEquals(expected, norm);
norm = vec1.normalize(2);
- assertEquals("norm is not equal to expected", norm, expected);
+ assertEquals(expected, norm);
norm2 = vec2.normalize(2);
- assertTrue("norm is not equivalent to expected", AbstractVector.equivalent(norm2, expected));
+ assertEquals(expected, norm2);
norm = vec1.normalize(1);
norm2 = vec2.normalize(1);
expected.setQuick(0, 1.0 / 6);
expected.setQuick(1, 2.0 / 6);
expected.setQuick(2, 3.0 / 6);
- assertEquals("norm is not equal to expected", norm, expected);
- assertTrue("norm is not equivalent to expected", AbstractVector.equivalent(norm2, expected));
+ assertEquals(expected, norm);
+ assertEquals(expected, norm2);
norm = vec1.normalize(3);
- // TODO this is not used
- expected = vec1.times(vec1).times(vec1);
+ //expected = vec1.times(vec1).times(vec1);
// double sum = expected.zSum();
// cube = Math.pow(sum, 1.0/3);
@@ -445,10 +479,9 @@ public class VectorTest extends TestCase
}
public void testDenseVector() throws Exception {
- DenseVector vec1 = new DenseVector(3);
- DenseVector vec2 = new DenseVector(3);
+ Vector vec1 = new DenseVector(3);
+ Vector vec2 = new DenseVector(3);
doTestVectors(vec1, vec2);
-
}
public void testVectorView() throws Exception {
@@ -456,10 +489,10 @@ public class VectorTest extends TestCase
RandomAccessSparseVector vec2 = new RandomAccessSparseVector(6);
SequentialAccessSparseVector vec3 = new SequentialAccessSparseVector(3);
SequentialAccessSparseVector vec4 = new SequentialAccessSparseVector(6);
- VectorView vecV1 = new VectorView(vec1, 0, 3);
- VectorView vecV2 = new VectorView(vec2, 2, 3);
- VectorView vecV3 = new VectorView(vec3, 0, 3);
- VectorView vecV4 = new VectorView(vec4, 2, 3);
+ Vector vecV1 = new VectorView(vec1, 0, 3);
+ Vector vecV2 = new VectorView(vec2, 2, 3);
+ Vector vecV3 = new VectorView(vec3, 0, 3);
+ Vector vecV4 = new VectorView(vec4, 2, 3);
doTestVectors(vecV1, vecV2);
doTestVectors(vecV3, vecV4);
}
@@ -524,23 +557,23 @@ public class VectorTest extends TestCase
doTestAggregation(w, v);
}
- private static void doTestAggregation(Vector v, Vector w) throws Exception {
+ private static void doTestAggregation(Vector v, Vector w) {
assertEquals("aggregate(plus, pow(2)) not equal to " + v.getLengthSquared(),
v.getLengthSquared(),
- v.aggregate(plus, pow(2)));
+ v.aggregate(Functions.plus, Functions.pow(2)));
assertEquals("aggregate(plus, abs) not equal to " + v.norm(1),
v.norm(1),
- v.aggregate(plus, abs));
+ v.aggregate(Functions.plus, Functions.abs));
assertEquals("aggregate(max, abs) not equal to " + v.norm(Double.POSITIVE_INFINITY),
v.norm(Double.POSITIVE_INFINITY),
- v.aggregate(max, abs));
+ v.aggregate(Functions.max, Functions.abs));
assertEquals("v.dot(w) != v.aggregate(w, plus, mult)",
v.dot(w),
- v.aggregate(w, plus, mult));
+ v.aggregate(w, Functions.plus, Functions.mult));
assertEquals("|(v-w)|^2 != v.aggregate(w, plus, chain(pow(2), minus))",
v.minus(w).dot(v.minus(w)),
- v.aggregate(w, plus, chain(pow(2), minus)));
+ v.aggregate(w, Functions.plus, Functions.chain(Functions.pow(2), Functions.minus)));
}
private static void setUpFirstVector(Vector v) {
@@ -576,18 +609,14 @@ public class VectorTest extends TestCase
try {
test1.get("Fee");
fail();
- } catch (IndexException e) {
- fail();
} catch (UnboundLabelException e) {
- assertTrue(true);
}
-
}
public void testNameSerialization() throws Exception {
double[] values = {1.1, 2.2, 3.3};
- Vector test = new DenseVector("foo", values);
+ Vector test = new DenseVector(values);
String formatString = test.asFormatString();
Vector decode = AbstractVector.decodeVector(formatString);
@@ -623,10 +652,7 @@ public class VectorTest extends TestCase
try {
test1.get("Fee");
fail();
- } catch (IndexException e) {
- fail();
} catch (UnboundLabelException e) {
- assertTrue(true);
}
}
@@ -674,11 +700,11 @@ public class VectorTest extends TestCase
assertEquals(sparseLeft, sparseRight);
assertEquals(sparseLeft.hashCode(), sparseRight.hashCode());
- DenseVector emptyLeft = new DenseVector("foo", 0);
- Vector emptyRight = new SequentialAccessSparseVector("foo", 0);
+ DenseVector emptyLeft = new DenseVector(0);
+ Vector emptyRight = new SequentialAccessSparseVector(0);
assertEquals(emptyLeft, emptyRight);
assertEquals(emptyLeft.hashCode(), emptyRight.hashCode());
- emptyRight = new RandomAccessSparseVector("foo", 0);
+ emptyRight = new RandomAccessSparseVector(0);
assertEquals(emptyLeft, emptyRight);
assertEquals(emptyLeft.hashCode(), emptyRight.hashCode());
}
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java?rev=936183&r1=936182&r2=936183&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java Wed Apr 21 07:41:55 2010
@@ -52,10 +52,6 @@ public class VectorHelper {
*/
public static String vectorToString(Vector vector, String[] dictionary) {
StringBuilder bldr = new StringBuilder(2048);
- String name = vector.getName();
- if (name != null && name.length() > 0) {
- bldr.append("Name: ").append(name).append(' ');
- }
bldr.append("elts: {");
Iterator<Vector.Element> iter = vector.iterateNonZero();
boolean first = true;
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/common/PartialVectorMergeReducer.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/common/PartialVectorMergeReducer.java?rev=936183&r1=936182&r2=936183&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/common/PartialVectorMergeReducer.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/common/PartialVectorMergeReducer.java Wed Apr 21 07:41:55 2010
@@ -36,9 +36,7 @@ import org.apache.mahout.math.VectorWrit
*/
public class PartialVectorMergeReducer extends MapReduceBase implements
Reducer<WritableComparable<?>,VectorWritable,WritableComparable<?>,VectorWritable> {
-
- private final VectorWritable vectorWritable = new VectorWritable();
-
+
private double normPower;
private int dimension;
private boolean sequentialAccess;
@@ -49,7 +47,7 @@ public class PartialVectorMergeReducer e
OutputCollector<WritableComparable<?>,VectorWritable> output,
Reporter reporter) throws IOException {
- Vector vector = new RandomAccessSparseVector(key.toString(), dimension, 10);
+ Vector vector = new RandomAccessSparseVector(dimension, 10);
while (values.hasNext()) {
VectorWritable value = values.next();
value.get().addTo(vector);
@@ -60,7 +58,7 @@ public class PartialVectorMergeReducer e
if (sequentialAccess) {
vector = new SequentialAccessSparseVector(vector);
}
- vectorWritable.set(vector);
+ VectorWritable vectorWritable = new VectorWritable(vector);
output.collect(key, vectorWritable);
}
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/lucene/LuceneIterable.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/lucene/LuceneIterable.java?rev=936183&r1=936182&r2=936183&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/lucene/LuceneIterable.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/lucene/LuceneIterable.java Wed Apr 21 07:41:55 2010
@@ -117,12 +117,6 @@ public class LuceneIterable implements I
if (result == null) {
return null;
}
- if (idField != null) {
- String id = indexReader.document(doc, idFieldSelector).get(idField);
- result.setName(id);
- } else {
- result.setName(String.valueOf(doc));
- }
if (normPower != NO_NORMALIZING) {
result = result.normalize(normPower);
}
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/text/term/TFPartialVectorReducer.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/text/term/TFPartialVectorReducer.java?rev=936183&r1=936182&r2=936183&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/text/term/TFPartialVectorReducer.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/text/term/TFPartialVectorReducer.java Wed Apr 21 07:41:55 2010
@@ -49,10 +49,9 @@ import org.apache.mahout.utils.vectors.t
*/
public class TFPartialVectorReducer extends MapReduceBase implements
Reducer<Text,StringTuple,Text,VectorWritable> {
+
private final OpenObjectIntHashMap<String> dictionary = new OpenObjectIntHashMap<String>();
-
- private final VectorWritable vectorWritable = new VectorWritable();
-
+
private int dimension;
private boolean sequentialAccess;
@@ -68,8 +67,7 @@ public class TFPartialVectorReducer exte
}
StringTuple value = values.next();
- Vector vector = new RandomAccessSparseVector(key.toString(), dimension, value.length()); // guess at
- // initial size
+ Vector vector = new RandomAccessSparseVector(dimension, value.length()); // guess at initial size
if (maxNGramSize >= 2) {
ShingleFilter sf = new ShingleFilter(new IteratorTokenStream(value.getEntries().iterator()),
@@ -104,7 +102,7 @@ public class TFPartialVectorReducer exte
}
// if the vector has no nonZero entries (nothing in the dictionary), let's not waste space sending it to disk.
if(vector.getNumNondefaultElements() > 0) {
- vectorWritable.set(vector);
+ VectorWritable vectorWritable = new VectorWritable(vector);
output.collect(key, vectorWritable);
} else {
reporter.incrCounter("TFParticalVectorReducer", "emptyVectorCount", 1);
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/tfidf/TFIDFPartialVectorReducer.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/tfidf/TFIDFPartialVectorReducer.java?rev=936183&r1=936182&r2=936183&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/tfidf/TFIDFPartialVectorReducer.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/tfidf/TFIDFPartialVectorReducer.java Wed Apr 21 07:41:55 2010
@@ -49,7 +49,6 @@ public class TFIDFPartialVectorReducer e
Reducer<WritableComparable<?>,VectorWritable,WritableComparable<?>,VectorWritable> {
private final OpenIntLongHashMap dictionary = new OpenIntLongHashMap();
- private final VectorWritable vectorWritable = new VectorWritable();
private final TFIDF tfidf = new TFIDF();
private int minDf = 1;
private int maxDfPercent = 99;
@@ -67,8 +66,7 @@ public class TFIDFPartialVectorReducer e
}
Vector value = values.next().get();
Iterator<Element> it = value.iterateNonZero();
- Vector vector = new RandomAccessSparseVector(key.toString(), (int) featureCount, value
- .getNumNondefaultElements());
+ Vector vector = new RandomAccessSparseVector((int) featureCount, value.getNumNondefaultElements());
while (it.hasNext()) {
Element e = it.next();
if (!dictionary.containsKey(e.index())) {
@@ -87,7 +85,7 @@ public class TFIDFPartialVectorReducer e
if (sequentialAccess) {
vector = new SequentialAccessSparseVector(vector);
}
- vectorWritable.set(vector);
+ VectorWritable vectorWritable = new VectorWritable(vector);
output.collect(key, vectorWritable);
}