eigen2_cwiseop.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) 2008 Gael Guennebaud <g.gael@free.fr>
00005 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #include "main.h"
00027 #include <functional>
00028 #include <Eigen/Array>
00029 
00030 using namespace std;
00031 
00032 template<typename Scalar> struct AddIfNull {
00033     const Scalar operator() (const Scalar a, const Scalar b) const {return a<=1e-3 ? b : a;}
00034     enum { Cost = NumTraits<Scalar>::AddCost };
00035 };
00036 
00037 template<typename MatrixType> void cwiseops(const MatrixType& m)
00038 {
00039   typedef typename MatrixType::Scalar Scalar;
00040   typedef typename NumTraits<Scalar>::Real RealScalar;
00041   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00042 
00043   int rows = m.rows();
00044   int cols = m.cols();
00045 
00046   MatrixType m1 = MatrixType::Random(rows, cols),
00047              m2 = MatrixType::Random(rows, cols),
00048              m3(rows, cols),
00049              m4(rows, cols),
00050              mzero = MatrixType::Zero(rows, cols),
00051              mones = MatrixType::Ones(rows, cols),
00052              identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00053                               ::Identity(rows, rows),
00054              square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
00055   VectorType v1 = VectorType::Random(rows),
00056              v2 = VectorType::Random(rows),
00057              vzero = VectorType::Zero(rows),
00058              vones = VectorType::Ones(rows),
00059              v3(rows);
00060 
00061   int r = ei_random<int>(0, rows-1),
00062       c = ei_random<int>(0, cols-1);
00063   
00064   Scalar s1 = ei_random<Scalar>();
00065   
00066   // test Zero, Ones, Constant, and the set* variants
00067   m3 = MatrixType::Constant(rows, cols, s1);
00068   for (int j=0; j<cols; ++j)
00069     for (int i=0; i<rows; ++i)
00070     {
00071       VERIFY_IS_APPROX(mzero(i,j), Scalar(0));
00072       VERIFY_IS_APPROX(mones(i,j), Scalar(1));
00073       VERIFY_IS_APPROX(m3(i,j), s1);
00074     }
00075   VERIFY(mzero.isZero());
00076   VERIFY(mones.isOnes());
00077   VERIFY(m3.isConstant(s1));
00078   VERIFY(identity.isIdentity());
00079   VERIFY_IS_APPROX(m4.setConstant(s1), m3);
00080   VERIFY_IS_APPROX(m4.setConstant(rows,cols,s1), m3);
00081   VERIFY_IS_APPROX(m4.setZero(), mzero);
00082   VERIFY_IS_APPROX(m4.setZero(rows,cols), mzero);
00083   VERIFY_IS_APPROX(m4.setOnes(), mones);
00084   VERIFY_IS_APPROX(m4.setOnes(rows,cols), mones);
00085   m4.fill(s1);
00086   VERIFY_IS_APPROX(m4, m3);
00087   
00088   VERIFY_IS_APPROX(v3.setConstant(rows, s1), VectorType::Constant(rows,s1));
00089   VERIFY_IS_APPROX(v3.setZero(rows), vzero);
00090   VERIFY_IS_APPROX(v3.setOnes(rows), vones);
00091 
00092   m2 = m2.template binaryExpr<AddIfNull<Scalar> >(mones);
00093 
00094   VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().abs2());
00095   VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
00096   VERIFY_IS_APPROX(m1.cwise().pow(3), m1.cwise().cube());
00097 
00098   VERIFY_IS_APPROX(m1 + mones, m1.cwise()+Scalar(1));
00099   VERIFY_IS_APPROX(m1 - mones, m1.cwise()-Scalar(1));
00100   m3 = m1; m3.cwise() += 1;
00101   VERIFY_IS_APPROX(m1 + mones, m3);
00102   m3 = m1; m3.cwise() -= 1;
00103   VERIFY_IS_APPROX(m1 - mones, m3);
00104 
00105   VERIFY_IS_APPROX(m2, m2.cwise() * mones);
00106   VERIFY_IS_APPROX(m1.cwise() * m2,  m2.cwise() * m1);
00107   m3 = m1;
00108   m3.cwise() *= m2;
00109   VERIFY_IS_APPROX(m3, m1.cwise() * m2);
00110   
00111   VERIFY_IS_APPROX(mones,    m2.cwise()/m2);
00112   if(NumTraits<Scalar>::HasFloatingPoint)
00113   {
00114     VERIFY_IS_APPROX(m1.cwise() / m2,    m1.cwise() * (m2.cwise().inverse()));
00115     m3 = m1.cwise().abs().cwise().sqrt();
00116     VERIFY_IS_APPROX(m3.cwise().square(), m1.cwise().abs());
00117     VERIFY_IS_APPROX(m1.cwise().square().cwise().sqrt(), m1.cwise().abs());
00118     VERIFY_IS_APPROX(m1.cwise().abs().cwise().log().cwise().exp() , m1.cwise().abs());
00119 
00120     VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
00121     m3 = (m1.cwise().abs().cwise()<=RealScalar(0.01)).select(mones,m1);
00122     VERIFY_IS_APPROX(m3.cwise().pow(-1), m3.cwise().inverse());
00123     m3 = m1.cwise().abs();
00124     VERIFY_IS_APPROX(m3.cwise().pow(RealScalar(0.5)), m3.cwise().sqrt());
00125     
00126 //     VERIFY_IS_APPROX(m1.cwise().tan(), m1.cwise().sin().cwise() / m1.cwise().cos());
00127     VERIFY_IS_APPROX(mones, m1.cwise().sin().cwise().square() + m1.cwise().cos().cwise().square());
00128     m3 = m1;
00129     m3.cwise() /= m2;
00130     VERIFY_IS_APPROX(m3, m1.cwise() / m2);
00131   }
00132 
00133   // check min
00134   VERIFY_IS_APPROX( m1.cwise().min(m2), m2.cwise().min(m1) );
00135   VERIFY_IS_APPROX( m1.cwise().min(m1+mones), m1 );
00136   VERIFY_IS_APPROX( m1.cwise().min(m1-mones), m1-mones );
00137 
00138   // check max
00139   VERIFY_IS_APPROX( m1.cwise().max(m2), m2.cwise().max(m1) );
00140   VERIFY_IS_APPROX( m1.cwise().max(m1-mones), m1 );
00141   VERIFY_IS_APPROX( m1.cwise().max(m1+mones), m1+mones );
00142   
00143   VERIFY( (m1.cwise() == m1).all() );
00144   VERIFY( (m1.cwise() != m2).any() );
00145   VERIFY(!(m1.cwise() == (m1+mones)).any() );
00146   if (rows*cols>1)
00147   {
00148     m3 = m1;
00149     m3(r,c) += 1;
00150     VERIFY( (m1.cwise() == m3).any() );
00151     VERIFY( !(m1.cwise() == m3).all() );
00152   }
00153   VERIFY( (m1.cwise().min(m2).cwise() <= m2).all() );
00154   VERIFY( (m1.cwise().max(m2).cwise() >= m2).all() );
00155   VERIFY( (m1.cwise().min(m2).cwise() < (m1+mones)).all() );
00156   VERIFY( (m1.cwise().max(m2).cwise() > (m1-mones)).all() );
00157 
00158   VERIFY( (m1.cwise()<m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).all() );
00159   VERIFY( !(m1.cwise()<m1.unaryExpr(bind2nd(minus<Scalar>(), Scalar(1)))).all() );
00160   VERIFY( !(m1.cwise()>m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).any() );
00161 }
00162 
00163 void test_eigen2_cwiseop()
00164 {
00165   for(int i = 0; i < g_repeat ; i++) {
00166     CALL_SUBTEST_1( cwiseops(Matrix<float, 1, 1>()) );
00167     CALL_SUBTEST_2( cwiseops(Matrix4d()) );
00168     CALL_SUBTEST_3( cwiseops(MatrixXf(3, 3)) );
00169     CALL_SUBTEST_3( cwiseops(MatrixXf(22, 22)) );
00170     CALL_SUBTEST_4( cwiseops(MatrixXi(8, 12)) );
00171     CALL_SUBTEST_5( cwiseops(MatrixXd(20, 20)) );
00172   }
00173 }


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