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.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.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 #define EIGEN2_SUPPORT
00027 #define EIGEN_NO_STATIC_ASSERT
00028 #include "main.h"
00029 #include <functional>
00030 
00031 using namespace std;
00032 
00033 template<typename Scalar> struct AddIfNull {
00034     const Scalar operator() (const Scalar a, const Scalar b) const {return a<=1e-3 ? b : a;}
00035     enum { Cost = NumTraits<Scalar>::AddCost };
00036 };
00037 
00038 template<typename MatrixType> void cwiseops(const MatrixType& m)
00039 {
00040   typedef typename MatrixType::Index Index;
00041   typedef typename MatrixType::Scalar Scalar;
00042   typedef typename NumTraits<Scalar>::Real RealScalar;
00043   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00044 
00045   Index rows = m.rows();
00046   Index cols = m.cols();
00047 
00048   MatrixType m1 = MatrixType::Random(rows, cols),
00049              m2 = MatrixType::Random(rows, cols),
00050              m3(rows, cols),
00051              m4(rows, cols),
00052              mzero = MatrixType::Zero(rows, cols),
00053              mones = MatrixType::Ones(rows, cols),
00054              identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00055                               ::Identity(rows, rows),
00056              square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
00057   VectorType v1 = VectorType::Random(rows),
00058              v2 = VectorType::Random(rows),
00059              vzero = VectorType::Zero(rows),
00060              vones = VectorType::Ones(rows),
00061              v3(rows);
00062 
00063   Index r = internal::random<Index>(0, rows-1),
00064         c = internal::random<Index>(0, cols-1);
00065 
00066   Scalar s1 = internal::random<Scalar>();
00067 
00068   // test Zero, Ones, Constant, and the set* variants
00069   m3 = MatrixType::Constant(rows, cols, s1);
00070   for (int j=0; j<cols; ++j)
00071     for (int i=0; i<rows; ++i)
00072     {
00073       VERIFY_IS_APPROX(mzero(i,j), Scalar(0));
00074       VERIFY_IS_APPROX(mones(i,j), Scalar(1));
00075       VERIFY_IS_APPROX(m3(i,j), s1);
00076     }
00077   VERIFY(mzero.isZero());
00078   VERIFY(mones.isOnes());
00079   VERIFY(m3.isConstant(s1));
00080   VERIFY(identity.isIdentity());
00081   VERIFY_IS_APPROX(m4.setConstant(s1), m3);
00082   VERIFY_IS_APPROX(m4.setConstant(rows,cols,s1), m3);
00083   VERIFY_IS_APPROX(m4.setZero(), mzero);
00084   VERIFY_IS_APPROX(m4.setZero(rows,cols), mzero);
00085   VERIFY_IS_APPROX(m4.setOnes(), mones);
00086   VERIFY_IS_APPROX(m4.setOnes(rows,cols), mones);
00087   m4.fill(s1);
00088   VERIFY_IS_APPROX(m4, m3);
00089 
00090   VERIFY_IS_APPROX(v3.setConstant(rows, s1), VectorType::Constant(rows,s1));
00091   VERIFY_IS_APPROX(v3.setZero(rows), vzero);
00092   VERIFY_IS_APPROX(v3.setOnes(rows), vones);
00093 
00094   m2 = m2.template binaryExpr<AddIfNull<Scalar> >(mones);
00095 
00096   VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().abs2());
00097   VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
00098   VERIFY_IS_APPROX(m1.cwise().pow(3), m1.cwise().cube());
00099 
00100   VERIFY_IS_APPROX(m1 + mones, m1.cwise()+Scalar(1));
00101   VERIFY_IS_APPROX(m1 - mones, m1.cwise()-Scalar(1));
00102   m3 = m1; m3.cwise() += 1;
00103   VERIFY_IS_APPROX(m1 + mones, m3);
00104   m3 = m1; m3.cwise() -= 1;
00105   VERIFY_IS_APPROX(m1 - mones, m3);
00106 
00107   VERIFY_IS_APPROX(m2, m2.cwise() * mones);
00108   VERIFY_IS_APPROX(m1.cwise() * m2,  m2.cwise() * m1);
00109   m3 = m1;
00110   m3.cwise() *= m2;
00111   VERIFY_IS_APPROX(m3, m1.cwise() * m2);
00112 
00113   VERIFY_IS_APPROX(mones,    m2.cwise()/m2);
00114   if(!NumTraits<Scalar>::IsInteger)
00115   {
00116     VERIFY_IS_APPROX(m1.cwise() / m2,    m1.cwise() * (m2.cwise().inverse()));
00117     m3 = m1.cwise().abs().cwise().sqrt();
00118     VERIFY_IS_APPROX(m3.cwise().square(), m1.cwise().abs());
00119     VERIFY_IS_APPROX(m1.cwise().square().cwise().sqrt(), m1.cwise().abs());
00120     VERIFY_IS_APPROX(m1.cwise().abs().cwise().log().cwise().exp() , m1.cwise().abs());
00121 
00122     VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
00123     m3 = (m1.cwise().abs().cwise()<=RealScalar(0.01)).select(mones,m1);
00124     VERIFY_IS_APPROX(m3.cwise().pow(-1), m3.cwise().inverse());
00125     m3 = m1.cwise().abs();
00126     VERIFY_IS_APPROX(m3.cwise().pow(RealScalar(0.5)), m3.cwise().sqrt());
00127 
00128 //     VERIFY_IS_APPROX(m1.cwise().tan(), m1.cwise().sin().cwise() / m1.cwise().cos());
00129     VERIFY_IS_APPROX(mones, m1.cwise().sin().cwise().square() + m1.cwise().cos().cwise().square());
00130     m3 = m1;
00131     m3.cwise() /= m2;
00132     VERIFY_IS_APPROX(m3, m1.cwise() / m2);
00133   }
00134 
00135   // check min
00136   VERIFY_IS_APPROX( m1.cwise().min(m2), m2.cwise().min(m1) );
00137   VERIFY_IS_APPROX( m1.cwise().min(m1+mones), m1 );
00138   VERIFY_IS_APPROX( m1.cwise().min(m1-mones), m1-mones );
00139 
00140   // check max
00141   VERIFY_IS_APPROX( m1.cwise().max(m2), m2.cwise().max(m1) );
00142   VERIFY_IS_APPROX( m1.cwise().max(m1-mones), m1 );
00143   VERIFY_IS_APPROX( m1.cwise().max(m1+mones), m1+mones );
00144 
00145   VERIFY( (m1.cwise() == m1).all() );
00146   VERIFY( (m1.cwise() != m2).any() );
00147   VERIFY(!(m1.cwise() == (m1+mones)).any() );
00148   if (rows*cols>1)
00149   {
00150     m3 = m1;
00151     m3(r,c) += 1;
00152     VERIFY( (m1.cwise() == m3).any() );
00153     VERIFY( !(m1.cwise() == m3).all() );
00154   }
00155   VERIFY( (m1.cwise().min(m2).cwise() <= m2).all() );
00156   VERIFY( (m1.cwise().max(m2).cwise() >= m2).all() );
00157   VERIFY( (m1.cwise().min(m2).cwise() < (m1+mones)).all() );
00158   VERIFY( (m1.cwise().max(m2).cwise() > (m1-mones)).all() );
00159 
00160   VERIFY( (m1.cwise()<m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).all() );
00161   VERIFY( !(m1.cwise()<m1.unaryExpr(bind2nd(minus<Scalar>(), Scalar(1)))).all() );
00162   VERIFY( !(m1.cwise()>m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).any() );
00163 }
00164 
00165 void test_cwiseop()
00166 {
00167   for(int i = 0; i < g_repeat ; i++) {
00168     CALL_SUBTEST_1( cwiseops(Matrix<float, 1, 1>()) );
00169     CALL_SUBTEST_2( cwiseops(Matrix4d()) );
00170     CALL_SUBTEST_3( cwiseops(MatrixXf(3, 3)) );
00171     CALL_SUBTEST_4( cwiseops(MatrixXf(22, 22)) );
00172     CALL_SUBTEST_5( cwiseops(MatrixXi(8, 12)) );
00173     CALL_SUBTEST_6( cwiseops(MatrixXd(20, 20)) );
00174   }
00175 }


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