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 2008/03/16 22:24:51 UTC
svn commit: r637664 [2/3] - in /lucene/mahout/trunk/src:
main/java/org/apache/mahout/matrix/ test/java/org/apache/mahout/matrix/
Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/UnaryFunction.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/UnaryFunction.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/UnaryFunction.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/UnaryFunction.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,34 @@
+/* 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.matrix;
+
+/**
+ * This interface allows the formulation of unary functions to be applied to
+ * matrices inside the inner loops of their implementations.
+ *
+ */
+public interface UnaryFunction {
+
+ /**
+ * Apply the function to the argument and return the result
+ *
+ * @param arg1 double for the argument
+ * @return the result of applying the function
+ */
+ public double apply(double arg1);
+
+}
Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/UnaryFunction.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Vector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Vector.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Vector.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Vector.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,280 @@
+/* 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.matrix;
+
+import org.apache.hadoop.io.WritableComparable;
+
+
+/**
+ * The basic interface including numerous convenience functions
+ *
+ */
+public interface Vector {
+
+ /**
+ * Return a formatted WritableComparable suitable for output
+ *
+ * @return formatted WritableComparable
+ */
+ WritableComparable asWritableComparable();
+
+ /**
+ * Assign the value to all elements of the receiver
+ *
+ * @param value a double value
+ * @return the modified receiver
+ */
+ Vector assign(double value);
+
+ /**
+ * Assign the values to the receiver
+ *
+ * @param values a double[] of values
+ * @return the modified receiver
+ * @throws CardinalityException if the cardinalities differ
+ */
+ Vector assign(double[] values) throws CardinalityException;
+
+ /**
+ * Assign the other matrix values to the receiver
+ *
+ * @param other a Vector
+ * @return the modified receiver
+ * @throws CardinalityException if the cardinalities differ
+ */
+ Vector assign(Vector other) throws CardinalityException;
+
+ /**
+ * Apply the function to each element of the receiver
+ *
+ * @param function a DoubleFunction to apply
+ * @return the modified receiver
+ */
+ Vector assign(UnaryFunction function);
+
+ /**
+ * Apply the function to each element of the receiver and the corresponding
+ * element of the other argument
+ *
+ * @param other a Vector containing the second arguments to the function
+ * @param function a DoubleDoubleFunction to apply
+ * @return the modified receiver
+ * @throws CardinalityException if the cardinalities differ
+ */
+ Vector assign(Vector other, BinaryFunction function)
+ throws CardinalityException;
+
+ /**
+ * Return the cardinality of the recipient (the maximum number of values)
+ *
+ * @return an int
+ */
+ int cardinality();
+
+ /**
+ * Return a copy of the recipient
+ *
+ * @return a new Vector
+ */
+ Vector copy();
+
+ /**
+ * Return a new matrix containing the values of the recipient divided by the
+ * argument
+ *
+ * @param x a double value
+ * @return a new Vector
+ */
+ Vector divide(double x);
+
+ /**
+ * Return the dot product of the recipient and the argument
+ *
+ * @param x a Vector
+ * @return a new Vector
+ * @throws CardinalityException if the cardinalities differ
+ */
+ double dot(Vector x) throws CardinalityException;
+
+ /**
+ * Return the value at the given index
+ *
+ * @param index an int index
+ * @return the double at the index
+ * @throws IndexException if the index is out of bounds
+ */
+ double get(int index) throws IndexException;
+
+ /**
+ * Return the value at the given index, without checking bounds
+ *
+ * @param index an int index
+ * @return the double at the index
+ */
+ double getQuick(int index);
+
+ /**
+ * Return if the other matrix and the receiver share any underlying data cells
+ *
+ * @param other a Vector
+ * @return true if the other matrix has common data cells
+ */
+ boolean haveSharedCells(Vector other);
+
+ /**
+ * Return an empty matrix of the same underlying class as the receiver
+ *
+ * @return a Vector
+ */
+ Vector like();
+
+ /**
+ * Return an empty matrix of the same underlying class as the receiver and of
+ * the given cardinality
+ *
+ * @param cardinality an int specifying the desired cardinality
+ * @return a Vector
+ */
+ Vector like(int cardinality);
+
+ /**
+ * Return a new matrix containing the element by element difference of the
+ * recipient and the argument
+ *
+ * @param x a Vector
+ * @return a new Vector
+ * @throws CardinalityException if the cardinalities differ
+ */
+ Vector minus(Vector x) throws CardinalityException;
+
+ /**
+ * Return a new matrix containing the normalized values of the recipient
+ *
+ * @return a new Vector
+ */
+ Vector normalize();
+
+ /**
+ * Return a new matrix containing the sum of each value of the recipient and
+ * the argument
+ *
+ * @param x a double
+ * @return a new Vector
+ */
+ Vector plus(double x);
+
+ /**
+ * Return a new matrix containing the element by element sum of the recipient
+ * and the argument
+ *
+ * @param x a Vector
+ * @return a new Vector
+ * @throws CardinalityException if the cardinalities differ
+ */
+ Vector plus(Vector x) throws CardinalityException;
+
+ /**
+ * Set the value at the given index
+ *
+ * @param index an int index into the receiver
+ * @param value a double value to set
+ * @throws IndexException if the index is out of bounds
+ */
+ void set(int index, double value) throws IndexException;
+
+ /**
+ * Set the value at the given index, without checking bounds
+ *
+ * @param index an int index into the receiver
+ * @param value a double value to set
+ */
+ void setQuick(int index, double value);
+
+ /**
+ * Return the number of values in the recipient
+ *
+ * @return an int
+ */
+ int size();
+
+ /**
+ * Return a new matrix containing the product of each value of the recipient
+ * and the argument
+ *
+ * @param x a double argument
+ * @return a new Vector
+ */
+ Vector times(double x);
+
+ /**
+ * Return a new matrix containing the element-wise product of the recipient
+ * and the argument
+ *
+ * @param x a Vector argument
+ * @return a new Vector
+ * @throws CardinalityException if the cardinalities differ
+ */
+ Vector times(Vector x) throws CardinalityException;
+
+ /**
+ * Return the element of the recipient as a double[]
+ *
+ * @return a double[]
+ */
+ double[] toArray();
+
+ /**
+ * Return a new matrix containing the subset of the recipient
+ *
+ * @param offset an int offset into the receiver
+ * @param length the cardinality of the desired result
+ * @return a new Vector
+ * @throws CardinalityException if the length is greater than the cardinality
+ * of the receiver
+ * @throws IndexException if the offset is negative or the offset+length is
+ * outside of the receiver
+ */
+ Vector viewPart(int offset, int length) throws CardinalityException,
+ IndexException;
+
+ /**
+ * Return the sum of all the elements of the receiver
+ *
+ * @return a double
+ */
+ double zSum();
+
+ /**
+ * Return the cross product of the receiver and the other vector
+ *
+ * @param other another Vector
+ * @return a Matrix
+ */
+ Matrix cross(Vector other);
+
+ /*
+ * Need stories for these but keeping them here for now.
+ *
+ */
+ // void getNonZeros(IntArrayList jx, DoubleArrayList values);
+ // void foreachNonZero(IntDoubleFunction f);
+ // double aggregate(DoubleDoubleFunction aggregator, DoubleFunction map);
+ // double aggregate(Vector other, DoubleDoubleFunction aggregator,
+ // DoubleDoubleFunction map);
+ // NewVector assign(Vector y, DoubleDoubleFunction function, IntArrayList
+ // nonZeroIndexes);
+}
Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Vector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorView.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorView.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorView.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,117 @@
+/* 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.matrix;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.WritableComparable;
+
+/**
+ * Implements subset view of a Vector
+ */
+public class VectorView extends AbstractVector {
+ private Vector vector;
+
+ // the offset into the Vector
+ private int offset;
+
+ // the cardinality of the view
+ private int cardinality;
+
+ public VectorView(Vector vector, int offset, int cardinality) {
+ super();
+ this.vector = vector;
+ this.offset = offset;
+ this.cardinality = cardinality;
+ }
+
+ @Override
+ protected Matrix matrixLike(int rows, int columns) {
+ return ((AbstractVector) vector).matrixLike(rows, columns);
+ }
+
+ @Override
+ public WritableComparable asWritableComparable() {
+ StringBuilder out = new StringBuilder();
+ out.append("[");
+ for (int i = offset; i < offset + cardinality; i++)
+ out.append(getQuick(i)).append(", ");
+ out.append("] ");
+ return new Text(out.toString());
+ }
+
+ @Override
+ public int cardinality() {
+ return cardinality;
+ }
+
+ @Override
+ public Vector copy() {
+ return new VectorView(vector.copy(), offset, cardinality);
+ }
+
+ @Override
+ public double getQuick(int index) {
+ return vector.getQuick(offset + index);
+ }
+
+ @Override
+ public Vector like() {
+ return vector.like();
+ }
+
+ @Override
+ public Vector like(int cardinality) {
+ return vector.like(cardinality);
+ }
+
+ @Override
+ public void setQuick(int index, double value) {
+ vector.setQuick(offset + index, value);
+ }
+
+ @Override
+ public int size() {
+ return cardinality;
+ }
+
+ @Override
+ public double[] toArray() {
+ double[] result = new double[cardinality];
+ for (int i = 0; i < cardinality; i++)
+ result[i] = vector.getQuick(offset + i);
+ return result;
+ }
+
+ @Override
+ public Vector viewPart(int offset, int length) throws CardinalityException,
+ IndexException {
+ if (length > cardinality)
+ throw new CardinalityException();
+ if (offset < 0 || offset + length > cardinality)
+ throw new IndexException();
+ Vector result = new VectorView(vector, offset + this.offset, length);
+ return result;
+ }
+
+ @Override
+ public boolean haveSharedCells(Vector other) {
+ if (other instanceof VectorView)
+ return other == this || vector.haveSharedCells(other);
+ else
+ return other.haveSharedCells(vector);
+ }
+}
Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorView.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseMatrix.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseMatrix.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseMatrix.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,474 @@
+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 TestDenseMatrix extends TestCase {
+ private static int ROW = AbstractMatrix.ROW;
+
+ private static int COL = AbstractMatrix.COL;
+
+ private double[][] values = {{1.1, 2.2}, {3.3, 4.4}, {5.5, 6.6}};
+
+ private Matrix test;
+
+ public TestDenseMatrix(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ test = new DenseMatrix(values);
+ }
+
+ public void testAsFormatString() {
+ assertEquals("format", "[[, 1.1, 2.2, ], 3.3, 4.4, ], 5.5, 6.6, ], ] ",
+ test.asWritableComparable().toString());
+ }
+
+ public void testCardinality() {
+ int[] c = test.cardinality();
+ assertEquals("row cardinality", values.length, c[ROW]);
+ assertEquals("col cardinality", values[0].length, c[COL]);
+ }
+
+ public void testCopy() {
+ int[] c = test.cardinality();
+ Matrix copy = test.copy();
+ assertTrue("wrong class", copy instanceof DenseMatrix);
+ 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.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col], test
+ .getQuick(row, col));
+ }
+
+ public void testHaveSharedCells() {
+ assertTrue("same", test.haveSharedCells(test));
+ assertFalse("different", test.haveSharedCells(test.copy()));
+ }
+
+ public void testLike() {
+ Matrix like = test.like();
+ assertTrue("type", like instanceof DenseMatrix);
+ assertEquals("rows", test.cardinality()[ROW], like.cardinality()[ROW]);
+ assertEquals("columns", test.cardinality()[COL], like.cardinality()[COL]);
+ }
+
+ public void testLikeIntInt() {
+ Matrix like = test.like(4, 4);
+ assertTrue("type", like instanceof DenseMatrix);
+ assertEquals("rows", 4, like.cardinality()[ROW]);
+ assertEquals("columns", 4, like.cardinality()[COL]);
+ }
+
+ public void testSetQuick() {
+ int[] c = test.cardinality();
+ 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.size();
+ assertEquals("row size", values.length, c[ROW]);
+ assertEquals("col size", values[0].length, c[COL]);
+ }
+
+ public void testToArray() {
+ double[][] array = test.toArray();
+ int[] c = test.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col],
+ array[row][col]);
+ }
+
+ public void testViewPart() throws Exception {
+ int[] offset = {1, 1};
+ int[] size = {2, 1};
+ Matrix view = test.viewPart(offset, size);
+ int[] c = view.cardinality();
+ 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], 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col],
+ 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.cardinality();
+ 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.cardinality();
+ 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][col], test
+ .getQuick(row, col));
+ }
+
+ public void testDivide() {
+ int[] c = test.cardinality();
+ 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][col] / 4.53, value.getQuick(row, col));
+ }
+
+ public void testGet() throws Exception {
+ int[] c = test.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col], test
+ .get(row, col));
+ }
+
+ public void testGetIndexUnder() {
+ int[] c = test.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col] + 4.53, value.getQuick(row, col));
+ }
+
+ public void testPlusMatrix() throws Exception {
+ int[] c = test.cardinality();
+ 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][col] * 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col] * 4.53, value.getQuick(row, col));
+ }
+
+ public void testTimesMatrix() throws Exception {
+ int[] c = test.cardinality();
+ Matrix transpose = test.transpose();
+ Matrix value = test.times(transpose);
+ int[] v = value.cardinality();
+ 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.cardinality();
+ Matrix transpose = test.transpose();
+ int[] t = transpose.cardinality();
+ 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", 23.1, 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.size());
+ }
+
+ 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.size());
+ }
+
+ 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);
+ }
+ }
+}
Propchange: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseMatrix.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseVector.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseVector.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseVector.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,334 @@
+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 TestDenseVector extends TestCase {
+
+ double[] values = {1.1, 2.2, 3.3};
+
+ Vector test = new DenseVector(values);
+
+ public TestDenseVector(String name) {
+ super(name);
+ }
+
+ public void testAsFormatString() {
+ String formatString = test.asWritableComparable().toString();
+ assertEquals("format", "[, 1.1, 2.2, 3.3, ] ", 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], 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[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[i + 1], part.get(i));
+ }
+
+ public void testViewPartUnder() {
+ try {
+ test.viewPart(-1, values.length);
+ fail("no exception");
+ } catch (CardinalityException e) {
+ fail("wrong exception");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testViewPartOver() {
+ try {
+ test.viewPart(2, values.length);
+ fail("no exception");
+ } catch (CardinalityException e) {
+ fail("wrong exception");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testViewPartCardinality() {
+ try {
+ test.viewPart(1, values.length + 1);
+ fail("no exception");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ } catch (IndexException e) {
+ fail("wrong exception");
+ }
+ }
+
+ public void testDecodeFormat() throws Exception {
+ Vector val = DenseVector.decodeFormat(test.asWritableComparable());
+ for (int i = 0; i < test.cardinality(); i++)
+ assertEquals("get [" + i + "]", test.get(i), val.get(i));
+ }
+
+ public void testDenseVectorDoubleArray() throws Exception {
+ for (int i = 0; i < test.cardinality(); i++)
+ assertEquals("test[" + i + "]", values[i], test.get(i));
+ }
+
+ public void testDenseVectorInt() throws Exception {
+ Vector val = new DenseVector(4);
+ assertEquals("cardinality", 4, val.cardinality());
+ for (int i = 0; i < test.cardinality(); 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 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[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[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[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[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[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[i] * values[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 = 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++)
+ assertEquals("value[" + i + "]", -values[i], test.getQuick(i));
+ }
+
+ public void testAssignBinaryFunction() throws Exception {
+ test.assign(test, new PlusFunction());
+ for (int i = 0; i < values.length; i++)
+ assertEquals("value[" + i + "]", 2 * values[i], 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/TestDenseVector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseColumnMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseColumnMatrix.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseColumnMatrix.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseColumnMatrix.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,479 @@
+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 TestSparseColumnMatrix extends TestCase {
+ private static int ROW = AbstractMatrix.ROW;
+
+ private static int COL = AbstractMatrix.COL;
+
+ private double[][] values = {{1.1, 2.2}, {3.3, 4.4}, {5.5, 6.6}};
+
+ private Matrix test;
+
+ public TestSparseColumnMatrix(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ int[] cardinality = {values.length, values[0].length};
+ test = new SparseColumnMatrix(cardinality);
+ for (int row = 0; row < cardinality[ROW]; row++)
+ for (int col = 0; col < cardinality[COL]; col++)
+ test.setQuick(row, col, values[row][col]);
+ }
+
+ public void testAsFormatString() {
+ assertEquals("format", "[[, 1.1, 2.2, ], 3.3, 4.4, ], 5.5, 6.6, ], ] ",
+ test.asWritableComparable().toString());
+ }
+
+ public void testCardinality() {
+ int[] c = test.cardinality();
+ assertEquals("row cardinality", values.length, c[ROW]);
+ assertEquals("col cardinality", values[0].length, c[COL]);
+ }
+
+ public void testCopy() {
+ int[] c = test.cardinality();
+ Matrix copy = test.copy();
+ assertTrue("wrong class", copy instanceof SparseColumnMatrix);
+ 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.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col], test
+ .getQuick(row, col));
+ }
+
+ public void testHaveSharedCells() {
+ assertTrue("same", test.haveSharedCells(test));
+ assertFalse("different", test.haveSharedCells(test.copy()));
+ }
+
+ public void testLike() {
+ Matrix like = test.like();
+ assertTrue("type", like instanceof SparseColumnMatrix);
+ assertEquals("rows", test.cardinality()[ROW], like.cardinality()[ROW]);
+ assertEquals("columns", test.cardinality()[COL], like.cardinality()[COL]);
+ }
+
+ public void testLikeIntInt() {
+ Matrix like = test.like(4, 4);
+ assertTrue("type", like instanceof SparseColumnMatrix);
+ assertEquals("rows", 4, like.cardinality()[ROW]);
+ assertEquals("columns", 4, like.cardinality()[COL]);
+ }
+
+ public void testSetQuick() {
+ int[] c = test.cardinality();
+ 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.size();
+ assertEquals("row size", values.length, c[ROW]);
+ assertEquals("col size", values[0].length, c[COL]);
+ }
+
+ public void testToArray() {
+ double[][] array = test.toArray();
+ int[] c = test.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col],
+ array[row][col]);
+ }
+
+ public void testViewPart() throws Exception {
+ int[] offset = {1, 1};
+ int[] size = {2, 1};
+ Matrix view = test.viewPart(offset, size);
+ int[] c = view.cardinality();
+ 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], 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col],
+ 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.cardinality();
+ 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.cardinality();
+ 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][col], test
+ .getQuick(row, col));
+ }
+
+ public void testDivide() {
+ int[] c = test.cardinality();
+ 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][col] / 4.53, value.getQuick(row, col));
+ }
+
+ public void testGet() throws Exception {
+ int[] c = test.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col], test
+ .get(row, col));
+ }
+
+ public void testGetIndexUnder() {
+ int[] c = test.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col] + 4.53, value.getQuick(row, col));
+ }
+
+ public void testPlusMatrix() throws Exception {
+ int[] c = test.cardinality();
+ 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][col] * 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col] * 4.53, value.getQuick(row, col));
+ }
+
+ public void testTimesMatrix() throws Exception {
+ int[] c = test.cardinality();
+ Matrix transpose = test.transpose();
+ Matrix value = test.times(transpose);
+ int[] v = value.cardinality();
+ 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.cardinality();
+ Matrix transpose = test.transpose();
+ int[] t = transpose.cardinality();
+ 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", 23.1, 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.size());
+ }
+
+ 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.size());
+ }
+
+ 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);
+ }
+ }
+
+}
Propchange: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseColumnMatrix.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseMatrix.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseMatrix.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseMatrix.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,481 @@
+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 TestSparseMatrix extends TestCase {
+ private static int ROW = AbstractMatrix.ROW;
+
+ private static int COL = AbstractMatrix.COL;
+
+ private double[][] values = {{1.1, 2.2}, {3.3, 4.4}, {5.5, 6.6}};
+
+ private Matrix test;
+
+ public TestSparseMatrix(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ int[] cardinality = {values.length, values[0].length};
+ test = new SparseMatrix(cardinality);
+ for (int row = 0; row < cardinality[ROW]; row++)
+ for (int col = 0; col < cardinality[COL]; col++)
+ test.setQuick(row, col, values[row][col]);
+ }
+
+ public void testAsFormatString() {
+ assertEquals(
+ "format",
+ "[s3, [s2, 1:6.6, 0:5.5, ] [s2, 1:4.4, 0:3.3, ] [s2, 1:2.2, 0:1.1, ] ] ",
+ test.asWritableComparable().toString());
+ }
+
+ public void testCardinality() {
+ int[] c = test.cardinality();
+ assertEquals("row cardinality", values.length, c[ROW]);
+ assertEquals("col cardinality", values[0].length, c[COL]);
+ }
+
+ public void testCopy() {
+ int[] c = test.cardinality();
+ Matrix copy = test.copy();
+ assertTrue("wrong class", copy instanceof SparseMatrix);
+ 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.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col], test
+ .getQuick(row, col));
+ }
+
+ public void testHaveSharedCells() {
+ assertTrue("same", test.haveSharedCells(test));
+ assertFalse("different", test.haveSharedCells(test.copy()));
+ }
+
+ public void testLike() {
+ Matrix like = test.like();
+ assertTrue("type", like instanceof SparseMatrix);
+ assertEquals("rows", test.cardinality()[ROW], like.cardinality()[ROW]);
+ assertEquals("columns", test.cardinality()[COL], like.cardinality()[COL]);
+ }
+
+ public void testLikeIntInt() {
+ Matrix like = test.like(4, 4);
+ assertTrue("type", like instanceof SparseMatrix);
+ assertEquals("rows", 4, like.cardinality()[ROW]);
+ assertEquals("columns", 4, like.cardinality()[COL]);
+ }
+
+ public void testSetQuick() {
+ int[] c = test.cardinality();
+ 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.size();
+ assertEquals("row size", values.length, c[ROW]);
+ assertEquals("col size", values[0].length, c[COL]);
+ }
+
+ public void testToArray() {
+ double[][] array = test.toArray();
+ int[] c = test.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col],
+ array[row][col]);
+ }
+
+ public void testViewPart() throws Exception {
+ int[] offset = {1, 1};
+ int[] size = {2, 1};
+ Matrix view = test.viewPart(offset, size);
+ int[] c = view.cardinality();
+ 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], 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col],
+ 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.cardinality();
+ 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.cardinality();
+ 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][col], test
+ .getQuick(row, col));
+ }
+
+ public void testDivide() {
+ int[] c = test.cardinality();
+ 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][col] / 4.53, value.getQuick(row, col));
+ }
+
+ public void testGet() throws Exception {
+ int[] c = test.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col], test
+ .get(row, col));
+ }
+
+ public void testGetIndexUnder() {
+ int[] c = test.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col] + 4.53, value.getQuick(row, col));
+ }
+
+ public void testPlusMatrix() throws Exception {
+ int[] c = test.cardinality();
+ 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][col] * 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col] * 4.53, value.getQuick(row, col));
+ }
+
+ public void testTimesMatrix() throws Exception {
+ int[] c = test.cardinality();
+ Matrix transpose = test.transpose();
+ Matrix value = test.times(transpose);
+ int[] v = value.cardinality();
+ 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.cardinality();
+ Matrix transpose = test.transpose();
+ int[] t = transpose.cardinality();
+ 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", 23.1, 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.size());
+ }
+
+ 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.size());
+ }
+
+ 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);
+ }
+ }
+
+}
Propchange: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseMatrix.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseRowMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseRowMatrix.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseRowMatrix.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseRowMatrix.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,479 @@
+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 TestSparseRowMatrix extends TestCase {
+ private static int ROW = AbstractMatrix.ROW;
+
+ private static int COL = AbstractMatrix.COL;
+
+ private double[][] values = {{1.1, 2.2}, {3.3, 4.4}, {5.5, 6.6}};
+
+ private Matrix test;
+
+ public TestSparseRowMatrix(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ int[] cardinality = {values.length, values[0].length};
+ test = new SparseRowMatrix(cardinality);
+ for (int row = 0; row < cardinality[ROW]; row++)
+ for (int col = 0; col < cardinality[COL]; col++)
+ test.setQuick(row, col, values[row][col]);
+ }
+
+ public void testAsFormatString() {
+ assertEquals("format", "[[, 1.1, 2.2, ], 3.3, 4.4, ], 5.5, 6.6, ], ] ",
+ test.asWritableComparable().toString());
+ }
+
+ public void testCardinality() {
+ int[] c = test.cardinality();
+ assertEquals("row cardinality", values.length, c[ROW]);
+ assertEquals("col cardinality", values[0].length, c[COL]);
+ }
+
+ public void testCopy() {
+ int[] c = test.cardinality();
+ Matrix copy = test.copy();
+ assertTrue("wrong class", copy instanceof SparseRowMatrix);
+ 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.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col], test
+ .getQuick(row, col));
+ }
+
+ public void testHaveSharedCells() {
+ assertTrue("same", test.haveSharedCells(test));
+ assertFalse("different", test.haveSharedCells(test.copy()));
+ }
+
+ public void testLike() {
+ Matrix like = test.like();
+ assertTrue("type", like instanceof SparseRowMatrix);
+ assertEquals("rows", test.cardinality()[ROW], like.cardinality()[ROW]);
+ assertEquals("columns", test.cardinality()[COL], like.cardinality()[COL]);
+ }
+
+ public void testLikeIntInt() {
+ Matrix like = test.like(4, 4);
+ assertTrue("type", like instanceof SparseRowMatrix);
+ assertEquals("rows", 4, like.cardinality()[ROW]);
+ assertEquals("columns", 4, like.cardinality()[COL]);
+ }
+
+ public void testSetQuick() {
+ int[] c = test.cardinality();
+ 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.size();
+ assertEquals("row size", values.length, c[ROW]);
+ assertEquals("col size", values[0].length, c[COL]);
+ }
+
+ public void testToArray() {
+ double[][] array = test.toArray();
+ int[] c = test.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col],
+ array[row][col]);
+ }
+
+ public void testViewPart() throws Exception {
+ int[] offset = {1, 1};
+ int[] size = {2, 1};
+ Matrix view = test.viewPart(offset, size);
+ int[] c = view.cardinality();
+ 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], 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col],
+ 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.cardinality();
+ 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.cardinality();
+ 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][col], test
+ .getQuick(row, col));
+ }
+
+ public void testDivide() {
+ int[] c = test.cardinality();
+ 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][col] / 4.53, value.getQuick(row, col));
+ }
+
+ public void testGet() throws Exception {
+ int[] c = test.cardinality();
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]", values[row][col], test
+ .get(row, col));
+ }
+
+ public void testGetIndexUnder() {
+ int[] c = test.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col] + 4.53, value.getQuick(row, col));
+ }
+
+ public void testPlusMatrix() throws Exception {
+ int[] c = test.cardinality();
+ 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][col] * 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.cardinality();
+ 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.cardinality();
+ 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.cardinality();
+ 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][col] * 4.53, value.getQuick(row, col));
+ }
+
+ public void testTimesMatrix() throws Exception {
+ int[] c = test.cardinality();
+ Matrix transpose = test.transpose();
+ Matrix value = test.times(transpose);
+ int[] v = value.cardinality();
+ 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.cardinality();
+ Matrix transpose = test.transpose();
+ int[] t = transpose.cardinality();
+ 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", 23.1, 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.size());
+ }
+
+ 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.size());
+ }
+
+ 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);
+ }
+ }
+
+}
Propchange: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseRowMatrix.java
------------------------------------------------------------------------------
svn:eol-style = native