Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 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@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 Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 27 | template<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 Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 31 | m2 = m1.template triangularView<Lower>(); |
| 32 | VERIFY_IS_APPROX(rhs22 = (rhs2) * (m2).template selfadjointView<Lower>(), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 33 | rhs23 = (rhs2) * (m1)); |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 34 | VERIFY_IS_APPROX(rhs22 = (s2*rhs2) * (s1*m2).template selfadjointView<Lower>(), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 35 | rhs23 = (s2*rhs2) * (s1*m1)); |
| 36 | } |
| 37 | }; |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 38 | |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 39 | template<> struct symm_extra<1> { |
| 40 | template<typename M1, typename M2, typename Scalar> |
Gael Guennebaud | 264fe82 | 2009-07-28 17:13:13 +0200 | [diff] [blame] | 41 | static void run(M1&, M1&, M2&, M2&, M2&, Scalar, Scalar) {} |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | template<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 Jacob | 92da574 | 2010-03-21 11:28:03 -0400 | [diff] [blame] | 51 | enum { order = OtherSize==1 ? 0 : RowMajor }; |
| 52 | typedef Matrix<Scalar, Size, OtherSize,order> Rhs3; |
Gael Guennebaud | 37dcdb1 | 2010-06-22 23:43:12 +0200 | [diff] [blame] | 53 | typedef typename MatrixType::Index Index; |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 54 | |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 55 | Index rows = size; |
| 56 | Index cols = size; |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 57 | |
| 58 | MatrixType m1 = MatrixType::Random(rows, cols), |
Benoit Jacob | 92da574 | 2010-03-21 11:28:03 -0400 | [diff] [blame] | 59 | m2 = MatrixType::Random(rows, cols), m3; |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 60 | |
| 61 | m1 = (m1+m1.adjoint()).eval(); |
| 62 | |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 63 | 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 Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 66 | |
| 67 | Scalar s1 = ei_random<Scalar>(), |
| 68 | s2 = ei_random<Scalar>(); |
| 69 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 70 | m2 = m1.template triangularView<Lower>(); |
Benoit Jacob | 92da574 | 2010-03-21 11:28:03 -0400 | [diff] [blame] | 71 | m3 = m2.template selfadjointView<Lower>(); |
| 72 | VERIFY_IS_EQUAL(m1, m3); |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 73 | VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs1), |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 74 | rhs13 = (s1*m1) * (s2*rhs1)); |
| 75 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 76 | m2 = m1.template triangularView<Upper>(); rhs12.setRandom(); rhs13 = rhs12; |
Benoit Jacob | 92da574 | 2010-03-21 11:28:03 -0400 | [diff] [blame] | 77 | m3 = m2.template selfadjointView<Upper>(); |
| 78 | VERIFY_IS_EQUAL(m1, m3); |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 79 | VERIFY_IS_APPROX(rhs12 += (s1*m2).template selfadjointView<Upper>() * (s2*rhs1), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 80 | rhs13 += (s1*m1) * (s2*rhs1)); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 81 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 82 | m2 = m1.template triangularView<Lower>(); |
| 83 | VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs2.adjoint()), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 84 | rhs13 = (s1*m1) * (s2*rhs2.adjoint())); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 85 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 86 | m2 = m1.template triangularView<Upper>(); |
| 87 | VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Upper>() * (s2*rhs2.adjoint()), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 88 | rhs13 = (s1*m1) * (s2*rhs2.adjoint())); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 89 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 90 | m2 = m1.template triangularView<Upper>(); |
| 91 | VERIFY_IS_APPROX(rhs12 = (s1*m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs2.adjoint()), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 92 | rhs13 = (s1*m1.adjoint()) * (s2*rhs2.adjoint())); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 93 | |
| 94 | // test row major = <...> |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 95 | m2 = m1.template triangularView<Lower>(); rhs12.setRandom(); rhs13 = rhs12; |
| 96 | VERIFY_IS_APPROX(rhs12 -= (s1*m2).template selfadjointView<Lower>() * (s2*rhs3), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 97 | rhs13 -= (s1*m1) * (s2 * rhs3)); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 98 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 99 | m2 = m1.template triangularView<Upper>(); |
| 100 | VERIFY_IS_APPROX(rhs12 = (s1*m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs3).conjugate(), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 101 | rhs13 = (s1*m1.adjoint()) * (s2*rhs3).conjugate()); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 102 | |
Gael Guennebaud | afbd73b | 2009-08-11 15:15:06 +0200 | [diff] [blame] | 103 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 104 | m2 = m1.template triangularView<Upper>(); rhs13 = rhs12; |
| 105 | VERIFY_IS_APPROX(rhs12.noalias() += s1 * ((m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs3).conjugate()), |
Gael Guennebaud | afbd73b | 2009-08-11 15:15:06 +0200 | [diff] [blame] | 106 | rhs13 += (s1*m1.adjoint()) * (s2*rhs3).conjugate()); |
| 107 | |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 108 | // test matrix * selfadjoint |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 109 | symm_extra<OtherSize>::run(m1,m2,rhs2,rhs22,rhs23,s1,s2); |
| 110 | |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 111 | } |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 112 | |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 113 | void test_product_symm() |
| 114 | { |
| 115 | for(int i = 0; i < g_repeat ; i++) |
| 116 | { |
Gael Guennebaud | 88e7a57 | 2010-06-24 23:06:21 +0200 | [diff] [blame] | 117 | 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 Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 120 | |
Gael Guennebaud | 88e7a57 | 2010-06-24 23:06:21 +0200 | [diff] [blame] | 121 | 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 Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 124 | } |
| 125 | } |