1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. Eigen itself is part of the KDE project. 3 // 4 // Copyright (C) 2006-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 miscMatrices(const MatrixType& m) 13 { 14 /* this test covers the following files: 15 DiagonalMatrix.h Ones.h 16 */ 17 18 typedef typename MatrixType::Scalar Scalar; 19 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; 20 typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType; 21 int rows = m.rows(); 22 int cols = m.cols(); 23 24 int r = ei_random<int>(0, rows-1), r2 = ei_random<int>(0, rows-1), c = ei_random<int>(0, cols-1); 25 VERIFY_IS_APPROX(MatrixType::Ones(rows,cols)(r,c), static_cast<Scalar>(1)); 26 MatrixType m1 = MatrixType::Ones(rows,cols); 27 VERIFY_IS_APPROX(m1(r,c), static_cast<Scalar>(1)); 28 VectorType v1 = VectorType::Random(rows); 29 v1[0]; 30 Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> 31 square = v1.asDiagonal(); 32 if(r==r2) VERIFY_IS_APPROX(square(r,r2), v1[r]); 33 else VERIFY_IS_MUCH_SMALLER_THAN(square(r,r2), static_cast<Scalar>(1)); 34 square = MatrixType::Zero(rows, rows); 35 square.diagonal() = VectorType::Ones(rows); 36 VERIFY_IS_APPROX(square, MatrixType::Identity(rows, rows)); 37 } 38 39 void test_eigen2_miscmatrices() 40 { 41 for(int i = 0; i < g_repeat; i++) { 42 CALL_SUBTEST_1( miscMatrices(Matrix<float, 1, 1>()) ); 43 CALL_SUBTEST_2( miscMatrices(Matrix4d()) ); 44 CALL_SUBTEST_3( miscMatrices(MatrixXcf(3, 3)) ); 45 CALL_SUBTEST_4( miscMatrices(MatrixXi(8, 12)) ); 46 CALL_SUBTEST_5( miscMatrices(MatrixXcd(20, 20)) ); 47 } 48 } 49