Home | History | Annotate | Download | only in eigen2
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra. Eigen itself is part of the KDE project.
      3 //
      4 // Copyright (C) 2008 Gael Guennebaud <g.gael (at) free.fr>
      5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1 (at) gmail.com>
      6 //
      7 // This Source Code Form is subject to the terms of the Mozilla
      8 // Public License v. 2.0. If a copy of the MPL was not distributed
      9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     10 
     11 #include "main.h"
     12 #include <functional>
     13 #include <Eigen/Array>
     14 
     15 using namespace std;
     16 
     17 template<typename Scalar> struct AddIfNull {
     18     const Scalar operator() (const Scalar a, const Scalar b) const {return a<=1e-3 ? b : a;}
     19     enum { Cost = NumTraits<Scalar>::AddCost };
     20 };
     21 
     22 template<typename MatrixType> void cwiseops(const MatrixType& m)
     23 {
     24   typedef typename MatrixType::Scalar Scalar;
     25   typedef typename NumTraits<Scalar>::Real RealScalar;
     26   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
     27 
     28   int rows = m.rows();
     29   int cols = m.cols();
     30 
     31   MatrixType m1 = MatrixType::Random(rows, cols),
     32              m2 = MatrixType::Random(rows, cols),
     33              m3(rows, cols),
     34              m4(rows, cols),
     35              mzero = MatrixType::Zero(rows, cols),
     36              mones = MatrixType::Ones(rows, cols),
     37              identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
     38                               ::Identity(rows, rows),
     39              square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
     40   VectorType v1 = VectorType::Random(rows),
     41              v2 = VectorType::Random(rows),
     42              vzero = VectorType::Zero(rows),
     43              vones = VectorType::Ones(rows),
     44              v3(rows);
     45 
     46   int r = ei_random<int>(0, rows-1),
     47       c = ei_random<int>(0, cols-1);
     48 
     49   Scalar s1 = ei_random<Scalar>();
     50 
     51   // test Zero, Ones, Constant, and the set* variants
     52   m3 = MatrixType::Constant(rows, cols, s1);
     53   for (int j=0; j<cols; ++j)
     54     for (int i=0; i<rows; ++i)
     55     {
     56       VERIFY_IS_APPROX(mzero(i,j), Scalar(0));
     57       VERIFY_IS_APPROX(mones(i,j), Scalar(1));
     58       VERIFY_IS_APPROX(m3(i,j), s1);
     59     }
     60   VERIFY(mzero.isZero());
     61   VERIFY(mones.isOnes());
     62   VERIFY(m3.isConstant(s1));
     63   VERIFY(identity.isIdentity());
     64   VERIFY_IS_APPROX(m4.setConstant(s1), m3);
     65   VERIFY_IS_APPROX(m4.setConstant(rows,cols,s1), m3);
     66   VERIFY_IS_APPROX(m4.setZero(), mzero);
     67   VERIFY_IS_APPROX(m4.setZero(rows,cols), mzero);
     68   VERIFY_IS_APPROX(m4.setOnes(), mones);
     69   VERIFY_IS_APPROX(m4.setOnes(rows,cols), mones);
     70   m4.fill(s1);
     71   VERIFY_IS_APPROX(m4, m3);
     72 
     73   VERIFY_IS_APPROX(v3.setConstant(rows, s1), VectorType::Constant(rows,s1));
     74   VERIFY_IS_APPROX(v3.setZero(rows), vzero);
     75   VERIFY_IS_APPROX(v3.setOnes(rows), vones);
     76 
     77   m2 = m2.template binaryExpr<AddIfNull<Scalar> >(mones);
     78 
     79   VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().abs2());
     80   VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
     81   VERIFY_IS_APPROX(m1.cwise().pow(3), m1.cwise().cube());
     82 
     83   VERIFY_IS_APPROX(m1 + mones, m1.cwise()+Scalar(1));
     84   VERIFY_IS_APPROX(m1 - mones, m1.cwise()-Scalar(1));
     85   m3 = m1; m3.cwise() += 1;
     86   VERIFY_IS_APPROX(m1 + mones, m3);
     87   m3 = m1; m3.cwise() -= 1;
     88   VERIFY_IS_APPROX(m1 - mones, m3);
     89 
     90   VERIFY_IS_APPROX(m2, m2.cwise() * mones);
     91   VERIFY_IS_APPROX(m1.cwise() * m2,  m2.cwise() * m1);
     92   m3 = m1;
     93   m3.cwise() *= m2;
     94   VERIFY_IS_APPROX(m3, m1.cwise() * m2);
     95 
     96   VERIFY_IS_APPROX(mones,    m2.cwise()/m2);
     97   if(NumTraits<Scalar>::HasFloatingPoint)
     98   {
     99     VERIFY_IS_APPROX(m1.cwise() / m2,    m1.cwise() * (m2.cwise().inverse()));
    100     m3 = m1.cwise().abs().cwise().sqrt();
    101     VERIFY_IS_APPROX(m3.cwise().square(), m1.cwise().abs());
    102     VERIFY_IS_APPROX(m1.cwise().square().cwise().sqrt(), m1.cwise().abs());
    103     VERIFY_IS_APPROX(m1.cwise().abs().cwise().log().cwise().exp() , m1.cwise().abs());
    104 
    105     VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
    106     m3 = (m1.cwise().abs().cwise()<=RealScalar(0.01)).select(mones,m1);
    107     VERIFY_IS_APPROX(m3.cwise().pow(-1), m3.cwise().inverse());
    108     m3 = m1.cwise().abs();
    109     VERIFY_IS_APPROX(m3.cwise().pow(RealScalar(0.5)), m3.cwise().sqrt());
    110 
    111 //     VERIFY_IS_APPROX(m1.cwise().tan(), m1.cwise().sin().cwise() / m1.cwise().cos());
    112     VERIFY_IS_APPROX(mones, m1.cwise().sin().cwise().square() + m1.cwise().cos().cwise().square());
    113     m3 = m1;
    114     m3.cwise() /= m2;
    115     VERIFY_IS_APPROX(m3, m1.cwise() / m2);
    116   }
    117 
    118   // check min
    119   VERIFY_IS_APPROX( m1.cwise().min(m2), m2.cwise().min(m1) );
    120   VERIFY_IS_APPROX( m1.cwise().min(m1+mones), m1 );
    121   VERIFY_IS_APPROX( m1.cwise().min(m1-mones), m1-mones );
    122 
    123   // check max
    124   VERIFY_IS_APPROX( m1.cwise().max(m2), m2.cwise().max(m1) );
    125   VERIFY_IS_APPROX( m1.cwise().max(m1-mones), m1 );
    126   VERIFY_IS_APPROX( m1.cwise().max(m1+mones), m1+mones );
    127 
    128   VERIFY( (m1.cwise() == m1).all() );
    129   VERIFY( (m1.cwise() != m2).any() );
    130   VERIFY(!(m1.cwise() == (m1+mones)).any() );
    131   if (rows*cols>1)
    132   {
    133     m3 = m1;
    134     m3(r,c) += 1;
    135     VERIFY( (m1.cwise() == m3).any() );
    136     VERIFY( !(m1.cwise() == m3).all() );
    137   }
    138   VERIFY( (m1.cwise().min(m2).cwise() <= m2).all() );
    139   VERIFY( (m1.cwise().max(m2).cwise() >= m2).all() );
    140   VERIFY( (m1.cwise().min(m2).cwise() < (m1+mones)).all() );
    141   VERIFY( (m1.cwise().max(m2).cwise() > (m1-mones)).all() );
    142 
    143   VERIFY( (m1.cwise()<m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).all() );
    144   VERIFY( !(m1.cwise()<m1.unaryExpr(bind2nd(minus<Scalar>(), Scalar(1)))).all() );
    145   VERIFY( !(m1.cwise()>m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).any() );
    146 }
    147 
    148 void test_eigen2_cwiseop()
    149 {
    150   for(int i = 0; i < g_repeat ; i++) {
    151     CALL_SUBTEST_1( cwiseops(Matrix<float, 1, 1>()) );
    152     CALL_SUBTEST_2( cwiseops(Matrix4d()) );
    153     CALL_SUBTEST_3( cwiseops(MatrixXf(3, 3)) );
    154     CALL_SUBTEST_3( cwiseops(MatrixXf(22, 22)) );
    155     CALL_SUBTEST_4( cwiseops(MatrixXi(8, 12)) );
    156     CALL_SUBTEST_5( cwiseops(MatrixXd(20, 20)) );
    157   }
    158 }
    159