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