Quantcast
Channel: Apache Timeline
Viewing all articles
Browse latest Browse all 5648

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

$
0
0
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

Viewing all articles
Browse latest Browse all 5648

Trending Articles