Stan Math Library  2.20.0
reverse mode automatic differentiation
qr_thin_R.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_QR_THIN_R_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_QR_THIN_R_HPP
3 
7 #include <algorithm>
8 
9 namespace stan {
10 namespace math {
11 
18 template <typename T>
19 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> qr_thin_R(
20  const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& m) {
21  typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrix_t;
22  check_nonzero_size("qr_thin_R", "m", m);
23  Eigen::HouseholderQR<matrix_t> qr(m.rows(), m.cols());
24  qr.compute(m);
25  const int min_size = std::min(m.rows(), m.cols());
26  matrix_t R = qr.matrixQR().topLeftCorner(min_size, m.cols());
27  for (int i = 0; i < min_size; i++) {
28  for (int j = 0; j < i; j++)
29  R.coeffRef(i, j) = 0.0;
30  if (R(i, i) < 0)
31  R.row(i) *= -1.0;
32  }
33  return R;
34 }
35 
36 } // namespace math
37 } // namespace stan
38 #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.
int min(const std::vector< int > &x)
Returns the minimum coefficient in the specified column vector.
Definition: min.hpp:20
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > qr_thin_R(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Returns the upper triangular factor of the thin QR decomposition.
Definition: qr_thin_R.hpp:19

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