You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mahout.apache.org by tuxdna <tu...@gmail.com> on 2014/04/26 16:45:23 UTC

How to apply an arbitrary function to all elements of a matrix using Math-Scala DSL?

First of all Mahout's Math-Scala DSL, is really nice. I can write
really compact code.

Here is a my problem description ( i referred to [1] ).

First I import all the required classes in Scala REPL:

import org.apache.mahout.math.{ Vector => MahoutVector }
import org.apache.mahout.math.scalabindings._
import org.apache.mahout.math.DenseMatrix
import scala.util.Random
import org.apache.mahout.math.Matrix
import Math.min
import RLikeOps._

Then I create a matrix

scala> val a = dense((1, 2, 3), (3, 4, 5))
a: org.apache.mahout.math.DenseMatrix =
{
  0  =>    {0:1.0,1:2.0,2:3.0}
  1  =>    {0:3.0,1:4.0,2:5.0}
}


Appply the function using a loop:

scala> for(r <- 0 until a.numRows; c <- 0 until a(r,::).size) { a(r,c)
= Math.log(a(r,c)) }
warning: there were 1 deprecation warnings; re-run with -deprecation for details

The resulting matrix

scala> a
res35: org.apache.mahout.math.DenseMatrix =
{
  0  =>    {1:0.6931471805599453,2:1.0986122886681098}
  1  =>    {0:1.0986122886681098,1:1.3862943611198906,2:1.6094379124341003}
}


I want to apply an arbitrary function ( such as Math.log, or a
function defined by me ), to a matrix or vector withoug using any
loops. This is possible in Octave/Matlab as described here [2].

How can I do this in Mahout using Scala DSL ?

Thanks in advance.

Regards,
Saleem

[1] https://mahout.apache.org/users/sparkbindings/home.html
[2] http://stackoverflow.com/questions/2470844/loopless-function-calls-on-vector-matrix-members-in-matlab-octave

Re: How to apply an arbitrary function to all elements of a matrix using Math-Scala DSL?

Posted by Dmitriy Lyubimov <dl...@gmail.com>.
i thought it was delegating to Matrix.assign(Function) in java api.


On Sat, Apr 26, 2014 at 11:49 AM, tuxdna <tu...@gmail.com> wrote:

> Hi Dmitriy,
>
> Thanks that worked just fine. I also figured out that it is
> implemented as a loop internally as of Mahout 0.9.
>
> > Note that this is not a sparsified operation though by default.
>
> >> Assignment accepts a function for inplace operators i.e scala way is
> >> a := (r,c,x) => log(x)
>
> Understood from [1] and [2].
>
> Perhaps MatrixOps.:=  should delegate the function call to
> VectorOps.:= for each of the row or column vectors?
>
>
> Regards,
> Saleem
>
> [1]
> https://github.com/apache/mahout/blob/mahout-0.9/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MatrixOps.scala#L126
> [2]
> https://github.com/apache/mahout/blob/mahout-0.9/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/VectorOps.scala#L61
>

Re: How to apply an arbitrary function to all elements of a matrix using Math-Scala DSL?

Posted by tuxdna <tu...@gmail.com>.
Hi Dmitriy,

Thanks that worked just fine. I also figured out that it is
implemented as a loop internally as of Mahout 0.9.

> Note that this is not a sparsified operation though by default.

>> Assignment accepts a function for inplace operators i.e scala way is
>> a := (r,c,x) => log(x)

Understood from [1] and [2].

Perhaps MatrixOps.:=  should delegate the function call to
VectorOps.:= for each of the row or column vectors?


Regards,
Saleem

[1] https://github.com/apache/mahout/blob/mahout-0.9/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MatrixOps.scala#L126
[2] https://github.com/apache/mahout/blob/mahout-0.9/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/VectorOps.scala#L61

Re: How to apply an arbitrary function to all elements of a matrix using Math-Scala DSL?

Posted by tuxdna <tu...@gmail.com>.
Hi Dmitriy,

Thanks that worked just fine. I also figured out that it is
implemented as a loop internally as of Mahout 0.9.

> Note that this is not a sparsified operation though by default.

>> Assignment accepts a function for inplace operators i.e scala way is
>> a := (r,c,x) => log(x)

Understood from [1] and [2].

Perhaps MatrixOps.:=  should delegate the function call to
VectorOps.:= for each of the row or column vectors?


Regards,
Saleem

[1] https://github.com/apache/mahout/blob/mahout-0.9/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MatrixOps.scala#L126
[2] https://github.com/apache/mahout/blob/mahout-0.9/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/VectorOps.scala#L61

Re: How to apply an arbitrary function to all elements of a matrix using Math-Scala DSL?

