Stan Math Library  2.20.0
reverse mode automatic differentiation
qr_R.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_QR_R_HPP
2 #define STAN_MATH_FWD_MAT_FUN_QR_R_HPP
3 
7 #include <stan/math/fwd/core.hpp>
8 
9 namespace stan {
10 namespace math {
11 
12 template <typename T>
13 Eigen::Matrix<fvar<T>, Eigen::Dynamic, Eigen::Dynamic> qr_R(
14  const Eigen::Matrix<fvar<T>, Eigen::Dynamic, Eigen::Dynamic>& m) {
15  typedef Eigen::Matrix<fvar<T>, Eigen::Dynamic, Eigen::Dynamic> matrix_fwd_t;
16  check_nonzero_size("qr_R", "m", m);
17  check_greater_or_equal("qr_R", "m.rows()", m.rows(), m.cols());
18  Eigen::HouseholderQR<matrix_fwd_t> qr(m.rows(), m.cols());
19  qr.compute(m);
20  matrix_fwd_t R = qr.matrixQR().topLeftCorner(m.rows(), m.cols());
21  for (int i = 0; i < R.rows(); i++) {
22  for (int j = 0; j < i; j++)
23  R(i, j) = 0.0;
24  if (i < R.cols() && R(i, i) < 0.0)
25  R.row(i) *= -1.0;
26  }
27  return R;
28 }
29 
30 } // namespace math
31 } // namespace stan
32 #endif
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
Eigen::Matrix< fvar< T >, Eigen::Dynamic, Eigen::Dynamic > qr_R(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, Eigen::Dynamic > &m)
Definition: qr_R.hpp:13
void check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low)
Check if y is greater or equal than low.
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

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