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


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