Posted by Dmitriy Lyubimov <dl...@gmail.com>.
Note that this is not a sparsified operation though by default.
On Apr 26, 2014 9:26 AM, "Dmitriy Lyubimov" <dl...@gmail.com> wrote:

> Assignment accepts a function for inplace operators i.e scala way is
> a := (r,c,x) => log(x)
>  On Apr 26, 2014 7:45 AM, "tuxdna" <tu...@gmail.com> wrote:
>
>> First of all Mahout's Math-Scala DSL, is really nice. I can write
>> really compact code.
>>
>> Here is a my problem description ( i referred to [1] ).
>>
>> First I import all the required classes in Scala REPL:
>>
>> import org.apache.mahout.math.{ Vector => MahoutVector }
>> import org.apache.mahout.math.scalabindings._
>> import org.apache.mahout.math.DenseMatrix
>> import scala.util.Random
>> import org.apache.mahout.math.Matrix
>> import Math.min
>> import RLikeOps._
>>
>> Then I create a matrix
>>
>> scala> val a = dense((1, 2, 3), (3, 4, 5))
>> a: org.apache.mahout.math.DenseMatrix =
>> {
>>   0  =>    {0:1.0,1:2.0,2:3.0}
>>   1  =>    {0:3.0,1:4.0,2:5.0}
>> }
>>
>>
>> Appply the function using a loop:
>>
>> scala> for(r <- 0 until a.numRows; c <- 0 until a(r,::).size) { a(r,c)
>> = Math.log(a(r,c)) }
>> warning: there were 1 deprecation warnings; re-run with -deprecation for
>> details
>>
>> The resulting matrix
>>
>> scala> a
>> res35: org.apache.mahout.math.DenseMatrix =
>> {
>>   0  =>    {1:0.6931471805599453,2:1.0986122886681098}
>>   1  =>
>>  {0:1.0986122886681098,1:1.3862943611198906,2:1.6094379124341003}
>> }
>>
>>
>> I want to apply an arbitrary function ( such as Math.log, or a
>> function defined by me ), to a matrix or vector withoug using any
>> loops. This is possible in Octave/Matlab as described here [2].
>>
>> How can I do this in Mahout using Scala DSL ?
>>
>> Thanks in advance.
>>
>> Regards,
>> Saleem
>>
>> [1] https://mahout.apache.org/users/sparkbindings/home.html
>> [2]
>> http://stackoverflow.com/questions/2470844/loopless-function-calls-on-vector-matrix-members-in-matlab-octave
>>
>

Re: How to apply an arbitrary function to all elements of a matrix using Math-Scala DSL?

Posted by Dmitriy Lyubimov <dl...@gmail.com>.
Note that this is not a sparsified operation though by default.
On Apr 26, 2014 9:26 AM, "Dmitriy Lyubimov" <dl...@gmail.com> wrote:

> Assignment accepts a function for inplace operators i.e scala way is
> a := (r,c,x) => log(x)
>  On Apr 26, 2014 7:45 AM, "tuxdna" <tu...@gmail.com> wrote:
>
>> First of all Mahout's Math-Scala DSL, is really nice. I can write
>> really compact code.
>>
>> Here is a my problem description ( i referred to [1] ).
>>
>> First I import all the required classes in Scala REPL:
>>
>> import org.apache.mahout.math.{ Vector => MahoutVector }
>> import org.apache.mahout.math.scalabindings._
>> import org.apache.mahout.math.DenseMatrix
>> import scala.util.Random
>> import org.apache.mahout.math.Matrix
>> import Math.min
>> import RLikeOps._
>>
>> Then I create a matrix
>>
>> scala> val a = dense((1, 2, 3), (3, 4, 5))
>> a: org.apache.mahout.math.DenseMatrix =
>> {
>>   0  =>    {0:1.0,1:2.0,2:3.0}
>>   1  =>    {0:3.0,1:4.0,2:5.0}
>> }
>>
>>
>> Appply the function using a loop:
>>
>> scala> for(r <- 0 until a.numRows; c <- 0 until a(r,::).size) { a(r,c)
>> = Math.log(a(r,c)) }
>> warning: there were 1 deprecation warnings; re-run with -deprecation for
>> details
>>
>> The resulting matrix
>>
>> scala> a
>> res35: org.apache.mahout.math.DenseMatrix =
>> {
>>   0  =>    {1:0.6931471805599453,2:1.0986122886681098}
>>   1  =>
>>  {0:1.0986122886681098,1:1.3862943611198906,2:1.6094379124341003}
>> }
>>
>>
>> I want to apply an arbitrary function ( such as Math.log, or a
>> function defined by me ), to a matrix or vector withoug using any
>> loops. This is possible in Octave/Matlab as described here [2].
>>
>> How can I do this in Mahout using Scala DSL ?
>>
>> Thanks in advance.
>>
>> Regards,
>> Saleem
>>
>> [1] https://mahout.apache.org/users/sparkbindings/home.html
>> [2]
>> http://stackoverflow.com/questions/2470844/loopless-function-calls-on-vector-matrix-members-in-matlab-octave
>>
>

