title: Is it impossible to multiply matrices/vectors by scalar values in Stan?
body: <p>Consider the below Stan script, which I'm using via RStan:</p>
  <pre><code>data {
    int K; //outcome classes
    int N; //rows
    int D; //input dimensions
    int y[N];
    matrix[N, D] X;
    real days[N]; 
  }
  parameters {
    matrix[D, K] C;
    matrix[D, K] B;
  }
  model {
    matrix[N, K] pred = X*C + days*X*B; //If I remove days, it works fine. 
  
    to_vector(pred) ~ normal(0, 5);
  
    for (n in 1:N)
      y[n] ~ categorical_logit(pred[n]');
  }
  </code></pre>
  <p>An undesirable solution, is just duplicating the days column in R such that it IS a matrix and can be used in matrix multiplication. But is that really necessary? Should be easy to &quot;scale&quot; a matrix by a scalar value.</p>
  <p>The error is:</p>
  <p><a href="https://i.stack.imgur.com/ciH6T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ciH6T.png" alt="enter image description here" /></a></p>
  <p>Edit: I've also cast days as a matrix <code>matrix[N,1] days[N]</code>. While this does pass the &quot;scrub&quot; it gets rejected at compilation due to dimension mismatch.</p>
  <p>Edit2: I've adjusted the code, which does now execute. But I'm confused why this nested for loop is even necessary. There should be an easy way to multiply all vector elements by the same scalar value.</p>
  <pre><code>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]'; // now row_vector [1xD]
      // [D,K] x [1,D] + [D,K] x [1,D]  
      pred = C * ipt   +  B * (day_diag * ipt); 
      Y[n]~categorical_logit(pred);   
    }
  }
  </code></pre>