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.
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 #define EIGEN_NO_STATIC_ASSERT
00026 
00027 #include "main.h"
00028 
00029 template<typename MatrixType> void basicStuff(const MatrixType& m)
00030 {
00031   typedef typename MatrixType::Index Index;
00032   typedef typename MatrixType::Scalar Scalar;
00033   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00034   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
00035 
00036   Index rows = m.rows();
00037   Index 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              identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00046                               ::Identity(rows, rows),
00047              square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
00048   VectorType v1 = VectorType::Random(rows),
00049              v2 = VectorType::Random(rows),
00050              vzero = VectorType::Zero(rows);
00051   SquareMatrixType sm1 = SquareMatrixType::Random(rows,rows), sm2(rows,rows);
00052 
00053   Scalar x = 0;
00054   while(x == Scalar(0)) x = internal::random<Scalar>();
00055 
00056   Index r = internal::random<Index>(0, rows-1),
00057         c = internal::random<Index>(0, cols-1);
00058 
00059   m1.coeffRef(r,c) = x;
00060   VERIFY_IS_APPROX(x, m1.coeff(r,c));
00061   m1(r,c) = x;
00062   VERIFY_IS_APPROX(x, m1(r,c));
00063   v1.coeffRef(r) = x;
00064   VERIFY_IS_APPROX(x, v1.coeff(r));
00065   v1(r) = x;
00066   VERIFY_IS_APPROX(x, v1(r));
00067   v1[r] = x;
00068   VERIFY_IS_APPROX(x, v1[r]);
00069 
00070   VERIFY_IS_APPROX(               v1,    v1);
00071   VERIFY_IS_NOT_APPROX(           v1,    2*v1);
00072   VERIFY_IS_MUCH_SMALLER_THAN(    vzero, v1);
00073   if(!NumTraits<Scalar>::IsInteger)
00074     VERIFY_IS_MUCH_SMALLER_THAN(  vzero, v1.norm());
00075   VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1,    v1);
00076   VERIFY_IS_APPROX(               vzero, v1-v1);
00077   VERIFY_IS_APPROX(               m1,    m1);
00078   VERIFY_IS_NOT_APPROX(           m1,    2*m1);
00079   VERIFY_IS_MUCH_SMALLER_THAN(    mzero, m1);
00080   VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1,    m1);
00081   VERIFY_IS_APPROX(               mzero, m1-m1);
00082 
00083   // always test operator() on each read-only expression class,
00084   // in order to check const-qualifiers.
00085   // indeed, if an expression class (here Zero) is meant to be read-only,
00086   // hence has no _write() method, the corresponding MatrixBase method (here zero())
00087   // should return a const-qualified object so that it is the const-qualified
00088   // operator() that gets called, which in turn calls _read().
00089   VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows,cols)(r,c), static_cast<Scalar>(1));
00090 
00091   // now test copying a row-vector into a (column-)vector and conversely.
00092   square.col(r) = square.row(r).eval();
00093   Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows);
00094   Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> cv(rows);
00095   rv = square.row(r);
00096   cv = square.col(r);
00097   
00098   VERIFY_IS_APPROX(rv, cv.transpose());
00099 
00100   if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic)
00101   {
00102     VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1)));
00103   }
00104 
00105   if(cols!=1 && rows!=1)
00106   {
00107     VERIFY_RAISES_ASSERT(m1[0]);
00108     VERIFY_RAISES_ASSERT((m1+m1)[0]);
00109   }
00110 
00111   VERIFY_IS_APPROX(m3 = m1,m1);
00112   MatrixType m4;
00113   VERIFY_IS_APPROX(m4 = m1,m1);
00114 
00115   m3.real() = m1.real();
00116   VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
00117   VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
00118 
00119   // check == / != operators
00120   VERIFY(m1==m1);
00121   VERIFY(m1!=m2);
00122   VERIFY(!(m1==m2));
00123   VERIFY(!(m1!=m1));
00124   m1 = m2;
00125   VERIFY(m1==m2);
00126   VERIFY(!(m1!=m2));
00127   
00128   // check automatic transposition
00129   sm2.setZero();
00130   for(typename MatrixType::Index i=0;i<rows;++i)
00131     sm2.col(i) = sm1.row(i);
00132   VERIFY_IS_APPROX(sm2,sm1.transpose());
00133   
00134   sm2.setZero();
00135   for(typename MatrixType::Index i=0;i<rows;++i)
00136     sm2.col(i).noalias() = sm1.row(i);
00137   VERIFY_IS_APPROX(sm2,sm1.transpose());
00138   
00139   sm2.setZero();
00140   for(typename MatrixType::Index i=0;i<rows;++i)
00141     sm2.col(i).noalias() += sm1.row(i);
00142   VERIFY_IS_APPROX(sm2,sm1.transpose());
00143   
00144   sm2.setZero();
00145   for(typename MatrixType::Index i=0;i<rows;++i)
00146     sm2.col(i).noalias() -= sm1.row(i);
00147   VERIFY_IS_APPROX(sm2,-sm1.transpose());
00148 }
00149 
00150 template<typename MatrixType> void basicStuffComplex(const MatrixType& m)
00151 {
00152   typedef typename MatrixType::Index Index;
00153   typedef typename MatrixType::Scalar Scalar;
00154   typedef typename NumTraits<Scalar>::Real RealScalar;
00155   typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> RealMatrixType;
00156 
00157   Index rows = m.rows();
00158   Index cols = m.cols();
00159 
00160   Scalar s1 = internal::random<Scalar>(),
00161          s2 = internal::random<Scalar>();
00162 
00163   VERIFY(internal::real(s1)==internal::real_ref(s1));
00164   VERIFY(internal::imag(s1)==internal::imag_ref(s1));
00165   internal::real_ref(s1) = internal::real(s2);
00166   internal::imag_ref(s1) = internal::imag(s2);
00167   VERIFY(internal::isApprox(s1, s2, NumTraits<RealScalar>::epsilon()));
00168   // extended precision in Intel FPUs means that s1 == s2 in the line above is not guaranteed.
00169 
00170   RealMatrixType rm1 = RealMatrixType::Random(rows,cols),
00171                  rm2 = RealMatrixType::Random(rows,cols);
00172   MatrixType cm(rows,cols);
00173   cm.real() = rm1;
00174   cm.imag() = rm2;
00175   VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
00176   VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
00177   rm1.setZero();
00178   rm2.setZero();
00179   rm1 = cm.real();
00180   rm2 = cm.imag();
00181   VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
00182   VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
00183   cm.real().setZero();
00184   VERIFY(static_cast<const MatrixType&>(cm).real().isZero());
00185   VERIFY(!static_cast<const MatrixType&>(cm).imag().isZero());
00186 }
00187 
00188 #ifdef EIGEN_TEST_PART_2
00189 void casting()
00190 {
00191   Matrix4f m = Matrix4f::Random(), m2;
00192   Matrix4d n = m.cast<double>();
00193   VERIFY(m.isApprox(n.cast<float>()));
00194   m2 = m.cast<float>(); // check the specialization when NewType == Type
00195   VERIFY(m.isApprox(m2));
00196 }
00197 #endif
00198 
00199 template <typename Scalar>
00200 void fixedSizeMatrixConstruction()
00201 {
00202   const Scalar raw[3] = {1,2,3};
00203   Matrix<Scalar,3,1> m(raw);
00204   Array<Scalar,3,1> a(raw);
00205   VERIFY(m(0) == 1);
00206   VERIFY(m(1) == 2);
00207   VERIFY(m(2) == 3);
00208   VERIFY(a(0) == 1);
00209   VERIFY(a(1) == 2);
00210   VERIFY(a(2) == 3);  
00211 }
00212 
00213 void test_basicstuff()
00214 {
00215   for(int i = 0; i < g_repeat; i++) {
00216     CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
00217     CALL_SUBTEST_2( basicStuff(Matrix4d()) );
00218     CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,100), internal::random<int>(1,100))) );
00219     CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,100), internal::random<int>(1,100))) );
00220     CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,100), internal::random<int>(1,100))) );
00221     CALL_SUBTEST_6( basicStuff(Matrix<float, 100, 100>()) );
00222     CALL_SUBTEST_7( basicStuff(Matrix<long double,Dynamic,Dynamic>(internal::random<int>(1,100),internal::random<int>(1,100))) );
00223 
00224     CALL_SUBTEST_3( basicStuffComplex(MatrixXcf(internal::random<int>(1,100), internal::random<int>(1,100))) );
00225     CALL_SUBTEST_5( basicStuffComplex(MatrixXcd(internal::random<int>(1,100), internal::random<int>(1,100))) );
00226   }
00227 
00228   CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
00229   CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
00230   CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
00231 
00232   CALL_SUBTEST_2(casting());
00233 }


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