product_trmm.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 template<typename T>
14 {
15  const int factor = NumTraits<T>::ReadCost;
16  const int max_test_size = EIGEN_TEST_MAX_SIZE>2*factor ? EIGEN_TEST_MAX_SIZE/factor : EIGEN_TEST_MAX_SIZE;
17  return internal::random<int>(1,max_test_size);
18 }
19 
20 template<typename Scalar, int Mode, int TriOrder, int OtherOrder, int ResOrder, int OtherCols>
21 void trmm(int rows=get_random_size<Scalar>(),
22  int cols=get_random_size<Scalar>(),
23  int otherCols = OtherCols==Dynamic?get_random_size<Scalar>():OtherCols)
24 {
28 
31 
32  TriMatrix mat(rows,cols), tri(rows,cols), triTr(cols,rows), s1tri(rows,cols), s1triTr(cols,rows);
33 
34  OnTheRight ge_right(cols,otherCols);
35  OnTheLeft ge_left(otherCols,rows);
36  ResSX ge_sx, ge_sx_save;
37  ResXS ge_xs, ge_xs_save;
38 
39  Scalar s1 = internal::random<Scalar>(),
40  s2 = internal::random<Scalar>();
41 
42  mat.setRandom();
43  tri = mat.template triangularView<Mode>();
44  triTr = mat.transpose().template triangularView<Mode>();
45  s1tri = (s1*mat).template triangularView<Mode>();
46  s1triTr = (s1*mat).transpose().template triangularView<Mode>();
47  ge_right.setRandom();
48  ge_left.setRandom();
49 
50  VERIFY_IS_APPROX( ge_xs = mat.template triangularView<Mode>() * ge_right, tri * ge_right);
51  VERIFY_IS_APPROX( ge_sx = ge_left * mat.template triangularView<Mode>(), ge_left * tri);
52 
53  VERIFY_IS_APPROX( ge_xs.noalias() = mat.template triangularView<Mode>() * ge_right, tri * ge_right);
54  VERIFY_IS_APPROX( ge_sx.noalias() = ge_left * mat.template triangularView<Mode>(), ge_left * tri);
55 
56  if((Mode&UnitDiag)==0)
57  VERIFY_IS_APPROX( ge_xs.noalias() = (s1*mat.adjoint()).template triangularView<Mode>() * (s2*ge_left.transpose()), s1*triTr.conjugate() * (s2*ge_left.transpose()));
58 
59  VERIFY_IS_APPROX( ge_xs.noalias() = (s1*mat.transpose()).template triangularView<Mode>() * (s2*ge_left.transpose()), s1triTr * (s2*ge_left.transpose()));
60  VERIFY_IS_APPROX( ge_sx.noalias() = (s2*ge_left) * (s1*mat).template triangularView<Mode>(), (s2*ge_left)*s1tri);
61 
62  VERIFY_IS_APPROX( ge_sx.noalias() = ge_right.transpose() * mat.adjoint().template triangularView<Mode>(), ge_right.transpose() * triTr.conjugate());
63  VERIFY_IS_APPROX( ge_sx.noalias() = ge_right.adjoint() * mat.adjoint().template triangularView<Mode>(), ge_right.adjoint() * triTr.conjugate());
64 
65  ge_xs_save = ge_xs;
66  if((Mode&UnitDiag)==0)
67  VERIFY_IS_APPROX( (ge_xs_save + s1*triTr.conjugate() * (s2*ge_left.adjoint())).eval(), ge_xs.noalias() += (s1*mat.adjoint()).template triangularView<Mode>() * (s2*ge_left.adjoint()) );
68  ge_xs_save = ge_xs;
69  VERIFY_IS_APPROX( (ge_xs_save + s1triTr * (s2*ge_left.adjoint())).eval(), ge_xs.noalias() += (s1*mat.transpose()).template triangularView<Mode>() * (s2*ge_left.adjoint()) );
70  ge_sx.setRandom();
71  ge_sx_save = ge_sx;
72  if((Mode&UnitDiag)==0)
73  VERIFY_IS_APPROX( ge_sx_save - (ge_right.adjoint() * (-s1 * triTr).conjugate()).eval(), ge_sx.noalias() -= (ge_right.adjoint() * (-s1 * mat).adjoint().template triangularView<Mode>()).eval());
74 
75  if((Mode&UnitDiag)==0)
76  VERIFY_IS_APPROX( ge_xs = (s1*mat).adjoint().template triangularView<Mode>() * ge_left.adjoint(), numext::conj(s1) * triTr.conjugate() * ge_left.adjoint());
77  VERIFY_IS_APPROX( ge_xs = (s1*mat).transpose().template triangularView<Mode>() * ge_left.adjoint(), s1triTr * ge_left.adjoint());
78 
79  // TODO check with sub-matrix expressions ?
80 
81  // destination with a non-default inner-stride
82  // see bug 1741
83  {
84  VERIFY_IS_APPROX( ge_xs.noalias() = mat.template triangularView<Mode>() * ge_right, tri * ge_right);
85  typedef Matrix<Scalar,Dynamic,Dynamic> MatrixX;
86  MatrixX buffer(2*ge_xs.rows(),2*ge_xs.cols());
87  Map<ResXS,0,Stride<Dynamic,2> > map1(buffer.data(),ge_xs.rows(),ge_xs.cols(),Stride<Dynamic,2>(2*ge_xs.outerStride(),2));
88  buffer.setZero();
89  VERIFY_IS_APPROX( map1.noalias() = mat.template triangularView<Mode>() * ge_right, tri * ge_right);
90  }
91 }
92 
93 template<typename Scalar, int Mode, int TriOrder>
94 void trmv(int rows=get_random_size<Scalar>(), int cols=get_random_size<Scalar>())
95 {
96  trmm<Scalar,Mode,TriOrder,ColMajor,ColMajor,1>(rows,cols,1);
97 }
98 
99 template<typename Scalar, int Mode, int TriOrder, int OtherOrder, int ResOrder>
100 void trmm(int rows=get_random_size<Scalar>(), int cols=get_random_size<Scalar>(), int otherCols = get_random_size<Scalar>())
101 {
102  trmm<Scalar,Mode,TriOrder,OtherOrder,ResOrder,Dynamic>(rows,cols,otherCols);
103 }
104 
105 #define CALL_ALL_ORDERS(NB,SCALAR,MODE) \
106  EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, ColMajor,ColMajor,ColMajor>())); \
107  EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, ColMajor,ColMajor,RowMajor>())); \
108  EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, ColMajor,RowMajor,ColMajor>())); \
109  EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, ColMajor,RowMajor,RowMajor>())); \
110  EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, RowMajor,ColMajor,ColMajor>())); \
111  EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, RowMajor,ColMajor,RowMajor>())); \
112  EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, RowMajor,RowMajor,ColMajor>())); \
113  EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, RowMajor,RowMajor,RowMajor>())); \
114  \
115  EIGEN_CAT(CALL_SUBTEST_1,NB)((trmv<SCALAR, MODE, ColMajor>())); \
116  EIGEN_CAT(CALL_SUBTEST_1,NB)((trmv<SCALAR, MODE, RowMajor>()));
117 
118 
119 #define CALL_ALL(NB,SCALAR) \
120  CALL_ALL_ORDERS(EIGEN_CAT(1,NB),SCALAR,Upper) \
121  CALL_ALL_ORDERS(EIGEN_CAT(2,NB),SCALAR,UnitUpper) \
122  CALL_ALL_ORDERS(EIGEN_CAT(3,NB),SCALAR,StrictlyUpper) \
123  CALL_ALL_ORDERS(EIGEN_CAT(1,NB),SCALAR,Lower) \
124  CALL_ALL_ORDERS(EIGEN_CAT(2,NB),SCALAR,UnitLower) \
125  CALL_ALL_ORDERS(EIGEN_CAT(3,NB),SCALAR,StrictlyLower)
126 
127 
128 EIGEN_DECLARE_TEST(product_trmm)
129 {
130  for(int i = 0; i < g_repeat ; i++)
131  {
132  CALL_ALL(1,float); // EIGEN_SUFFIXES;11;111;21;121;31;131
133  CALL_ALL(2,double); // EIGEN_SUFFIXES;12;112;22;122;32;132
134  CALL_ALL(3,std::complex<float>); // EIGEN_SUFFIXES;13;113;23;123;33;133
135  CALL_ALL(4,std::complex<double>); // EIGEN_SUFFIXES;14;114;24;124;34;134
136  }
137 }
SCALAR Scalar
Definition: bench_gemm.cpp:46
void adjoint(const MatrixType &m)
Definition: adjoint.cpp:67
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
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_DECLARE_TEST(product_trmm)
int get_random_size()
AnnoyingScalar conj(const AnnoyingScalar &x)
#define VERIFY_IS_APPROX(a, b)
void trmm(int rows=get_random_size< Scalar >(), int cols=get_random_size< Scalar >(), int otherCols=OtherCols==Dynamic?get_random_size< Scalar >():OtherCols)
void trmv(int rows=get_random_size< Scalar >(), int cols=get_random_size< Scalar >())
static int g_repeat
Definition: main.h:169
#define CALL_ALL(NB, SCALAR)
EIGEN_DEVICE_FUNC ConjugateReturnType conjugate() const
Tridiagonalization< MatrixXf > tri
#define EIGEN_TEST_MAX_SIZE
internal::nested_eval< T, 1 >::type eval(const T &xpr)
const int Dynamic
Definition: Constants.h:22
The matrix class, also used for vectors and row-vectors.


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