Re: How to apply an arbitrary function to all elements of a matrix using Math-Scala DSL?

Posted by Dmitriy Lyubimov <dl...@gmail.com>.
Assignment accepts a function for inplace operators i.e scala way is
a := (r,c,x) => log(x)
 On Apr 26, 2014 7:45 AM, "tuxdna" <tu...@gmail.com> wrote:

> First of all Mahout's Math-Scala DSL, is really nice. I can write
> really compact code.
>
> Here is a my problem description ( i referred to [1] ).
>
> First I import all the required classes in Scala REPL:
>
> import org.apache.mahout.math.{ Vector => MahoutVector }
> import org.apache.mahout.math.scalabindings._
> import org.apache.mahout.math.DenseMatrix
> import scala.util.Random
> import org.apache.mahout.math.Matrix
> import Math.min
> import RLikeOps._
>
> Then I create a matrix
>
> scala> val a = dense((1, 2, 3), (3, 4, 5))
> a: org.apache.mahout.math.DenseMatrix =
> {
>   0  =>    {0:1.0,1:2.0,2:3.0}
>   1  =>    {0:3.0,1:4.0,2:5.0}
> }
>
>
> Appply the function using a loop:
>
> scala> for(r <- 0 until a.numRows; c <- 0 until a(r,::).size) { a(r,c)
> = Math.log(a(r,c)) }
> warning: there were 1 deprecation warnings; re-run with -deprecation for
> details
>
> The resulting matrix
>
> scala> a
> res35: org.apache.mahout.math.DenseMatrix =
> {
>   0  =>    {1:0.6931471805599453,2:1.0986122886681098}
>   1  =>    {0:1.0986122886681098,1:1.3862943611198906,2:1.6094379124341003}
> }
>
>
> I want to apply an arbitrary function ( such as Math.log, or a
> function defined by me ), to a matrix or vector withoug using any
> loops. This is possible in Octave/Matlab as described here [2].
>
> How can I do this in Mahout using Scala DSL ?
>
> Thanks in advance.
>
> Regards,
> Saleem
>
> [1] https://mahout.apache.org/users/sparkbindings/home.html
> [2]
> http://stackoverflow.com/questions/2470844/loopless-function-calls-on-vector-matrix-members-in-matlab-octave
>

Re: How to apply an arbitrary function to all elements of a matrix using Math-Scala DSL?

Posted by Dmitriy Lyubimov <dl...@gmail.com>.
Assignment accepts a function for inplace operators i.e scala way is
a := (r,c,x) => log(x)
 On Apr 26, 2014 7:45 AM, "tuxdna" <tu...@gmail.com> wrote:

> First of all Mahout's Math-Scala DSL, is really nice. I can write
> really compact code.
>
> Here is a my problem description ( i referred to [1] ).
>
> First I import all the required classes in Scala REPL:
>
> import org.apache.mahout.math.{ Vector => MahoutVector }
> import org.apache.mahout.math.scalabindings._
> import org.apache.mahout.math.DenseMatrix
> import scala.util.Random
> import org.apache.mahout.math.Matrix
> import Math.min
> import RLikeOps._
>
> Then I create a matrix
>
> scala> val a = dense((1, 2, 3), (3, 4, 5))
> a: org.apache.mahout.math.DenseMatrix =
> {
>   0  =>    {0:1.0,1:2.0,2:3.0}
>   1  =>    {0:3.0,1:4.0,2:5.0}
> }
>
>
> Appply the function using a loop:
>
> scala> for(r <- 0 until a.numRows; c <- 0 until a(r,::).size) { a(r,c)
> = Math.log(a(r,c)) }
> warning: there were 1 deprecation warnings; re-run with -deprecation for
> details
>
> The resulting matrix
>
> scala> a
> res35: org.apache.mahout.math.DenseMatrix =
> {
>   0  =>    {1:0.6931471805599453,2:1.0986122886681098}
>   1  =>    {0:1.0986122886681098,1:1.3862943611198906,2:1.6094379124341003}
> }
>
>
> I want to apply an arbitrary function ( such as Math.log, or a
> function defined by me ), to a matrix or vector withoug using any
> loops. This is possible in Octave/Matlab as described here [2].
>
> How can I do this in Mahout using Scala DSL ?
>
> Thanks in advance.
>
> Regards,
> Saleem
>
> [1] https://mahout.apache.org/users/sparkbindings/home.html
> [2]
> http://stackoverflow.com/questions/2470844/loopless-function-calls-on-vector-matrix-members-in-matlab-octave
>