1 #include <stan/math/prim.hpp>
2 #include <gtest/gtest.h>
3
TEST(MathMatrixPrimMat,qr_R)4 TEST(MathMatrixPrimMat, qr_R) {
5 stan::math::matrix_d m0(0, 0);
6 stan::math::matrix_d m1(4, 2);
7 m1 << 1, 2, 3, 4, 5, 6, 7, 8;
8
9 using stan::math::qr_Q;
10 using stan::math::qr_R;
11 using stan::math::transpose;
12 EXPECT_THROW(qr_R(m0), std::invalid_argument);
13 EXPECT_NO_THROW(qr_R(m1));
14
15 stan::math::matrix_d m2(4, 2);
16 stan::math::matrix_d Q(4, 4);
17 stan::math::matrix_d R(4, 2);
18 Q = qr_Q(m1);
19 R = qr_R(m1);
20 m2 = Q * R;
21 for (unsigned int i = 0; i < m1.rows(); i++) {
22 for (unsigned int j = 0; j < m1.cols(); j++) {
23 EXPECT_NEAR(m1(i, j), m2(i, j), 1e-12);
24 }
25 }
26 for (unsigned int j = 0; j < m1.cols(); j++)
27 EXPECT_TRUE(R(j, j) >= 0.0);
28 stan::math::matrix_d m3(2, 4);
29 m3 = qr_Q(transpose(m1)) * qr_R(transpose(m1));
30 for (unsigned int i = 0; i < m1.rows(); i++) {
31 for (unsigned int j = 0; j < m1.cols(); j++) {
32 EXPECT_NEAR(m1(i, j), m3(j, i), 1e-12);
33 }
34 }
35 }
36