Target += u for the vector u

Dear all,

A very quick question just to be sure. When I write:

vector[2] lps = ....;
target += lps;

Is it equivalent to target += sum(lps)? By other words, I don’t need to write the sum in my code.

Thank you in advance,
Andrei

Yes

3 Likes

Many thanks

1 Like

my copilot does not agree :o

```Not correct. Stan only auto-sums log densities (foo_lpdf, foo_lpmf, target += normal_lpdf(y|…)). Plain arithmetic with vectors isn’t auto-reduced.

Expression pieces:

  • log_param1_Weibull : vector[2]

  • log1p(sqr_cv_interval) : vector[2]

  • log(abs(digamma(…))) : vector[2]

  • 2 * log(inv_sqr_cv_interval) : vector[2]

Their sum is vector[2]. If jacobian is a real, jacobian += (vector[2]) is a type error. You must do jacobian += sum( … ). If jacobian is vector[2], later you still need target += sum(jacobian) (or incorporate per-component elsewhere). No silent summation happens here.```

Is the same applies to jacobian +=

Yes. Your LLM is wrong :)

4 Likes

haha, I suspected the same.

Thank you!