# Elementwise multiplication question

**URL:** <https://discourse.mc-stan.org/t/elementwise-multiplication-question/20042>\
**Category:** Modeling\
**Created:** [January 4, 2021, 5:29pm UTC](https://discourse.mc-stan.org/t/elementwise-multiplication-question/20042 "2021-01-04T17:29:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [January 4, 2021, 5:29pm UTC](https://discourse.mc-stan.org/t/elementwise-multiplication-question/20042/1 "2021-01-04T17:29:10Z")

</div>

A very simple question–I suspect the answer is staring me in the face…

If I have variables

```
row_vector[n_col] a[n_row];
vector[n_row] b;

```

What is the efficient way to get a new variable  
`row_vector[n_col] c[n_row]` given by

```
for(i in 1:nrow){
 for(j in 1:ncol){
  c[i,j] = a[i,j] * b[i]
 }
}

```

?

And similarly if I want `c[i,j] = a[i,j] + b[i]`?

---

<div class="post-metadata">

**Author:** ![hhau](https://avatars.discourse-cdn.com/v4/letter/h/2bfe46/32.png) [@hhau](https://discourse.mc-stan.org/u/hhau)\
**Post date:** [January 4, 2021, 6:00pm UTC](https://discourse.mc-stan.org/t/elementwise-multiplication-question/20042/2 "2021-01-04T18:00:10Z")

</div>

I don’t think there is? Recoding `a` and `c` using the `matrix` type would let you use partial indexing i.e. `c[, j] = a[, j] .* b;` and 1 for-loop. Alternatively, re-type `a` and `c` as `vector[n_row] {{a | c}} [n_col];` and then you can use 1 for-loop for `c[j] = a[j] .* b;`, but there’s probably a reason you’re not doing so already.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [January 4, 2021, 6:14pm UTC](https://discourse.mc-stan.org/t/elementwise-multiplication-question/20042/3 "2021-01-04T18:14:12Z")

</div>

> [@hhau](#):
>
> probably a reason you’re not doing so already

just that I ultimately need to access and work with the rows of c, and I think I remember that this is supposed to be more efficient. In my use case, n\_col is just 4, but n\_row is about 5e+05, and ‘b’ is a parameter so the whole operation needs to happen at every gradient evaluation.

Right now I’m just doing

```Stan
for(i in 1:nrow){
 c[i] = a[i]*b[i]
}

```

Any advice about whether this is likely to be substantially slower than re-typing as you suggest (to loop over 4 elements rather than 500K elements) but then extracting the columns (rather than the rows) would be much appreciated!

---

<div class="post-metadata">

**Author:** ![hhau](https://avatars.discourse-cdn.com/v4/letter/h/2bfe46/32.png) [@hhau](https://discourse.mc-stan.org/u/hhau)\
**Post date:** [January 4, 2021, 9:28pm UTC](https://discourse.mc-stan.org/t/elementwise-multiplication-question/20042/4 "2021-01-04T21:28:26Z")

</div>

Intuitively, I think that smaller loops with larger vectors will be faster. Probably worth doing some tests with a small subset of the data to see if this is correct.

> [@jsocolar](#):
>
> Right now I’m just doing
> 
> ```no-highlight
> for(i in 1:nrow){
> c[i] = a[i]*b[i]
> }
> 
> ```

Did you mean to do element wise multiplication (`.*` vs `*`) here?

---

<div class="post-metadata">

**Author:** ![stevebronder](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stevebronder/32/17242_2.png) [@stevebronder](https://discourse.mc-stan.org/u/stevebronder)\
**Post date:** [January 4, 2021, 9:57pm UTC](https://discourse.mc-stan.org/t/elementwise-multiplication-question/20042/5 "2021-01-04T21:57:30Z")

</div>

Can you just do a single index like the below? Then your filing each element in `c` with a `vector * scalar` multiplication

```stan
data {
  int n_row;
  int n_col;
}

transformed parameters {
  row_vector[n_col] a[n_row];
  vector[n_row] b;
  row_vector[n_col] c[n_row];
  for (i in 1:n_row) {
    c[i] = a[i] * b[i];
  }
}

```

Is there a reason you can’t just use a matrix here though?

```stan
data {
  int n_row;
  int n_col;
}

transformed parameters {
  matrix[n_row, n_col] a;
  vector[n_row] b;
  matrix[n_row, n_col] c = diag_pre_multiply(b, a);
}

```

`c = diag_pre_multiply(b, a)` does the equivalent of `c = as_diagonal(b) * a;`. And since you are multiplying each cell of `b` by it’s corresponding column in `a` I think they are the same? I’d double check that though. Otherwise check out `diag_post_mulitply()`

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [January 4, 2021, 10:22pm UTC](https://discourse.mc-stan.org/t/elementwise-multiplication-question/20042/6 "2021-01-04T22:22:54Z")

</div>

> [@hhau](#):
>
> Did you mean to do element wise multiplication ( `.*` vs `*` ) here?

> [@stevebronder](#):
>
> Can you just do a single index like the below?

What @stevebronder suggests is what I’m doing here:

> [@jsocolar](#):
>
> ```no-highlight
> for(i in 1:nrow){
> c[i] = a[i]*b[i]
> }
> 
> ```

I had hoped that I could vectorize over this (very long) for-loop while preserving the row\_vector structure of `c`, but it sounds like this isn’t easily achieved. The reason for this is that in a later loop I need to access the rows of `c`, and I think I remember from somewhere that it’s supposed to be faster to get the rows from a row-vector structure. So I’ll need to tinker a little bit to figure out whether typing as matrices and vectorizing with `diag_pre_multiply` (great tip @stevebronder! ) or by looping over the 4 columns rather than the 500K rows (great tip @hhau!) results in any savings.

---

<div class="post-metadata">

**Author:** ![stevebronder](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stevebronder/32/17242_2.png) [@stevebronder](https://discourse.mc-stan.org/u/stevebronder)\
**Post date:** [January 4, 2021, 10:38pm UTC](https://discourse.mc-stan.org/t/elementwise-multiplication-question/20042/7 "2021-01-04T22:38:52Z")

</div>

> [@jsocolar](#):
>
> So I’ll need to tinker a little bit to figure out whether typing as matrices and vectorizing with `diag_pre_multiply` (great tip @stevebronder! ) or by looping over the 4 columns rather than the 500K rows (great tip @hhau!) results in any savings.

Stan stores it’s data in column major order so I think the pre\_diag\_multiply() is gonna be your best bet. In general the sooner you can defer to vectorized Stan stuff the better as it will jump straight to a C++ function that’s specialized for that operation
