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.

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