eigen2_basicstuff.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 basicStuff(const MatrixType& m)
00028 {
00029   typedef typename MatrixType::Scalar Scalar;
00030   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00031 
00032   int rows = m.rows();
00033   int cols = m.cols();
00034 
00035   // this test relies a lot on Random.h, and there's not much more that we can do
00036   // to test it, hence I consider that we will have tested Random.h
00037   MatrixType m1 = MatrixType::Random(rows, cols),
00038              m2 = MatrixType::Random(rows, cols),
00039              m3(rows, cols),
00040              mzero = MatrixType::Zero(rows, cols),
00041              identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00042                               ::Identity(rows, rows),
00043              square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
00044   VectorType v1 = VectorType::Random(rows),
00045              v2 = VectorType::Random(rows),
00046              vzero = VectorType::Zero(rows);
00047 
00048   Scalar x = ei_random<Scalar>();
00049 
00050   int r = ei_random<int>(0, rows-1),
00051       c = ei_random<int>(0, cols-1);
00052 
00053   m1.coeffRef(r,c) = x;
00054   VERIFY_IS_APPROX(x, m1.coeff(r,c));
00055   m1(r,c) = x;
00056   VERIFY_IS_APPROX(x, m1(r,c));
00057   v1.coeffRef(r) = x;
00058   VERIFY_IS_APPROX(x, v1.coeff(r));
00059   v1(r) = x;
00060   VERIFY_IS_APPROX(x, v1(r));
00061   v1[r] = x;
00062   VERIFY_IS_APPROX(x, v1[r]);
00063 
00064   VERIFY_IS_APPROX(               v1,    v1);
00065   VERIFY_IS_NOT_APPROX(           v1,    2*v1);
00066   VERIFY_IS_MUCH_SMALLER_THAN(    vzero, v1);
00067   if(NumTraits<Scalar>::HasFloatingPoint)
00068     VERIFY_IS_MUCH_SMALLER_THAN(  vzero, v1.norm());
00069   VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1,    v1);
00070   VERIFY_IS_APPROX(               vzero, v1-v1);
00071   VERIFY_IS_APPROX(               m1,    m1);
00072   VERIFY_IS_NOT_APPROX(           m1,    2*m1);
00073   VERIFY_IS_MUCH_SMALLER_THAN(    mzero, m1);
00074   VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1,    m1);
00075   VERIFY_IS_APPROX(               mzero, m1-m1);
00076 
00077   // always test operator() on each read-only expression class,
00078   // in order to check const-qualifiers.
00079   // indeed, if an expression class (here Zero) is meant to be read-only,
00080   // hence has no _write() method, the corresponding MatrixBase method (here zero())
00081   // should return a const-qualified object so that it is the const-qualified
00082   // operator() that gets called, which in turn calls _read().
00083   VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows,cols)(r,c), static_cast<Scalar>(1));
00084 
00085   // now test copying a row-vector into a (column-)vector and conversely.
00086   square.col(r) = square.row(r).eval();
00087   Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows);
00088   Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> cv(rows);
00089   rv = square.row(r);
00090   cv = square.col(r);
00091   VERIFY_IS_APPROX(rv, cv.transpose());
00092 
00093   if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic)
00094   {
00095     VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1)));
00096   }
00097 
00098   VERIFY_IS_APPROX(m3 = m1,m1);
00099   MatrixType m4;
00100   VERIFY_IS_APPROX(m4 = m1,m1);
00101 
00102   // test swap
00103   m3 = m1;
00104   m1.swap(m2);
00105   VERIFY_IS_APPROX(m3, m2);
00106   if(rows*cols>=3)
00107   {
00108     VERIFY_IS_NOT_APPROX(m3, m1);
00109   }
00110 }
00111 
00112 void test_eigen2_basicstuff()
00113 {
00114   for(int i = 0; i < g_repeat; i++) {
00115     CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
00116     CALL_SUBTEST_2( basicStuff(Matrix4d()) );
00117     CALL_SUBTEST_3( basicStuff(MatrixXcf(3, 3)) );
00118     CALL_SUBTEST_4( basicStuff(MatrixXi(8, 12)) );
00119     CALL_SUBTEST_5( basicStuff(MatrixXcd(20, 20)) );
00120     CALL_SUBTEST_6( basicStuff(Matrix<float, 100, 100>()) );
00121     CALL_SUBTEST_7( basicStuff(Matrix<long double,Dynamic,Dynamic>(10,10)) );
00122   }
00123 }


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