|
5 | 5 | #include <stan/math/prim/err.hpp> |
6 | 6 | #include <stan/math/prim/fun/Eigen.hpp> |
7 | 7 | #include <stan/math/prim/fun/to_ref.hpp> |
| 8 | +#include <stan/math/prim/fun/inv_sqrt.hpp> |
8 | 9 | #include <stan/math/prim/fun/sqrt.hpp> |
9 | 10 | #include <stan/math/prim/functor/apply_vector_unary.hpp> |
10 | 11 | #include <cmath> |
@@ -47,15 +48,15 @@ inline plain_type_t<Vec> sum_to_zero_free(const Vec& z) { |
47 | 48 | return y; |
48 | 49 | } |
49 | 50 |
|
50 | | - y.coeffRef(N - 1) = -z_ref(N) * sqrt(N * (N + 1)) / N; |
| 51 | + y.coeffRef(N - 1) = -z_ref.coeff(N) * sqrt(N * (N + 1)) / N; |
51 | 52 |
|
52 | 53 | value_type_t<Vec> sum_w(0); |
53 | 54 |
|
54 | 55 | for (int i = N - 2; i >= 0; --i) { |
55 | 56 | double n = static_cast<double>(i + 1); |
56 | | - auto w = y(i + 1) / sqrt((n + 1) * (n + 2)); |
| 57 | + auto w = y.coeff(i + 1) / sqrt((n + 1) * (n + 2)); |
57 | 58 | sum_w += w; |
58 | | - y.coeffRef(i) = (sum_w - z_ref(i + 1)) * sqrt(n * (n + 1)) / n; |
| 59 | + y.coeffRef(i) = (sum_w - z_ref.coeff(i + 1)) * sqrt(n * (n + 1)) / n; |
59 | 60 | } |
60 | 61 |
|
61 | 62 | return y; |
@@ -88,18 +89,18 @@ inline plain_type_t<Mat> sum_to_zero_free(const Mat& z) { |
88 | 89 | for (int j = M - 1; j >= 0; --j) { |
89 | 90 | value_type_t<Mat> ax_previous(0); |
90 | 91 |
|
91 | | - double a_j = 1.0 / std::sqrt((j + 1.0) * (j + 2.0)); |
| 92 | + double a_j = inv_sqrt((j + 1.0) * (j + 2.0)); |
92 | 93 | double b_j = (j + 1.0) * a_j; |
93 | 94 |
|
94 | 95 | for (int i = N - 1; i >= 0; --i) { |
95 | | - double a_i = 1.0 / std::sqrt((i + 1.0) * (i + 2.0)); |
| 96 | + double a_i = inv_sqrt((i + 1.0) * (i + 2.0)); |
96 | 97 | double b_i = (i + 1.0) * a_i; |
97 | 98 |
|
98 | | - auto alpha_plus_beta = z_ref(i, j) + beta(i); |
| 99 | + auto alpha_plus_beta = z_ref.coeff(i, j) + beta.coeff(i); |
99 | 100 |
|
100 | | - x(i, j) = (alpha_plus_beta + b_j * ax_previous) / (b_j * b_i); |
101 | | - beta(i) += a_j * (b_i * x(i, j) - ax_previous); |
102 | | - ax_previous += a_i * x(i, j); |
| 101 | + x.coeffRef(i, j) = (alpha_plus_beta + b_j * ax_previous) / (b_j * b_i); |
| 102 | + beta.coeffRef(i) += a_j * (b_i * x.coeff(i, j) - ax_previous); |
| 103 | + ax_previous += a_i * x.coeff(i, j); |
103 | 104 | } |
104 | 105 | } |
105 | 106 |
|
|
0 commit comments