blob: 21c2f605b2ed7065554a9d1ba0860047f40e7b99 [file] [log] [blame]
Gael Guennebaud82c54382009-07-23 21:22:51 +02001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
Gael Guennebaud28e64b02010-06-24 23:21:58 +02004// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
Gael Guennebaud82c54382009-07-23 21:22:51 +02005//
6// Eigen is free software; you can redistribute it and/or
7// modify it under the terms of the GNU Lesser General Public
8// License as published by the Free Software Foundation; either
9// version 3 of the License, or (at your option) any later version.
10//
11// Alternatively, you can redistribute it and/or
12// modify it under the terms of the GNU General Public License as
13// published by the Free Software Foundation; either version 2 of
14// the License, or (at your option) any later version.
15//
16// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public
22// License and a copy of the GNU General Public License along with
23// Eigen. If not, see <http://www.gnu.org/licenses/>.
24
25#include "main.h"
26
Gael Guennebaud0590c182009-07-27 13:17:39 +020027template<typename Scalar, int Size, int OtherSize> void symm(int size = Size, int othersize = OtherSize)
28{
29 typedef typename NumTraits<Scalar>::Real RealScalar;
30
31 typedef Matrix<Scalar, Size, Size> MatrixType;
32 typedef Matrix<Scalar, Size, OtherSize> Rhs1;
33 typedef Matrix<Scalar, OtherSize, Size> Rhs2;
Benoit Jacob92da5742010-03-21 11:28:03 -040034 enum { order = OtherSize==1 ? 0 : RowMajor };
35 typedef Matrix<Scalar, Size, OtherSize,order> Rhs3;
Gael Guennebaud37dcdb12010-06-22 23:43:12 +020036 typedef typename MatrixType::Index Index;
Gael Guennebaud0590c182009-07-27 13:17:39 +020037
Hauke Heibelf1679c72010-06-20 17:37:56 +020038 Index rows = size;
39 Index cols = size;
Gael Guennebaud82c54382009-07-23 21:22:51 +020040
41 MatrixType m1 = MatrixType::Random(rows, cols),
Benoit Jacob92da5742010-03-21 11:28:03 -040042 m2 = MatrixType::Random(rows, cols), m3;
Gael Guennebaud82c54382009-07-23 21:22:51 +020043
44 m1 = (m1+m1.adjoint()).eval();
45
Gael Guennebaud0590c182009-07-27 13:17:39 +020046 Rhs1 rhs1 = Rhs1::Random(cols, othersize), rhs12(cols, othersize), rhs13(cols, othersize);
47 Rhs2 rhs2 = Rhs2::Random(othersize, rows), rhs22(othersize, rows), rhs23(othersize, rows);
48 Rhs3 rhs3 = Rhs3::Random(cols, othersize), rhs32(cols, othersize), rhs33(cols, othersize);
Gael Guennebaud82c54382009-07-23 21:22:51 +020049
Benoit Jacob47160402010-10-25 10:15:22 -040050 Scalar s1 = internal::random<Scalar>(),
51 s2 = internal::random<Scalar>();
Gael Guennebaud82c54382009-07-23 21:22:51 +020052
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010053 m2 = m1.template triangularView<Lower>();
Benoit Jacob92da5742010-03-21 11:28:03 -040054 m3 = m2.template selfadjointView<Lower>();
55 VERIFY_IS_EQUAL(m1, m3);
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010056 VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs1),
Gael Guennebaud82c54382009-07-23 21:22:51 +020057 rhs13 = (s1*m1) * (s2*rhs1));
58
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010059 m2 = m1.template triangularView<Upper>(); rhs12.setRandom(); rhs13 = rhs12;
Benoit Jacob92da5742010-03-21 11:28:03 -040060 m3 = m2.template selfadjointView<Upper>();
61 VERIFY_IS_EQUAL(m1, m3);
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010062 VERIFY_IS_APPROX(rhs12 += (s1*m2).template selfadjointView<Upper>() * (s2*rhs1),
Gael Guennebaud0590c182009-07-27 13:17:39 +020063 rhs13 += (s1*m1) * (s2*rhs1));
Gael Guennebaud82c54382009-07-23 21:22:51 +020064
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010065 m2 = m1.template triangularView<Lower>();
66 VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs2.adjoint()),
Gael Guennebaud0590c182009-07-27 13:17:39 +020067 rhs13 = (s1*m1) * (s2*rhs2.adjoint()));
Gael Guennebaud82c54382009-07-23 21:22:51 +020068
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010069 m2 = m1.template triangularView<Upper>();
70 VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Upper>() * (s2*rhs2.adjoint()),
Gael Guennebaud0590c182009-07-27 13:17:39 +020071 rhs13 = (s1*m1) * (s2*rhs2.adjoint()));
Gael Guennebaud82c54382009-07-23 21:22:51 +020072
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010073 m2 = m1.template triangularView<Upper>();
74 VERIFY_IS_APPROX(rhs12 = (s1*m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs2.adjoint()),
Gael Guennebaud0590c182009-07-27 13:17:39 +020075 rhs13 = (s1*m1.adjoint()) * (s2*rhs2.adjoint()));
Gael Guennebaud82c54382009-07-23 21:22:51 +020076
77 // test row major = <...>
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010078 m2 = m1.template triangularView<Lower>(); rhs12.setRandom(); rhs13 = rhs12;
79 VERIFY_IS_APPROX(rhs12 -= (s1*m2).template selfadjointView<Lower>() * (s2*rhs3),
Gael Guennebaud0590c182009-07-27 13:17:39 +020080 rhs13 -= (s1*m1) * (s2 * rhs3));
Gael Guennebaud82c54382009-07-23 21:22:51 +020081
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010082 m2 = m1.template triangularView<Upper>();
83 VERIFY_IS_APPROX(rhs12 = (s1*m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs3).conjugate(),
Gael Guennebaud0590c182009-07-27 13:17:39 +020084 rhs13 = (s1*m1.adjoint()) * (s2*rhs3).conjugate());
Gael Guennebaud82c54382009-07-23 21:22:51 +020085
Gael Guennebaudafbd73b2009-08-11 15:15:06 +020086
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010087 m2 = m1.template triangularView<Upper>(); rhs13 = rhs12;
88 VERIFY_IS_APPROX(rhs12.noalias() += s1 * ((m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs3).conjugate()),
Gael Guennebaudafbd73b2009-08-11 15:15:06 +020089 rhs13 += (s1*m1.adjoint()) * (s2*rhs3).conjugate());
90
Gael Guennebaud5354ffb2010-08-19 14:05:21 +020091 m2 = m1.template triangularView<Lower>();
92 VERIFY_IS_APPROX(rhs22 = (rhs2) * (m2).template selfadjointView<Lower>(), rhs23 = (rhs2) * (m1));
93 VERIFY_IS_APPROX(rhs22 = (s2*rhs2) * (s1*m2).template selfadjointView<Lower>(), rhs23 = (s2*rhs2) * (s1*m1));
Gael Guennebaud0590c182009-07-27 13:17:39 +020094
Gael Guennebaud82c54382009-07-23 21:22:51 +020095}
Gael Guennebaud0590c182009-07-27 13:17:39 +020096
Gael Guennebaud82c54382009-07-23 21:22:51 +020097void test_product_symm()
98{
99 for(int i = 0; i < g_repeat ; i++)
100 {
Benoit Jacob47160402010-10-25 10:15:22 -0400101 CALL_SUBTEST_1(( symm<float,Dynamic,Dynamic>(internal::random<int>(1,320),internal::random<int>(1,320)) ));
102 CALL_SUBTEST_2(( symm<double,Dynamic,Dynamic>(internal::random<int>(1,320),internal::random<int>(1,320)) ));
103 CALL_SUBTEST_3(( symm<std::complex<float>,Dynamic,Dynamic>(internal::random<int>(1,200),internal::random<int>(1,200)) ));
104 CALL_SUBTEST_4(( symm<std::complex<double>,Dynamic,Dynamic>(internal::random<int>(1,200),internal::random<int>(1,200)) ));
Gael Guennebaud0590c182009-07-27 13:17:39 +0200105
Benoit Jacob47160402010-10-25 10:15:22 -0400106 CALL_SUBTEST_5(( symm<float,Dynamic,1>(internal::random<int>(1,320)) ));
107 CALL_SUBTEST_6(( symm<double,Dynamic,1>(internal::random<int>(1,320)) ));
108 CALL_SUBTEST_7(( symm<std::complex<float>,Dynamic,1>(internal::random<int>(1,320)) ));
109 CALL_SUBTEST_8(( symm<std::complex<double>,Dynamic,1>(internal::random<int>(1,320)) ));
Gael Guennebaud82c54382009-07-23 21:22:51 +0200110 }
111}