Stan Math Library  2.20.0
reverse mode automatic differentiation
factor_U.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_FACTOR_U_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_FACTOR_U_HPP
3 
5 #include <cmath>
6 #include <cstddef>
7 #include <limits>
8 #include <stdexcept>
9 #include <vector>
10 
11 namespace stan {
12 namespace math {
13 
21 template <typename T>
22 void factor_U(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& U,
23  Eigen::Array<T, Eigen::Dynamic, 1>& CPCs) {
24  size_t K = U.rows();
25  size_t position = 0;
26  size_t pull = K - 1;
27 
28  if (K == 2) {
29  CPCs(0) = atanh(U(0, 1));
30  return;
31  }
32 
33  Eigen::Array<T, 1, Eigen::Dynamic> temp = U.row(0).tail(pull);
34 
35  CPCs.head(pull) = temp;
36 
37  Eigen::Array<T, Eigen::Dynamic, 1> acc(K);
38  acc(0) = -0.0;
39  acc.tail(pull) = 1.0 - temp.square();
40  for (size_t i = 1; i < (K - 1); i++) {
41  position += pull;
42  pull--;
43  temp = U.row(i).tail(pull);
44  temp /= sqrt(acc.tail(pull) / acc(i));
45  CPCs.segment(position, pull) = temp;
46  acc.tail(pull) *= 1.0 - temp.square();
47  }
48  CPCs = 0.5 * ((1.0 + CPCs) / (1.0 - CPCs)).log(); // now unbounded
49 }
50 
51 } // namespace math
52 
53 } // namespace stan
54 
55 #endif
fvar< T > atanh(const fvar< T > &x)
Return inverse hyperbolic tangent of specified value.
Definition: atanh.hpp:22
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:13
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
void factor_U(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &U, Eigen::Array< T, Eigen::Dynamic, 1 > &CPCs)
This function is intended to make starting values, given a unit upper-triangular matrix U such that U...
Definition: factor_U.hpp:22

     [ Stan Home Page ] © 2011–2018, Stan Development Team.