Stan Math Library  2.15.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 
6 
7 #include <cmath>
8 #include <cstddef>
9 #include <limits>
10 #include <stdexcept>
11 #include <vector>
12 
13 namespace stan {
14  namespace math {
15 
23  template<typename T>
24  void
25  factor_U(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& U,
26  Eigen::Array<T, Eigen::Dynamic, 1>& CPCs) {
27  size_t K = U.rows();
28  size_t position = 0;
29  size_t pull = K - 1;
30 
31  if (K == 2) {
32  CPCs(0) = atanh(U(0, 1));
33  return;
34  }
35 
36  Eigen::Array<T, 1, Eigen::Dynamic> temp = U.row(0).tail(pull);
37 
38  CPCs.head(pull) = temp;
39 
40  Eigen::Array<T, Eigen::Dynamic, 1> acc(K);
41  acc(0) = -0.0;
42  acc.tail(pull) = 1.0 - temp.square();
43  for (size_t i = 1; i < (K - 1); i++) {
44  position += pull;
45  pull--;
46  temp = U.row(i).tail(pull);
47  temp /= sqrt(acc.tail(pull) / acc(i));
48  CPCs.segment(position, pull) = temp;
49  acc.tail(pull) *= 1.0 - temp.square();
50  }
51  CPCs = 0.5 * ( (1.0 + CPCs) / (1.0 - CPCs) ).log(); // now unbounded
52  }
53 
54  }
55 
56 }
57 
58 #endif
fvar< T > atanh(const fvar< T > &x)
Return inverse hyperbolic tangent of specified value.
Definition: atanh.hpp:21
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:14
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
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:25

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