product_symm.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "main.h"
00026 
00027 template<typename Scalar, int Size, int OtherSize> void symm(int size = Size, int othersize = OtherSize)
00028 {
00029   typedef typename NumTraits<Scalar>::Real RealScalar;
00030 
00031   typedef Matrix<Scalar, Size, Size> MatrixType;
00032   typedef Matrix<Scalar, Size, OtherSize> Rhs1;
00033   typedef Matrix<Scalar, OtherSize, Size> Rhs2;
00034   enum { order = OtherSize==1 ? 0 : RowMajor };
00035   typedef Matrix<Scalar, Size, OtherSize,order> Rhs3;
00036   typedef typename MatrixType::Index Index;
00037 
00038   Index rows = size;
00039   Index cols = size;
00040 
00041   MatrixType m1 = MatrixType::Random(rows, cols),
00042              m2 = MatrixType::Random(rows, cols), m3;
00043 
00044   m1 = (m1+m1.adjoint()).eval();
00045 
00046   Rhs1 rhs1 = Rhs1::Random(cols, othersize), rhs12(cols, othersize), rhs13(cols, othersize);
00047   Rhs2 rhs2 = Rhs2::Random(othersize, rows), rhs22(othersize, rows), rhs23(othersize, rows);
00048   Rhs3 rhs3 = Rhs3::Random(cols, othersize), rhs32(cols, othersize), rhs33(cols, othersize);
00049 
00050   Scalar s1 = internal::random<Scalar>(),
00051          s2 = internal::random<Scalar>();
00052 
00053   m2 = m1.template triangularView<Lower>();
00054   m3 = m2.template selfadjointView<Lower>();
00055   VERIFY_IS_EQUAL(m1, m3);
00056   VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs1),
00057                    rhs13 = (s1*m1) * (s2*rhs1));
00058 
00059   m2 = m1.template triangularView<Upper>(); rhs12.setRandom(); rhs13 = rhs12;
00060   m3 = m2.template selfadjointView<Upper>();
00061   VERIFY_IS_EQUAL(m1, m3);
00062   VERIFY_IS_APPROX(rhs12 += (s1*m2).template selfadjointView<Upper>() * (s2*rhs1),
00063                    rhs13 += (s1*m1) * (s2*rhs1));
00064 
00065   m2 = m1.template triangularView<Lower>();
00066   VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs2.adjoint()),
00067                    rhs13 = (s1*m1) * (s2*rhs2.adjoint()));
00068 
00069   m2 = m1.template triangularView<Upper>();
00070   VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Upper>() * (s2*rhs2.adjoint()),
00071                    rhs13 = (s1*m1) * (s2*rhs2.adjoint()));
00072 
00073   m2 = m1.template triangularView<Upper>();
00074   VERIFY_IS_APPROX(rhs12 = (s1*m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs2.adjoint()),
00075                    rhs13 = (s1*m1.adjoint()) * (s2*rhs2.adjoint()));
00076 
00077   // test row major = <...>
00078   m2 = m1.template triangularView<Lower>(); rhs12.setRandom(); rhs13 = rhs12;
00079   VERIFY_IS_APPROX(rhs12 -= (s1*m2).template selfadjointView<Lower>() * (s2*rhs3),
00080                    rhs13 -= (s1*m1) * (s2 * rhs3));
00081 
00082   m2 = m1.template triangularView<Upper>();
00083   VERIFY_IS_APPROX(rhs12 = (s1*m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs3).conjugate(),
00084                    rhs13 = (s1*m1.adjoint()) * (s2*rhs3).conjugate());
00085 
00086 
00087   m2 = m1.template triangularView<Upper>(); rhs13 = rhs12;
00088   VERIFY_IS_APPROX(rhs12.noalias() += s1 * ((m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs3).conjugate()),
00089                    rhs13 += (s1*m1.adjoint()) * (s2*rhs3).conjugate());
00090 
00091   m2 = m1.template triangularView<Lower>();
00092   VERIFY_IS_APPROX(rhs22 = (rhs2) * (m2).template selfadjointView<Lower>(), rhs23 = (rhs2) * (m1));
00093   VERIFY_IS_APPROX(rhs22 = (s2*rhs2) * (s1*m2).template selfadjointView<Lower>(), rhs23 = (s2*rhs2) * (s1*m1));
00094 
00095 }
00096 
00097 void test_product_symm()
00098 {
00099   for(int i = 0; i < g_repeat ; i++)
00100   {
00101     CALL_SUBTEST_1(( symm<float,Dynamic,Dynamic>(internal::random<int>(1,320),internal::random<int>(1,320)) ));
00102     CALL_SUBTEST_2(( symm<double,Dynamic,Dynamic>(internal::random<int>(1,320),internal::random<int>(1,320)) ));
00103     CALL_SUBTEST_3(( symm<std::complex<float>,Dynamic,Dynamic>(internal::random<int>(1,200),internal::random<int>(1,200)) ));
00104     CALL_SUBTEST_4(( symm<std::complex<double>,Dynamic,Dynamic>(internal::random<int>(1,200),internal::random<int>(1,200)) ));
00105 
00106     CALL_SUBTEST_5(( symm<float,Dynamic,1>(internal::random<int>(1,320)) ));
00107     CALL_SUBTEST_6(( symm<double,Dynamic,1>(internal::random<int>(1,320)) ));
00108     CALL_SUBTEST_7(( symm<std::complex<float>,Dynamic,1>(internal::random<int>(1,320)) ));
00109     CALL_SUBTEST_8(( symm<std::complex<double>,Dynamic,1>(internal::random<int>(1,320)) ));
00110   }
00111 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:11