Skip to content

Commit 42d94c4

Browse files
authored
Merge pull request #3136 from stan-dev/chol2inv-simp
Simplify implementation for chol2inv
2 parents 9bae49d + dd26000 commit 42d94c4

1 file changed

Lines changed: 2 additions & 13 deletions

File tree

stan/math/prim/fun/chol2inv.hpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
#include <stan/math/prim/err.hpp>
55
#include <stan/math/prim/fun/Eigen.hpp>
6-
#include <stan/math/prim/fun/dot_self.hpp>
7-
#include <stan/math/prim/fun/dot_product.hpp>
86
#include <stan/math/prim/fun/mdivide_left_tri.hpp>
97
#include <stan/math/prim/fun/inv_square.hpp>
8+
#include <stan/math/prim/fun/crossprod.hpp>
109

1110
namespace stan {
1211
namespace math {
@@ -35,17 +34,7 @@ plain_type_t<T> chol2inv(const T& L) {
3534
X.coeffRef(0) = inv_square(L_ref.coeff(0, 0));
3635
return X;
3736
}
38-
T_result L_inv = mdivide_left_tri<Eigen::Lower>(L_ref);
39-
T_result X(K, K);
40-
for (int k = 0; k < K; ++k) {
41-
X.coeffRef(k, k) = dot_self(L_inv.col(k).tail(K - k).eval());
42-
for (int j = k + 1; j < K; ++j) {
43-
int Kmj = K - j;
44-
X.coeffRef(k, j) = X.coeffRef(j, k) = dot_product(
45-
L_inv.col(k).tail(Kmj).eval(), L_inv.col(j).tail(Kmj).eval());
46-
}
47-
}
48-
return X;
37+
return crossprod(mdivide_left_tri<Eigen::Lower>(L_ref));
4938
}
5039

5140
} // namespace math

0 commit comments

Comments
 (0)