Author: dlyubimov
Date: Wed Jul 31 05:41:29 2013
New Revision: 1508732

URL: http://svn.apache.org/r1508732
Log:
MAHOUT-1282: Optimize Diagonal matrix multiplication. add left/right multiply 
versions

Squashed commit of the following:

commit 2aa56618c3129e6735a84f990defe12c238e0736
Merge: 4b23cd8 0afce8a
Author: Dmitriy Lyubimov <[email protected]>
Date:   Tue Jul 30 22:31:28 2013 -0700

    Merge branch 'trunk' into MAHOUT-1282

commit 4b23cd8870336d36d40792fa33c7524f69f40448
Author: Dmitriy Lyubimov <[email protected]>
Date:   Sat Jul 27 22:33:53 2013 -0700

    added license header.

commit 29f3722e0805bd2b3087dfa695b133c5bc8f67a4
Author: Dmitriy Lyubimov <[email protected]>
Date:   Sat Jul 27 22:25:15 2013 -0700

    Renaming associative multiplication to timesRight and timesLeft (to conform 
to generic "times").
    Added MatrixTimesOps marker interface to mark implementations supporting 
timesLeft and timesRight.

commit a353f53401207453497921b4bb36797450acf6dd
Author: Dmitriy Lyubimov <[email protected]>
Date:   Wed Jul 10 17:47:54 2013 -0700

    Adding optimized left and right multiplications to the diagonal matrix.

Added:
    mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixTimesOps.java
    
mahout/trunk/math/src/test/java/org/apache/mahout/math/DiagonalMatrixTest.java
Modified:
    mahout/trunk/math/src/main/java/org/apache/mahout/math/DiagonalMatrix.java

Modified: 
mahout/trunk/math/src/main/java/org/apache/mahout/math/DiagonalMatrix.java
URL: 
http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/DiagonalMatrix.java?rev=1508732&r1=1508731&r2=1508732&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/DiagonalMatrix.java 
(original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/DiagonalMatrix.java 
Wed Jul 31 05:41:29 2013
@@ -17,7 +17,7 @@
 
 package org.apache.mahout.math;
 
-public class DiagonalMatrix extends AbstractMatrix {
+public class DiagonalMatrix extends AbstractMatrix implements MatrixTimesOps {
   private final Vector diagonal;
 
   public DiagonalMatrix(Vector values) {
@@ -139,4 +139,30 @@ public class DiagonalMatrix extends Abst
   public Matrix viewPart(int[] offset, int[] size) {
     return new MatrixView(this, offset, size);
   }
+
+  @Override
+  public Matrix times(Matrix other) {
+    return timesRight(other);
+  }
+
+  @Override
+  public Matrix timesRight(Matrix that) {
+    if (that.numRows() != diagonal.size())
+      throw new IllegalArgumentException("Incompatible number of rows in the 
right operand of matrix multiplication.");
+    Matrix m = that.like();
+    for (int row = 0; row < diagonal.size(); row++)
+      m.assignRow(row, that.viewRow(row).times(diagonal.getQuick(row)));
+    return m;
+  }
+
+  @Override
+  public Matrix timesLeft(Matrix that) {
+    if (that.numCols() != diagonal.size())
+      throw new IllegalArgumentException(
+          "Incompatible number of rows in the left operand of matrix-matrix 
multiplication.");
+    Matrix m = that.like();
+    for (int col = 0; col < diagonal.size(); col++)
+      m.assignColumn(col, that.viewColumn(col).times(diagonal.getQuick(col)));
+    return m;
+  }
 }

Added: 
mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixTimesOps.java
URL: 
http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixTimesOps.java?rev=1508732&view=auto
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixTimesOps.java 
(added)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixTimesOps.java 
Wed Jul 31 05:41:29 2013
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mahout.math;
+
+/**
+ * Optional interface for optimized matrix multiplications.
+ * Some concrete Matrix implementations may mix this in.
+ */
+public interface MatrixTimesOps {
+  /**
+   * computes matrix product of (this * that)
+   */
+  Matrix timesRight(Matrix that);
+
+  /**
+   * Computes matrix product of (that * this)
+   */
+  Matrix timesLeft(Matrix that);
+
+}

Added: 
mahout/trunk/math/src/test/java/org/apache/mahout/math/DiagonalMatrixTest.java
URL: 
http://svn.apache.org/viewvc/mahout/trunk/math/src/test/java/org/apache/mahout/math/DiagonalMatrixTest.java?rev=1508732&view=auto
==============================================================================
--- 
mahout/trunk/math/src/test/java/org/apache/mahout/math/DiagonalMatrixTest.java 
(added)
+++ 
mahout/trunk/math/src/test/java/org/apache/mahout/math/DiagonalMatrixTest.java 
Wed Jul 31 05:41:29 2013
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mahout.math;
+
+import org.apache.mahout.math.function.Functions;
+import org.junit.Test;
+
+public class DiagonalMatrixTest extends MahoutTestCase {
+  @Test
+  public void testBasics() {
+    DiagonalMatrix a = new DiagonalMatrix(new double[]{1, 2, 3, 4});
+
+    assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 2, 
3, 4})).norm(1), 1.0e-10);
+    assertEquals(0, a.viewPart(0, 3, 0, 3).viewDiagonal().minus(
+      new DenseVector(new double[]{1, 2, 3})).norm(1), 1.0e-10);
+
+    assertEquals(4, a.get(3, 3), 1.0e-10);
+
+    Matrix m = new DenseMatrix(4, 4);
+    m.assign(a);
+
+    assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 
1.0e-10);
+
+    assertEquals(0, 
m.transpose().times(m).minus(a.transpose().times(a)).aggregate(
+      Functions.PLUS, Functions.ABS), 1.0e-10);
+    assertEquals(0, m.plus(m).minus(a.plus(a)).aggregate(Functions.PLUS, 
Functions.ABS), 1.0e-10);
+
+    m = new DenseMatrix(new double[][]{{1, 2, 3, 4}, {5, 6, 7, 8}});
+
+    assertEquals(100, a.timesLeft(m).aggregate(Functions.PLUS, Functions.ABS), 
1.0e-10);
+    assertEquals(100, a.times(m.transpose()).aggregate(Functions.PLUS, 
Functions.ABS), 1.0e-10);
+  }
+
+}


Reply via email to