You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
3.1 KiB

  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. #include "main.h"
  10. template<typename MatrixType> void diagonal(const MatrixType& m)
  11. {
  12. typedef typename MatrixType::Index Index;
  13. typedef typename MatrixType::Scalar Scalar;
  14. Index rows = m.rows();
  15. Index cols = m.cols();
  16. MatrixType m1 = MatrixType::Random(rows, cols),
  17. m2 = MatrixType::Random(rows, cols);
  18. //check diagonal()
  19. VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal());
  20. m2.diagonal() = 2 * m1.diagonal();
  21. m2.diagonal()[0] *= 3;
  22. if (rows>2)
  23. {
  24. enum {
  25. N1 = MatrixType::RowsAtCompileTime>2 ? 2 : 0,
  26. N2 = MatrixType::RowsAtCompileTime>1 ? -1 : 0
  27. };
  28. // check sub/super diagonal
  29. if(MatrixType::SizeAtCompileTime!=Dynamic)
  30. {
  31. VERIFY(m1.template diagonal<N1>().RowsAtCompileTime == m1.diagonal(N1).size());
  32. VERIFY(m1.template diagonal<N2>().RowsAtCompileTime == m1.diagonal(N2).size());
  33. }
  34. m2.template diagonal<N1>() = 2 * m1.template diagonal<N1>();
  35. VERIFY_IS_APPROX(m2.template diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1));
  36. m2.template diagonal<N1>()[0] *= 3;
  37. VERIFY_IS_APPROX(m2.template diagonal<N1>()[0], static_cast<Scalar>(6) * m1.template diagonal<N1>()[0]);
  38. m2.template diagonal<N2>() = 2 * m1.template diagonal<N2>();
  39. m2.template diagonal<N2>()[0] *= 3;
  40. VERIFY_IS_APPROX(m2.template diagonal<N2>()[0], static_cast<Scalar>(6) * m1.template diagonal<N2>()[0]);
  41. m2.diagonal(N1) = 2 * m1.diagonal(N1);
  42. VERIFY_IS_APPROX(m2.template diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1));
  43. m2.diagonal(N1)[0] *= 3;
  44. VERIFY_IS_APPROX(m2.diagonal(N1)[0], static_cast<Scalar>(6) * m1.diagonal(N1)[0]);
  45. m2.diagonal(N2) = 2 * m1.diagonal(N2);
  46. VERIFY_IS_APPROX(m2.template diagonal<N2>(), static_cast<Scalar>(2) * m1.diagonal(N2));
  47. m2.diagonal(N2)[0] *= 3;
  48. VERIFY_IS_APPROX(m2.diagonal(N2)[0], static_cast<Scalar>(6) * m1.diagonal(N2)[0]);
  49. }
  50. }
  51. void test_diagonal()
  52. {
  53. for(int i = 0; i < g_repeat; i++) {
  54. CALL_SUBTEST_1( diagonal(Matrix<float, 1, 1>()) );
  55. CALL_SUBTEST_1( diagonal(Matrix<float, 4, 9>()) );
  56. CALL_SUBTEST_1( diagonal(Matrix<float, 7, 3>()) );
  57. CALL_SUBTEST_2( diagonal(Matrix4d()) );
  58. CALL_SUBTEST_2( diagonal(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  59. CALL_SUBTEST_2( diagonal(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  60. CALL_SUBTEST_2( diagonal(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  61. CALL_SUBTEST_1( diagonal(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  62. CALL_SUBTEST_1( diagonal(Matrix<float,Dynamic,4>(3, 4)) );
  63. }
  64. }