eigen2_submatrices.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra. Eigen itself is part of the KDE project.
00003 //
00004 // Copyright (C) 2006-2008 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 
00027 // check minor separately in order to avoid the possible creation of a zero-sized
00028 // array. Comes from a compilation error with gcc-3.4 or gcc-4 with -ansi -pedantic.
00029 // Another solution would be to declare the array like this: T m_data[Size==0?1:Size]; in ei_matrix_storage
00030 // but this is probably not bad to raise such an error at compile time...
00031 template<typename Scalar, int _Rows, int _Cols> struct CheckMinor
00032 {
00033     typedef Matrix<Scalar, _Rows, _Cols> MatrixType;
00034     CheckMinor(MatrixType& m1, int r1, int c1)
00035     {
00036         int rows = m1.rows();
00037         int cols = m1.cols();
00038 
00039         Matrix<Scalar, Dynamic, Dynamic> mi = m1.minor(0,0).eval();
00040         VERIFY_IS_APPROX(mi, m1.block(1,1,rows-1,cols-1));
00041         mi = m1.minor(r1,c1);
00042         VERIFY_IS_APPROX(mi.transpose(), m1.transpose().minor(c1,r1));
00043         //check operator(), both constant and non-constant, on minor()
00044         m1.minor(r1,c1)(0,0) = m1.minor(0,0)(0,0);
00045     }
00046 };
00047 
00048 template<typename Scalar> struct CheckMinor<Scalar,1,1>
00049 {
00050     typedef Matrix<Scalar, 1, 1> MatrixType;
00051     CheckMinor(MatrixType&, int, int) {}
00052 };
00053 
00054 template<typename MatrixType> void submatrices(const MatrixType& m)
00055 {
00056   /* this test covers the following files:
00057      Row.h Column.h Block.h Minor.h DiagonalCoeffs.h
00058   */
00059   typedef typename MatrixType::Scalar Scalar;
00060   typedef typename MatrixType::RealScalar RealScalar;
00061   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00062   typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
00063   int rows = m.rows();
00064   int cols = m.cols();
00065 
00066   MatrixType m1 = MatrixType::Random(rows, cols),
00067              m2 = MatrixType::Random(rows, cols),
00068              m3(rows, cols),
00069              mzero = MatrixType::Zero(rows, cols),
00070              ones = MatrixType::Ones(rows, cols),
00071              identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00072                               ::Identity(rows, rows),
00073              square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00074                               ::Random(rows, rows);
00075   VectorType v1 = VectorType::Random(rows),
00076              v2 = VectorType::Random(rows),
00077              v3 = VectorType::Random(rows),
00078              vzero = VectorType::Zero(rows);
00079 
00080   Scalar s1 = ei_random<Scalar>();
00081 
00082   int r1 = ei_random<int>(0,rows-1);
00083   int r2 = ei_random<int>(r1,rows-1);
00084   int c1 = ei_random<int>(0,cols-1);
00085   int c2 = ei_random<int>(c1,cols-1);
00086 
00087   //check row() and col()
00088   VERIFY_IS_APPROX(m1.col(c1).transpose(), m1.transpose().row(c1));
00089   VERIFY_IS_APPROX(square.row(r1).eigen2_dot(m1.col(c1)), (square.lazy() * m1.conjugate())(r1,c1));
00090   //check operator(), both constant and non-constant, on row() and col()
00091   m1.row(r1) += s1 * m1.row(r2);
00092   m1.col(c1) += s1 * m1.col(c2);
00093 
00094   //check block()
00095   Matrix<Scalar,Dynamic,Dynamic> b1(1,1); b1(0,0) = m1(r1,c1);
00096   RowVectorType br1(m1.block(r1,0,1,cols));
00097   VectorType bc1(m1.block(0,c1,rows,1));
00098   VERIFY_IS_APPROX(b1, m1.block(r1,c1,1,1));
00099   VERIFY_IS_APPROX(m1.row(r1), br1);
00100   VERIFY_IS_APPROX(m1.col(c1), bc1);
00101   //check operator(), both constant and non-constant, on block()
00102   m1.block(r1,c1,r2-r1+1,c2-c1+1) = s1 * m2.block(0, 0, r2-r1+1,c2-c1+1);
00103   m1.block(r1,c1,r2-r1+1,c2-c1+1)(r2-r1,c2-c1) = m2.block(0, 0, r2-r1+1,c2-c1+1)(0,0);
00104 
00105   //check minor()
00106   CheckMinor<Scalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> checkminor(m1,r1,c1);
00107 
00108   //check diagonal()
00109   VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal());
00110   m2.diagonal() = 2 * m1.diagonal();
00111   m2.diagonal()[0] *= 3;
00112   VERIFY_IS_APPROX(m2.diagonal()[0], static_cast<Scalar>(6) * m1.diagonal()[0]);
00113 
00114   enum {
00115     BlockRows = EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::RowsAtCompileTime,2),
00116     BlockCols = EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::ColsAtCompileTime,5)
00117   };
00118   if (rows>=5 && cols>=8)
00119   {
00120     // test fixed block() as lvalue
00121     m1.template block<BlockRows,BlockCols>(1,1) *= s1;
00122     // test operator() on fixed block() both as constant and non-constant
00123     m1.template block<BlockRows,BlockCols>(1,1)(0, 3) = m1.template block<2,5>(1,1)(1,2);
00124     // check that fixed block() and block() agree
00125     Matrix<Scalar,Dynamic,Dynamic> b = m1.template block<BlockRows,BlockCols>(3,3);
00126     VERIFY_IS_APPROX(b, m1.block(3,3,BlockRows,BlockCols));
00127   }
00128 
00129   if (rows>2)
00130   {
00131     // test sub vectors
00132     VERIFY_IS_APPROX(v1.template start<2>(), v1.block(0,0,2,1));
00133     VERIFY_IS_APPROX(v1.template start<2>(), v1.start(2));
00134     VERIFY_IS_APPROX(v1.template start<2>(), v1.segment(0,2));
00135     VERIFY_IS_APPROX(v1.template start<2>(), v1.template segment<2>(0));
00136     int i = rows-2;
00137     VERIFY_IS_APPROX(v1.template end<2>(), v1.block(i,0,2,1));
00138     VERIFY_IS_APPROX(v1.template end<2>(), v1.end(2));
00139     VERIFY_IS_APPROX(v1.template end<2>(), v1.segment(i,2));
00140     VERIFY_IS_APPROX(v1.template end<2>(), v1.template segment<2>(i));
00141     i = ei_random(0,rows-2);
00142     VERIFY_IS_APPROX(v1.segment(i,2), v1.template segment<2>(i));
00143   }
00144 
00145   // stress some basic stuffs with block matrices
00146   VERIFY(ei_real(ones.col(c1).sum()) == RealScalar(rows));
00147   VERIFY(ei_real(ones.row(r1).sum()) == RealScalar(cols));
00148 
00149   VERIFY(ei_real(ones.col(c1).eigen2_dot(ones.col(c2))) == RealScalar(rows));
00150   VERIFY(ei_real(ones.row(r1).eigen2_dot(ones.row(r2))) == RealScalar(cols));
00151 }
00152 
00153 void test_eigen2_submatrices()
00154 {
00155   for(int i = 0; i < g_repeat; i++) {
00156     CALL_SUBTEST_1( submatrices(Matrix<float, 1, 1>()) );
00157     CALL_SUBTEST_2( submatrices(Matrix4d()) );
00158     CALL_SUBTEST_3( submatrices(MatrixXcf(3, 3)) );
00159     CALL_SUBTEST_4( submatrices(MatrixXi(8, 12)) );
00160     CALL_SUBTEST_5( submatrices(MatrixXcd(20, 20)) );
00161     CALL_SUBTEST_6( submatrices(MatrixXf(20, 20)) );
00162   }
00163 }


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