blob: 9f99d9afed94dce85140076d25b0e31e57ab77c7 [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//
4// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@gmail.com>
5//
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<int OtherSize> struct symm_extra {
28 template<typename M1, typename M2, typename Scalar>
29 static void run(M1& m1, M1& m2, M2& rhs2, M2& rhs22, M2& rhs23, Scalar s1, Scalar s2)
30 {
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010031 m2 = m1.template triangularView<Lower>();
32 VERIFY_IS_APPROX(rhs22 = (rhs2) * (m2).template selfadjointView<Lower>(),
Gael Guennebaud0590c182009-07-27 13:17:39 +020033 rhs23 = (rhs2) * (m1));
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010034 VERIFY_IS_APPROX(rhs22 = (s2*rhs2) * (s1*m2).template selfadjointView<Lower>(),
Gael Guennebaud0590c182009-07-27 13:17:39 +020035 rhs23 = (s2*rhs2) * (s1*m1));
36 }
37};
Gael Guennebaud82c54382009-07-23 21:22:51 +020038
Gael Guennebaud0590c182009-07-27 13:17:39 +020039template<> struct symm_extra<1> {
40 template<typename M1, typename M2, typename Scalar>
Gael Guennebaud264fe822009-07-28 17:13:13 +020041 static void run(M1&, M1&, M2&, M2&, M2&, Scalar, Scalar) {}
Gael Guennebaud0590c182009-07-27 13:17:39 +020042};
43
44template<typename Scalar, int Size, int OtherSize> void symm(int size = Size, int othersize = OtherSize)
45{
46 typedef typename NumTraits<Scalar>::Real RealScalar;
47
48 typedef Matrix<Scalar, Size, Size> MatrixType;
49 typedef Matrix<Scalar, Size, OtherSize> Rhs1;
50 typedef Matrix<Scalar, OtherSize, Size> Rhs2;
Benoit Jacob92da5742010-03-21 11:28:03 -040051 enum { order = OtherSize==1 ? 0 : RowMajor };
52 typedef Matrix<Scalar, Size, OtherSize,order> Rhs3;
Gael Guennebaud37dcdb12010-06-22 23:43:12 +020053 typedef typename MatrixType::Index Index;
Gael Guennebaud0590c182009-07-27 13:17:39 +020054
Hauke Heibelf1679c72010-06-20 17:37:56 +020055 Index rows = size;
56 Index cols = size;
Gael Guennebaud82c54382009-07-23 21:22:51 +020057
58 MatrixType m1 = MatrixType::Random(rows, cols),
Benoit Jacob92da5742010-03-21 11:28:03 -040059 m2 = MatrixType::Random(rows, cols), m3;
Gael Guennebaud82c54382009-07-23 21:22:51 +020060
61 m1 = (m1+m1.adjoint()).eval();
62
Gael Guennebaud0590c182009-07-27 13:17:39 +020063 Rhs1 rhs1 = Rhs1::Random(cols, othersize), rhs12(cols, othersize), rhs13(cols, othersize);
64 Rhs2 rhs2 = Rhs2::Random(othersize, rows), rhs22(othersize, rows), rhs23(othersize, rows);
65 Rhs3 rhs3 = Rhs3::Random(cols, othersize), rhs32(cols, othersize), rhs33(cols, othersize);
Gael Guennebaud82c54382009-07-23 21:22:51 +020066
67 Scalar s1 = ei_random<Scalar>(),
68 s2 = ei_random<Scalar>();
69
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010070 m2 = m1.template triangularView<Lower>();
Benoit Jacob92da5742010-03-21 11:28:03 -040071 m3 = m2.template selfadjointView<Lower>();
72 VERIFY_IS_EQUAL(m1, m3);
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010073 VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs1),
Gael Guennebaud82c54382009-07-23 21:22:51 +020074 rhs13 = (s1*m1) * (s2*rhs1));
75
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010076 m2 = m1.template triangularView<Upper>(); rhs12.setRandom(); rhs13 = rhs12;
Benoit Jacob92da5742010-03-21 11:28:03 -040077 m3 = m2.template selfadjointView<Upper>();
78 VERIFY_IS_EQUAL(m1, m3);
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010079 VERIFY_IS_APPROX(rhs12 += (s1*m2).template selfadjointView<Upper>() * (s2*rhs1),
Gael Guennebaud0590c182009-07-27 13:17:39 +020080 rhs13 += (s1*m1) * (s2*rhs1));
Gael Guennebaud82c54382009-07-23 21:22:51 +020081
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010082 m2 = m1.template triangularView<Lower>();
83 VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs2.adjoint()),
Gael Guennebaud0590c182009-07-27 13:17:39 +020084 rhs13 = (s1*m1) * (s2*rhs2.adjoint()));
Gael Guennebaud82c54382009-07-23 21:22:51 +020085
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010086 m2 = m1.template triangularView<Upper>();
87 VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Upper>() * (s2*rhs2.adjoint()),
Gael Guennebaud0590c182009-07-27 13:17:39 +020088 rhs13 = (s1*m1) * (s2*rhs2.adjoint()));
Gael Guennebaud82c54382009-07-23 21:22:51 +020089
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010090 m2 = m1.template triangularView<Upper>();
91 VERIFY_IS_APPROX(rhs12 = (s1*m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs2.adjoint()),
Gael Guennebaud0590c182009-07-27 13:17:39 +020092 rhs13 = (s1*m1.adjoint()) * (s2*rhs2.adjoint()));
Gael Guennebaud82c54382009-07-23 21:22:51 +020093
94 // test row major = <...>
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010095 m2 = m1.template triangularView<Lower>(); rhs12.setRandom(); rhs13 = rhs12;
96 VERIFY_IS_APPROX(rhs12 -= (s1*m2).template selfadjointView<Lower>() * (s2*rhs3),
Gael Guennebaud0590c182009-07-27 13:17:39 +020097 rhs13 -= (s1*m1) * (s2 * rhs3));
Gael Guennebaud82c54382009-07-23 21:22:51 +020098
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +010099 m2 = m1.template triangularView<Upper>();
100 VERIFY_IS_APPROX(rhs12 = (s1*m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs3).conjugate(),
Gael Guennebaud0590c182009-07-27 13:17:39 +0200101 rhs13 = (s1*m1.adjoint()) * (s2*rhs3).conjugate());
Gael Guennebaud82c54382009-07-23 21:22:51 +0200102
Gael Guennebaudafbd73b2009-08-11 15:15:06 +0200103
Gael Guennebaudc5d7c9f2010-01-07 21:15:32 +0100104 m2 = m1.template triangularView<Upper>(); rhs13 = rhs12;
105 VERIFY_IS_APPROX(rhs12.noalias() += s1 * ((m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs3).conjugate()),
Gael Guennebaudafbd73b2009-08-11 15:15:06 +0200106 rhs13 += (s1*m1.adjoint()) * (s2*rhs3).conjugate());
107
Gael Guennebaud82c54382009-07-23 21:22:51 +0200108 // test matrix * selfadjoint
Gael Guennebaud0590c182009-07-27 13:17:39 +0200109 symm_extra<OtherSize>::run(m1,m2,rhs2,rhs22,rhs23,s1,s2);
110
Gael Guennebaud82c54382009-07-23 21:22:51 +0200111}
Gael Guennebaud0590c182009-07-27 13:17:39 +0200112
Gael Guennebaud82c54382009-07-23 21:22:51 +0200113void test_product_symm()
114{
115 for(int i = 0; i < g_repeat ; i++)
116 {
Gael Guennebaud88e7a572010-06-24 23:06:21 +0200117 CALL_SUBTEST_1(( symm<float,Dynamic,Dynamic>(ei_random<int>(1,320),ei_random<int>(1,320)) ));
118 CALL_SUBTEST_2(( symm<double,Dynamic,Dynamic>(ei_random<int>(1,320),ei_random<int>(1,320)) ));
119 CALL_SUBTEST_3(( symm<std::complex<double>,Dynamic,Dynamic>(ei_random<int>(1,320),ei_random<int>(1,320)) ));
Gael Guennebaud0590c182009-07-27 13:17:39 +0200120
Gael Guennebaud88e7a572010-06-24 23:06:21 +0200121 CALL_SUBTEST_4(( symm<float,Dynamic,1>(ei_random<int>(1,320)) ));
122 CALL_SUBTEST_5(( symm<double,Dynamic,1>(ei_random<int>(1,320)) ));
123 CALL_SUBTEST_6(( symm<std::complex<double>,Dynamic,1>(ei_random<int>(1,320)) ));
Gael Guennebaud82c54382009-07-23 21:22:51 +0200124 }
125}