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 | // |
Gael Guennebaud | 28e64b0 | 2010-06-24 23:21:58 +0200 | [diff] [blame] | 4 | // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr> |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 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<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 Jacob | 92da574 | 2010-03-21 11:28:03 -0400 | [diff] [blame] | 34 | enum { order = OtherSize==1 ? 0 : RowMajor }; |
| 35 | typedef Matrix<Scalar, Size, OtherSize,order> Rhs3; |
Gael Guennebaud | 37dcdb1 | 2010-06-22 23:43:12 +0200 | [diff] [blame] | 36 | typedef typename MatrixType::Index Index; |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 37 | |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 38 | Index rows = size; |
| 39 | Index cols = size; |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 40 | |
| 41 | MatrixType m1 = MatrixType::Random(rows, cols), |
Benoit Jacob | 92da574 | 2010-03-21 11:28:03 -0400 | [diff] [blame] | 42 | m2 = MatrixType::Random(rows, cols), m3; |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 43 | |
| 44 | m1 = (m1+m1.adjoint()).eval(); |
| 45 | |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 46 | 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 Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 49 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 50 | Scalar s1 = internal::random<Scalar>(), |
| 51 | s2 = internal::random<Scalar>(); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 52 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 53 | m2 = m1.template triangularView<Lower>(); |
Benoit Jacob | 92da574 | 2010-03-21 11:28:03 -0400 | [diff] [blame] | 54 | m3 = m2.template selfadjointView<Lower>(); |
| 55 | VERIFY_IS_EQUAL(m1, m3); |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 56 | VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs1), |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 57 | rhs13 = (s1*m1) * (s2*rhs1)); |
| 58 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 59 | m2 = m1.template triangularView<Upper>(); rhs12.setRandom(); rhs13 = rhs12; |
Benoit Jacob | 92da574 | 2010-03-21 11:28:03 -0400 | [diff] [blame] | 60 | m3 = m2.template selfadjointView<Upper>(); |
| 61 | VERIFY_IS_EQUAL(m1, m3); |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 62 | VERIFY_IS_APPROX(rhs12 += (s1*m2).template selfadjointView<Upper>() * (s2*rhs1), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 63 | rhs13 += (s1*m1) * (s2*rhs1)); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 64 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 65 | m2 = m1.template triangularView<Lower>(); |
| 66 | VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs2.adjoint()), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 67 | rhs13 = (s1*m1) * (s2*rhs2.adjoint())); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 68 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 69 | m2 = m1.template triangularView<Upper>(); |
| 70 | VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Upper>() * (s2*rhs2.adjoint()), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 71 | rhs13 = (s1*m1) * (s2*rhs2.adjoint())); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 72 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 73 | m2 = m1.template triangularView<Upper>(); |
| 74 | 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] | 75 | rhs13 = (s1*m1.adjoint()) * (s2*rhs2.adjoint())); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 76 | |
| 77 | // test row major = <...> |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 78 | m2 = m1.template triangularView<Lower>(); rhs12.setRandom(); rhs13 = rhs12; |
| 79 | VERIFY_IS_APPROX(rhs12 -= (s1*m2).template selfadjointView<Lower>() * (s2*rhs3), |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 80 | rhs13 -= (s1*m1) * (s2 * rhs3)); |
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<Upper>(); |
| 83 | 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] | 84 | rhs13 = (s1*m1.adjoint()) * (s2*rhs3).conjugate()); |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 85 | |
Gael Guennebaud | afbd73b | 2009-08-11 15:15:06 +0200 | [diff] [blame] | 86 | |
Gael Guennebaud | c5d7c9f | 2010-01-07 21:15:32 +0100 | [diff] [blame] | 87 | m2 = m1.template triangularView<Upper>(); rhs13 = rhs12; |
| 88 | 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] | 89 | rhs13 += (s1*m1.adjoint()) * (s2*rhs3).conjugate()); |
| 90 | |
Gael Guennebaud | 5354ffb | 2010-08-19 14:05:21 +0200 | [diff] [blame] | 91 | 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 Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 94 | |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 95 | } |
Gael Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 96 | |
Gael Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 97 | void test_product_symm() |
| 98 | { |
| 99 | for(int i = 0; i < g_repeat ; i++) |
| 100 | { |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 101 | 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 Guennebaud | 0590c18 | 2009-07-27 13:17:39 +0200 | [diff] [blame] | 105 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 106 | 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 Guennebaud | 82c5438 | 2009-07-23 21:22:51 +0200 | [diff] [blame] | 110 | } |
| 111 | } |