In the stan_math/stan/math/prim/mat/fun directory we have cumulative_sum.hpp with the simple implementation:

```
template <typename T, int R, int C>
inline Eigen::Matrix<T, R, C> cumulative_sum(const Eigen::Matrix<T, R, C>& m) {
Eigen::Matrix<T, R, C> result(m.rows(), m.cols()));
if(m.size() == 0)
return result;
result(0) = m(0);
for(int i = 1; i < result.size(); i++)
result(i) = m(i) + result(i - 1);
return result;
}
```

if we alter this to:

```
template <typename T, int R, int C>
inline Eigen::Matrix<T, R, C> cumulative_sum_new_way(const Eigen::Matrix<T, R, C>& m) {
Eigen::Matrix<T, R, C> result(m.rows(), m.cols()));
if(m.size() == 0)
return result;
// Here's the change. Using an array to do the calculations.
T resultArray[m.size()];
resultArray[0] = ((stan::math::var)m(0)).val();
for(int i = 1; i < m.size(); ++i) {
resultArray[i] = ((stan::math::var)m(i)).val() + resultArray[i - 1];
}
// Moving the values back to the Eigen matrix.
for(int i=0; i < m.size(); ++i) {
result(i) = resultArray[i];
}
return result;
}
```

the two functions give the same values doing a stepwise comparison (â€śValues not equalâ€ť never prints):

```
for(int i = 0; i < size; i++) {
if(originalWay(i) != newWay(i)) {
printf("Values not equal");
}
}
```

But running my model using cumulative_sum_new_way() takes about 19 times longer and yields slightly different results (i.e. a mean of 0.52381 vs 0.51734).

Iâ€™d like the new implementation to yield the same results in terms of speed and values, but Iâ€™m unable to figure out how. BTW, this is just an example with a simple function. I wouldnâ€™t actually do things this way for this function.