eigen2_array.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 //
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 #include <Eigen/Array>
00027 
00028 template<typename MatrixType> void array(const MatrixType& m)
00029 {
00030   /* this test covers the following files:
00031      Array.cpp
00032   */
00033 
00034   typedef typename MatrixType::Scalar Scalar;
00035   typedef typename NumTraits<Scalar>::Real RealScalar;
00036   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00037 
00038   int rows = m.rows();
00039   int cols = m.cols();
00040 
00041   MatrixType m1 = MatrixType::Random(rows, cols),
00042              m2 = MatrixType::Random(rows, cols),
00043              m3(rows, cols);
00044 
00045   Scalar  s1 = ei_random<Scalar>(),
00046           s2 = ei_random<Scalar>();
00047 
00048   // scalar addition
00049   VERIFY_IS_APPROX(m1.cwise() + s1, s1 + m1.cwise());
00050   VERIFY_IS_APPROX(m1.cwise() + s1, MatrixType::Constant(rows,cols,s1) + m1);
00051   VERIFY_IS_APPROX((m1*Scalar(2)).cwise() - s2, (m1+m1) - MatrixType::Constant(rows,cols,s2) );
00052   m3 = m1;
00053   m3.cwise() += s2;
00054   VERIFY_IS_APPROX(m3, m1.cwise() + s2);
00055   m3 = m1;
00056   m3.cwise() -= s1;
00057   VERIFY_IS_APPROX(m3, m1.cwise() - s1);
00058 
00059   // reductions
00060   VERIFY_IS_APPROX(m1.colwise().sum().sum(), m1.sum());
00061   VERIFY_IS_APPROX(m1.rowwise().sum().sum(), m1.sum());
00062   if (!ei_isApprox(m1.sum(), (m1+m2).sum()))
00063     VERIFY_IS_NOT_APPROX(((m1+m2).rowwise().sum()).sum(), m1.sum());
00064   VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(internal::scalar_sum_op<Scalar>()));
00065 }
00066 
00067 template<typename MatrixType> void comparisons(const MatrixType& m)
00068 {
00069   typedef typename MatrixType::Scalar Scalar;
00070   typedef typename NumTraits<Scalar>::Real RealScalar;
00071   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00072 
00073   int rows = m.rows();
00074   int cols = m.cols();
00075 
00076   int r = ei_random<int>(0, rows-1),
00077       c = ei_random<int>(0, cols-1);
00078 
00079   MatrixType m1 = MatrixType::Random(rows, cols),
00080              m2 = MatrixType::Random(rows, cols),
00081              m3(rows, cols);
00082 
00083   VERIFY(((m1.cwise() + Scalar(1)).cwise() > m1).all());
00084   VERIFY(((m1.cwise() - Scalar(1)).cwise() < m1).all());
00085   if (rows*cols>1)
00086   {
00087     m3 = m1;
00088     m3(r,c) += 1;
00089     VERIFY(! (m1.cwise() < m3).all() );
00090     VERIFY(! (m1.cwise() > m3).all() );
00091   }
00092   
00093   // comparisons to scalar
00094   VERIFY( (m1.cwise() != (m1(r,c)+1) ).any() );
00095   VERIFY( (m1.cwise() > (m1(r,c)-1) ).any() );
00096   VERIFY( (m1.cwise() < (m1(r,c)+1) ).any() );
00097   VERIFY( (m1.cwise() == m1(r,c) ).any() );
00098   
00099   // test Select
00100   VERIFY_IS_APPROX( (m1.cwise()<m2).select(m1,m2), m1.cwise().min(m2) );
00101   VERIFY_IS_APPROX( (m1.cwise()>m2).select(m1,m2), m1.cwise().max(m2) );
00102   Scalar mid = (m1.cwise().abs().minCoeff() + m1.cwise().abs().maxCoeff())/Scalar(2);
00103   for (int j=0; j<cols; ++j)
00104   for (int i=0; i<rows; ++i)
00105     m3(i,j) = ei_abs(m1(i,j))<mid ? 0 : m1(i,j);
00106   VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<MatrixType::Constant(rows,cols,mid))
00107                         .select(MatrixType::Zero(rows,cols),m1), m3);
00108   // shorter versions:
00109   VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<MatrixType::Constant(rows,cols,mid))
00110                         .select(0,m1), m3);
00111   VERIFY_IS_APPROX( (m1.cwise().abs().cwise()>=MatrixType::Constant(rows,cols,mid))
00112                         .select(m1,0), m3);
00113   // even shorter version:
00114   VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<mid).select(0,m1), m3);
00115   
00116   // count
00117   VERIFY(((m1.cwise().abs().cwise()+1).cwise()>RealScalar(0.1)).count() == rows*cols);
00118   VERIFY_IS_APPROX(((m1.cwise().abs().cwise()+1).cwise()>RealScalar(0.1)).colwise().count().template cast<int>(), RowVectorXi::Constant(cols,rows));
00119   VERIFY_IS_APPROX(((m1.cwise().abs().cwise()+1).cwise()>RealScalar(0.1)).rowwise().count().template cast<int>(), VectorXi::Constant(rows, cols));
00120 }
00121 
00122 template<typename VectorType> void lpNorm(const VectorType& v)
00123 {
00124   VectorType u = VectorType::Random(v.size());
00125 
00126   VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), u.cwise().abs().maxCoeff());
00127   VERIFY_IS_APPROX(u.template lpNorm<1>(), u.cwise().abs().sum());
00128   VERIFY_IS_APPROX(u.template lpNorm<2>(), ei_sqrt(u.cwise().abs().cwise().square().sum()));
00129   VERIFY_IS_APPROX(ei_pow(u.template lpNorm<5>(), typename VectorType::RealScalar(5)), u.cwise().abs().cwise().pow(5).sum());
00130 }
00131 
00132 void test_eigen2_array()
00133 {
00134   for(int i = 0; i < g_repeat; i++) {
00135     CALL_SUBTEST_1( array(Matrix<float, 1, 1>()) );
00136     CALL_SUBTEST_2( array(Matrix2f()) );
00137     CALL_SUBTEST_3( array(Matrix4d()) );
00138     CALL_SUBTEST_4( array(MatrixXcf(3, 3)) );
00139     CALL_SUBTEST_5( array(MatrixXf(8, 12)) );
00140     CALL_SUBTEST_6( array(MatrixXi(8, 12)) );
00141   }
00142   for(int i = 0; i < g_repeat; i++) {
00143     CALL_SUBTEST_1( comparisons(Matrix<float, 1, 1>()) );
00144     CALL_SUBTEST_2( comparisons(Matrix2f()) );
00145     CALL_SUBTEST_3( comparisons(Matrix4d()) );
00146     CALL_SUBTEST_5( comparisons(MatrixXf(8, 12)) );
00147     CALL_SUBTEST_6( comparisons(MatrixXi(8, 12)) );
00148   }
00149   for(int i = 0; i < g_repeat; i++) {
00150     CALL_SUBTEST_1( lpNorm(Matrix<float, 1, 1>()) );
00151     CALL_SUBTEST_2( lpNorm(Vector2f()) );
00152     CALL_SUBTEST_3( lpNorm(Vector3d()) );
00153     CALL_SUBTEST_4( lpNorm(Vector4f()) );
00154     CALL_SUBTEST_5( lpNorm(VectorXf(16)) );
00155     CALL_SUBTEST_7( lpNorm(VectorXcd(10)) );
00156   }
00157 }


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