diagonalmatrices.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) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
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 using namespace std;
00027 template<typename MatrixType> void diagonalmatrices(const MatrixType& m)
00028 {
00029   typedef typename MatrixType::Index Index;
00030   typedef typename MatrixType::Scalar Scalar;
00031   typedef typename MatrixType::RealScalar RealScalar;
00032   enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
00033   typedef Matrix<Scalar, Rows, 1> VectorType;
00034   typedef Matrix<Scalar, 1, Cols> RowVectorType;
00035   typedef Matrix<Scalar, Rows, Rows> SquareMatrixType;
00036   typedef DiagonalMatrix<Scalar, Rows> LeftDiagonalMatrix;
00037   typedef DiagonalMatrix<Scalar, Cols> RightDiagonalMatrix;
00038   typedef Matrix<Scalar, Rows==Dynamic?Dynamic:2*Rows, Cols==Dynamic?Dynamic:2*Cols> BigMatrix;
00039   Index rows = m.rows();
00040   Index cols = m.cols();
00041 
00042   MatrixType m1 = MatrixType::Random(rows, cols),
00043              m2 = MatrixType::Random(rows, cols);
00044   VectorType v1 = VectorType::Random(rows),
00045              v2 = VectorType::Random(rows);
00046   RowVectorType rv1 = RowVectorType::Random(cols),
00047              rv2 = RowVectorType::Random(cols);
00048   LeftDiagonalMatrix ldm1(v1), ldm2(v2);
00049   RightDiagonalMatrix rdm1(rv1), rdm2(rv2);
00050 
00051   SquareMatrixType sq_m1 (v1.asDiagonal());
00052   VERIFY_IS_APPROX(sq_m1, v1.asDiagonal().toDenseMatrix());
00053   sq_m1 = v1.asDiagonal();
00054   VERIFY_IS_APPROX(sq_m1, v1.asDiagonal().toDenseMatrix());
00055   SquareMatrixType sq_m2 = v1.asDiagonal();
00056   VERIFY_IS_APPROX(sq_m1, sq_m2);
00057   
00058   ldm1 = v1.asDiagonal();
00059   LeftDiagonalMatrix ldm3(v1);
00060   VERIFY_IS_APPROX(ldm1.diagonal(), ldm3.diagonal());
00061   LeftDiagonalMatrix ldm4 = v1.asDiagonal();
00062   VERIFY_IS_APPROX(ldm1.diagonal(), ldm4.diagonal());
00063   
00064   sq_m1.block(0,0,rows,rows) = ldm1;
00065   VERIFY_IS_APPROX(sq_m1, ldm1.toDenseMatrix());
00066   sq_m1.transpose() = ldm1;
00067   VERIFY_IS_APPROX(sq_m1, ldm1.toDenseMatrix());
00068   
00069   Index i = internal::random<Index>(0, rows-1);
00070   Index j = internal::random<Index>(0, cols-1);
00071   
00072   VERIFY_IS_APPROX( ((ldm1 * m1)(i,j))  , ldm1.diagonal()(i) * m1(i,j) );
00073   VERIFY_IS_APPROX( ((ldm1 * (m1+m2))(i,j))  , ldm1.diagonal()(i) * (m1+m2)(i,j) );
00074   VERIFY_IS_APPROX( ((m1 * rdm1)(i,j))  , rdm1.diagonal()(j) * m1(i,j) );
00075   VERIFY_IS_APPROX( ((v1.asDiagonal() * m1)(i,j))  , v1(i) * m1(i,j) );
00076   VERIFY_IS_APPROX( ((m1 * rv1.asDiagonal())(i,j))  , rv1(j) * m1(i,j) );
00077   VERIFY_IS_APPROX( (((v1+v2).asDiagonal() * m1)(i,j))  , (v1+v2)(i) * m1(i,j) );
00078   VERIFY_IS_APPROX( (((v1+v2).asDiagonal() * (m1+m2))(i,j))  , (v1+v2)(i) * (m1+m2)(i,j) );
00079   VERIFY_IS_APPROX( ((m1 * (rv1+rv2).asDiagonal())(i,j))  , (rv1+rv2)(j) * m1(i,j) );
00080   VERIFY_IS_APPROX( (((m1+m2) * (rv1+rv2).asDiagonal())(i,j))  , (rv1+rv2)(j) * (m1+m2)(i,j) );
00081 
00082   BigMatrix big;
00083   big.setZero(2*rows, 2*cols);
00084   
00085   big.block(i,j,rows,cols) = m1;
00086   big.block(i,j,rows,cols) = v1.asDiagonal() * big.block(i,j,rows,cols);
00087   
00088   VERIFY_IS_APPROX((big.block(i,j,rows,cols)) , v1.asDiagonal() * m1 );
00089   
00090   big.block(i,j,rows,cols) = m1;
00091   big.block(i,j,rows,cols) = big.block(i,j,rows,cols) * rv1.asDiagonal();
00092   VERIFY_IS_APPROX((big.block(i,j,rows,cols)) , m1 * rv1.asDiagonal() );
00093   
00094 }
00095 
00096 void test_diagonalmatrices()
00097 {
00098   for(int i = 0; i < g_repeat; i++) {
00099     CALL_SUBTEST_1( diagonalmatrices(Matrix<float, 1, 1>()) );
00100     CALL_SUBTEST_2( diagonalmatrices(Matrix3f()) );
00101     CALL_SUBTEST_3( diagonalmatrices(Matrix<double,3,3,RowMajor>()) );
00102     CALL_SUBTEST_4( diagonalmatrices(Matrix4d()) );
00103     CALL_SUBTEST_5( diagonalmatrices(Matrix<float,4,4,RowMajor>()) );
00104     CALL_SUBTEST_6( diagonalmatrices(MatrixXcf(3, 5)) );
00105     CALL_SUBTEST_7( diagonalmatrices(MatrixXi(10, 8)) );
00106     CALL_SUBTEST_8( diagonalmatrices(Matrix<double,Dynamic,Dynamic,RowMajor>(20, 20)) );
00107     CALL_SUBTEST_9( diagonalmatrices(MatrixXf(21, 24)) );
00108   }
00109 }


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