Automatic Differentiation
 
Loading...
Searching...
No Matches
qr_R.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_QR_R_HPP
2#define STAN_MATH_PRIM_FUN_QR_R_HPP
3
6#include <algorithm>
7
8namespace stan {
9namespace math {
10
18template <typename EigMat, require_eigen_t<EigMat>* = nullptr>
19Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic> qr_R(
20 const EigMat& m) {
21 using matrix_t
22 = Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic>;
23 if (unlikely(m.size() == 0)) {
24 return matrix_t(0, 0);
25 }
26 Eigen::HouseholderQR<matrix_t> qr(m.rows(), m.cols());
27 qr.compute(m);
28 matrix_t R = qr.matrixQR();
29 if (m.rows() > m.cols()) {
30 R.bottomRows(m.rows() - m.cols()).setZero();
31 }
32 const int min_size = std::min(m.rows(), m.cols());
33 for (int i = 0; i < min_size; i++) {
34 for (int j = 0; j < i; j++) {
35 R.coeffRef(i, j) = 0.0;
36 }
37 if (R(i, i) < 0) {
38 R.row(i) *= -1.0;
39 }
40 }
41 return R;
42}
43
44} // namespace math
45} // namespace stan
46
47#endif
#define unlikely(x)
matrix_cl< double > qr_R(T_m &&m)
Returns the orthogonal factor of the fat QR decomposition.
Definition qr_R.hpp:20
std::tuple< Eigen::Matrix< value_type_t< EigMat >, Eigen::Dynamic, Eigen::Dynamic >, Eigen::Matrix< value_type_t< EigMat >, Eigen::Dynamic, Eigen::Dynamic > > qr(const EigMat &m)
Returns the fat QR decomposition.
Definition qr.hpp:25
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...