You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by je...@apache.org on 2008/04/18 21:28:51 UTC
svn commit: r649662 - in /lucene/mahout/trunk/src:
main/java/org/apache/mahout/matrix/MatrixView.java
test/java/org/apache/mahout/matrix/TestMatrixView.java
Author: jeastman
Date: Fri Apr 18 12:28:49 2008
New Revision: 649662
URL: http://svn.apache.org/viewvc?rev=649662&view=rev
Log:
Committed new TextMatrixView and some defect repairs in MatrixView from MAHOUT-23b
Added:
lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestMatrixView.java
Modified:
lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/MatrixView.java
Modified: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/MatrixView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/MatrixView.java?rev=649662&r1=649661&r2=649662&view=diff
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/MatrixView.java (original)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/MatrixView.java Fri Apr 18 12:28:49 2008
@@ -19,7 +19,6 @@
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
-
/**
* Implements subset view of a Matrix
*/
@@ -35,6 +34,7 @@
/**
* Construct a view of the matrix with given offset and cardinality
+ *
* @param matrix an underlying Matrix
* @param offset the int[2] offset into the underlying matrix
* @param cardinality the int[2] cardinality of the view
@@ -46,15 +46,18 @@
this.cardinality = cardinality;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
*/
@Override
public WritableComparable asWritableComparable() {
StringBuilder out = new StringBuilder();
- out.append("[[, ");
- for (int row = offset[ROW]; row < offset[ROW] + cardinality[ROW]; row++) {
- for (int col = offset[COL]; col < offset[COL] + cardinality[COL]; row++)
+ out.append("[, ");
+ for (int row = 0; row < cardinality[ROW]; row++) {
+ out.append("[, ");
+ for (int col = 0; col < cardinality[COL]; col++)
out.append(getQuick(row, col)).append(", ");
out.append("], ");
}
@@ -62,7 +65,9 @@
return new Text(out.toString());
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#cardinality()
*/
@Override
@@ -70,7 +75,9 @@
return cardinality;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#copy()
*/
@Override
@@ -78,7 +85,9 @@
return new MatrixView(matrix.copy(), offset, cardinality);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#getQuick(int, int)
*/
@Override
@@ -86,15 +95,19 @@
return matrix.getQuick(offset[ROW] + row, offset[COL] + column);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#like()
*/
@Override
public Matrix like() {
- return matrix.like();
+ return matrix.like(cardinality[ROW], cardinality[COL]);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#like(int, int)
*/
@Override
@@ -103,7 +116,9 @@
return matrix.like(rows, columns);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int, double)
*/
@Override
@@ -111,7 +126,9 @@
matrix.setQuick(offset[ROW] + row, offset[COL] + column, value);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#size()
*/
@Override
@@ -119,7 +136,9 @@
return cardinality;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#toArray()
*/
@Override
@@ -147,7 +166,9 @@
return result;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#haveSharedCells(org.apache.mahout.matrix.Matrix)
*/
@Override
@@ -158,46 +179,62 @@
return other.haveSharedCells(matrix);
}
- /* (non-Javadoc)
- * @see org.apache.mahout.matrix.AbstractMatrix#assignColumn(int, org.apache.mahout.vector.Vector)
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.mahout.matrix.AbstractMatrix#assignColumn(int,
+ * org.apache.mahout.vector.Vector)
*/
@Override
public Matrix assignColumn(int column, Vector other)
throws CardinalityException {
if (cardinality[ROW] != other.cardinality())
throw new CardinalityException();
- matrix.assignColumn(column + offset[COL], other);
+ for (int row = 0; row < cardinality[ROW]; row++)
+ matrix.setQuick(row + offset[ROW], column + offset[COL], other
+ .getQuick(row));
return this;
}
- /* (non-Javadoc)
- * @see org.apache.mahout.matrix.AbstractMatrix#assignRow(int, org.apache.mahout.vector.Vector)
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.mahout.matrix.AbstractMatrix#assignRow(int,
+ * org.apache.mahout.vector.Vector)
*/
@Override
public Matrix assignRow(int row, Vector other) throws CardinalityException {
if (cardinality[COL] != other.cardinality())
throw new CardinalityException();
- matrix.assignRow(row + offset[ROW], other);
+ for (int col = 0; col < cardinality[COL]; col++)
+ matrix
+ .setQuick(row + offset[ROW], col + offset[COL], other.getQuick(col));
return this;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#getColumn(int)
*/
@Override
public Vector getColumn(int column) throws IndexException {
if (column < 0 || column >= cardinality[COL])
throw new IndexException();
- return matrix.getColumn(column + offset[COL]);
+ return new VectorView(matrix.getColumn(column + offset[COL]), offset[ROW],
+ cardinality[ROW]);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.mahout.matrix.AbstractMatrix#getRow(int)
*/
@Override
public Vector getRow(int row) throws IndexException {
if (row < 0 || row >= cardinality[ROW])
throw new IndexException();
- return matrix.getRow(row + offset[ROW]);
+ return new VectorView(matrix.getRow(row + offset[ROW]), offset[COL],
+ cardinality[COL]);
}
}
Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestMatrixView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestMatrixView.java?rev=649662&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestMatrixView.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestMatrixView.java Fri Apr 18 12:28:49 2008
@@ -0,0 +1,480 @@
+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 TestMatrixView extends TestCase {
+ private static int ROW = AbstractMatrix.ROW;
+
+ private static int COL = AbstractMatrix.COL;
+
+ private double[][] values = { { 0.0, 1.1, 2.2 }, { 1.1, 2.2, 3.3 },
+ { 3.3, 4.4, 5.5 }, { 5.5, 6.6, 7.7 }, { 7.7, 8.8, 9.9 } };
+
+ private Matrix test;
+
+ public TestMatrixView(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ int[] offset = { 1, 1 };
+ int[] card = { 3, 2 };
+ test = new MatrixView(new DenseMatrix(values), offset, card);
+ }
+
+ public void testAsFormatString() {
+ assertEquals("format",
+ "[, [, 2.2, 3.3, ], [, 4.4, 5.5, ], [, 6.6, 7.7, ], ] ", test
+ .asWritableComparable().toString());
+ }
+
+ public void testCardinality() {
+ int[] c = test.cardinality();
+ assertEquals("row cardinality", values.length - 2, c[ROW]);
+ assertEquals("col cardinality", values[0].length - 1, c[COL]);
+ }
+
+ public void testCopy() {
+ int[] c = test.cardinality();
+ Matrix copy = test.copy();
+ assertTrue("wrong class", copy instanceof MatrixView);
+ for (int row = 0; row < c[ROW]; row++)
+ for (int col = 0; col < c[COL]; col++)
+ assertEquals("value[" + row + "][" + col + "]",
+ test.getQuick(row, col), copy.getQuick(row, col));
+ }
+
+ public void testGetQuick() {
+ int[] c = test.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], 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 - 2, c[ROW]);
+ assertEquals("col size", values[0].length - 1, 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 + 1][col + 1], 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 + 2][col + 2], view.getQuick(row, col));
+ }
+
+ public void testViewPartCardinality() {
+ int[] offset = { 1, 1 };
+ int[] size = { 3, 3 };
+ try {
+ test.viewPart(offset, size);
+ fail("exception expected");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ } catch (IndexException e) {
+ fail("cardinality exception expected");
+ }
+ }
+
+ public void testViewPartIndexOver() {
+ int[] offset = { 1, 1 };
+ int[] size = { 2, 2 };
+ try {
+ test.viewPart(offset, size);
+ fail("exception expected");
+ } catch (CardinalityException e) {
+ fail("index exception expected");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testViewPartIndexUnder() {
+ int[] offset = { -1, -1 };
+ int[] size = { 2, 2 };
+ try {
+ test.viewPart(offset, size);
+ fail("exception expected");
+ } catch (CardinalityException e) {
+ fail("index exception expected");
+ } catch (IndexException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testAssignDouble() {
+ int[] c = test.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 + 1][col + 1], test.getQuick(row, col));
+ }
+
+ public void testAssignMatrixBinaryFunctionCardinality() {
+ try {
+ test.assign(test.transpose(), new PlusFunction());
+ fail("exception expected");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testAssignMatrix() throws Exception {
+ int[] c = test.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 + 1][col + 1], 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 + 1][col + 1] / 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 + 1][col + 1], 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 + 1][col + 1] + 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 + 1][col + 1] * 2, value.getQuick(row, col));
+ }
+
+ public void testPlusMatrixCardinality() {
+ try {
+ test.plus(test.transpose());
+ fail("cardinality exception expected");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testSetUnder() {
+ int[] c = test.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 + 1][col + 1] * 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", 29.7, sum);
+ }
+
+ public void testAssignRow() throws Exception {
+ double[] data = { 2.1, 3.2 };
+ test.assignRow(1, new DenseVector(data));
+ assertEquals("test[1][0]", 2.1, test.getQuick(1, 0));
+ assertEquals("test[1][1]", 3.2, test.getQuick(1, 1));
+ }
+
+ public void testAssignRowCardinality() {
+ double[] data = { 2.1, 3.2, 4.3 };
+ try {
+ test.assignRow(1, new DenseVector(data));
+ fail("expecting cardinality exception");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testAssignColumn() throws Exception {
+ double[] data = { 2.1, 3.2, 4.3 };
+ test.assignColumn(1, new DenseVector(data));
+ assertEquals("test[0][1]", 2.1, test.getQuick(0, 1));
+ assertEquals("test[1][1]", 3.2, test.getQuick(1, 1));
+ assertEquals("test[2][1]", 4.3, test.getQuick(2, 1));
+ }
+
+ public void testAssignColumnCardinality() {
+ double[] data = { 2.1, 3.2 };
+ try {
+ test.assignColumn(1, new DenseVector(data));
+ fail("expecting cardinality exception");
+ } catch (CardinalityException e) {
+ assertTrue(true);
+ }
+ }
+
+ public void testGetRow() throws Exception {
+ Vector row = test.getRow(1);
+ assertEquals("row size", 2, row.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);
+ }
+ }
+
+}