product_trsolve.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 
12 #define VERIFY_TRSM(TRI,XB) { \
13  (XB).setRandom(); ref = (XB); \
14  (TRI).solveInPlace(XB); \
15  VERIFY_IS_APPROX((TRI).toDenseMatrix() * (XB), ref); \
16  (XB).setRandom(); ref = (XB); \
17  (XB) = (TRI).solve(XB); \
18  VERIFY_IS_APPROX((TRI).toDenseMatrix() * (XB), ref); \
19  }
20 
21 #define VERIFY_TRSM_ONTHERIGHT(TRI,XB) { \
22  (XB).setRandom(); ref = (XB); \
23  (TRI).transpose().template solveInPlace<OnTheRight>(XB.transpose()); \
24  VERIFY_IS_APPROX((XB).transpose() * (TRI).transpose().toDenseMatrix(), ref.transpose()); \
25  (XB).setRandom(); ref = (XB); \
26  (XB).transpose() = (TRI).transpose().template solve<OnTheRight>(XB.transpose()); \
27  VERIFY_IS_APPROX((XB).transpose() * (TRI).transpose().toDenseMatrix(), ref.transpose()); \
28  }
29 
30 template<typename Scalar,int Size, int Cols> void trsolve(int size=Size,int cols=Cols)
31 {
32  typedef typename NumTraits<Scalar>::Real RealScalar;
33 
36 
37  enum { colmajor = Size==1 ? RowMajor : ColMajor,
38  rowmajor = Cols==1 ? ColMajor : RowMajor };
42 
43  cmLhs.setRandom(); cmLhs *= static_cast<RealScalar>(0.1); cmLhs.diagonal().array() += static_cast<RealScalar>(1);
44  rmLhs.setRandom(); rmLhs *= static_cast<RealScalar>(0.1); rmLhs.diagonal().array() += static_cast<RealScalar>(1);
45 
46  VERIFY_TRSM(cmLhs.conjugate().template triangularView<Lower>(), cmRhs);
47  VERIFY_TRSM(cmLhs.adjoint() .template triangularView<Lower>(), cmRhs);
48  VERIFY_TRSM(cmLhs .template triangularView<Upper>(), cmRhs);
49  VERIFY_TRSM(cmLhs .template triangularView<Lower>(), rmRhs);
50  VERIFY_TRSM(cmLhs.conjugate().template triangularView<Upper>(), rmRhs);
51  VERIFY_TRSM(cmLhs.adjoint() .template triangularView<Upper>(), rmRhs);
52 
53  VERIFY_TRSM(cmLhs.conjugate().template triangularView<UnitLower>(), cmRhs);
54  VERIFY_TRSM(cmLhs .template triangularView<UnitUpper>(), rmRhs);
55 
56  VERIFY_TRSM(rmLhs .template triangularView<Lower>(), cmRhs);
57  VERIFY_TRSM(rmLhs.conjugate().template triangularView<UnitUpper>(), rmRhs);
58 
59 
60  VERIFY_TRSM_ONTHERIGHT(cmLhs.conjugate().template triangularView<Lower>(), cmRhs);
61  VERIFY_TRSM_ONTHERIGHT(cmLhs .template triangularView<Upper>(), cmRhs);
62  VERIFY_TRSM_ONTHERIGHT(cmLhs .template triangularView<Lower>(), rmRhs);
63  VERIFY_TRSM_ONTHERIGHT(cmLhs.conjugate().template triangularView<Upper>(), rmRhs);
64 
65  VERIFY_TRSM_ONTHERIGHT(cmLhs.conjugate().template triangularView<UnitLower>(), cmRhs);
66  VERIFY_TRSM_ONTHERIGHT(cmLhs .template triangularView<UnitUpper>(), rmRhs);
67 
68  VERIFY_TRSM_ONTHERIGHT(rmLhs .template triangularView<Lower>(), cmRhs);
69  VERIFY_TRSM_ONTHERIGHT(rmLhs.conjugate().template triangularView<UnitUpper>(), rmRhs);
70 
71  int c = internal::random<int>(0,cols-1);
72  VERIFY_TRSM(rmLhs.template triangularView<Lower>(), rmRhs.col(c));
73  VERIFY_TRSM(cmLhs.template triangularView<Lower>(), rmRhs.col(c));
74 
75  // destination with a non-default inner-stride
76  // see bug 1741
77  {
78  typedef Matrix<Scalar,Dynamic,Dynamic> MatrixX;
79  MatrixX buffer(2*cmRhs.rows(),2*cmRhs.cols());
82  buffer.setZero();
83  VERIFY_TRSM(cmLhs.conjugate().template triangularView<Lower>(), map1);
84  buffer.setZero();
85  VERIFY_TRSM(cmLhs .template triangularView<Lower>(), map2);
86  }
87 
88  if(Size==Dynamic)
89  {
90  cmLhs.resize(0,0);
91  cmRhs.resize(0,cmRhs.cols());
92  Matrix<Scalar,Size,Cols,colmajor> res = cmLhs.template triangularView<Lower>().solve(cmRhs);
93  VERIFY_IS_EQUAL(res.rows(),0);
94  VERIFY_IS_EQUAL(res.cols(),cmRhs.cols());
95  res = cmRhs;
96  cmLhs.template triangularView<Lower>().solveInPlace(res);
97  VERIFY_IS_EQUAL(res.rows(),0);
98  VERIFY_IS_EQUAL(res.cols(),cmRhs.cols());
99  }
100 }
101 
102 EIGEN_DECLARE_TEST(product_trsolve)
103 {
104  for(int i = 0; i < g_repeat ; i++)
105  {
106  // matrices
107  CALL_SUBTEST_1((trsolve<float,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
108  CALL_SUBTEST_2((trsolve<double,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
109  CALL_SUBTEST_3((trsolve<std::complex<float>,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2),internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))));
110  CALL_SUBTEST_4((trsolve<std::complex<double>,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2),internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))));
111 
112  // vectors
113  CALL_SUBTEST_5((trsolve<float,Dynamic,1>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
114  CALL_SUBTEST_6((trsolve<double,Dynamic,1>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
115  CALL_SUBTEST_7((trsolve<std::complex<float>,Dynamic,1>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
116  CALL_SUBTEST_8((trsolve<std::complex<double>,Dynamic,1>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
117 
118  // meta-unrollers
119  CALL_SUBTEST_9((trsolve<float,4,1>()));
120  CALL_SUBTEST_10((trsolve<double,4,1>()));
121  CALL_SUBTEST_11((trsolve<std::complex<float>,4,1>()));
122  CALL_SUBTEST_12((trsolve<float,1,1>()));
123  CALL_SUBTEST_13((trsolve<float,1,2>()));
124  CALL_SUBTEST_14((trsolve<float,3,1>()));
125 
126  }
127 }
#define CALL_SUBTEST_12(FUNC)
#define CALL_SUBTEST_9(FUNC)
#define CALL_SUBTEST_6(FUNC)
#define CALL_SUBTEST_4(FUNC)
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
#define CALL_SUBTEST_13(FUNC)
#define CALL_SUBTEST_3(FUNC)
#define CALL_SUBTEST_7(FUNC)
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
#define CALL_SUBTEST_11(FUNC)
#define CALL_SUBTEST_14(FUNC)
Holds strides information for Map.
Definition: Stride.h:48
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outerStride() const EIGEN_NOEXCEPT
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
#define CALL_SUBTEST_10(FUNC)
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
EIGEN_DECLARE_TEST(product_trsolve)
#define VERIFY_TRSM_ONTHERIGHT(TRI, XB)
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
#define CALL_SUBTEST_1(FUNC)
static int g_repeat
Definition: main.h:169
void trsolve(int size=Size, int cols=Cols)
#define CALL_SUBTEST_8(FUNC)
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
static const int Cols
#define CALL_SUBTEST_5(FUNC)
#define EIGEN_TEST_MAX_SIZE
A triangularView< Lower >().adjoint().solveInPlace(B)
#define CALL_SUBTEST_2(FUNC)
const int Dynamic
Definition: Constants.h:22
#define VERIFY_TRSM(TRI, XB)
The matrix class, also used for vectors and row-vectors.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > & setRandom(Index size)
Definition: Random.h:151


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:19