You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by dl...@apache.org on 2014/08/22 21:27:27 UTC

git commit: (NOJIRA) adding scalar-associated operations to enable things such as 5 * matrix, 5 * vector

Repository: mahout
Updated Branches:
  refs/heads/master 656225c67 -> 336f0e7db


(NOJIRA) adding scalar-associated operations to enable things such as 5 * matrix, 5 * vector

Conflicts:

	math-scala/src/test/scala/org/apache/mahout/math/scalabindings/VectorOpsSuite.scala


Project: http://git-wip-us.apache.org/repos/asf/mahout/repo
Commit: http://git-wip-us.apache.org/repos/asf/mahout/commit/336f0e7d
Tree: http://git-wip-us.apache.org/repos/asf/mahout/tree/336f0e7d
Diff: http://git-wip-us.apache.org/repos/asf/mahout/diff/336f0e7d

Branch: refs/heads/master
Commit: 336f0e7dba88253838397dc2c579d5a6a472870f
Parents: 656225c
Author: Dmitriy Lyubimov <dl...@apache.org>
Authored: Fri Aug 22 12:10:33 2014 -0700
Committer: Dmitriy Lyubimov <dl...@apache.org>
Committed: Fri Aug 22 12:12:59 2014 -0700

----------------------------------------------------------------------
 .../mahout/math/drm/DrmDoubleScalarOps.scala    | 33 +++++++++++++++
 .../apache/mahout/math/drm/RLikeDrmOps.scala    |  7 ++++
 .../math/scalabindings/DoubleScalarOps.scala    | 42 ++++++++++++++++++++
 .../mahout/math/scalabindings/MatrixOps.scala   |  2 +
 .../math/scalabindings/RLikeMatrixOps.scala     |  2 +
 .../mahout/math/scalabindings/RLikeOps.scala    |  2 +
 .../math/scalabindings/RLikeVectorOps.scala     |  3 ++
 .../mahout/math/scalabindings/VectorOps.scala   |  2 +
 .../mahout/math/drm/RLikeDrmOpsSuiteBase.scala  | 14 ++++++-
 .../scalabindings/RLikeMatrixOpsSuite.scala     | 14 +++++++
 .../math/scalabindings/VectorOpsSuite.scala     | 10 +++++
 11 files changed, 130 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mahout/blob/336f0e7d/math-scala/src/main/scala/org/apache/mahout/math/drm/DrmDoubleScalarOps.scala
