How to apply a user defined function to the breeze matrix elementwise

222 views
Skip to first unread message

Alexander Ulanov

unread,
Oct 21, 2015, 1:42:24 PM10/21/15
to Scala Breeze
Dear Breeze developers,

I need to apply user-defined function to the breeze matrix or vector in element-wise manner. Functions can have 1,2 and 3 arguments: 
e.g. Double => Double, (Double, Double) => Double and (Double, Double, Double) => Double. 
I need also to provide the matrix that should be used to store the result of function application breeze.linalg.DenseMatrix[Double]. Can I use UFunc with InPlaceImpl? If yes, could you give me an example?

Best regards, Alexander

David Hall

unread,
Oct 21, 2015, 4:03:37 PM10/21/15
to scala-...@googlegroups.com
to be clear, you presumably want something that takes 1, 2 and 3 identically structured matrices?

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze...@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-breeze/c1d940e5-40c8-4bad-8978-923174a4cff9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexander Ulanov

unread,
Oct 21, 2015, 5:11:04 PM10/21/15
to Scala Breeze
Yes

David Hall

unread,
Oct 22, 2015, 12:20:14 AM10/22/15
to scala-...@googlegroups.com
ok, well, there's .map on DenseMatrix, of course, but 2-ary and 3-ary there's nothing atm. I'll see about making something nowish

David Hall

unread,
Oct 22, 2015, 1:24:02 AM10/22/15
to scala-...@googlegroups.com
Already, pushed a change to allow easy UFuncification for binary and unary ufuncs:

test("WrappedUFunc") {
val f = UFunc(math.exp _)
assert(f(3.0) == math.exp(3.0))
assert(f(DenseVector(3.0)) == DenseVector(math.exp(3.0)))
assert(f(DenseMatrix(3.0)) == DenseMatrix(math.exp(3.0)))
}

test("WrappedUFunc2") {
val f = UFunc(math.pow _)
assert(f(3.0, 4.0) == math.pow(3.0, 4.0))
assert(f(DenseVector(3.0), 4.0) == DenseVector(math.pow(3.0, 4.0)))
assert(f(DenseMatrix(3.0), DenseMatrix(4.0)) == DenseMatrix(math.pow(3.0, 4.0)))
}

David Hall

unread,
Oct 22, 2015, 1:24:13 AM10/22/15
to scala-...@googlegroups.com
already -> alright

Alexander Ulanov

unread,
Oct 22, 2015, 3:37:41 PM10/22/15
to Scala Breeze
Thank you! Does that allow specifying the output DenseMatrix? I.e. there is matrix X, I want to apply function Double => Double and put the result in the matrix Y?

David Hall

unread,
Oct 22, 2015, 4:12:48 PM10/22/15
to scala-...@googlegroups.com

CanMapValues and CanZipMapValues are the type classes to use

Alexander Ulanov

unread,
Oct 22, 2015, 9:04:19 PM10/22/15
to Scala Breeze
Could you give an example? If I want to compute exponent of x and put the result to y?

var x: BDV[Double]
var y: BDV[Double]
val f = UFunc(math.exp _)
....?

David Hall

unread,
Oct 23, 2015, 1:31:22 AM10/23/15
to scala-...@googlegroups.com
ah, I misunderstood. Breeze has basically no support right now for specifying "sinks" for an operation. It's something I want to add, but I haven't gotten there yet.

Reply all
Reply to author
Forward
0 new messages