qr.cpp File Reference
#include "main.h"
#include <Eigen/QR>
#include "solverbase.h"

Functions

template<typename MatrixType >
void qr (const MatrixType &m)
 
template<typename MatrixType , int Cols2>
void qr_fixedsize ()
 
template<typename MatrixType >
void qr_invertible ()
 
template<typename MatrixType >
void qr_verify_assert ()
 
 EIGEN_DECLARE_TEST (qr)
 

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( qr  )
109  {
110  for (int i = 0; i < g_repeat; i++) {
112  qr(MatrixXf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
113  CALL_SUBTEST_2(qr(MatrixXcd(internal::random<int>(1, EIGEN_TEST_MAX_SIZE / 2),
114  internal::random<int>(1, EIGEN_TEST_MAX_SIZE / 2))));
119  }
120 
121  for (int i = 0; i < g_repeat; i++) {
122  CALL_SUBTEST_1(qr_invertible<MatrixXf>());
123  CALL_SUBTEST_6(qr_invertible<MatrixXd>());
124  CALL_SUBTEST_7(qr_invertible<MatrixXcf>());
125  CALL_SUBTEST_8(qr_invertible<MatrixXcd>());
126  }
127 
128  CALL_SUBTEST_9(qr_verify_assert<Matrix3f>());
129  CALL_SUBTEST_10(qr_verify_assert<Matrix3d>());
130  CALL_SUBTEST_1(qr_verify_assert<MatrixXf>());
131  CALL_SUBTEST_6(qr_verify_assert<MatrixXd>());
132  CALL_SUBTEST_7(qr_verify_assert<MatrixXcf>());
133  CALL_SUBTEST_8(qr_verify_assert<MatrixXcd>());
134 
135  // Test problem size constructors
137 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
Householder QR decomposition of a matrix.
Definition: HouseholderQR.h:59
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
static int g_repeat
Definition: main.h:191
void qr(const MatrixType &m)
Definition: qr.cpp:15
void qr_fixedsize()
Definition: qr.cpp:33
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
#define CALL_SUBTEST_11(FUNC)
Definition: split_test_helper.h:64
#define CALL_SUBTEST_12(FUNC)
Definition: split_test_helper.h:70
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
#define CALL_SUBTEST_10(FUNC)
Definition: split_test_helper.h:58

References CALL_SUBTEST_1, CALL_SUBTEST_10, CALL_SUBTEST_11, CALL_SUBTEST_12, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, CALL_SUBTEST_6, CALL_SUBTEST_7, CALL_SUBTEST_8, CALL_SUBTEST_9, EIGEN_TEST_MAX_SIZE, Eigen::g_repeat, i, qr(), and qr_fixedsize().

◆ qr()

template<typename MatrixType >
void qr ( const MatrixType m)
15  {
16  Index rows = m.rows();
17  Index cols = m.cols();
18 
19  typedef typename MatrixType::Scalar Scalar;
21 
22  MatrixType a = MatrixType::Random(rows, cols);
24 
25  MatrixQType q = qrOfA.householderQ();
27 
28  MatrixType r = qrOfA.matrixQR().template triangularView<Upper>();
29  VERIFY_IS_APPROX(a, qrOfA.householderQ() * r);
30 }
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
const Scalar * a
Definition: level2_cplx_impl.h:32
int * m
Definition: level2_cplx_impl.h:294
#define VERIFY_IS_UNITARY(a)
Definition: main.h:378
EIGEN_DEVICE_FUNC const Scalar & q
Definition: SpecialFunctionsImpl.h:2019
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
r
Definition: UniformPSDSelfTest.py:20

References a, cols, Eigen::HouseholderQR< MatrixType_ >::householderQ(), m, Eigen::HouseholderQR< MatrixType_ >::matrixQR(), Eigen::numext::q, UniformPSDSelfTest::r, rows, VERIFY_IS_APPROX, and VERIFY_IS_UNITARY.

Referenced by EIGEN_DECLARE_TEST(), qr_fixedsize(), qr_invertible(), and qr_verify_assert().

◆ qr_fixedsize()

template<typename MatrixType , int Cols2>
void qr_fixedsize ( )
33  {
34  enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
35  typedef typename MatrixType::Scalar Scalar;
38 
39  Matrix<Scalar, Rows, Cols> r = qr.matrixQR();
40  // FIXME need better way to construct trapezoid
41  for (int i = 0; i < Rows; i++)
42  for (int j = 0; j < Cols; j++)
43  if (i > j) r(i, j) = Scalar(0);
44 
45  VERIFY_IS_APPROX(m1, qr.householderQ() * r);
46 
47  check_solverbase<Matrix<Scalar, Cols, Cols2>, Matrix<Scalar, Rows, Cols2> >(m1, qr, Rows, Cols, Cols2);
48 }
Matrix3d m1
Definition: IOFormat.cpp:2
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References i, j, m1, qr(), UniformPSDSelfTest::r, and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().

◆ qr_invertible()

template<typename MatrixType >
void qr_invertible ( )
51  {
52  using std::abs;
53  using std::log;
54  using std::max;
55  using std::pow;
57  typedef typename MatrixType::Scalar Scalar;
58 
59  STATIC_CHECK((internal::is_same<typename HouseholderQR<MatrixType>::StorageIndex, int>::value));
60 
61  int size = internal::random<int>(10, 50);
62 
63  MatrixType m1(size, size), m2(size, size), m3(size, size);
64  m1 = MatrixType::Random(size, size);
65 
67  // let's build a matrix more stable to inverse
68  MatrixType a = MatrixType::Random(size, size * 4);
69  m1 += a * a.adjoint();
70  }
71 
73 
74  check_solverbase<MatrixType, MatrixType>(m1, qr, size, size, size);
75 
76  // now construct a matrix with prescribed determinant
77  m1.setZero();
78  for (int i = 0; i < size; i++) m1(i, i) = internal::random<Scalar>();
79  Scalar det = m1.diagonal().prod();
80  RealScalar absdet = abs(det);
81  m3 = qr.householderQ(); // get a unitary
82  m1 = m3 * m1 * m3.adjoint();
83  qr.compute(m1);
84  VERIFY_IS_APPROX(log(absdet), qr.logAbsDeterminant());
85  VERIFY_IS_APPROX(numext::sign(det), qr.signDeterminant());
86  // This test is tricky if the determinant becomes too small.
87  // Since we generate random numbers with magnitude range [0,1], the average determinant is 0.5^size
88  RealScalar tol =
89  numext::maxi(RealScalar(pow(0.5, size)), numext::maxi<RealScalar>(abs(absdet), abs(qr.absDeterminant())));
90  VERIFY_IS_MUCH_SMALLER_THAN(abs(det - qr.determinant()), tol);
91  VERIFY_IS_MUCH_SMALLER_THAN(abs(absdet - qr.absDeterminant()), tol);
92 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
MatrixType m2(n_dims)
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
EIGEN_DEVICE_FUNC const GlobalUnaryPowReturnType< Derived, ScalarExponent > pow(const Eigen::ArrayBase< Derived > &x, const ScalarExponent &exponent)
Definition: GlobalFunctions.h:137
#define max(a, b)
Definition: datatypes.h:23
#define STATIC_CHECK(COND)
Definition: main.h:380
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:371
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 pow(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:625
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 log(const bfloat16 &a)
Definition: BFloat16.h:618
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Definition: MathFunctions.h:926
squared absolute value
Definition: GlobalFunctions.h:87
T sign(T x)
Definition: cxx11_tensor_builtins_sycl.cpp:172
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217

References a, abs(), i, Eigen::bfloat16_impl::log(), m1, m2(), max, Eigen::numext::maxi(), Eigen::bfloat16_impl::pow(), Eigen::ArrayBase< Derived >::pow(), qr(), SYCL::sign(), size, STATIC_CHECK, Eigen::value, VERIFY_IS_APPROX, and VERIFY_IS_MUCH_SMALLER_THAN.

◆ qr_verify_assert()

template<typename MatrixType >
void qr_verify_assert ( )
95  {
97 
99  VERIFY_RAISES_ASSERT(qr.matrixQR())
103  VERIFY_RAISES_ASSERT(qr.householderQ())
105  VERIFY_RAISES_ASSERT(qr.absDeterminant())
106  VERIFY_RAISES_ASSERT(qr.signDeterminant())
107 }
void adjoint(const MatrixType &m)
Definition: adjoint.cpp:85
void determinant(const MatrixType &m)
Definition: determinant.cpp:15
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:329
void transpose()
Definition: skew_symmetric_matrix3.cpp:135
Update the problem specs before solve
Definition: steady_axisym_advection_diffusion.cc:353

References qr(), tmp, and VERIFY_RAISES_ASSERT.