1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2010-2017 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 CHECK_MMTR(DEST, TRI, OP) { \
13 ref3 = DEST; \
14 ref2 = ref1 = DEST; \
15 DEST.template triangularView<TRI>() OP; \
16 ref1 OP; \
17 ref2.template triangularView<TRI>() \
18 = ref1.template triangularView<TRI>(); \
19 VERIFY_IS_APPROX(DEST,ref2); \
20 \
21 DEST = ref3; \
22 ref3 = ref2; \
23 ref3.diagonal() = DEST.diagonal(); \
24 DEST.template triangularView<TRI|ZeroDiag>() OP; \
25 VERIFY_IS_APPROX(DEST,ref3); \
26 }
27
mmtr(int size)28 template<typename Scalar> void mmtr(int size)
29 {
30 typedef Matrix<Scalar,Dynamic,Dynamic,ColMajor> MatrixColMaj;
31 typedef Matrix<Scalar,Dynamic,Dynamic,RowMajor> MatrixRowMaj;
32
33 DenseIndex othersize = internal::random<DenseIndex>(1,200);
34
35 MatrixColMaj matc = MatrixColMaj::Zero(size, size);
36 MatrixRowMaj matr = MatrixRowMaj::Zero(size, size);
37 MatrixColMaj ref1(size, size), ref2(size, size), ref3(size,size);
38
39 MatrixColMaj soc(size,othersize); soc.setRandom();
40 MatrixColMaj osc(othersize,size); osc.setRandom();
41 MatrixRowMaj sor(size,othersize); sor.setRandom();
42 MatrixRowMaj osr(othersize,size); osr.setRandom();
43 MatrixColMaj sqc(size,size); sqc.setRandom();
44 MatrixRowMaj sqr(size,size); sqr.setRandom();
45
46 Scalar s = internal::random<Scalar>();
47
48 CHECK_MMTR(matc, Lower, = s*soc*sor.adjoint());
49 CHECK_MMTR(matc, Upper, = s*(soc*soc.adjoint()));
50 CHECK_MMTR(matr, Lower, = s*soc*soc.adjoint());
51 CHECK_MMTR(matr, Upper, = soc*(s*sor.adjoint()));
52
53 CHECK_MMTR(matc, Lower, += s*soc*soc.adjoint());
54 CHECK_MMTR(matc, Upper, += s*(soc*sor.transpose()));
55 CHECK_MMTR(matr, Lower, += s*sor*soc.adjoint());
56 CHECK_MMTR(matr, Upper, += soc*(s*soc.adjoint()));
57
58 CHECK_MMTR(matc, Lower, -= s*soc*soc.adjoint());
59 CHECK_MMTR(matc, Upper, -= s*(osc.transpose()*osc.conjugate()));
60 CHECK_MMTR(matr, Lower, -= s*soc*soc.adjoint());
61 CHECK_MMTR(matr, Upper, -= soc*(s*soc.adjoint()));
62
63 CHECK_MMTR(matc, Lower, -= s*sqr*sqc.template triangularView<Upper>());
64 CHECK_MMTR(matc, Upper, = s*sqc*sqr.template triangularView<Upper>());
65 CHECK_MMTR(matc, Lower, += s*sqr*sqc.template triangularView<Lower>());
66 CHECK_MMTR(matc, Upper, = s*sqc*sqc.template triangularView<Lower>());
67
68 CHECK_MMTR(matc, Lower, = (s*sqr).template triangularView<Upper>()*sqc);
69 CHECK_MMTR(matc, Upper, -= (s*sqc).template triangularView<Upper>()*sqc);
70 CHECK_MMTR(matc, Lower, = (s*sqr).template triangularView<Lower>()*sqc);
71 CHECK_MMTR(matc, Upper, += (s*sqc).template triangularView<Lower>()*sqc);
72
73 // check aliasing
74 ref2 = ref1 = matc;
75 ref1 = sqc.adjoint() * matc * sqc;
76 ref2.template triangularView<Upper>() = ref1.template triangularView<Upper>();
77 matc.template triangularView<Upper>() = sqc.adjoint() * matc * sqc;
78 VERIFY_IS_APPROX(matc, ref2);
79
80 ref2 = ref1 = matc;
81 ref1 = sqc * matc * sqc.adjoint();
82 ref2.template triangularView<Lower>() = ref1.template triangularView<Lower>();
83 matc.template triangularView<Lower>() = sqc * matc * sqc.adjoint();
84 VERIFY_IS_APPROX(matc, ref2);
85 }
86
test_product_mmtr()87 void test_product_mmtr()
88 {
89 for(int i = 0; i < g_repeat ; i++)
90 {
91 CALL_SUBTEST_1((mmtr<float>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
92 CALL_SUBTEST_2((mmtr<double>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
93 CALL_SUBTEST_3((mmtr<std::complex<float> >(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))));
94 CALL_SUBTEST_4((mmtr<std::complex<double> >(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))));
95 }
96 }
97