# How to multiply all vector elements by the same scalar value

**URL:** <https://discourse.mc-stan.org/t/how-to-multiply-all-vector-elements-by-the-same-scalar-value/18958>\
**Category:** General\
**Created:** [October 28, 2020, 7:05pm UTC](https://discourse.mc-stan.org/t/how-to-multiply-all-vector-elements-by-the-same-scalar-value/18958 "2020-10-28T19:05:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacob\_Moore](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jacob_moore/32/9687_2.png) [@Jacob\_Moore](https://discourse.mc-stan.org/u/Jacob_Moore)\
**Post date:** [October 28, 2020, 7:05pm UTC](https://discourse.mc-stan.org/t/how-to-multiply-all-vector-elements-by-the-same-scalar-value/18958/1 "2020-10-28T19:05:00Z")

</div>

The below Stan script, does work - it just confuses me why a nested for loop is even necessary. In the line `pred = C * ipt + B * (day_diag * ipt); `: ipt is a “one hot” vector, where all elements are set to 0, save for 1. The goal was simply to multiply the matrix, B, by the vector ipt, and scale all vector elements by the current value of `days[n]`. This seemed impossible so I created a diagonal matrix where every element along the diagonal equals the same thing. This works but it’s inefficient.

How can vector scalar multiplication be executed more efficiently? Has it purposely been omitted from the language?

```
data {
  int K; //outcome classes, 3
  int N; //num rows
  int D; //input dimensions, 5
  int Y[N];
  matrix[N,D] X;
  int days[N]; 
}
parameters {
  matrix[D, K] C; //[5,3] 
  matrix[D, K] B; //[5,3]
}
model {
  for (n in 1:N){
    vector[K] pred;
    vector[D] ipt;
    matrix[K,K] day_diag;
    for (i in 1:K){
      for (j in 1:K){
        if (i == j)
          day_diag[i,j] = days[n];
        else
          day_diag[i,j] = 0; 
      }
    }
    
    ipt = X[n]'; 
    pred = C * ipt + B * (day_diag * ipt); 
    Y[n]~categorical_logit(pred);   
  }
}

```

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [October 28, 2020, 7:36pm UTC](https://discourse.mc-stan.org/t/how-to-multiply-all-vector-elements-by-the-same-scalar-value/18958/2 "2020-10-28T19:36:11Z")

</div>

Vector-scalar multiplication works. The model compiles with change.

```stan
    pred = C * ipt + B * (days[n] * ipt);

```

---

<div class="post-metadata">

**Author:** ![Jacob\_Moore](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jacob_moore/32/9687_2.png) [@Jacob\_Moore](https://discourse.mc-stan.org/u/Jacob_Moore)\
**Post date:** [October 28, 2020, 8:02pm UTC](https://discourse.mc-stan.org/t/how-to-multiply-all-vector-elements-by-the-same-scalar-value/18958/3 "2020-10-28T20:02:11Z")

</div>

@nhuurre ,How does Stan process matrix multiplication?  
In `C * ipt` I have a matrix vector multiplication of dimensions. `[5,3]` x `[1,5]`  
I’m familiar with `[3,5] x [5,1] => [3,1]`. So the order in which Stan expects arguments for matrix-vector multiplication confuses me.

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [October 29, 2020, 6:36am UTC](https://discourse.mc-stan.org/t/how-to-multiply-all-vector-elements-by-the-same-scalar-value/18958/4 "2020-10-29T06:36:44Z")

</div>

Indeed, ` [3,5] x [5,1] => [3,1]` holds in Stan as well. The code _compiles_ but that doesn’t mean it will _run_. If `D` and `K` are not equal the model will stop with something like “initialization failed: dimension mismatch in matrix multiplication.”

Also note that a `vector[5]` is a `[5,1]` matrix while a `row_vector[5]` is a `[1,5]` matrix.

---

<div class="post-metadata">

**Author:** ![Jacob\_Moore](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jacob_moore/32/9687_2.png) [@Jacob\_Moore](https://discourse.mc-stan.org/u/Jacob_Moore)\
**Post date:** [October 29, 2020, 1:13pm UTC](https://discourse.mc-stan.org/t/how-to-multiply-all-vector-elements-by-the-same-scalar-value/18958/5 "2020-10-29T13:13:58Z")

</div>

> [@nhuurre](#):
>
> ` pred = C * ipt + B * (days[n] * ipt);`

@nhuurre Right, so why does this work?  
`[5,3] x [1,5]` for `C * ipt` shouldn’t work, but it does! That’s why I’m curious about expected argument order. Do you just always specify the matrix first and Stan will sort it out?

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [October 29, 2020, 1:23pm UTC](https://discourse.mc-stan.org/t/how-to-multiply-all-vector-elements-by-the-same-scalar-value/18958/6 "2020-10-29T13:23:31Z")

</div>

Because `matrix[N,D] X;` is `[5,3]` and you have `ipt = X[n]'`  
`X[n]` takes a row\_vector from X, which is `[1,3]` and then the transpose makes that `[3,1]`

so you are doing `[5,3]` times `[3,1]` which produces `[5,1]`

---

<div class="post-metadata">

**Author:** ![Jacob\_Moore](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jacob_moore/32/9687_2.png) [@Jacob\_Moore](https://discourse.mc-stan.org/u/Jacob_Moore)\
**Post date:** [October 29, 2020, 1:47pm UTC](https://discourse.mc-stan.org/t/how-to-multiply-all-vector-elements-by-the-same-scalar-value/18958/7 "2020-10-29T13:47:53Z")

</div>

Uhoh, I’m supposed to have K output classes (3), so it looks that I made an error in my model design. Also, N is the number of data observations, ~1000.

This did the trick tho!

```
data {
  int K; //outcome classes, 3
  int N; //num rows
  int D; //input dimensions, 5
  int Y[N];
  matrix[N,D] X;
  int days[N]; 
}
parameters {
  matrix[D, K] C; //[5,3] 
  matrix[D, K] B; //[5,3]
  
}
model {
  for (n in 1:N){
    row_vector[K] pred;
    row_vector[D] ipt;
    ipt = X[n]; 
    
    // [1,5]*[5,3] + [1,5]*[5,3]  
    pred = ipt * C + (ipt * days[n]) * B;
    Y[n]~categorical_logit(pred');   
  }
}

```
