1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1 (at) gmail.com> 5 // 6 // This Source Code Form is subject to the terms of the Mozilla 7 // Public License v. 2.0. If a copy of the MPL was not distributed 8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 10 #include "main.h" 11 12 template<typename MatrixType> void matrixVisitor(const MatrixType& p) 13 { 14 typedef typename MatrixType::Scalar Scalar; 15 16 int rows = p.rows(); 17 int cols = p.cols(); 18 19 // construct a random matrix where all coefficients are different 20 MatrixType m; 21 m = MatrixType::Random(rows, cols); 22 for(int i = 0; i < m.size(); i++) 23 for(int i2 = 0; i2 < i; i2++) 24 while(m(i) == m(i2)) // yes, == 25 m(i) = ei_random<Scalar>(); 26 27 Scalar minc = Scalar(1000), maxc = Scalar(-1000); 28 int minrow=0,mincol=0,maxrow=0,maxcol=0; 29 for(int j = 0; j < cols; j++) 30 for(int i = 0; i < rows; i++) 31 { 32 if(m(i,j) < minc) 33 { 34 minc = m(i,j); 35 minrow = i; 36 mincol = j; 37 } 38 if(m(i,j) > maxc) 39 { 40 maxc = m(i,j); 41 maxrow = i; 42 maxcol = j; 43 } 44 } 45 int eigen_minrow, eigen_mincol, eigen_maxrow, eigen_maxcol; 46 Scalar eigen_minc, eigen_maxc; 47 eigen_minc = m.minCoeff(&eigen_minrow,&eigen_mincol); 48 eigen_maxc = m.maxCoeff(&eigen_maxrow,&eigen_maxcol); 49 VERIFY(minrow == eigen_minrow); 50 VERIFY(maxrow == eigen_maxrow); 51 VERIFY(mincol == eigen_mincol); 52 VERIFY(maxcol == eigen_maxcol); 53 VERIFY_IS_APPROX(minc, eigen_minc); 54 VERIFY_IS_APPROX(maxc, eigen_maxc); 55 VERIFY_IS_APPROX(minc, m.minCoeff()); 56 VERIFY_IS_APPROX(maxc, m.maxCoeff()); 57 } 58 59 template<typename VectorType> void vectorVisitor(const VectorType& w) 60 { 61 typedef typename VectorType::Scalar Scalar; 62 63 int size = w.size(); 64 65 // construct a random vector where all coefficients are different 66 VectorType v; 67 v = VectorType::Random(size); 68 for(int i = 0; i < size; i++) 69 for(int i2 = 0; i2 < i; i2++) 70 while(v(i) == v(i2)) // yes, == 71 v(i) = ei_random<Scalar>(); 72 73 Scalar minc = Scalar(1000), maxc = Scalar(-1000); 74 int minidx=0,maxidx=0; 75 for(int i = 0; i < size; i++) 76 { 77 if(v(i) < minc) 78 { 79 minc = v(i); 80 minidx = i; 81 } 82 if(v(i) > maxc) 83 { 84 maxc = v(i); 85 maxidx = i; 86 } 87 } 88 int eigen_minidx, eigen_maxidx; 89 Scalar eigen_minc, eigen_maxc; 90 eigen_minc = v.minCoeff(&eigen_minidx); 91 eigen_maxc = v.maxCoeff(&eigen_maxidx); 92 VERIFY(minidx == eigen_minidx); 93 VERIFY(maxidx == eigen_maxidx); 94 VERIFY_IS_APPROX(minc, eigen_minc); 95 VERIFY_IS_APPROX(maxc, eigen_maxc); 96 VERIFY_IS_APPROX(minc, v.minCoeff()); 97 VERIFY_IS_APPROX(maxc, v.maxCoeff()); 98 } 99 100 void test_eigen2_visitor() 101 { 102 for(int i = 0; i < g_repeat; i++) { 103 CALL_SUBTEST_1( matrixVisitor(Matrix<float, 1, 1>()) ); 104 CALL_SUBTEST_2( matrixVisitor(Matrix2f()) ); 105 CALL_SUBTEST_3( matrixVisitor(Matrix4d()) ); 106 CALL_SUBTEST_4( matrixVisitor(MatrixXd(8, 12)) ); 107 CALL_SUBTEST_5( matrixVisitor(Matrix<double,Dynamic,Dynamic,RowMajor>(20, 20)) ); 108 CALL_SUBTEST_6( matrixVisitor(MatrixXi(8, 12)) ); 109 } 110 for(int i = 0; i < g_repeat; i++) { 111 CALL_SUBTEST_7( vectorVisitor(Vector4f()) ); 112 CALL_SUBTEST_4( vectorVisitor(VectorXd(10)) ); 113 CALL_SUBTEST_4( vectorVisitor(RowVectorXd(10)) ); 114 CALL_SUBTEST_8( vectorVisitor(VectorXf(33)) ); 115 } 116 } 117