You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by ka...@apache.org on 2008/04/11 16:00:33 UTC
svn commit: r647170 - in /lucene/mahout/trunk/src:
main/java/org/apache/mahout/matrix/ test/java/org/apache/mahout/matrix/
Author: kalle
Date: Fri Apr 11 07:00:29 2008
New Revision: 647170
URL: http://svn.apache.org/viewvc?rev=647170&view=rev
Log:
MAHOUT-33 and multiplication bugfix from MAHOUT-26
Added:
lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/MatrixTest.java
Modified:
lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java
lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseMatrix.java
lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseColumnMatrix.java
lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseMatrix.java
lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseRowMatrix.java
Modified: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java?rev=647170&r1=647169&r2=647170&view=diff
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java (original)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java Fri Apr 11 07:00:29 2008
@@ -312,7 +312,7 @@
throw new CardinalityException();
Matrix result = like(c[ROW], o[COL]);
for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++) {
+ for (int col = 0; col < o[COL]; col++) {
double sum = 0;
for (int k = 0; k < c[COL]; k++)
sum += getQuick(row, k) * other.getQuick(k, col);
Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/MatrixTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/MatrixTest.java?rev=647170&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/MatrixTest.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/MatrixTest.java Fri Apr 11 07:00:29 2008
@@ -0,0 +1,475 @@
+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 abstract class MatrixTest extends TestCase {
+ protected static int ROW = AbstractMatrix.ROW;
+
+ protected static int COL = AbstractMatrix.COL;
+
+ protected double[][] values = {{1.1, 2.2}, {3.3, 4.4}, {5.5, 6.6}};
+
+ protected Matrix test;
+
+ public MatrixTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ test = matrixFactory(values);
+ }
+
+ public abstract Matrix matrixFactory(double[][] values);
+
+ 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.getClass().equals(test.getClass()));
+ 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.getClass().equals(test.getClass()));
+ 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.getClass().equals(test.getClass()));
+ 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
+ Matrix timestest = new DenseMatrix(10,1);
+ /* will throw ArrayIndexOutOfBoundsException exception without MAHOUT-26 */
+ timestest.transpose().times(timestest);
+ }
+
+ 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);
+ }
+ }
+}
Modified: 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=647170&r1=647169&r2=647170&view=diff
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseMatrix.java (original)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseMatrix.java Fri Apr 11 07:00:29 2008
@@ -16,16 +16,7 @@
* 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 class TestDenseMatrix extends MatrixTest {
public TestDenseMatrix(String name) {
super(name);
@@ -33,7 +24,10 @@
protected void setUp() throws Exception {
super.setUp();
- test = new DenseMatrix(values);
+ }
+
+ public Matrix matrixFactory(double[][] values) {
+ return new DenseMatrix(values);
}
public void testAsFormatString() {
@@ -41,434 +35,4 @@
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);
- }
- }
}
Modified: 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=647170&r1=647169&r2=647170&view=diff
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseColumnMatrix.java (original)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseColumnMatrix.java Fri Apr 11 07:00:29 2008
@@ -18,14 +18,7 @@
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 class TestSparseColumnMatrix extends MatrixTest {
public TestSparseColumnMatrix(String name) {
super(name);
@@ -33,447 +26,21 @@
protected void setUp() throws Exception {
super.setUp();
+ }
+
+
+ public Matrix matrixFactory(double[][] values) {
int[] cardinality = {values.length, values[0].length};
- test = new SparseColumnMatrix(cardinality);
+ Matrix matrix = 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]);
+ matrix.setQuick(row, col, values[row][col]);
+ return matrix;
}
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);
- }
}
}
Modified: 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=647170&r1=647169&r2=647170&view=diff
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseMatrix.java (original)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseMatrix.java Fri Apr 11 07:00:29 2008
@@ -16,16 +16,7 @@
* 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 class TestSparseMatrix extends MatrixTest {
public TestSparseMatrix(String name) {
super(name);
@@ -33,11 +24,15 @@
protected void setUp() throws Exception {
super.setUp();
+ }
+
+ public Matrix matrixFactory(double[][] values) {
int[] cardinality = {values.length, values[0].length};
- test = new SparseMatrix(cardinality);
+ Matrix matrix = 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]);
+ matrix.setQuick(row, col, values[row][col]);
+ return matrix;
}
public void testAsFormatString() {
@@ -45,437 +40,6 @@
"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);
- }
}
}
Modified: 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=647170&r1=647169&r2=647170&view=diff
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseRowMatrix.java (original)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseRowMatrix.java Fri Apr 11 07:00:29 2008
@@ -16,16 +16,8 @@
* 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 class TestSparseRowMatrix extends MatrixTest {
public TestSparseRowMatrix(String name) {
super(name);
@@ -33,11 +25,17 @@
protected void setUp() throws Exception {
super.setUp();
+
+ }
+
+
+ public Matrix matrixFactory(double[][] values) {
int[] cardinality = {values.length, values[0].length};
- test = new SparseRowMatrix(cardinality);
+ Matrix matrix = 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]);
+ matrix.setQuick(row, col, values[row][col]);
+ return matrix;
}
public void testAsFormatString() {
@@ -45,435 +43,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);
- }
- }
+
}