product_selfadjoint.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 MatrixType> void product_selfadjoint(const MatrixType& m)
00028 {
00029   typedef typename MatrixType::Index Index;
00030   typedef typename MatrixType::Scalar Scalar;
00031   typedef typename NumTraits<Scalar>::Real RealScalar;
00032   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00033   typedef Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> RowVectorType;
00034 
00035   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, Dynamic, RowMajor> RhsMatrixType;
00036 
00037   Index rows = m.rows();
00038   Index cols = m.cols();
00039 
00040   MatrixType m1 = MatrixType::Random(rows, cols),
00041              m2 = MatrixType::Random(rows, cols),
00042              m3;
00043   VectorType v1 = VectorType::Random(rows),
00044              v2 = VectorType::Random(rows),
00045              v3(rows);
00046   RowVectorType r1 = RowVectorType::Random(rows),
00047                 r2 = RowVectorType::Random(rows);
00048   RhsMatrixType m4 = RhsMatrixType::Random(rows,10);
00049 
00050   Scalar s1 = internal::random<Scalar>(),
00051          s2 = internal::random<Scalar>(),
00052          s3 = internal::random<Scalar>();
00053 
00054   m1 = (m1.adjoint() + m1).eval();
00055 
00056   // rank2 update
00057   m2 = m1.template triangularView<Lower>();
00058   m2.template selfadjointView<Lower>().rankUpdate(v1,v2);
00059   VERIFY_IS_APPROX(m2, (m1 + v1 * v2.adjoint()+ v2 * v1.adjoint()).template triangularView<Lower>().toDenseMatrix());
00060 
00061   m2 = m1.template triangularView<Upper>();
00062   m2.template selfadjointView<Upper>().rankUpdate(-v1,s2*v2,s3);
00063   VERIFY_IS_APPROX(m2, (m1 + (s3*(-v1)*(s2*v2).adjoint()+internal::conj(s3)*(s2*v2)*(-v1).adjoint())).template triangularView<Upper>().toDenseMatrix());
00064 
00065   m2 = m1.template triangularView<Upper>();
00066   m2.template selfadjointView<Upper>().rankUpdate(-s2*r1.adjoint(),r2.adjoint()*s3,s1);
00067   VERIFY_IS_APPROX(m2, (m1 + s1*(-s2*r1.adjoint())*(r2.adjoint()*s3).adjoint() + internal::conj(s1)*(r2.adjoint()*s3) * (-s2*r1.adjoint()).adjoint()).template triangularView<Upper>().toDenseMatrix());
00068 
00069   if (rows>1)
00070   {
00071     m2 = m1.template triangularView<Lower>();
00072     m2.block(1,1,rows-1,cols-1).template selfadjointView<Lower>().rankUpdate(v1.tail(rows-1),v2.head(cols-1));
00073     m3 = m1;
00074     m3.block(1,1,rows-1,cols-1) += v1.tail(rows-1) * v2.head(cols-1).adjoint()+ v2.head(cols-1) * v1.tail(rows-1).adjoint();
00075     VERIFY_IS_APPROX(m2, m3.template triangularView<Lower>().toDenseMatrix());
00076   }
00077 }
00078 
00079 void test_product_selfadjoint()
00080 {
00081   int s;
00082   for(int i = 0; i < g_repeat ; i++) {
00083     CALL_SUBTEST_1( product_selfadjoint(Matrix<float, 1, 1>()) );
00084     CALL_SUBTEST_2( product_selfadjoint(Matrix<float, 2, 2>()) );
00085     CALL_SUBTEST_3( product_selfadjoint(Matrix3d()) );
00086     s = internal::random<int>(1,150);
00087     CALL_SUBTEST_4( product_selfadjoint(MatrixXcf(s, s)) );
00088     s = internal::random<int>(1,150);
00089     CALL_SUBTEST_5( product_selfadjoint(MatrixXcd(s,s)) );
00090     s = internal::random<int>(1,320);
00091     CALL_SUBTEST_6( product_selfadjoint(MatrixXd(s,s)) );
00092     s = internal::random<int>(1,320);
00093     CALL_SUBTEST_7( product_selfadjoint(Matrix<float,Dynamic,Dynamic,RowMajor>(s,s)) );
00094   }
00095 }


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