Hi!

Do we have some expression template type of stuff in stan-math? What I am seeing as a use pattern very often in stan-math is a pattern like this when computing the lpdf or lpmf in vectorized form:

```
// make an objected which is indexed by i, even if n or lambda is only a scalar and not a vector
scalar_seq_view<T_n> n_vec(n);
scalar_seq_view<T_rate> lambda_vec(lambda);
size_t size = max_size(n, lambda);
// then we loop over the terms
for (size_t i = 0; i < size; i++) {
if (!(lambda_vec[i] == 0 && n_vec[i] == 0)) {
if (include_summand<propto>::value)
logp -= lgamma(n_vec[i] + 1.0);
if (include_summand<propto, T_rate>::value)
logp += multiply_log(n_vec[i], value_of(lambda_vec[i]))
- value_of(lambda_vec[i]);
}
// ...
}
```

Thus, we will compute, for example, for each element `lgamma(n_vec[i] + 1.0)`

- even if `n_vec`

is just a scalar. Obviously it is not too good for the performance to keep recomputing a function like `lgamma`

over and over again when all we need is a single evaluation. This even gets worse in case whatever scalar is itself a `var`

as we then increase substantially the AD tree in an unneeded way if I am not mistaken.

This is a very common pattern we use. A solution could be to introduce a `scalar_seq_view_expr`

object which would act a bit smarter for the case when there is indeed only a scalar.

… or is this all unneeded, because the compiler is smart enough / this would not buy us performance?

Do we have such a facility already? My intuition tells me that this could buy us quite some extra speed… which I will try with a POC shortly.

Best,

Sebastian