mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gsing...@apache.org
Subject svn commit: r637664 [3/3] - in /lucene/mahout/trunk/src: main/java/org/apache/mahout/matrix/ test/java/org/apache/mahout/matrix/
Date Sun, 16 Mar 2008 21:24:51 GMT
Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseVector.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseVector.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseVector.java Sun Mar
16 14:24:48 2008
@@ -0,0 +1,367 @@
+package org.apache.mahout.matrix;
+
+import junit.framework.TestCase;
+/**
+ * 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.
+ */
+
+import junit.framework.TestCase;
+
+public class TestSparseVector extends TestCase {
+
+  double[] values = {1.1, 2.2, 3.3};
+
+  Vector test = new SparseVector(values.length + 2);
+
+  public TestSparseVector(String name) {
+    super(name);
+  }
+
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+    for (int i = 0; i < values.length; i++)
+      test.set(i + 1, values[i]);
+  }
+
+  public void testAsFormatString() {
+    String formatString = test.asWritableComparable().toString();
+    assertEquals("format", "[s5, 2:2.2, 1:1.1, 3:3.3, ] ", formatString);
+  }
+
+  public void testCardinality() {
+    assertEquals("cardinality", 5, test.cardinality());
+  }
+
+  public void testCopy() throws Exception {
+    Vector copy = test.copy();
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("copy [" + i + "]", test.get(i), copy.get(i));
+  }
+
+  public void testGet() throws Exception {
+    for (int i = 0; i < test.cardinality(); i++)
+      if (i > 0 && i < 4)
+        assertEquals("get [" + i + "]", values[i - 1], test.get(i));
+      else
+        assertEquals("get [" + i + "]", 0.0, test.get(i));
+  }
+
+  public void testGetOver() {
+    try {
+      test.get(test.cardinality());
+      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.cardinality(); i++)
+      if (i == 0 || i == 4)
+        assertEquals("get [" + i + "]", 0.0, test.get(i));
+      else if (i == 2)
+        assertEquals("set [" + i + "]", 4.5, test.get(i));
+      else
+        assertEquals("set [" + i + "]", values[i - 1], test.get(i));
+  }
+
+  public void testSize() throws Exception {
+    assertEquals("size", 3, test.size());
+  }
+
+  public void testToArray() throws Exception {
+    double[] val = test.toArray();
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("get [" + i + "]", val[i], test.get(i));
+  }
+
+  public void testViewPart() throws Exception {
+    Vector part = test.viewPart(1, 2);
+    assertEquals("part size", 2, part.size());
+    for (int i = 0; i < part.cardinality(); i++)
+      assertEquals("part[" + i + "]", values[i], 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, 5);
+      fail("no exception");
+    } catch (CardinalityException e) {
+      fail("wrong exception");
+    } catch (IndexException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testViewPartCardinality() {
+    try {
+      test.viewPart(1, 6);
+      fail("no exception");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    } catch (IndexException e) {
+      fail("wrong exception");
+    }
+  }
+
+  public void testDecodeFormat() throws Exception {
+    Vector val = SparseVector.decodeFormat(test.asWritableComparable());
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("get [" + i + "]", test.get(i), val.get(i));
+  }
+
+  public void testSparseDoubleVectorInt() throws Exception {
+    Vector val = new SparseVector(4);
+    assertEquals("cardinality", 4, val.cardinality());
+    for (int i = 0; i < 4; 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.cardinality() + 1));
+      fail("expected exception");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testNormalize() throws Exception {
+    Vector val = test.normalize();
+    double mag = Math.sqrt(1.1 * 1.1 + 2.2 * 2.2 + 3.3 * 3.3);
+    for (int i = 0; i < test.cardinality(); i++)
+      if (i == 0 || i == 4)
+        assertEquals("get [" + i + "]", 0.0, val.get(i));
+      else
+        assertEquals("dot", values[i - 1] / mag, val.get(i));
+  }
+
+  public void testMinus() throws Exception {
+    Vector val = test.minus(test);
+    assertEquals("cardinality", test.cardinality(), val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("get [" + i + "]", 0.0, val.get(i));
+  }
+
+  public void testPlusDouble() throws Exception {
+    Vector val = test.plus(1);
+    assertEquals("cardinality", test.cardinality(), val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      if (i == 0 || i == 4)
+        assertEquals("get [" + i + "]", 1.0, val.get(i));
+      else
+        assertEquals("get [" + i + "]", values[i - 1] + 1, val.get(i));
+  }
+
+  public void testPlusVector() throws Exception {
+    Vector val = test.plus(test);
+    assertEquals("cardinality", test.cardinality(), val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      if (i == 0 || i == 4)
+        assertEquals("get [" + i + "]", 0.0, val.get(i));
+      else
+        assertEquals("get [" + i + "]", values[i - 1] * 2, val.get(i));
+  }
+
+  public void testPlusVectorCardinality() {
+    try {
+      test.plus(new DenseVector(test.cardinality() + 1));
+      fail("expected exception");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testTimesDouble() throws Exception {
+    Vector val = test.times(3);
+    assertEquals("cardinality", test.cardinality(), val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      if (i == 0 || i == 4)
+        assertEquals("get [" + i + "]", 0.0, val.get(i));
+      else
+        assertEquals("get [" + i + "]", values[i - 1] * 3, val.get(i));
+  }
+
+  public void testDivideDouble() throws Exception {
+    Vector val = test.divide(3);
+    assertEquals("cardinality", test.cardinality(), val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      if (i == 0 || i == 4)
+        assertEquals("get [" + i + "]", 0.0, val.get(i));
+      else
+        assertEquals("get [" + i + "]", values[i - 1] / 3, val.get(i));
+  }
+
+  public void testTimesVector() throws Exception {
+    Vector val = test.times(test);
+    assertEquals("cardinality", test.cardinality(), val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      if (i == 0 || i == 4)
+        assertEquals("get [" + i + "]", 0.0, val.get(i));
+      else
+        assertEquals("get [" + i + "]", values[i - 1] * values[i - 1], val
+                .get(i));
+  }
+
+  public void testTimesVectorCardinality() {
+    try {
+      test.times(new DenseVector(test.cardinality() + 1));
+      fail("expected exception");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testZSum() {
+    double expected = 0;
+    for (int i = 0; i < values.length; i++)
+      expected += values[i];
+    assertEquals("wrong zSum", expected, test.zSum());
+  }
+
+  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.cardinality()];
+    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.cardinality() + 1];
+    try {
+      test.assign(array);
+      fail("cardinality exception expected");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testAssignVector() throws Exception {
+    Vector other = new DenseVector(test.cardinality());
+    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.cardinality() - 1);
+    try {
+      test.assign(other);
+      fail("cardinality exception expected");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testAssignUnaryFunction() {
+    test.assign(new NegateFunction());
+    for (int i = 0; i < values.length; i++)
+      if (i == 0 || i == 4)
+        assertEquals("get [" + i + "]", 0.0, test.getQuick(i));
+      else
+        assertEquals("value[" + i + "]", -values[i - 1], test.getQuick(i));
+  }
+
+  public void testAssignBinaryFunction() throws Exception {
+    test.assign(test, new PlusFunction());
+    for (int i = 0; i < values.length; i++)
+      if (i == 0 || i == 4)
+        assertEquals("get [" + i + "]", 0.0, test.get(i));
+      else
+        assertEquals("value[" + i + "]", 2 * values[i - 1], test.getQuick(i));
+  }
+
+  public void testAssignBinaryFunctionCardinality() {
+    try {
+      test.assign(test.like(2), new PlusFunction());
+      fail("Cardinality exception expected");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testThisHaveSharedCells() throws Exception {
+    assertTrue("test not shared?", test.haveSharedCells(test));
+  }
+
+  public void testViewHaveSharedCells() throws Exception {
+    Vector view = test.viewPart(1, 2);
+    assertTrue("view not shared?", view.haveSharedCells(test));
+    assertTrue("test not shared?", test.haveSharedCells(view));
+  }
+
+  public void testViewsHaveSharedCells() throws Exception {
+    Vector view1 = test.viewPart(0, 2);
+    Vector view2 = test.viewPart(1, 2);
+    assertTrue("view1 not shared?", view1.haveSharedCells(view2));
+    assertTrue("view2 not shared?", view2.haveSharedCells(view1));
+  }
+
+  public void testLike() {
+    Vector other = test.like();
+    assertTrue("not like", other instanceof SparseVector);
+    assertEquals("cardinality", test.cardinality(), other.cardinality());
+  }
+
+  public void testLikeN() {
+    Vector other = test.like(8);
+    assertTrue("not like", other instanceof SparseVector);
+    assertEquals("cardinality", 8, other.cardinality());
+  }
+
+  public void testCrossProduct() {
+    Matrix result = test.cross(test);
+    assertEquals("row cardinality", test.cardinality(), result.cardinality()[0]);
+    assertEquals("col cardinality", test.cardinality(), result.cardinality()[1]);
+    for (int row = 0; row < result.cardinality()[0]; row++)
+      for (int col = 0; col < result.cardinality()[1]; col++)
+        assertEquals("cross[" + row + "][" + col + "]", test.getQuick(row)
+                * test.getQuick(col), result.getQuick(row, col));
+  }
+}

Propchange: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseVector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestVectorView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestVectorView.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestVectorView.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestVectorView.java Sun Mar
16 14:24:48 2008
@@ -0,0 +1,318 @@
+package org.apache.mahout.matrix;
+/**
+ * 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.
+ */
+
+import junit.framework.TestCase;
+
+public class TestVectorView extends TestCase {
+
+  private static final int cardinality = 3;
+
+  private static final int offset = 1;
+
+  double[] values = {0.0, 1.1, 2.2, 3.3, 4.4, 5.5};
+
+  Vector test = new VectorView(new DenseVector(values), offset, cardinality);
+
+  public TestVectorView(String name) {
+    super(name);
+  }
+
+  public void testAsFormatString() {
+    String formatString = test.asWritableComparable().toString();
+    assertEquals("format", "[2.2, 3.3, 4.4, ] ", formatString);
+  }
+
+  public void testCardinality() {
+    assertEquals("cardinality", 3, test.cardinality());
+  }
+
+  public void testCopy() throws Exception {
+    Vector copy = test.copy();
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("copy [" + i + "]", test.get(i), copy.get(i));
+  }
+
+  public void testGet() throws Exception {
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("get [" + i + "]", values[i + offset], test.get(i));
+  }
+
+  public void testGetOver() {
+    try {
+      test.get(test.cardinality());
+      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.cardinality(); i++)
+      if (i == 2)
+        assertEquals("set [" + i + "]", 4.5, test.get(i));
+      else
+        assertEquals("set [" + i + "]", values[offset + i], test.get(i));
+  }
+
+  public void testSize() throws Exception {
+    assertEquals("size", 3, test.size());
+  }
+
+  public void testToArray() throws Exception {
+    double[] val = test.toArray();
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("get [" + i + "]", val[i], test.get(i));
+  }
+
+  public void testViewPart() throws Exception {
+    Vector part = test.viewPart(1, 2);
+    assertEquals("part size", 2, part.size());
+    for (int i = 0; i < part.cardinality(); i++)
+      assertEquals("part[" + i + "]", values[offset + i + 1], part.get(i));
+  }
+
+  public void testViewPartUnder() {
+    try {
+      test.viewPart(-1, cardinality);
+      fail("no exception");
+    } catch (CardinalityException e) {
+      fail("expected index exception");
+    } catch (IndexException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testViewPartOver() {
+    try {
+      test.viewPart(2, cardinality);
+      fail("no exception");
+    } catch (CardinalityException e) {
+      fail("expected index 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("expected cardinality exception");
+    }
+  }
+
+  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.cardinality() + 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.cardinality(); i++)
+      assertEquals("dot", values[offset + i] / mag, res.get(i));
+  }
+
+  public void testMinus() throws Exception {
+    Vector val = test.minus(test);
+    assertEquals("cardinality", 3, val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("get [" + i + "]", 0.0, val.get(i));
+  }
+
+  public void testPlusDouble() throws Exception {
+    Vector val = test.plus(1);
+    assertEquals("cardinality", 3, val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("get [" + i + "]", values[offset + i] + 1, val.get(i));
+  }
+
+  public void testPlusVector() throws Exception {
+    Vector val = test.plus(test);
+    assertEquals("cardinality", 3, val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("get [" + i + "]", values[offset + i] * 2, val.get(i));
+  }
+
+  public void testPlusVectorCardinality() {
+    try {
+      test.plus(new DenseVector(test.cardinality() + 1));
+      fail("expected exception");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testTimesDouble() throws Exception {
+    Vector val = test.times(3);
+    assertEquals("cardinality", 3, val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("get [" + i + "]", values[offset + i] * 3, val.get(i));
+  }
+
+  public void testDivideDouble() throws Exception {
+    Vector val = test.divide(3);
+    assertEquals("cardinality", 3, val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("get [" + i + "]", values[offset + i] / 3, val.get(i));
+  }
+
+  public void testTimesVector() throws Exception {
+    Vector val = test.times(test);
+    assertEquals("cardinality", 3, val.cardinality());
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("get [" + i + "]", values[offset + i] * values[offset + i],
+              val.get(i));
+  }
+
+  public void testTimesVectorCardinality() {
+    try {
+      test.times(new DenseVector(test.cardinality() + 1));
+      fail("expected exception");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testZSum() {
+    double expected = 0;
+    for (int i = offset; i < offset + cardinality; i++)
+      expected += values[i];
+    assertEquals("wrong zSum", expected, test.zSum());
+  }
+
+  public void testAssignDouble() {
+    test.assign(0);
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("value[" + i + "]", 0.0, test.getQuick(i));
+  }
+
+  public void testAssignDoubleArray() throws Exception {
+    double[] array = new double[test.cardinality()];
+    test.assign(array);
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("value[" + i + "]", 0.0, test.getQuick(i));
+  }
+
+  public void testAssignDoubleArrayCardinality() {
+    double[] array = new double[test.cardinality() + 1];
+    try {
+      test.assign(array);
+      fail("cardinality exception expected");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testAssignVector() throws Exception {
+    Vector other = new DenseVector(test.cardinality());
+    test.assign(other);
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("value[" + i + "]", 0.0, test.getQuick(i));
+  }
+
+  public void testAssignVectorCardinality() {
+    Vector other = new DenseVector(test.cardinality() - 1);
+    try {
+      test.assign(other);
+      fail("cardinality exception expected");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testAssignUnaryFunction() {
+    test.assign(new NegateFunction());
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("value[" + i + "]", -values[i + 1], test.getQuick(i));
+  }
+
+  public void testAssignBinaryFunction() throws Exception {
+    test.assign(test, new PlusFunction());
+    for (int i = 0; i < test.cardinality(); i++)
+      assertEquals("value[" + i + "]", 2 * values[i + 1], test.getQuick(i));
+  }
+
+  public void testAssignBinaryFunctionCardinality() {
+    try {
+      test.assign(test.like(2), new PlusFunction());
+      fail("Cardinality exception expected");
+    } catch (CardinalityException e) {
+      assertTrue(true);
+    }
+  }
+
+  public void testThisHaveSharedCells() throws Exception {
+    assertTrue("test not shared?", test.haveSharedCells(test));
+  }
+
+  public void testViewHaveSharedCells() throws Exception {
+    Vector view = test.viewPart(1, 2);
+    assertTrue("view not shared?", view.haveSharedCells(test));
+    assertTrue("test not shared?", test.haveSharedCells(view));
+  }
+
+  public void testViewsHaveSharedCells() throws Exception {
+    Vector view1 = test.viewPart(0, 2);
+    Vector view2 = test.viewPart(1, 2);
+    assertTrue("view1 not shared?", view1.haveSharedCells(view2));
+    assertTrue("view2 not shared?", view2.haveSharedCells(view1));
+  }
+
+  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("cardinality", 5, other.cardinality());
+  }
+
+  public void testCrossProduct() {
+    Matrix result = test.cross(test);
+    assertEquals("row cardinality", test.cardinality(), result.cardinality()[0]);
+    assertEquals("col cardinality", test.cardinality(), result.cardinality()[1]);
+    for (int row = 0; row < result.cardinality()[0]; row++)
+      for (int col = 0; col < result.cardinality()[1]; col++)
+        assertEquals("cross[" + row + "][" + col + "]", test.getQuick(row)
+                * test.getQuick(col), result.getQuick(row, col));
+  }
+}

Propchange: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestVectorView.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/VectorTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/VectorTest.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/VectorTest.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/VectorTest.java Sun Mar 16
14:24:48 2008
@@ -0,0 +1,68 @@
+package org.apache.mahout.matrix;
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+import junit.framework.TestCase;
+
+public class VectorTest extends TestCase {
+
+
+  public VectorTest(String s) {
+    super(s);
+  }
+
+  protected void setUp() {
+  }
+
+  protected void tearDown() {
+
+  }
+
+  public void testSparseVector() throws Exception {
+    SparseVector vec1 = new SparseVector(3);
+    SparseVector vec2 = new SparseVector(3);
+    testVectors(vec1, vec2);
+
+  }
+
+  private void testVectors(Vector left, Vector right) throws CardinalityException, IndexException
{
+    left.setQuick(0, 1);
+    left.setQuick(1, 2);
+    left.setQuick(2, 3);
+    right.setQuick(0, 4);
+    right.setQuick(1, 5);
+    right.setQuick(2, 6);
+    double result = left.dot(right);
+    assertTrue(result + " does not equal: " + 32, result == 32);
+  }
+
+
+  public void testDenseVector() throws Exception {
+    DenseVector vec1 = new DenseVector(3);
+    DenseVector vec2 = new DenseVector(3);
+    testVectors(vec1, vec2);
+
+  }
+
+  public void testVectorView() throws Exception {
+    SparseVector vec1 = new SparseVector(3);
+    SparseVector vec2 = new SparseVector(6);
+    VectorView vecV1 = new VectorView(vec1, 0, 3);
+    VectorView vecV2 = new VectorView(vec2, 2, 3);
+    testVectors(vecV1, vecV2);
+  }
+}
\ No newline at end of file

Propchange: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/VectorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message