# Matrix multiplication and element-wise operations without extra pre-allocation of memory

**URL:** https://discourse.julialang.org/t/matrix-multiplication-and-element-wise-operations-without-extra-pre-allocation-of-memory/42855
**Category:** Performance
**Created:** [July 10, 2020, 5:18pm UTC](https://discourse.julialang.org/t/matrix-multiplication-and-element-wise-operations-without-extra-pre-allocation-of-memory/42855 "2020-07-10T17:18:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [July 10, 2020, 5:18pm UTC](https://discourse.julialang.org/t/matrix-multiplication-and-element-wise-operations-without-extra-pre-allocation-of-memory/42855/1 "2020-07-10T17:18:33Z")

</div>

Hi, I would like to perform the following element operations to a matrix **W** (_m_ rows and _n_ columns):

_W_(_i_, _j_) := _a_\*_W_(_i_, _j_) - _b_\*_d_(_j_)_y_(_i_),

where _a_ and _b_ are scalars, _d_(_j_) are the elements of a vector **d** with _n_ elements, and _y_(_i_) are the elements of a vector **y** with _m_ elements.  
In Julia, this can be easily programmed using the following code:

`W .= a.*W .- b.*(y*d')`

Although the dots prevent some memory allocation, matrix product `y*d'` allocates memory. (This operation is to be performed multiple time inside a cycle.)

Is there any way to do the whole operation with simple operations and without memory allocation (if there was no matrix product this could be easy done with broadcasting)? Or the only way out is to do two nested for cycles?

I can pre-allocate memory for the matrix product, but I would like to avoid that.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [July 10, 2020, 5:26pm UTC](https://discourse.julialang.org/t/matrix-multiplication-and-element-wise-operations-without-extra-pre-allocation-of-memory/42855/2 "2020-07-10T17:26:30Z")

</div>

Sure, you just need one more dot:

```julia
W = rand(3,4); a,b = rand(2); d = rand(4); y = rand(3);
W2 = similar(W);
f1!(W2, W,a,b,d,y) = W2 .= a.*W .- b.*(y*d')
f2!(W2, W,a,b,d,y) = W2 .= a.*W .- b.*(y .* d')
using Einsum
f3!(W2, W,a,b,d,y) = @einsum W2[i,j] = a * W[i,j] - b * y[i] * d[j]

```

```julia
julia> @btime f1!($W2, $W,$a,$b,$d,$y);
  92.883 ns (1 allocation: 176 bytes)

julia> @btime f2!($W2, $W,$a,$b,$d,$y);
  37.475 ns (0 allocations: 0 bytes)

julia> @btime f3!($W2, $W,$a,$b,$d,$y);
  28.712 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [July 10, 2020, 5:32pm UTC](https://discourse.julialang.org/t/matrix-multiplication-and-element-wise-operations-without-extra-pre-allocation-of-memory/42855/3 "2020-07-10T17:32:11Z")

</div>

Wow, beautiful! Thank you very much.

---

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [July 10, 2020, 5:38pm UTC](https://discourse.julialang.org/t/matrix-multiplication-and-element-wise-operations-without-extra-pre-allocation-of-memory/42855/4 "2020-07-10T17:38:29Z")

</div>

Could one enforce matrix multiplication if there were two squares matrices with the same size? I guess in this case the `.*` would do element-wise multiplication.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [July 10, 2020, 5:42pm UTC](https://discourse.julialang.org/t/matrix-multiplication-and-element-wise-operations-without-extra-pre-allocation-of-memory/42855/5 "2020-07-10T17:42:51Z")

</div>

Not sure I follow. On matrices `A * B` and `A .* B` are quite different, it’s just for vectors that `v * w'` and `v .* w'` happen to agree. But there is `mul!`, if that’s what you want?

---

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [July 10, 2020, 5:45pm UTC](https://discourse.julialang.org/t/matrix-multiplication-and-element-wise-operations-without-extra-pre-allocation-of-memory/42855/6 "2020-07-10T17:45:35Z")

</div>

It can be done using `mul!`, but I will have to use two line of code. Matrix multiplication first and the element-wise operations after. Just thought it would look nicer in a single line of code just with operators.

Thank you very much.