----------------------------------------------------------------------
diff --git a/math-scala/src/main/scala/org/apache/mahout/math/drm/DrmDoubleScalarOps.scala b/math-scala/src/main/scala/org/apache/mahout/math/drm/DrmDoubleScalarOps.scala
new file mode 100644
index 0000000..e5cf563
--- /dev/null
+++ b/math-scala/src/main/scala/org/apache/mahout/math/drm/DrmDoubleScalarOps.scala
@@ -0,0 +1,33 @@
+/*
+ * 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.drm
+
+import RLikeDrmOps._
+import scala.reflect.ClassTag
+
+class DrmDoubleScalarOps(val x:Double) extends AnyVal{
+
+  def +[K:ClassTag](that:DrmLike[K]) = that + x
+
+  def *[K:ClassTag](that:DrmLike[K]) = that * x
+
+  def -[K:ClassTag](that:DrmLike[K]) = x -: that
+
+  def /[K:ClassTag](that:DrmLike[K]) = x /: that
+
+}

http://git-wip-us.apache.org/repos/asf/mahout/blob/336f0e7d/math-scala/src/main/scala/org/apache/mahout/math/drm/RLikeDrmOps.scala
----------------------------------------------------------------------
diff --git a/math-scala/src/main/scala/org/apache/mahout/math/drm/RLikeDrmOps.scala b/math-scala/src/main/scala/org/apache/mahout/math/drm/RLikeDrmOps.scala
index 15a0975..026ab75 100644
--- a/math-scala/src/main/scala/org/apache/mahout/math/drm/RLikeDrmOps.scala
+++ b/math-scala/src/main/scala/org/apache/mahout/math/drm/RLikeDrmOps.scala
@@ -35,12 +35,16 @@ class RLikeDrmOps[K: ClassTag](drm: DrmLike[K]) extends DrmLikeOps[K](drm) {
 
   def +(that: Double): DrmLike[K] = OpAewScalar[K](A = this, scalar = that, op = "+")
 
+  def +:(that: Double): DrmLike[K] = OpAewScalar[K](A = this, scalar = that, op = "+")
+
   def -(that: Double): DrmLike[K] = OpAewScalar[K](A = this, scalar = that, op = "-")
 
   def -:(that: Double): DrmLike[K] = OpAewScalar[K](A = this, scalar = that, op = "-:")
 
   def *(that: Double): DrmLike[K] = OpAewScalar[K](A = this, scalar = that, op = "*")
 
+  def *:(that: Double): DrmLike[K] = OpAewScalar[K](A = this, scalar = that, op = "*")
+
   def /(that: Double): DrmLike[K] = OpAewScalar[K](A = this, scalar = that, op = "/")
 
   def /:(that: Double): DrmLike[K] = OpAewScalar[K](A = this, scalar = that, op = "/:")
@@ -110,6 +114,9 @@ class RLikeDrmIntOps(drm: DrmLike[Int]) extends RLikeDrmOps[Int](drm) {
 }
 
 object RLikeDrmOps {
+
+  implicit def double2ScalarOps(x:Double) = new DrmDoubleScalarOps(x)
+
   implicit def drmInt2RLikeOps(drm: DrmLike[Int]): RLikeDrmIntOps = new RLikeDrmIntOps(drm)
 
   implicit def drm2RLikeOps[K: ClassTag](drm: DrmLike[K]): RLikeDrmOps[K] = new RLikeDrmOps[K](drm)

http://git-wip-us.apache.org/repos/asf/mahout/blob/336f0e7d/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/DoubleScalarOps.scala
----------------------------------------------------------------------
diff --git a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/DoubleScalarOps.scala b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/DoubleScalarOps.scala
new file mode 100644
index 0000000..9fdd6e5
--- /dev/null
+++ b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/DoubleScalarOps.scala
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mahout.math.scalabindings
+
+import org.apache.mahout.math._
+
+class DoubleScalarOps(val x:Double) extends AnyVal{
+
+  import RLikeOps._
+
+  def +(that:Matrix) = that + x
+
+  def +(that:Vector) = that + x
+
+  def *(that:Matrix) = that * x
+
+  def *(that:Vector) = that * x
+
+  def -(that:Matrix) = x -: that
+
+  def -(that:Vector) = x -: that
+
+  def /(that:Matrix) = x /: that
+
+  def /(that:Vector) = x /: that
+
+}

http://git-wip-us.apache.org/repos/asf/mahout/blob/336f0e7d/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MatrixOps.scala
----------------------------------------------------------------------
diff --git a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MatrixOps.scala b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MatrixOps.scala
index 732a98a..7abcece 100644
--- a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MatrixOps.scala
+++ b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MatrixOps.scala
@@ -61,6 +61,8 @@ class MatrixOps(val m: Matrix) {
 
   def +(that: Double) = cloned += that
 
+  def +:(that:Double) = cloned += that
+
   def -(that: Double) = cloned -= that
 
   def -:(that: Double) = that -=: cloned

http://git-wip-us.apache.org/repos/asf/mahout/blob/336f0e7d/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeMatrixOps.scala
----------------------------------------------------------------------
diff --git a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeMatrixOps.scala b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeMatrixOps.scala
index af53c51..97e06cf 100644
--- a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeMatrixOps.scala
+++ b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeMatrixOps.scala
@@ -47,6 +47,8 @@ class RLikeMatrixOps(m: Matrix) extends MatrixOps(m) {
 
   def *(that: Double) = cloned *= that
 
+  def *:(that:Double) = cloned *= that
+
   def /(that: Matrix) = cloned /= that
 
   def /:(that: Matrix) = that / m

http://git-wip-us.apache.org/repos/asf/mahout/blob/336f0e7d/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeOps.scala
----------------------------------------------------------------------
diff --git a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeOps.scala b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeOps.scala
index c96526f..ba32304 100644
--- a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeOps.scala
+++ b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeOps.scala
@@ -24,6 +24,8 @@ import org.apache.mahout.math.{Vector, MatrixTimesOps, Matrix}
  */
 object RLikeOps {
 
+  implicit def double2Scalar(x:Double) = new DoubleScalarOps(x)
+
   implicit def v2vOps(v: Vector) = new RLikeVectorOps(v)
 
   implicit def el2elOps(el: Vector.Element) = new ElementOps(el)

http://git-wip-us.apache.org/repos/asf/mahout/blob/336f0e7d/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeVectorOps.scala
----------------------------------------------------------------------
diff --git a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeVectorOps.scala b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeVectorOps.scala
index dbde836..d2198bd 100644
--- a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeVectorOps.scala
+++ b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/RLikeVectorOps.scala
@@ -52,6 +52,9 @@ class RLikeVectorOps(_v: Vector) extends VectorOps(_v) {
   /** Elementwise * */
   def *(that: Double) = cloned *= that
 
+  /** Elementwise * */
+  def *:(that: Double) = cloned *= that
+
   /** Elementwise / */
   def /(that: Vector) = cloned /= that
 

http://git-wip-us.apache.org/repos/asf/mahout/blob/336f0e7d/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/VectorOps.scala
----------------------------------------------------------------------
diff --git a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/VectorOps.scala b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/VectorOps.scala
index c1b5a69..48c1619 100644
--- a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/VectorOps.scala
+++ b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/VectorOps.scala
@@ -95,6 +95,8 @@ class VectorOps(private[scalabindings] val v: Vector) {
 
   def +(that: Double) = cloned += that
 
+  def +:(that: Double) = cloned += that
+
   def -(that: Double) = cloned -= that
 
   def -:(that: Double) = that -=: v.cloned

http://git-wip-us.apache.org/repos/asf/mahout/blob/336f0e7d/math-scala/src/test/scala/org/apache/mahout/math/drm/RLikeDrmOpsSuiteBase.scala
----------------------------------------------------------------------
diff --git a/math-scala/src/test/scala/org/apache/mahout/math/drm/RLikeDrmOpsSuiteBase.scala b/math-scala/src/test/scala/org/apache/mahout/math/drm/RLikeDrmOpsSuiteBase.scala
index 76f4624..2e6204d 100644
--- a/math-scala/src/test/scala/org/apache/mahout/math/drm/RLikeDrmOpsSuiteBase.scala
+++ b/math-scala/src/test/scala/org/apache/mahout/math/drm/RLikeDrmOpsSuiteBase.scala
@@ -534,5 +534,17 @@ trait RLikeDrmOpsSuiteBase extends DistributedMahoutSuite with Matchers {
 
     (A.rbind(emptyB) -: controlC).norm should be < 1e-10
   }
-  
+
+  /** Test dsl overloads over scala operations over matrices */
+  test("scalarOps") {
+    val drmA = drmParallelize(m = dense(
+      (1, 2, 3),
+      (3, 4, 5),
+      (7, 8, 9)
+    ),
+      numPartitions = 2)
+
+    (10 * drmA - (10 *: drmA)).norm shouldBe 0
+
+  }
 }

http://git-wip-us.apache.org/repos/asf/mahout/blob/336f0e7d/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/RLikeMatrixOpsSuite.scala
----------------------------------------------------------------------
diff --git a/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/RLikeMatrixOpsSuite.scala b/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/RLikeMatrixOpsSuite.scala
index f0a2e7a..a943c5f 100644
--- a/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/RLikeMatrixOpsSuite.scala
+++ b/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/RLikeMatrixOpsSuite.scala
@@ -63,4 +63,18 @@ class RLikeMatrixOpsSuite extends FunSuite with MahoutSuite {
 
   }
 
+  /** Test dsl overloads over scala operations over matrices */
+  test ("scalarOps") {
+    val a = dense(
+      (1, 2, 3),
+      (3, 4, 5)
+    )
+
+    (10 * a - (10 *: a)).norm shouldBe 0
+    (10 + a - (10 +: a)).norm shouldBe 0
+    (10 - a - (10 -: a)).norm shouldBe 0
+    (10 / a - (10 /: a)).norm shouldBe 0
+
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/mahout/blob/336f0e7d/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/VectorOpsSuite.scala
----------------------------------------------------------------------
diff --git a/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/VectorOpsSuite.scala b/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/VectorOpsSuite.scala
index fda2038..037f562 100644
--- a/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/VectorOpsSuite.scala
+++ b/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/VectorOpsSuite.scala
@@ -69,4 +69,14 @@ class VectorOpsSuite extends FunSuite with MahoutSuite {
 
   }
 
+  test ("scalarOps") {
+    val a = dvec(1 to 5):Vector
+
+    10 * a shouldBe 10 *: a
+    10 + a shouldBe 10 +: a
+    10 - a shouldBe 10 -: a
+    10 / a shouldBe 10 /: a
+
+  }
+
 }