1

I am new to breeze. Please help with this formula

I have a Vector called data, a constant called alpha, and another Vector called delay. I want to generate an output Vector adjData.

The elemental operation is

adjData_i = (1 - alpha * delay_i)Data_i

how to express this using vector operation in Breeze?

bhomass
  • 3,414
  • 8
  • 45
  • 75

2 Answers2

2
val adjData = (1.0 - delay * alpha) :* data

* is for scalar or matrix multiplication (not elementwise), while :* is elementwise multiplication, as described in the Breeze Cheat Sheet. Otherwise, it mirrors your formula closely.

dlwh
  • 2,257
  • 11
  • 23
  • Could you provide an explanation for this answer? Thanks! – meetar Sep 11 '15 at 01:21
  • Not quite. Remember, delay is a Vector, not a constant. (1.0 - delay * alpha) does not work because of that. Another word, 1.0 is also a Vector of 1.0's. – bhomass Sep 11 '15 at 21:25
  • What. I'm pretty sure I'm right. Did you even try it: scala> import breeze.linalg._ import breeze.linalg._ scala> val delay = DenseVector.rand(5) delay: breeze.linalg.DenseVector[Double] = DenseVector(0.9776512215294542, 0.520453583016343, 0.0707914240024825, 0.2683433214302806, 0.7361769748712919) scala> val data = DenseVector.rand(5) data: breeze.linalg.DenseVector[Double] = DenseVector(0.7326643036788687, 0.9681615533879682, 0.13045469055706738, 0.41302213717901903, 0.0793376135032593) scala> (1.0 - delay * 0.5) :* data res0: breeze.linalg.DenseVector[Double] = ... – dlwh Sep 12 '15 at 02:00
  • Again, delay is a Vector. you are still treating it like a scalar. – bhomass Sep 14 '15 at 03:43
  • 2
    I literally wrote Breeze. I know how it works. He wants to do elementwise multiplication of two vectors, after scaling and a single operation. My code literally produces the same answer yours does, except it doesn't use nearly as many allocations. – dlwh Sep 14 '15 at 04:15
-1

I think this does the job

(DenseVector.ones[Double](n) - DenseVector.fill(n){alpha} :* delay) :* data
bhomass
  • 3,414
  • 8
  • 45
  • 75
  • 1
    no. you can't multiply two vectors using `*`, only `:*`. that's the whole point. – dlwh Sep 12 '15 at 02:05
  • An answer solely consisting of code with no or little explanation makes it difficult to understand for future visitors which mistakes were made originally or which approach should be taken and why your answer is viable. Please add explanations to your code. – Sebastian Simon Sep 12 '15 at 04:08
  • to dlwh, I omitted one : in the first *. I have added it in since. The key is the entire term must be presented as Vectors, thus, the DenseVector.ones[Double](n) and DenseVector.ones[Double](n) parts. – bhomass Sep 14 '15 at 03:45
  • here is more explanation of the breeze lib. (DenseVector.ones[Double](n) creates a vector of n dimension, whose elements are all 1. DenseVector.fill(n){alpha} creates another vector, whose elements are all alpha. DenseVector.fill(n){alpha} :* delay is an element wise vector multiplication. Hope its clear now. – bhomass Sep 14 '15 at 03:45