How to perform elementary scalar operations on a vector using Scala Breeze?

Using Scalala, it was possible to perform elementary operations on a vector using a scalar operand. Let's say you have a vector of random numbers between 0 and 1, and you want to subtract each value from 1:

import breeze.linalg._ val x = DenseVector.rand(5) val y = 1d :- x //DOESN'T COMPILE: "value :- is not a member of Double" 

Unlike Scalala, Breeze cannot compile this approach. You can get around this by creating a vector of them, but it seems like there should be a better way.

 val y = DenseVector.ones[Double](x.size) :- x 

Another workaround would be to use the mapValues ​​method, which is more readable:

 val y = x mapValues { 1 - _ } 

What is the right way to achieve this with Breeze?

+4
source share

Source: https://habr.com/ru/post/1441166/


All Articles