Loading [MathJax]/extensions/TeX/AMSsymbols.js
Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
cholesky_corr_free.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_FREE_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_FREE_HPP
3
8#include <cmath>
9
10namespace stan {
11namespace math {
12
13template <typename T, require_eigen_t<T>* = nullptr>
14inline auto cholesky_corr_free(const T& x) {
15 using Eigen::Dynamic;
16 using Eigen::Matrix;
17 using std::sqrt;
18
19 check_square("cholesky_corr_free", "x", x);
20 // should validate lower-triangular, unit lengths
21
22 const auto& x_ref = to_ref(x);
23 int K = (x.rows() * (x.rows() - 1)) / 2;
24 Matrix<value_type_t<T>, Dynamic, 1> z(K);
25 int k = 0;
26 for (int i = 1; i < x.rows(); ++i) {
27 z.coeffRef(k++) = corr_free(x_ref.coeff(i, 0));
28 auto sum_sqs = square(x_ref.coeff(i, 0));
29 for (int j = 1; j < i; ++j) {
30 z.coeffRef(k++) = corr_free(x_ref.coeff(i, j) / sqrt(1.0 - sum_sqs));
31 sum_sqs += square(x_ref.coeff(i, j));
32 }
33 }
34 return z;
35}
36
44template <typename T, require_std_vector_t<T>* = nullptr>
45inline auto cholesky_corr_free(const T& x) {
46 return apply_vector_unary<T>::apply(
47 x, [](auto&& v) { return cholesky_corr_free(v); });
48}
49
50} // namespace math
51} // namespace stan
52#endif
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
auto cholesky_corr_free(const T &x)
Overload of cholesky_corr_free() to untransform each matrix in a standard vector.
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:18
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
plain_type_t< T > corr_free(const T &y)
Return the unconstrained scalar that when transformed to a valid correlation produces the specified v...
Definition corr_free.hpp:28
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...