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@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 12template<typename MatrixType> void matrixVisitor(const MatrixType& p) 13{ 14 typedef typename MatrixType::Scalar Scalar; 15 typedef typename MatrixType::Index Index; 16 17 Index rows = p.rows(); 18 Index cols = p.cols(); 19 20 // construct a random matrix where all coefficients are different 21 MatrixType m; 22 m = MatrixType::Random(rows, cols); 23 for(Index i = 0; i < m.size(); i++) 24 for(Index i2 = 0; i2 < i; i2++) 25 while(m(i) == m(i2)) // yes, == 26 m(i) = internal::random<Scalar>(); 27 28 Scalar minc = Scalar(1000), maxc = Scalar(-1000); 29 Index minrow=0,mincol=0,maxrow=0,maxcol=0; 30 for(Index j = 0; j < cols; j++) 31 for(Index i = 0; i < rows; i++) 32 { 33 if(m(i,j) < minc) 34 { 35 minc = m(i,j); 36 minrow = i; 37 mincol = j; 38 } 39 if(m(i,j) > maxc) 40 { 41 maxc = m(i,j); 42 maxrow = i; 43 maxcol = j; 44 } 45 } 46 Index eigen_minrow, eigen_mincol, eigen_maxrow, eigen_maxcol; 47 Scalar eigen_minc, eigen_maxc; 48 eigen_minc = m.minCoeff(&eigen_minrow,&eigen_mincol); 49 eigen_maxc = m.maxCoeff(&eigen_maxrow,&eigen_maxcol); 50 VERIFY(minrow == eigen_minrow); 51 VERIFY(maxrow == eigen_maxrow); 52 VERIFY(mincol == eigen_mincol); 53 VERIFY(maxcol == eigen_maxcol); 54 VERIFY_IS_APPROX(minc, eigen_minc); 55 VERIFY_IS_APPROX(maxc, eigen_maxc); 56 VERIFY_IS_APPROX(minc, m.minCoeff()); 57 VERIFY_IS_APPROX(maxc, m.maxCoeff()); 58} 59 60template<typename VectorType> void vectorVisitor(const VectorType& w) 61{ 62 typedef typename VectorType::Scalar Scalar; 63 typedef typename VectorType::Index Index; 64 65 Index size = w.size(); 66 67 // construct a random vector where all coefficients are different 68 VectorType v; 69 v = VectorType::Random(size); 70 for(Index i = 0; i < size; i++) 71 for(Index i2 = 0; i2 < i; i2++) 72 while(v(i) == v(i2)) // yes, == 73 v(i) = internal::random<Scalar>(); 74 75 Scalar minc = Scalar(1000), maxc = Scalar(-1000); 76 Index minidx=0,maxidx=0; 77 for(Index i = 0; i < size; i++) 78 { 79 if(v(i) < minc) 80 { 81 minc = v(i); 82 minidx = i; 83 } 84 if(v(i) > maxc) 85 { 86 maxc = v(i); 87 maxidx = i; 88 } 89 } 90 Index eigen_minidx, eigen_maxidx; 91 Scalar eigen_minc, eigen_maxc; 92 eigen_minc = v.minCoeff(&eigen_minidx); 93 eigen_maxc = v.maxCoeff(&eigen_maxidx); 94 VERIFY(minidx == eigen_minidx); 95 VERIFY(maxidx == eigen_maxidx); 96 VERIFY_IS_APPROX(minc, eigen_minc); 97 VERIFY_IS_APPROX(maxc, eigen_maxc); 98 VERIFY_IS_APPROX(minc, v.minCoeff()); 99 VERIFY_IS_APPROX(maxc, v.maxCoeff()); 100} 101 102void test_visitor() 103{ 104 for(int i = 0; i < g_repeat; i++) { 105 CALL_SUBTEST_1( matrixVisitor(Matrix<float, 1, 1>()) ); 106 CALL_SUBTEST_2( matrixVisitor(Matrix2f()) ); 107 CALL_SUBTEST_3( matrixVisitor(Matrix4d()) ); 108 CALL_SUBTEST_4( matrixVisitor(MatrixXd(8, 12)) ); 109 CALL_SUBTEST_5( matrixVisitor(Matrix<double,Dynamic,Dynamic,RowMajor>(20, 20)) ); 110 CALL_SUBTEST_6( matrixVisitor(MatrixXi(8, 12)) ); 111 } 112 for(int i = 0; i < g_repeat; i++) { 113 CALL_SUBTEST_7( vectorVisitor(Vector4f()) ); 114 CALL_SUBTEST_8( vectorVisitor(VectorXd(10)) ); 115 CALL_SUBTEST_9( vectorVisitor(RowVectorXd(10)) ); 116 CALL_SUBTEST_10( vectorVisitor(VectorXf(33)) ); 117 } 118} 119