eigen2_visitor.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 Benoit Jacob <jacob.benoit.1@gmail.com>
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 
00027 template<typename MatrixType> void matrixVisitor(const MatrixType& p)
00028 {
00029   typedef typename MatrixType::Scalar Scalar;
00030 
00031   int rows = p.rows();
00032   int cols = p.cols();
00033 
00034   // construct a random matrix where all coefficients are different
00035   MatrixType m;
00036   m = MatrixType::Random(rows, cols);
00037   for(int i = 0; i < m.size(); i++)
00038     for(int i2 = 0; i2 < i; i2++)
00039       while(m(i) == m(i2)) // yes, ==
00040         m(i) = ei_random<Scalar>();
00041   
00042   Scalar minc = Scalar(1000), maxc = Scalar(-1000);
00043   int minrow=0,mincol=0,maxrow=0,maxcol=0;
00044   for(int j = 0; j < cols; j++)
00045   for(int i = 0; i < rows; i++)
00046   {
00047     if(m(i,j) < minc)
00048     {
00049       minc = m(i,j);
00050       minrow = i;
00051       mincol = j;
00052     }
00053     if(m(i,j) > maxc)
00054     {
00055       maxc = m(i,j);
00056       maxrow = i;
00057       maxcol = j;
00058     }
00059   }
00060   int eigen_minrow, eigen_mincol, eigen_maxrow, eigen_maxcol;
00061   Scalar eigen_minc, eigen_maxc;
00062   eigen_minc = m.minCoeff(&eigen_minrow,&eigen_mincol);
00063   eigen_maxc = m.maxCoeff(&eigen_maxrow,&eigen_maxcol);
00064   VERIFY(minrow == eigen_minrow);
00065   VERIFY(maxrow == eigen_maxrow);
00066   VERIFY(mincol == eigen_mincol);
00067   VERIFY(maxcol == eigen_maxcol);
00068   VERIFY_IS_APPROX(minc, eigen_minc);
00069   VERIFY_IS_APPROX(maxc, eigen_maxc);
00070   VERIFY_IS_APPROX(minc, m.minCoeff());
00071   VERIFY_IS_APPROX(maxc, m.maxCoeff());
00072 }
00073 
00074 template<typename VectorType> void vectorVisitor(const VectorType& w)
00075 {
00076   typedef typename VectorType::Scalar Scalar;
00077 
00078   int size = w.size();
00079 
00080   // construct a random vector where all coefficients are different
00081   VectorType v;
00082   v = VectorType::Random(size);
00083   for(int i = 0; i < size; i++)
00084     for(int i2 = 0; i2 < i; i2++)
00085       while(v(i) == v(i2)) // yes, ==
00086         v(i) = ei_random<Scalar>();
00087   
00088   Scalar minc = Scalar(1000), maxc = Scalar(-1000);
00089   int minidx=0,maxidx=0;
00090   for(int i = 0; i < size; i++)
00091   {
00092     if(v(i) < minc)
00093     {
00094       minc = v(i);
00095       minidx = i;
00096     }
00097     if(v(i) > maxc)
00098     {
00099       maxc = v(i);
00100       maxidx = i;
00101     }
00102   }
00103   int eigen_minidx, eigen_maxidx;
00104   Scalar eigen_minc, eigen_maxc;
00105   eigen_minc = v.minCoeff(&eigen_minidx);
00106   eigen_maxc = v.maxCoeff(&eigen_maxidx);
00107   VERIFY(minidx == eigen_minidx);
00108   VERIFY(maxidx == eigen_maxidx);
00109   VERIFY_IS_APPROX(minc, eigen_minc);
00110   VERIFY_IS_APPROX(maxc, eigen_maxc);
00111   VERIFY_IS_APPROX(minc, v.minCoeff());
00112   VERIFY_IS_APPROX(maxc, v.maxCoeff());
00113 }
00114 
00115 void test_eigen2_visitor()
00116 {
00117   for(int i = 0; i < g_repeat; i++) {
00118     CALL_SUBTEST_1( matrixVisitor(Matrix<float, 1, 1>()) );
00119     CALL_SUBTEST_2( matrixVisitor(Matrix2f()) );
00120     CALL_SUBTEST_3( matrixVisitor(Matrix4d()) );
00121     CALL_SUBTEST_4( matrixVisitor(MatrixXd(8, 12)) );
00122     CALL_SUBTEST_5( matrixVisitor(Matrix<double,Dynamic,Dynamic,RowMajor>(20, 20)) );
00123     CALL_SUBTEST_6( matrixVisitor(MatrixXi(8, 12)) );
00124   }
00125   for(int i = 0; i < g_repeat; i++) {
00126     CALL_SUBTEST_7( vectorVisitor(Vector4f()) );
00127     CALL_SUBTEST_4( vectorVisitor(VectorXd(10)) );
00128     CALL_SUBTEST_4( vectorVisitor(RowVectorXd(10)) );
00129     CALL_SUBTEST_8( vectorVisitor(VectorXf(33)) );
00130   }
00131 }


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