Automatic Differentiation
 
Loading...
Searching...
No Matches
cholesky_factor_free.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_FACTOR_FREE_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_FACTOR_FREE_HPP
3
8#include <cmath>
9#include <stdexcept>
10
11namespace stan {
12namespace math {
13
25template <typename T, require_eigen_t<T>* = nullptr>
26Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, 1> cholesky_factor_free(
27 const T& y) {
28 using std::log;
29
30 const auto& y_ref = to_ref(y);
31 check_cholesky_factor("cholesky_factor_free", "y", y_ref);
32 int M = y.rows();
33 int N = y.cols();
34 Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, 1> x((N * (N + 1)) / 2
35 + (M - N) * N);
36 int pos = 0;
37
38 for (int m = 0; m < N; ++m) {
39 x.segment(pos, m) = y_ref.row(m).head(m);
40 pos += m;
41 x.coeffRef(pos++) = log(y_ref.coeff(m, m));
42 }
43
44 for (int m = N; m < M; ++m) {
45 x.segment(pos, N) = y_ref.row(m);
46 pos += N;
47 }
48 return x;
49}
50
58template <typename T, require_std_vector_t<T>* = nullptr>
59auto cholesky_factor_free(const T& x) {
61 x, [](auto&& v) { return cholesky_factor_free(v); });
62}
63
64} // namespace math
65} // namespace stan
66
67#endif
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, 1 > cholesky_factor_free(const T &y)
Return the unconstrained vector of parameters corresponding to the specified Cholesky factor.
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_cholesky_factor(const char *function, const char *name, const Mat &y)
Throw an exception if the specified matrix is not a valid Cholesky factor.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...