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.

83 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>1 ? 1 : 0,
  29. N2 = MatrixType::RowsAtCompileTime>2 ? -2 : 0
  30. };
  31. // check sub/super diagonal
  32. if(m1.template diagonal<N1>().RowsAtCompileTime!=Dynamic)
  33. {
  34. VERIFY(m1.template diagonal<N1>().RowsAtCompileTime == m1.diagonal(N1).size());
  35. }
  36. if(m1.template diagonal<N2>().RowsAtCompileTime!=Dynamic)
  37. {
  38. VERIFY(m1.template diagonal<N2>().RowsAtCompileTime == m1.diagonal(N2).size());
  39. }
  40. m2.template diagonal<N1>() = 2 * m1.template diagonal<N1>();
  41. VERIFY_IS_APPROX(m2.template diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1));
  42. m2.template diagonal<N1>()[0] *= 3;
  43. VERIFY_IS_APPROX(m2.template diagonal<N1>()[0], static_cast<Scalar>(6) * m1.template diagonal<N1>()[0]);
  44. m2.template diagonal<N2>() = 2 * m1.template diagonal<N2>();
  45. m2.template diagonal<N2>()[0] *= 3;
  46. VERIFY_IS_APPROX(m2.template diagonal<N2>()[0], static_cast<Scalar>(6) * m1.template diagonal<N2>()[0]);
  47. m2.diagonal(N1) = 2 * m1.diagonal(N1);
  48. VERIFY_IS_APPROX(m2.diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1));
  49. m2.diagonal(N1)[0] *= 3;
  50. VERIFY_IS_APPROX(m2.diagonal(N1)[0], static_cast<Scalar>(6) * m1.diagonal(N1)[0]);
  51. m2.diagonal(N2) = 2 * m1.diagonal(N2);
  52. VERIFY_IS_APPROX(m2.diagonal<N2>(), static_cast<Scalar>(2) * m1.diagonal(N2));
  53. m2.diagonal(N2)[0] *= 3;
  54. VERIFY_IS_APPROX(m2.diagonal(N2)[0], static_cast<Scalar>(6) * m1.diagonal(N2)[0]);
  55. }
  56. }
  57. void test_diagonal()
  58. {
  59. for(int i = 0; i < g_repeat; i++) {
  60. CALL_SUBTEST_1( diagonal(Matrix<float, 1, 1>()) );
  61. CALL_SUBTEST_1( diagonal(Matrix<float, 4, 9>()) );
  62. CALL_SUBTEST_1( diagonal(Matrix<float, 7, 3>()) );
  63. CALL_SUBTEST_2( diagonal(Matrix4d()) );
  64. CALL_SUBTEST_2( diagonal(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  65. CALL_SUBTEST_2( diagonal(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  66. CALL_SUBTEST_2( diagonal(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  67. CALL_SUBTEST_1( diagonal(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  68. CALL_SUBTEST_1( diagonal(Matrix<float,Dynamic,4>(3, 4)) );
  69. }
  70. }