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


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