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


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:34:04