1// This file is part of Eigen, a lightweight C++ template library 2// for linear algebra. 3// 4// Copyright (C) 2006-2010 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 diagonal(const MatrixType& m) 13{ 14 typedef typename MatrixType::Index Index; 15 typedef typename MatrixType::Scalar Scalar; 16 typedef typename MatrixType::RealScalar RealScalar; 17 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; 18 typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType; 19 20 Index rows = m.rows(); 21 Index cols = m.cols(); 22 23 MatrixType m1 = MatrixType::Random(rows, cols), 24 m2 = MatrixType::Random(rows, cols); 25 26 //check diagonal() 27 VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal()); 28 m2.diagonal() = 2 * m1.diagonal(); 29 m2.diagonal()[0] *= 3; 30 31 if (rows>2) 32 { 33 enum { 34 N1 = MatrixType::RowsAtCompileTime>1 ? 1 : 0, 35 N2 = MatrixType::RowsAtCompileTime>2 ? -2 : 0 36 }; 37 38 // check sub/super diagonal 39 if(m1.template diagonal<N1>().RowsAtCompileTime!=Dynamic) 40 { 41 VERIFY(m1.template diagonal<N1>().RowsAtCompileTime == m1.diagonal(N1).size()); 42 } 43 if(m1.template diagonal<N2>().RowsAtCompileTime!=Dynamic) 44 { 45 VERIFY(m1.template diagonal<N2>().RowsAtCompileTime == m1.diagonal(N2).size()); 46 } 47 48 m2.template diagonal<N1>() = 2 * m1.template diagonal<N1>(); 49 VERIFY_IS_APPROX(m2.template diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1)); 50 m2.template diagonal<N1>()[0] *= 3; 51 VERIFY_IS_APPROX(m2.template diagonal<N1>()[0], static_cast<Scalar>(6) * m1.template diagonal<N1>()[0]); 52 53 54 m2.template diagonal<N2>() = 2 * m1.template diagonal<N2>(); 55 m2.template diagonal<N2>()[0] *= 3; 56 VERIFY_IS_APPROX(m2.template diagonal<N2>()[0], static_cast<Scalar>(6) * m1.template diagonal<N2>()[0]); 57 58 m2.diagonal(N1) = 2 * m1.diagonal(N1); 59 VERIFY_IS_APPROX(m2.diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1)); 60 m2.diagonal(N1)[0] *= 3; 61 VERIFY_IS_APPROX(m2.diagonal(N1)[0], static_cast<Scalar>(6) * m1.diagonal(N1)[0]); 62 63 m2.diagonal(N2) = 2 * m1.diagonal(N2); 64 VERIFY_IS_APPROX(m2.diagonal<N2>(), static_cast<Scalar>(2) * m1.diagonal(N2)); 65 m2.diagonal(N2)[0] *= 3; 66 VERIFY_IS_APPROX(m2.diagonal(N2)[0], static_cast<Scalar>(6) * m1.diagonal(N2)[0]); 67 } 68} 69 70void test_diagonal() 71{ 72 for(int i = 0; i < g_repeat; i++) { 73 CALL_SUBTEST_1( diagonal(Matrix<float, 1, 1>()) ); 74 CALL_SUBTEST_1( diagonal(Matrix<float, 4, 9>()) ); 75 CALL_SUBTEST_1( diagonal(Matrix<float, 7, 3>()) ); 76 CALL_SUBTEST_2( diagonal(Matrix4d()) ); 77 CALL_SUBTEST_2( diagonal(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 78 CALL_SUBTEST_2( diagonal(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 79 CALL_SUBTEST_2( diagonal(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 80 CALL_SUBTEST_1( diagonal(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 81 CALL_SUBTEST_1( diagonal(Matrix<float,Dynamic,4>(3, 4)) ); 82 } 83} 84