Automatic Differentiation
 
Loading...
Searching...
No Matches
cholesky_corr_free.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_CHOLESKY_CORR_FREE_HPP
2#define STAN_MATH_PRIM_FUN_CHOLESKY_CORR_FREE_HPP
3
8#include <cmath>
9
10namespace stan {
11namespace math {
12
13template <typename T, require_eigen_t<T>* = nullptr>
14auto cholesky_corr_free(const T& x) {
15 using Eigen::Dynamic;
16 using Eigen::Matrix;
17
18 check_square("cholesky_corr_free", "x", x);
19 // should validate lower-triangular, unit lengths
20
21 const auto& x_ref = to_ref(x);
22 int K = (x.rows() * (x.rows() - 1)) / 2;
23 Matrix<value_type_t<T>, Dynamic, 1> z(K);
24 int k = 0;
25 for (int i = 1; i < x.rows(); ++i) {
26 z.coeffRef(k++) = corr_free(x_ref.coeff(i, 0));
27 double sum_sqs = square(x_ref.coeff(i, 0));
28 for (int j = 1; j < i; ++j) {
29 z.coeffRef(k++) = corr_free(x_ref.coeff(i, j) / std::sqrt(1.0 - sum_sqs));
30 sum_sqs += square(x_ref.coeff(i, j));
31 }
32 }
33 return z;
34}
35
43template <typename T, require_std_vector_t<T>* = nullptr>
44auto cholesky_corr_free(const T& x) {
45 return apply_vector_unary<T>::apply(
46 x, [](auto&& v) { return cholesky_corr_free(v); });
47}
48
49} // namespace math
50} // namespace stan
51#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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
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 ...
Definition fvar.hpp:9