Stan Math Library  2.11.0
reverse mode automatic differentiation
cholesky_factor_free.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CHOLESKY_FACTOR_FREE_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CHOLESKY_FACTOR_FREE_HPP
3 
6 #include <cmath>
7 #include <stdexcept>
8 
9 namespace stan {
10 
11  namespace math {
12 
22  template <typename T>
23  Eigen::Matrix<T, Eigen::Dynamic, 1>
24  cholesky_factor_free(const Eigen::Matrix
25  <T, Eigen::Dynamic, Eigen::Dynamic>& y) {
26  using std::log;
27  if (!stan::math::check_cholesky_factor("cholesky_factor_free", "y", y))
28  throw std::domain_error("cholesky_factor_free: "
29  "y is not a Cholesky factor");
30  int M = y.rows();
31  int N = y.cols();
32  Eigen::Matrix<T, Eigen::Dynamic, 1> x((N * (N + 1)) / 2 + (M - N) * N);
33  int pos = 0;
34  // lower triangle of upper square
35  for (int m = 0; m < N; ++m) {
36  for (int n = 0; n < m; ++n)
37  x(pos++) = y(m, n);
38  // diagonal of upper square
39  x(pos++) = log(y(m, m));
40  }
41  // lower rectangle
42  for (int m = N; m < M; ++m)
43  for (int n = 0; n < N; ++n)
44  x(pos++) = y(m, n);
45  return x;
46  }
47 
48 
49  }
50 
51 }
52 
53 #endif
bool check_cholesky_factor(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is a valid Cholesky factor.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
Eigen::Matrix< T, Eigen::Dynamic, 1 > cholesky_factor_free(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &y)
Return the unconstrained vector of parameters correspdonding to the specified Cholesky factor...
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.

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