eigen2_linearstructure.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 template<typename MatrixType> void linearStructure(const MatrixType& m)
00028 {
00029   /* this test covers the following files:
00030      Sum.h Difference.h Opposite.h ScalarMultiple.h
00031   */
00032 
00033   typedef typename MatrixType::Scalar Scalar;
00034   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00035 
00036   int rows = m.rows();
00037   int cols = m.cols();
00038 
00039   // this test relies a lot on Random.h, and there's not much more that we can do
00040   // to test it, hence I consider that we will have tested Random.h
00041   MatrixType m1 = MatrixType::Random(rows, cols),
00042              m2 = MatrixType::Random(rows, cols),
00043              m3(rows, cols),
00044              mzero = MatrixType::Zero(rows, cols);
00045 
00046   Scalar s1 = ei_random<Scalar>();
00047   while (ei_abs(s1)<1e-3) s1 = ei_random<Scalar>();
00048 
00049   int r = ei_random<int>(0, rows-1),
00050       c = ei_random<int>(0, cols-1);
00051 
00052   VERIFY_IS_APPROX(-(-m1),                  m1);
00053   VERIFY_IS_APPROX(m1+m1,                   2*m1);
00054   VERIFY_IS_APPROX(m1+m2-m1,                m2);
00055   VERIFY_IS_APPROX(-m2+m1+m2,               m1);
00056   VERIFY_IS_APPROX(m1*s1,                   s1*m1);
00057   VERIFY_IS_APPROX((m1+m2)*s1,              s1*m1+s1*m2);
00058   VERIFY_IS_APPROX((-m1+m2)*s1,             -s1*m1+s1*m2);
00059   m3 = m2; m3 += m1;
00060   VERIFY_IS_APPROX(m3,                      m1+m2);
00061   m3 = m2; m3 -= m1;
00062   VERIFY_IS_APPROX(m3,                      m2-m1);
00063   m3 = m2; m3 *= s1;
00064   VERIFY_IS_APPROX(m3,                      s1*m2);
00065   if(NumTraits<Scalar>::HasFloatingPoint)
00066   {
00067     m3 = m2; m3 /= s1;
00068     VERIFY_IS_APPROX(m3,                    m2/s1);
00069   }
00070 
00071   // again, test operator() to check const-qualification
00072   VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c)));
00073   VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c)));
00074   VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
00075   VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c)));
00076   VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1);
00077   if(NumTraits<Scalar>::HasFloatingPoint)
00078     VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1);
00079 
00080   // use .block to disable vectorization and compare to the vectorized version
00081   VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1);
00082   VERIFY_IS_APPROX(m1.cwise() * m1.block(0,0,rows,cols), m1.cwise() * m1);
00083   VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1);
00084   VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1);
00085 }
00086 
00087 void test_eigen2_linearstructure()
00088 {
00089   for(int i = 0; i < g_repeat; i++) {
00090     CALL_SUBTEST_1( linearStructure(Matrix<float, 1, 1>()) );
00091     CALL_SUBTEST_2( linearStructure(Matrix2f()) );
00092     CALL_SUBTEST_3( linearStructure(Vector3d()) );
00093     CALL_SUBTEST_4( linearStructure(Matrix4d()) );
00094     CALL_SUBTEST_5( linearStructure(MatrixXcf(3, 3)) );
00095     CALL_SUBTEST_6( linearStructure(MatrixXf(8, 12)) );
00096     CALL_SUBTEST_7( linearStructure(MatrixXi(8, 12)) );
00097     CALL_SUBTEST_8( linearStructure(MatrixXcd(20, 20)) );
00098   }
00099 }


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:38