Stan Math Library  2.20.0
reverse mode automatic differentiation
cholesky_decompose.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CHOLESKY_DECOMPOSE_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CHOLESKY_DECOMPOSE_HPP
3 
8 #ifdef STAN_OPENCL
13 #endif
14 
15 #include <cmath>
16 
17 namespace stan {
18 namespace math {
19 
34 template <typename T>
35 inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> cholesky_decompose(
36  const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& m) {
37  check_square("cholesky_decompose", "m", m);
38  check_symmetric("cholesky_decompose", "m", m);
39  Eigen::LLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > llt(m.rows());
40  llt.compute(m);
41  check_pos_definite("cholesky_decompose", "m", llt);
42  return llt.matrixL();
43 }
44 
59 template <>
60 inline Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> cholesky_decompose(
61  const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& m) {
62  check_square("cholesky_decompose", "m", m);
63 #ifdef STAN_OPENCL
65  matrix_cl m_cl(m);
66  check_symmetric("cholesky_decompose", "m", m_cl);
67  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> m_chol(m.rows(),
68  m.cols());
69  cholesky_decompose(m_cl);
70  check_nan("cholesky_decompose (OpenCL)", "Matrix m", m_cl);
71  check_diagonal_zeros("cholesky_decompose (OpenCL)", "Matrix m", m_cl);
72  m_chol = from_matrix_cl(m_cl);
73  return m_chol;
74  } else {
75  check_symmetric("cholesky_decompose", "m", m);
76  Eigen::LLT<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> > llt(
77  m.rows());
78  llt.compute(m);
79  check_pos_definite("cholesky_decompose", "m", llt);
80  return llt.matrixL();
81  }
82 #else
83  check_symmetric("cholesky_decompose", "m", m);
84  Eigen::LLT<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> > llt(
85  m.rows());
86  llt.compute(m);
87  check_pos_definite("cholesky_decompose", "m", llt);
88  return llt.matrixL();
89 #endif
90 }
91 } // namespace math
92 
93 } // namespace stan
94 #endif
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > from_matrix_cl(const matrix_cl &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix...
Definition: copy.hpp:70
void check_diagonal_zeros(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl has zeros on the diagonal.
void check_square(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is square.
The API to access the methods and values in opencl_context_base.
opencl_context_base::tuning_struct & tuning_opts()
Returns the thread block size for the Cholesky Decompositions L_11.
void cholesky_decompose(matrix_cl &A)
Performs an in-place of the the lower-triangular Cholesky factor (i.e., matrix square root) of the sp...
Represents a matrix on the OpenCL device.
Definition: matrix_cl.hpp:29
Initialization for OpenCL:
void check_pos_definite(const char *function, const char *name, const Eigen::Matrix< T_y, -1, -1 > &y)
Check if the specified square, symmetric matrix is positive definite.
void check_symmetric(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is symmetric.
void check_nan(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl has NaN values.
Definition: check_nan.hpp:24

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