You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by gs...@apache.org on 2009/12/18 00:22:41 UTC
svn commit: r891983 [46/47] - in /lucene/mahout/trunk: ./ core/
core/src/main/java/org/apache/mahout/cf/taste/hadoop/item/
core/src/main/java/org/apache/mahout/clustering/
core/src/main/java/org/apache/mahout/clustering/canopy/
core/src/main/java/org/a...
Added: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestMatrixView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestMatrixView.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestMatrixView.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestMatrixView.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,602 @@
+/**
+ * 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;
+
+import junit.framework.TestCase;
+import org.apache.hadoop.io.DataOutputBuffer;
+import org.apache.mahout.math.AbstractMatrix;
+import org.apache.mahout.math.CardinalityException;
+import org.apache.mahout.math.DenseMatrix;
+import org.apache.mahout.math.DenseVector;
+import org.apache.mahout.math.IndexException;
+import org.apache.mahout.math.Matrix;
+import org.apache.mahout.math.MatrixView;
+import org.apache.mahout.math.NegateFunction;
+import org.apache.mahout.math.PlusFunction;
+import org.apache.mahout.math.UnboundLabelException;
+import org.apache.mahout.math.Vector;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+public class TestMatrixView extends TestCase {
+
+ private static final int ROW = AbstractMatrix.ROW;
+
+ private static final int COL = AbstractMatrix.COL;
+
+ private final double[][] values = {{0.0, 1.1, 2.2}, {1.1, 2.2, 3.3},
+ {3.3, 4.4, 5.5}, {5.5, 6.6, 7.7}, {7.7, 8.8, 9.9}};
+
+ private Matrix test;
+
+ public TestMatrixView(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ int[] offset = {1, 1};
+ int[] card = {3, 2};
+ test = new MatrixView(new DenseMatrix(values), offset, card);
+ }
+
+ public void testAsFormatString() {
+ String string = test.asFormatString();
+ Matrix m = AbstractMatrix.decodeMatrix(string);
+ int[] c = m.size();
+ assertEquals("row cardinality", values.length - 2, c[ROW]);
+ assertEquals("col cardinality", values[0].length - 1, c[COL]);
+ }
+
+ public void testCardinality() {
+ int[] c = test.size();
+ assertEquals("row cardinality", values.length - 2, c[ROW]);
+ assertEquals("col cardinality", values[0].length - 1, c[COL]);
+ }
+
+ public void testCopy() {
+ int[] c = test.size();
+ Matrix copy = test.clone();
+ assertTrue("wrong class", copy instanceof MatrixView);
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ test.getQuick(row, col), copy.getQuick(row, col));
+ }
+ }
+ }
+
+ public void testGetQuick() {
+ int[] c = test.size();
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ values[row + 1][col + 1], test.getQuick(row, col));
+ }
+ }
+ }
+
+ public void testHaveSharedCells() {
+ assertTrue("same", test.haveSharedCells(test));
+ assertFalse("different", test.haveSharedCells(test.clone()));
+ }
+
+ public void testLike() {
+ Matrix like = test.like();
+ assertTrue("type", like instanceof DenseMatrix);
+ assertEquals("rows", test.size()[ROW], like.size()[ROW]);
+ assertEquals("columns", test.size()[COL], like.size()[COL]);
+ }
+
+ public void testLikeIntInt() {
+ Matrix like = test.like(4, 4);
+ assertTrue("type", like instanceof DenseMatrix);
+ assertEquals("rows", 4, like.size()[ROW]);
+ assertEquals("columns", 4, like.size()[COL]);
+ }
+
+ public void testSetQuick() {
+ int[] c = test.size();
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ test.setQuick(row, col, 1.23);
+ assertEquals("value[" + row + "][" + col + ']', 1.23, test.getQuick(
+ row, col));
+ }
+ }
+ }
+
+ public void testSize() {
+ int[] c = test.getNumNondefaultElements();
+ assertEquals("row size", values.length - 2, c[ROW]);
+ assertEquals("col size", values[0].length - 1, c[COL]);
+ }
+
+ public void testViewPart() throws Exception {
+ int[] offset = {1, 1};
+ int[] size = {2, 1};
+ Matrix view = test.viewPart(offset, size);
+ int[] c = view.size();
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ values[row + 2][col + 2], view.getQuick(row, col));
+ }
+ }
+ }
+
+ public void testViewPartCardinality() {
+ int[] offset = {1, 1};
+ int[] size = {3, 3};
+ try {
+ test.viewPart(offset, size);
+ fail("exception expected");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ } catch (IndexException e) {
+ fail("cardinality exception expected");
+ }
+ }
+
+ public void testViewPartIndexOver() {
+ int[] offset = {1, 1};
+ int[] size = {2, 2};
+ try {
+ test.viewPart(offset, size);
+ fail("exception expected");
+ } catch (CardinalityException e) {
+ fail("index exception expected");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testViewPartIndexUnder() {
+ int[] offset = {-1, -1};
+ int[] size = {2, 2};
+ try {
+ test.viewPart(offset, size);
+ fail("exception expected");
+ } catch (CardinalityException e) {
+ fail("index exception expected");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testAssignDouble() {
+ int[] c = test.size();
+ test.assign(4.53);
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']', 4.53, test.getQuick(
+ row, col));
+ }
+ }
+ }
+
+ public void testAssignDoubleArrayArray() throws Exception {
+ int[] c = test.size();
+ test.assign(new double[3][2]);
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']', 0.0, test.getQuick(row,
+ col));
+ }
+ }
+ }
+
+ public void testAssignDoubleArrayArrayCardinality() {
+ int[] c = test.size();
+ try {
+ test.assign(new double[c[ROW] + 1][c[COL]]);
+ fail("exception expected");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testAssignMatrixBinaryFunction() throws Exception {
+ int[] c = test.size();
+ test.assign(test, new PlusFunction());
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ 2 * values[row + 1][col + 1], test.getQuick(row, col));
+ }
+ }
+ }
+
+ public void testAssignMatrixBinaryFunctionCardinality() {
+ try {
+ test.assign(test.transpose(), new PlusFunction());
+ fail("exception expected");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testAssignMatrix() throws Exception {
+ int[] c = test.size();
+ Matrix value = test.like();
+ value.assign(test);
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ test.getQuick(row, col), value.getQuick(row, col));
+ }
+ }
+ }
+
+ public void testAssignMatrixCardinality() {
+ try {
+ test.assign(test.transpose());
+ fail("exception expected");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testAssignUnaryFunction() {
+ int[] c = test.size();
+ test.assign(new NegateFunction());
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ -values[row + 1][col + 1], test.getQuick(row, col));
+ }
+ }
+ }
+
+ public void testDivide() {
+ int[] c = test.size();
+ Matrix value = test.divide(4.53);
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ values[row + 1][col + 1] / 4.53, value.getQuick(row, col));
+ }
+ }
+ }
+
+ public void testGet() throws Exception {
+ int[] c = test.size();
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ values[row + 1][col + 1], test.get(row, col));
+ }
+ }
+ }
+
+ public void testGetIndexUnder() {
+ int[] c = test.size();
+ try {
+ for (int row = -1; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ test.get(row, col);
+ }
+ }
+ fail("index exception expected");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testGetIndexOver() {
+ int[] c = test.size();
+ try {
+ for (int row = 0; row < c[ROW] + 1; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ test.get(row, col);
+ }
+ }
+ fail("index exception expected");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testMinus() throws Exception {
+ int[] c = test.size();
+ Matrix value = test.minus(test);
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']', 0.0, value.getQuick(
+ row, col));
+ }
+ }
+ }
+
+ public void testMinusCardinality() {
+ try {
+ test.minus(test.transpose());
+ fail("cardinality exception expected");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testPlusDouble() {
+ int[] c = test.size();
+ Matrix value = test.plus(4.53);
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ values[row + 1][col + 1] + 4.53, value.getQuick(row, col));
+ }
+ }
+ }
+
+ public void testPlusMatrix() throws Exception {
+ int[] c = test.size();
+ Matrix value = test.plus(test);
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ values[row + 1][col + 1] * 2, value.getQuick(row, col));
+ }
+ }
+ }
+
+ public void testPlusMatrixCardinality() {
+ try {
+ test.plus(test.transpose());
+ fail("cardinality exception expected");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testSetUnder() {
+ int[] c = test.size();
+ try {
+ for (int row = -1; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ test.set(row, col, 1.23);
+ }
+ }
+ fail("index exception expected");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testSetOver() {
+ int[] c = test.size();
+ try {
+ for (int row = 0; row < c[ROW] + 1; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ test.set(row, col, 1.23);
+ }
+ }
+ fail("index exception expected");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testTimesDouble() {
+ int[] c = test.size();
+ Matrix value = test.times(4.53);
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ values[row + 1][col + 1] * 4.53, value.getQuick(row, col));
+ }
+ }
+ }
+
+ public void testTimesMatrix() throws Exception {
+ int[] c = test.size();
+ Matrix transpose = test.transpose();
+ Matrix value = test.times(transpose);
+ int[] v = value.size();
+ assertEquals("rows", c[ROW], v[ROW]);
+ assertEquals("cols", c[ROW], v[COL]);
+ // TODO: check the math too, lazy
+ }
+
+ public void testTimesMatrixCardinality() {
+ Matrix other = test.like(5, 8);
+ try {
+ test.times(other);
+ fail("cardinality exception expected");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testTranspose() {
+ int[] c = test.size();
+ Matrix transpose = test.transpose();
+ int[] t = transpose.size();
+ assertEquals("rows", c[COL], t[ROW]);
+ assertEquals("cols", c[ROW], t[COL]);
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
+ assertEquals("value[" + row + "][" + col + ']',
+ test.getQuick(row, col), transpose.getQuick(col, row));
+ }
+ }
+ }
+
+ public void testZSum() {
+ double sum = test.zSum();
+ assertEquals("zsum", 29.7, sum);
+ }
+
+ public void testAssignRow() throws Exception {
+ double[] data = {2.1, 3.2};
+ test.assignRow(1, new DenseVector(data));
+ assertEquals("test[1][0]", 2.1, test.getQuick(1, 0));
+ assertEquals("test[1][1]", 3.2, test.getQuick(1, 1));
+ }
+
+ public void testAssignRowCardinality() {
+ double[] data = {2.1, 3.2, 4.3};
+ try {
+ test.assignRow(1, new DenseVector(data));
+ fail("expecting cardinality exception");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testAssignColumn() throws Exception {
+ double[] data = {2.1, 3.2, 4.3};
+ test.assignColumn(1, new DenseVector(data));
+ assertEquals("test[0][1]", 2.1, test.getQuick(0, 1));
+ assertEquals("test[1][1]", 3.2, test.getQuick(1, 1));
+ assertEquals("test[2][1]", 4.3, test.getQuick(2, 1));
+ }
+
+ public void testAssignColumnCardinality() {
+ double[] data = {2.1, 3.2};
+ try {
+ test.assignColumn(1, new DenseVector(data));
+ fail("expecting cardinality exception");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testGetRow() throws Exception {
+ Vector row = test.getRow(1);
+ assertEquals("row size", 2, row.getNumNondefaultElements());
+ }
+
+ public void testGetRowIndexUnder() {
+ try {
+ test.getRow(-1);
+ fail("expecting index exception");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testGetRowIndexOver() {
+ try {
+ test.getRow(5);
+ fail("expecting index exception");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testGetColumn() throws Exception {
+ Vector column = test.getColumn(1);
+ assertEquals("row size", 3, column.getNumNondefaultElements());
+ }
+
+ public void testGetColumnIndexUnder() {
+ try {
+ test.getColumn(-1);
+ fail("expecting index exception");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testGetColumnIndexOver() {
+ try {
+ test.getColumn(5);
+ fail("expecting index exception");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testMatrixWritable() throws IOException {
+ DataOutputBuffer out = new DataOutputBuffer();
+ test.write(out);
+ out.close();
+
+ DataInputStream in = new DataInputStream(new ByteArrayInputStream(out
+ .getData()));
+ Matrix m2 = test.clone();
+ m2.readFields(in);
+ in.close();
+ assertEquals("row size", test.size()[ROW], m2.size()[ROW]);
+ assertEquals("col size", test.size()[COL], m2.size()[COL]);
+ }
+
+ public void testLabelBindings() {
+ assertNull("row bindings", test.getRowLabelBindings());
+ assertNull("col bindings", test.getColumnLabelBindings());
+ Map<String, Integer> rowBindings = new HashMap<String, Integer>();
+ rowBindings.put("Fee", 0);
+ rowBindings.put("Fie", 1);
+ test.setRowLabelBindings(rowBindings);
+ assertEquals("row", rowBindings, test.getRowLabelBindings());
+ Map<String, Integer> colBindings = new HashMap<String, Integer>();
+ colBindings.put("Foo", 0);
+ colBindings.put("Bar", 1);
+ test.setColumnLabelBindings(colBindings);
+ assertEquals("row", rowBindings, test.getRowLabelBindings());
+ assertEquals("Fee", test.get(0, 1), test.get("Fee", "Bar"));
+
+ double[] newrow = {9, 8};
+ test.set("Fie", newrow);
+ assertEquals("FeeBar", test.get(0, 1), test.get("Fee", "Bar"));
+ }
+
+ public void testSettingLabelBindings() {
+ assertNull("row bindings", test.getRowLabelBindings());
+ assertNull("col bindings", test.getColumnLabelBindings());
+ test.set("Fee", "Foo", 1, 1, 9);
+ assertNotNull("row", test.getRowLabelBindings());
+ assertNotNull("row", test.getRowLabelBindings());
+ assertEquals("Fee", 1, test.getRowLabelBindings().get("Fee").intValue());
+ assertEquals("Foo", 1, test.getColumnLabelBindings().get("Foo").intValue());
+ assertEquals("FeeFoo", test.get(1, 1), test.get("Fee", "Foo"));
+ try {
+ test.get("Fie", "Foe");
+ fail("Expected UnboundLabelException");
+ } catch (IndexException e) {
+ fail("Expected UnboundLabelException");
+ } catch (UnboundLabelException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testLabelBindingSerialization() {
+ assertNull("row bindings", test.getRowLabelBindings());
+ assertNull("col bindings", test.getColumnLabelBindings());
+ Map<String, Integer> rowBindings = new HashMap<String, Integer>();
+ rowBindings.put("Fee", 0);
+ rowBindings.put("Fie", 1);
+ rowBindings.put("Foe", 2);
+ test.setRowLabelBindings(rowBindings);
+ assertEquals("row", rowBindings, test.getRowLabelBindings());
+ Map<String, Integer> colBindings = new HashMap<String, Integer>();
+ colBindings.put("Foo", 0);
+ colBindings.put("Bar", 1);
+ colBindings.put("Baz", 2);
+ test.setColumnLabelBindings(colBindings);
+ String json = test.asFormatString();
+ Matrix mm = AbstractMatrix.decodeMatrix(json);
+ assertEquals("Fee", test.get(0, 1), mm.get("Fee", "Bar"));
+ }
+
+}
Propchange: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestMatrixView.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestOrderedIntDoubleMapping.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestOrderedIntDoubleMapping.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestOrderedIntDoubleMapping.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestOrderedIntDoubleMapping.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,71 @@
+/**
+ * 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;
+
+import org.apache.mahout.math.OrderedIntDoubleMapping;
+
+import junit.framework.TestCase;
+
+public class TestOrderedIntDoubleMapping extends TestCase {
+
+ public void testGetSet() {
+
+ OrderedIntDoubleMapping mapping = new OrderedIntDoubleMapping(1);
+
+ assertEquals(0, mapping.getNumMappings());
+ assertEquals(0.0, mapping.get(0));
+ assertEquals(0.0, mapping.get(1));
+
+ mapping.set(0, 1.1);
+ assertEquals(1, mapping.getNumMappings());
+ assertEquals(1.1, mapping.get(0));
+ assertEquals(0.0, mapping.get(1));
+
+ mapping.set(5, 6.6);
+ assertEquals(2, mapping.getNumMappings());
+ assertEquals(1.1, mapping.get(0));
+ assertEquals(0.0, mapping.get(1));
+ assertEquals(6.6, mapping.get(5));
+ assertEquals(0.0, mapping.get(6));
+
+ mapping.set(0, 0.0);
+ assertEquals(1, mapping.getNumMappings());
+ assertEquals(0.0, mapping.get(0));
+ assertEquals(0.0, mapping.get(1));
+ assertEquals(6.6, mapping.get(5));
+
+ mapping.set(5, 0.0);
+ assertEquals(0, mapping.getNumMappings());
+ assertEquals(0.0, mapping.get(0));
+ assertEquals(0.0, mapping.get(1));
+ assertEquals(0.0, mapping.get(5));
+ }
+
+ public void testClone() throws Exception {
+ OrderedIntDoubleMapping mapping = new OrderedIntDoubleMapping(1);
+ mapping.set(0, 1.1);
+ mapping.set(5, 6.6);
+ OrderedIntDoubleMapping clone = mapping.clone();
+ assertEquals(2, clone.getNumMappings());
+ assertEquals(1.1, clone.get(0));
+ assertEquals(0.0, clone.get(1));
+ assertEquals(6.6, clone.get(5));
+ assertEquals(0.0, clone.get(6));
+ }
+
+}
Propchange: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestOrderedIntDoubleMapping.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseColumnMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseColumnMatrix.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseColumnMatrix.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseColumnMatrix.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,41 @@
+/**
+ * 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;
+
+import org.apache.mahout.math.Matrix;
+import org.apache.mahout.math.SparseColumnMatrix;
+
+public class TestSparseColumnMatrix extends MatrixTest {
+
+ public TestSparseColumnMatrix(String name) {
+ super(name);
+ }
+
+ @Override
+ public Matrix matrixFactory(double[][] values) {
+ int[] cardinality = {values.length, values[0].length};
+ Matrix matrix = new SparseColumnMatrix(cardinality);
+ for (int row = 0; row < cardinality[ROW]; row++) {
+ for (int col = 0; col < cardinality[COL]; col++) {
+ matrix.setQuick(row, col, values[row][col]);
+ }
+ }
+ return matrix;
+ }
+
+}
Propchange: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseColumnMatrix.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseMatrix.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseMatrix.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseMatrix.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,41 @@
+/**
+ * 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;
+
+import org.apache.mahout.math.Matrix;
+import org.apache.mahout.math.SparseMatrix;
+
+public class TestSparseMatrix extends MatrixTest {
+
+ public TestSparseMatrix(String name) {
+ super(name);
+ }
+
+ @Override
+ public Matrix matrixFactory(double[][] values) {
+ int[] cardinality = {values.length, values[0].length};
+ Matrix matrix = new SparseMatrix(cardinality);
+ for (int row = 0; row < cardinality[ROW]; row++) {
+ for (int col = 0; col < cardinality[COL]; col++) {
+ matrix.setQuick(row, col, values[row][col]);
+ }
+ }
+ return matrix;
+ }
+
+}
Propchange: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseMatrix.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseRowMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseRowMatrix.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseRowMatrix.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseRowMatrix.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,42 @@
+/**
+ * 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;
+
+import org.apache.mahout.math.Matrix;
+import org.apache.mahout.math.SparseRowMatrix;
+
+public class TestSparseRowMatrix extends MatrixTest {
+
+ public TestSparseRowMatrix(String name) {
+ super(name);
+ }
+
+ @Override
+ public Matrix matrixFactory(double[][] values) {
+ int[] cardinality = {values.length, values[0].length};
+ Matrix matrix = new SparseRowMatrix(cardinality);
+ for (int row = 0; row < cardinality[ROW]; row++) {
+ for (int col = 0; col < cardinality[COL]; col++) {
+ matrix.setQuick(row, col, values[row][col]);
+ }
+ }
+ return matrix;
+ }
+
+
+}
Propchange: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseRowMatrix.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseVector.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseVector.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseVector.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,477 @@
+/**
+ * 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;
+
+import junit.framework.TestCase;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.mahout.math.AbstractVector;
+import org.apache.mahout.math.CardinalityException;
+import org.apache.mahout.math.DenseVector;
+import org.apache.mahout.math.IndexException;
+import org.apache.mahout.math.Matrix;
+import org.apache.mahout.math.NegateFunction;
+import org.apache.mahout.math.PlusFunction;
+import org.apache.mahout.math.SparseVector;
+import org.apache.mahout.math.TimesFunction;
+import org.apache.mahout.math.Vector;
+
+public class TestSparseVector extends TestCase {
+
+ final double[] values = {1.1, 2.2, 3.3};
+ final double[] gold = {0, 1.1, 2.2, 3.3, 0};
+ final 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.asFormatString();
+ Vector vec = AbstractVector.decodeVector(formatString);
+ assertEquals(vec, test);
+ }
+
+ public void testCardinality() {
+ assertEquals("size", 5, test.size());
+ }
+
+ public void testIterator() throws Exception {
+ Iterator<Vector.Element> iterator = test.iterateNonZero();
+ checkIterator(iterator, gold);
+
+ iterator = test.iterateAll();
+ checkIterator(iterator, gold);
+
+ double[] doubles = {0.0, 5.0, 0, 3.0};
+ SparseVector zeros = new SparseVector(doubles.length);
+ for (int i = 0; i < doubles.length; i++) {
+ zeros.setQuick(i, doubles[i]);
+ }
+ iterator = zeros.iterateNonZero();
+ checkIterator(iterator, doubles);
+ iterator = zeros.iterateAll();
+ checkIterator(iterator, doubles);
+
+ doubles = new double[]{0.0, 0.0, 0, 0.0};
+ zeros = new SparseVector(doubles.length);
+ for (int i = 0; i < doubles.length; i++) {
+ zeros.setQuick(i, doubles[i]);
+ }
+ iterator = zeros.iterateNonZero();
+ checkIterator(iterator, doubles);
+ iterator = zeros.iterateAll();
+ checkIterator(iterator, doubles);
+
+ }
+
+ private static void checkIterator(Iterator<Vector.Element> nzIter, double[] values) {
+ 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);
+ }
+ }
+
+ 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++) {
+ 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.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 == 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.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], 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 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));
+ }
+ }
+
+ public void testSparseDoubleVectorInt() throws Exception {
+ Vector val = new SparseVector(4);
+ assertEquals("size", 4, val.size());
+ 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);
+ double expected = 3.3 * 3.3 + 2.2 * 2.2 + 1.1 * 1.1;
+ assertEquals("dot", expected, 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 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.size(); 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("size", test.size(), val.size());
+ for (int i = 0; i < test.size(); i++) {
+ assertEquals("get [" + i + ']', 0.0, val.get(i));
+ }
+ }
+
+ public void testPlusDouble() throws Exception {
+ Vector val = test.plus(1);
+ assertEquals("size", test.size(), val.size());
+ for (int i = 0; i < test.size(); 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("size", test.size(), val.size());
+ for (int i = 0; i < test.size(); 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.size() + 1));
+ fail("expected exception");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testTimesDouble() throws Exception {
+ Vector val = test.times(3);
+ assertEquals("size", test.size(), val.size());
+ for (int i = 0; i < test.size(); 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("size", test.size(), val.size());
+ for (int i = 0; i < test.size(); 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("size", test.size(), val.size());
+ for (int i = 0; i < test.size(); 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.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 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(new NegateFunction());
+ for (int i = 0; i < values.length; i++) {
+ assertEquals("value[" + i + ']', -values[i], test.getQuick(i+1));
+ }
+ }
+
+ 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 testAssignBinaryFunction2() throws Exception {
+ test.assign(new PlusFunction(), 4);
+ for (int i = 0; i < values.length; i++) {
+ if (i == 0 || i == 4) {
+ assertEquals("get [" + i + ']', 4.0, test.get(i));
+ } else {
+ assertEquals("value[" + i + ']', values[i - 1] + 4, test.getQuick(i));
+ }
+ }
+ }
+
+ public void testAssignBinaryFunction3() throws Exception {
+ test.assign(new TimesFunction(), 4);
+ 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 + ']', values[i - 1] * 4, 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("size", test.size(), other.size());
+ }
+
+ public void testLikeN() {
+ Vector other = test.like(8);
+ assertTrue("not like", other instanceof SparseVector);
+ assertEquals("size", 8, 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"));
+ }
+}
Propchange: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseVector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: 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=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,391 @@
+/**
+ * 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;
+
+import junit.framework.TestCase;
+
+import java.util.Iterator;
+
+import org.apache.mahout.math.AbstractVector;
+import org.apache.mahout.math.CardinalityException;
+import org.apache.mahout.math.DenseVector;
+import org.apache.mahout.math.IndexException;
+import org.apache.mahout.math.Matrix;
+import org.apache.mahout.math.NegateFunction;
+import org.apache.mahout.math.PlusFunction;
+import org.apache.mahout.math.TimesFunction;
+import org.apache.mahout.math.Vector;
+import org.apache.mahout.math.VectorView;
+
+public class TestVectorView extends TestCase {
+
+ private static final int cardinality = 3;
+
+ private static final int offset = 1;
+
+ final double[] values = {0.0, 1.1, 2.2, 3.3, 4.4, 5.5};
+
+ final Vector test = new VectorView(new DenseVector(values), offset,
+ cardinality);
+
+ public TestVectorView(String name) {
+ super(name);
+ }
+
+ public void testAsFormatString() {
+ String formatString = test.asFormatString();
+ Vector v = AbstractVector.decodeVector(formatString);
+ assertEquals("size", test.size(), v.size());
+ }
+
+ public void testCardinality() {
+ assertEquals("size", 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 + offset], test.get(i));
+ }
+ }
+
+ public void testGetOver() {
+ try {
+ test.get(test.size());
+ fail("expected exception");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testIterator() throws Exception {
+
+ VectorView view = new VectorView(new DenseVector(values), offset, cardinality);
+ double[] gold = {1.1, 2.2, 3.3};
+ Iterator<Vector.Element> iter = view.iterateAll();
+ 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();
+ checkIterator(iter, gold);
+ gold = new double[]{1.1, 2.2};
+ iter = view.iterateNonZero();
+ checkIterator(iter, gold);
+
+ }
+
+ private static void checkIterator(Iterator<Vector.Element> iter, double[] gold) {
+ int i = 0;
+ while (iter.hasNext()) {
+ Vector.Element elt = iter.next();
+ assertEquals((elt.index()) + " Value: " + gold[i]
+ + " does not equal: " + elt.get(), gold[i], elt.get(), 0.0);
+ i++;
+ }
+ }
+
+ 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[offset + i], test.get(i));
+ }
+ }
+ }
+
+ 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[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.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[offset + i] / mag, res.get(i));
+ }
+ }
+
+ public void testMinus() throws Exception {
+ Vector val = test.minus(test);
+ assertEquals("size", 3, val.size());
+ for (int i = 0; i < test.size(); i++) {
+ assertEquals("get [" + i + ']', 0.0, val.get(i));
+ }
+ }
+
+ public void testPlusDouble() throws Exception {
+ Vector val = test.plus(1);
+ assertEquals("size", 3, val.size());
+ for (int i = 0; i < test.size(); i++) {
+ assertEquals("get [" + i + ']', values[offset + i] + 1, val.get(i));
+ }
+ }
+
+ public void testPlusVector() throws Exception {
+ Vector val = test.plus(test);
+ assertEquals("size", 3, val.size());
+ for (int i = 0; i < test.size(); i++) {
+ assertEquals("get [" + i + ']', values[offset + 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("size", 3, val.size());
+ for (int i = 0; i < test.size(); i++) {
+ assertEquals("get [" + i + ']', values[offset + i] * 3, val.get(i));
+ }
+ }
+
+ public void testDivideDouble() throws Exception {
+ Vector val = test.divide(3);
+ assertEquals("size", 3, val.size());
+ for (int i = 0; i < test.size(); i++) {
+ assertEquals("get [" + i + ']', values[offset + i] / 3, val.get(i));
+ }
+ }
+
+ public void testTimesVector() throws Exception {
+ Vector val = test.times(test);
+ assertEquals("size", 3, val.size());
+ for (int i = 0; i < test.size(); i++) {
+ assertEquals("get [" + i + ']', values[offset + i] * values[offset + 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 (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.size(); 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 < test.size(); 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 < test.size(); 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(new NegateFunction());
+ for (int i = 0; i < test.size(); 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.size(); i++) {
+ assertEquals("value[" + i + ']', 2 * values[i + 1], test.getQuick(i));
+ }
+ }
+
+ public void testAssignBinaryFunction2() throws Exception {
+ test.assign(new PlusFunction(), 4);
+ for (int i = 0; i < test.size(); i++) {
+ assertEquals("value[" + i + ']', values[i + 1] + 4, test.getQuick(i));
+ }
+ }
+
+ public void testAssignBinaryFunction3() throws Exception {
+ test.assign(new TimesFunction(), 4);
+ for (int i = 0; i < test.size(); i++) {
+ assertEquals("value[" + i + ']', values[i + 1] * 4, 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("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));
+ }
+ }
+ }
+}
Propchange: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorWritable.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorWritable.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorWritable.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorWritable.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,68 @@
+/**
+ * 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;
+
+import junit.framework.TestCase;
+import org.apache.hadoop.io.DataOutputBuffer;
+import org.apache.mahout.math.DenseVector;
+import org.apache.mahout.math.SparseVector;
+import org.apache.mahout.math.Vector;
+import org.apache.mahout.math.VectorView;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+
+public class TestVectorWritable extends TestCase {
+
+ private static final int cardinality = 10;
+
+ private static void doTest(Vector writable) throws Exception {
+ for (int i = 0; i < cardinality; i++) {
+ writable.set(i, i);
+ }
+ DataOutputBuffer out = new DataOutputBuffer();
+ writable.write(out);
+ out.close();
+
+ DataInputStream in = new DataInputStream(new ByteArrayInputStream(out
+ .getData()));
+ writable.readFields(in);
+ in.close();
+
+ assertEquals(cardinality, writable.size());
+ for (int i = 0; i < cardinality; i++) {
+ assertEquals((double) i, writable.get(i));
+ }
+
+ in = new DataInputStream(new ByteArrayInputStream(out.getData()));
+ writable.readFields(in);
+ in.close();
+
+ assertEquals(cardinality, writable.size());
+ for (int i = 0; i < cardinality; i++) {
+ assertEquals((double) i, writable.get(i));
+ }
+ }
+
+ public void testVectors() throws Exception {
+ doTest(new SparseVector(cardinality));
+ doTest(new DenseVector(cardinality));
+ doTest(new VectorView(new SparseVector(cardinality + 1), 1, cardinality));
+ doTest(new VectorView(new DenseVector(cardinality + 1), 1, cardinality));
+ }
+}
Propchange: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorWritable.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: 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=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,466 @@
+/**
+ * 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;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.reflect.TypeToken;
+import junit.framework.TestCase;
+
+import java.lang.reflect.Type;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Random;
+
+import org.apache.mahout.math.AbstractVector;
+import org.apache.mahout.math.DenseVector;
+import org.apache.mahout.math.IndexException;
+import org.apache.mahout.math.JsonVectorAdapter;
+import org.apache.mahout.math.SparseVector;
+import org.apache.mahout.math.UnboundLabelException;
+import org.apache.mahout.math.Vector;
+import org.apache.mahout.math.VectorView;
+
+public class VectorTest extends TestCase {
+
+ public VectorTest(String s) {
+ super(s);
+ }
+
+ public void testSparseVector() throws Exception {
+ SparseVector vec1 = new SparseVector(3);
+ SparseVector vec2 = new SparseVector(3);
+ doTestVectors(vec1, vec2);
+ }
+
+ public void testEquivalent() throws Exception {
+ //names are not used for equivalent
+ SparseVector left = new SparseVector("foo", 3);
+ DenseVector right = new DenseVector("foo", 3);
+ left.setQuick(0, 1);
+ left.setQuick(1, 2);
+ left.setQuick(2, 3);
+ right.setQuick(0, 1);
+ right.setQuick(1, 2);
+ right.setQuick(2, 3);
+ assertTrue("equivalent didn't work", AbstractVector.equivalent(left, right));
+ assertEquals("equals didn't work", left, right);
+ assertEquals("equivalent didn't work", false, AbstractVector.strictEquivalence(left, right));
+
+ DenseVector leftBar = new DenseVector("bar", 3);
+ leftBar.setQuick(0, 1);
+ leftBar.setQuick(1, 2);
+ leftBar.setQuick(2, 3);
+ assertTrue("equivalent didn't work", AbstractVector.equivalent(leftBar, right));
+ assertEquals("equals didn't work", false, leftBar.equals(right));
+ assertEquals("equivalent didn't work", false, AbstractVector.strictEquivalence(left, right));
+
+ SparseVector rightBar = new SparseVector("bar", 3);
+ rightBar.setQuick(0, 1);
+ rightBar.setQuick(1, 2);
+ rightBar.setQuick(2, 3);
+ assertTrue("equivalent didn't work", AbstractVector.equivalent(left, rightBar));
+ assertEquals("equals didn't work", false, left.equals(rightBar));
+ assertEquals("equivalent didn't work", false, AbstractVector.strictEquivalence(left, rightBar));
+
+ right.setQuick(2, 4);
+ assertEquals("equivalent didn't work", false, AbstractVector.equivalent(left, right));
+ assertEquals("equals didn't work", false, left.equals(right));
+ right = new DenseVector(4);
+ right.setQuick(0, 1);
+ right.setQuick(1, 2);
+ right.setQuick(2, 3);
+ right.setQuick(3, 3);
+ assertEquals("equivalent didn't work", false, AbstractVector.equivalent(left, right));
+ assertEquals("equals didn't work", false, left.equals(right));
+ left = new SparseVector(2);
+ left.setQuick(0, 1);
+ left.setQuick(1, 2);
+ assertEquals("equivalent didn't work", false, AbstractVector.equivalent(left, right));
+ assertEquals("equals didn't work", false, left.equals(right));
+
+ DenseVector dense = new DenseVector(3);
+ right = new DenseVector(3);
+ right.setQuick(0, 1);
+ right.setQuick(1, 2);
+ right.setQuick(2, 3);
+ dense.setQuick(0, 1);
+ dense.setQuick(1, 2);
+ dense.setQuick(2, 3);
+ assertEquals("equivalent didn't work", true, AbstractVector
+ .equivalent(dense, right));
+ assertEquals("equals didn't work", true, dense.equals(right));
+
+ SparseVector sparse = new SparseVector(3);
+ left = new SparseVector(3);
+ sparse.setQuick(0, 1);
+ sparse.setQuick(1, 2);
+ sparse.setQuick(2, 3);
+ left.setQuick(0, 1);
+ left.setQuick(1, 2);
+ left.setQuick(2, 3);
+ assertEquals("equivalent didn't work", true, AbstractVector
+ .equivalent(sparse, left));
+ assertEquals("equals didn't work", true, left.equals(sparse));
+
+ VectorView v1 = new VectorView(left, 0, 2);
+ VectorView v2 = new VectorView(right, 0, 2);
+ assertEquals("equivalent didn't work", true, AbstractVector.equivalent(v1, v2));
+ assertEquals("equals didn't work", true, v1.equals(v2));
+ sparse = new SparseVector(2);
+ sparse.setQuick(0, 1);
+ sparse.setQuick(1, 2);
+ assertEquals("equivalent didn't work", true, AbstractVector.equivalent(v1, sparse));
+ assertEquals("equals didn't work", true, v1.equals(sparse));
+
+ }
+
+ private static void doTestVectors(Vector left, Vector right) {
+ 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);
+ assertEquals(result + " does not equal: " + 32, 32.0, result);
+ String formattedString = left.asFormatString();
+ 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));
+ }
+
+ public void testNormalize() throws Exception {
+ SparseVector vec1 = new SparseVector(3);
+
+ vec1.setQuick(0, 1);
+ vec1.setQuick(1, 2);
+ vec1.setQuick(2, 3);
+ Vector norm = vec1.normalize();
+ assertNotNull("norm1 is null and it shouldn't be", norm);
+ Vector expected = new SparseVector(3);
+
+ expected.setQuick(0, 0.2672612419124244);
+ expected.setQuick(1, 0.5345224838248488);
+ expected.setQuick(2, 0.8017837257372732);
+ assertEquals("norm is not equal to expected", norm, expected);
+
+ norm = vec1.normalize(2);
+ assertEquals("norm is not equal to expected", norm, expected);
+
+ norm = vec1.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);
+ norm = vec1.normalize(3);
+ // TODO this is not used
+ expected = vec1.times(vec1).times(vec1);
+
+ // double sum = expected.zSum();
+ // cube = Math.pow(sum, 1.0/3);
+ double cube = Math.pow(36, 1.0 / 3);
+ expected = vec1.divide(cube);
+
+ assertEquals("norm: " + norm.asFormatString() + " is not equal to expected: "
+ + expected.asFormatString(), norm, expected);
+
+ norm = vec1.normalize(Double.POSITIVE_INFINITY);
+ // The max is 3, so we divide by that.
+ expected.setQuick(0, 1.0 / 3);
+ expected.setQuick(1, 2.0 / 3);
+ expected.setQuick(2, 3.0 / 3);
+ assertEquals("norm: " + norm.asFormatString() + " is not equal to expected: "
+ + expected.asFormatString(), norm, expected);
+
+ norm = vec1.normalize(0);
+ // The max is 3, so we divide by that.
+ expected.setQuick(0, 1.0 / 3);
+ expected.setQuick(1, 2.0 / 3);
+ expected.setQuick(2, 3.0 / 3);
+ assertEquals("norm: " + norm.asFormatString() + " is not equal to expected: "
+ + expected.asFormatString(), norm, expected);
+
+ try {
+ vec1.normalize(-1);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ }
+
+ public void testMax() throws Exception {
+ SparseVector vec1 = new SparseVector(3);
+
+ vec1.setQuick(0, 1);
+ vec1.setQuick(1, 3);
+ vec1.setQuick(2, 2);
+
+ double max = vec1.maxValue();
+ assertEquals(max + " does not equal: " + 3, 3, max, 0.0);
+
+ int idx = vec1.maxValueIndex();
+ assertEquals(idx + " does not equal: " + 1, 1, idx);
+
+ }
+
+ public void testDenseVector() throws Exception {
+ DenseVector vec1 = new DenseVector(3);
+ DenseVector vec2 = new DenseVector(3);
+ doTestVectors(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);
+ doTestVectors(vecV1, vecV2);
+ }
+
+ /** Asserts a vector using enumeration equals a given dense vector */
+ private static void doTestEnumeration(double[] apriori, Vector vector) {
+ double[] test = new double[apriori.length];
+ Iterator<Vector.Element> iter = vector.iterateNonZero();
+ while (iter.hasNext()) {
+ Vector.Element e = iter.next();
+ test[e.index()] = e.get();
+ }
+
+ for (int i = 0; i < test.length; i++) {
+ assertEquals(apriori[i], test[i]);
+ }
+ }
+
+ public void testEnumeration() throws Exception {
+ double[] apriori = {0, 1, 2, 3, 4};
+
+ doTestEnumeration(apriori, new VectorView(new DenseVector(new double[]{
+ -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}), 2, 5));
+
+ doTestEnumeration(apriori, new DenseVector(new double[]{0, 1, 2, 3, 4}));
+
+ SparseVector sparse = new SparseVector(5);
+ sparse.set(0, 0);
+ sparse.set(1, 1);
+ sparse.set(2, 2);
+ sparse.set(3, 3);
+ sparse.set(4, 4);
+ doTestEnumeration(apriori, sparse);
+ }
+
+ /*public void testSparseVectorTimesX() {
+ RandomUtils.useTestSeed();
+ Random rnd = RandomUtils.getRandom();
+ Vector v1 = randomSparseVector(rnd);
+ double x = rnd.nextDouble();
+ long t0 = System.currentTimeMillis();
+ SparseVector.optimizeTimes = false;
+ Vector rRef = null;
+ for (int i = 0; i < 10; i++)
+ rRef = v1.times(x);
+ long t1 = System.currentTimeMillis();
+ SparseVector.optimizeTimes = true;
+ Vector rOpt = null;
+ for (int i = 0; i < 10; i++)
+ rOpt = v1.times(x);
+ long t2 = System.currentTimeMillis();
+ long tOpt = t2 - t1;
+ long tRef = t1 - t0;
+ assertTrue(tOpt < tRef);
+ System.out.println("testSparseVectorTimesX tRef-tOpt=" + (tRef - tOpt)
+ + " ms for 10 iterations");
+ for (int i = 0; i < 50000; i++)
+ assertEquals("i=" + i, rRef.getQuick(i), rOpt.getQuick(i));
+ }*/
+
+ /*public void testSparseVectorTimesV() {
+ RandomUtils.useTestSeed();
+ Random rnd = RandomUtils.getRandom();
+ Vector v1 = randomSparseVector(rnd);
+ Vector v2 = randomSparseVector(rnd);
+ long t0 = System.currentTimeMillis();
+ SparseVector.optimizeTimes = false;
+ Vector rRef = null;
+ for (int i = 0; i < 10; i++)
+ rRef = v1.times(v2);
+ long t1 = System.currentTimeMillis();
+ SparseVector.optimizeTimes = true;
+ Vector rOpt = null;
+ for (int i = 0; i < 10; i++)
+ rOpt = v1.times(v2);
+ long t2 = System.currentTimeMillis();
+ long tOpt = t2 - t1;
+ long tRef = t1 - t0;
+ assertTrue(tOpt < tRef);
+ System.out.println("testSparseVectorTimesV tRef-tOpt=" + (tRef - tOpt)
+ + " ms for 10 iterations");
+ for (int i = 0; i < 50000; i++)
+ assertEquals("i=" + i, rRef.getQuick(i), rOpt.getQuick(i));
+ }*/
+
+ private static Vector randomSparseVector(Random rnd) {
+ SparseVector v1 = new SparseVector(50000);
+ for (int i = 0; i < 1000; i++) {
+ v1.setQuick(rnd.nextInt(50000), rnd.nextDouble());
+ }
+ return v1;
+ }
+
+ public void testLabelSerializationDense() {
+ double[] values = {1.1, 2.2, 3.3};
+ Vector test = new DenseVector(values);
+ Map<String, Integer> bindings = new HashMap<String, Integer>();
+ bindings.put("Fee", 0);
+ bindings.put("Fie", 1);
+ bindings.put("Foe", 2);
+ test.setLabelBindings(bindings);
+
+ Type vectorType = new TypeToken<Vector>() {
+ }.getType();
+
+ GsonBuilder builder = new GsonBuilder();
+ builder.registerTypeAdapter(vectorType, new JsonVectorAdapter());
+ Gson gson = builder.create();
+ String json = gson.toJson(test, vectorType);
+ Vector test1 = gson.fromJson(json, vectorType);
+ 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);
+ String formatString = test.asFormatString();
+
+ Vector decode = AbstractVector.decodeVector(formatString);
+ assertEquals("test and decode are not equal", test, decode);
+
+ Vector noName = new DenseVector(values);
+ formatString = noName.asFormatString();
+
+ decode = AbstractVector.decodeVector(formatString);
+ assertEquals("noName and decode are not equal", noName, decode);
+ }
+
+ public void testLabelSerializationSparse() {
+ double[] values = {1.1, 2.2, 3.3};
+ Vector test = new SparseVector(3);
+ for (int i = 0; i < values.length; i++) {
+ test.set(i, values[i]);
+ }
+ Map<String, Integer> bindings = new HashMap<String, Integer>();
+ bindings.put("Fee", 0);
+ bindings.put("Fie", 1);
+ bindings.put("Foe", 2);
+ test.setLabelBindings(bindings);
+
+ Type vectorType = new TypeToken<Vector>() {
+ }.getType();
+
+ GsonBuilder builder = new GsonBuilder();
+ builder.registerTypeAdapter(vectorType, new JsonVectorAdapter());
+ Gson gson = builder.create();
+ String json = gson.toJson(test, vectorType);
+ Vector test1 = gson.fromJson(json, vectorType);
+ try {
+ test1.get("Fee");
+ fail();
+ } catch (IndexException e) {
+ fail();
+ } catch (UnboundLabelException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testLabelSet() {
+ Vector test = new DenseVector(3);
+ test.set("Fee", 0, 1.1);
+ test.set("Fie", 1, 2.2);
+ test.set("Foe", 2, 3.3);
+ assertEquals("Fee", 1.1, test.get("Fee"));
+ assertEquals("Fie", 2.2, test.get("Fie"));
+ assertEquals("Foe", 3.3, test.get("Foe"));
+ }
+
+ public void testHashCodeEquivalence() {
+ // Hash codes must be equal if the vectors are considered equal
+ SparseVector sparseLeft = new SparseVector(3);
+ DenseVector denseRight = new DenseVector(3);
+ sparseLeft.setQuick(0, 1);
+ sparseLeft.setQuick(1, 2);
+ sparseLeft.setQuick(2, 3);
+ denseRight.setQuick(0, 1);
+ denseRight.setQuick(1, 2);
+ denseRight.setQuick(2, 3);
+ assertEquals(sparseLeft, denseRight);
+ assertEquals(sparseLeft.hashCode(), denseRight.hashCode());
+
+ DenseVector denseLeft = new DenseVector(3);
+ denseLeft.setQuick(0, 1);
+ denseLeft.setQuick(1, 2);
+ denseLeft.setQuick(2, 3);
+ assertEquals(denseLeft, denseRight);
+ assertEquals(denseLeft.hashCode(), denseRight.hashCode());
+
+ SparseVector sparseRight = new SparseVector(3);
+ sparseRight.setQuick(0, 1);
+ sparseRight.setQuick(1, 2);
+ sparseRight.setQuick(2, 3);
+ assertEquals(sparseLeft, sparseRight);
+ assertEquals(sparseLeft.hashCode(), sparseRight.hashCode());
+
+ DenseVector emptyLeft = new DenseVector("foo", 0);
+ SparseVector emptyRight = new SparseVector("foo", 0);
+ assertEquals(emptyLeft, emptyRight);
+ assertEquals(emptyLeft.hashCode(), emptyRight.hashCode());
+ }
+
+ public void testHashCode() {
+ // Make sure that hash([1,0,2]) != hash([1,2,0])
+ SparseVector left = new SparseVector(3);
+ SparseVector right = new SparseVector(3);
+ left.setQuick(0, 1);
+ left.setQuick(2, 2);
+ right.setQuick(0, 1);
+ right.setQuick(1, 2);
+ assertFalse(left.equals(right));
+ assertFalse(left.hashCode() == right.hashCode());
+
+ // Make sure that hash([1,0,2,0,0,0]) != hash([1,0,2])
+ right = new SparseVector(5);
+ right.setQuick(0, 1);
+ right.setQuick(2, 2);
+ assertFalse(left.equals(right));
+ assertFalse(left.hashCode() == right.hashCode());
+ }
+
+}
Propchange: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/map/OpenIntIntHashMapTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/map/OpenIntIntHashMapTest.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/map/OpenIntIntHashMapTest.java (added)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/map/OpenIntIntHashMapTest.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1 @@
+package org.apache.mahout.math.map;
Propchange: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/map/OpenIntIntHashMapTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: lucene/mahout/trunk/pom.xml
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/pom.xml?rev=891983&r1=891982&r2=891983&view=diff
==============================================================================
--- lucene/mahout/trunk/pom.xml (original)
+++ lucene/mahout/trunk/pom.xml Thu Dec 17 23:22:16 2009
@@ -26,7 +26,7 @@
<modules>
<module>maven</module>
- <module>matrix</module>
+ <module>math</module>
<module>core</module>
<module>taste-web</module>
<module>examples</module>
@@ -108,17 +108,6 @@
</plugins>
</build>
</profile>
- <profile>
- <id>colt</id>
- <modules>
- <module>maven</module>
- <module>core</module>
- <module>taste-web</module>
- <module>examples</module>
- <module>utils</module>
- <module>colt</module>
- </modules>
- </profile>
</profiles>
<scm>
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/clustering/ClusterDumper.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/clustering/ClusterDumper.java?rev=891983&r1=891982&r2=891983&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/clustering/ClusterDumper.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/clustering/ClusterDumper.java Thu Dec 17 23:22:16 2009
@@ -34,7 +34,7 @@
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.jobcontrol.Job;
import org.apache.mahout.clustering.ClusterBase;
-import org.apache.mahout.matrix.Vector;
+import org.apache.mahout.math.Vector;
import org.apache.mahout.common.CommandLineUtil;
import org.apache.mahout.common.FileLineIterator;
import org.slf4j.Logger;
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/SequenceFileVectorIterable.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/SequenceFileVectorIterable.java?rev=891983&r1=891982&r2=891983&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/SequenceFileVectorIterable.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/SequenceFileVectorIterable.java Thu Dec 17 23:22:16 2009
@@ -19,16 +19,16 @@
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Writable;
-import org.apache.mahout.matrix.Vector;
+import org.apache.mahout.math.Vector;
import java.util.Iterator;
import java.io.IOException;
/**
- * Reads in a file containing {@link org.apache.mahout.matrix.Vector}s.
+ * Reads in a file containing {@link org.apache.mahout.math.Vector}s.
* <p/>
- * The key is any {@link org.apache.hadoop.io.Writable} and the value is a {@link org.apache.mahout.matrix.Vector}.
+ * The key is any {@link org.apache.hadoop.io.Writable} and the value is a {@link org.apache.mahout.math.Vector}.
* It can handle any class that implements Vector as long as it has a no-arg constructor.
*/
public class SequenceFileVectorIterable implements Iterable<Vector> {
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorDumper.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorDumper.java?rev=891983&r1=891982&r2=891983&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorDumper.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/VectorDumper.java Thu Dec 17 23:22:16 2009
@@ -32,7 +32,7 @@
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.jobcontrol.Job;
-import org.apache.mahout.matrix.Vector;
+import org.apache.mahout.math.Vector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -43,8 +43,8 @@
/**
- * Can read in a {@link org.apache.hadoop.io.SequenceFile} of {@link org.apache.mahout.matrix.Vector}s
- * and dump out the results using {@link org.apache.mahout.matrix.Vector#asFormatString()} to either the console
+ * Can read in a {@link org.apache.hadoop.io.SequenceFile} of {@link org.apache.mahout.math.Vector}s
+ * and dump out the results using {@link org.apache.mahout.math.Vector#asFormatString()} to either the console
* or to a file.
*/
public final class VectorDumper {
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/arff/ARFFVectorIterable.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/arff/ARFFVectorIterable.java?rev=891983&r1=891982&r2=891983&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/arff/ARFFVectorIterable.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/arff/ARFFVectorIterable.java Thu Dec 17 23:22:16 2009
@@ -17,9 +17,9 @@
package org.apache.mahout.utils.vectors.arff;
-import org.apache.mahout.matrix.DenseVector;
-import org.apache.mahout.matrix.SparseVector;
-import org.apache.mahout.matrix.Vector;
+import org.apache.mahout.math.DenseVector;
+import org.apache.mahout.math.SparseVector;
+import org.apache.mahout.math.Vector;
import org.apache.mahout.common.IOUtils;
import java.io.BufferedReader;
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/arff/Driver.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/arff/Driver.java?rev=891983&r1=891982&r2=891983&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/arff/Driver.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/arff/Driver.java Thu Dec 17 23:22:16 2009
@@ -30,7 +30,7 @@
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
-import org.apache.mahout.matrix.SparseVector;
+import org.apache.mahout.math.SparseVector;
import org.apache.mahout.common.CommandLineUtil;
import org.apache.mahout.utils.strings.StringUtil;
import org.apache.mahout.utils.vectors.io.JWriterVectorWriter;
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/JWriterVectorWriter.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/JWriterVectorWriter.java?rev=891983&r1=891982&r2=891983&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/JWriterVectorWriter.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/JWriterVectorWriter.java Thu Dec 17 23:22:16 2009
@@ -17,7 +17,7 @@
package org.apache.mahout.utils.vectors.io;
-import org.apache.mahout.matrix.Vector;
+import org.apache.mahout.math.Vector;
import java.io.IOException;
import java.io.Writer;
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/SequenceFileVectorWriter.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/SequenceFileVectorWriter.java?rev=891983&r1=891982&r2=891983&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/SequenceFileVectorWriter.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/SequenceFileVectorWriter.java Thu Dec 17 23:22:16 2009
@@ -17,7 +17,7 @@
package org.apache.mahout.utils.vectors.io;
-import org.apache.mahout.matrix.Vector;
+import org.apache.mahout.math.Vector;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.LongWritable;
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/VectorWriter.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/VectorWriter.java?rev=891983&r1=891982&r2=891983&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/VectorWriter.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/io/VectorWriter.java Thu Dec 17 23:22:16 2009
@@ -17,7 +17,7 @@
package org.apache.mahout.utils.vectors.io;
-import org.apache.mahout.matrix.Vector;
+import org.apache.mahout.math.Vector;
import java.io.IOException;
Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/lucene/Driver.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/lucene/Driver.java?rev=891983&r1=891982&r2=891983&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/lucene/Driver.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/lucene/Driver.java Thu Dec 17 23:22:16 2009
@@ -33,7 +33,7 @@
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
-import org.apache.mahout.matrix.SparseVector;
+import org.apache.mahout.math.SparseVector;
import org.apache.mahout.common.CommandLineUtil;
import org.apache.mahout.utils.vectors.TF;
import org.apache.mahout.utils.vectors.TFIDF;