selfadjoint.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // This file is triangularView of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  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. #define TEST_CHECK_STATIC_ASSERTIONS
  10. #include "main.h"
  11. // This file tests the basic selfadjointView API,
  12. // the related products and decompositions are tested in specific files.
  13. template<typename MatrixType> void selfadjoint(const MatrixType& m)
  14. {
  15. typedef typename MatrixType::Scalar Scalar;
  16. Index rows = m.rows();
  17. Index cols = m.cols();
  18. MatrixType m1 = MatrixType::Random(rows, cols),
  19. m2 = MatrixType::Random(rows, cols),
  20. m3(rows, cols),
  21. m4(rows, cols);
  22. m1.diagonal() = m1.diagonal().real().template cast<Scalar>();
  23. // check selfadjoint to dense
  24. m3 = m1.template selfadjointView<Upper>();
  25. VERIFY_IS_APPROX(MatrixType(m3.template triangularView<Upper>()), MatrixType(m1.template triangularView<Upper>()));
  26. VERIFY_IS_APPROX(m3, m3.adjoint());
  27. m3 = m1.template selfadjointView<Lower>();
  28. VERIFY_IS_APPROX(MatrixType(m3.template triangularView<Lower>()), MatrixType(m1.template triangularView<Lower>()));
  29. VERIFY_IS_APPROX(m3, m3.adjoint());
  30. m3 = m1.template selfadjointView<Upper>();
  31. m4 = m2;
  32. m4 += m1.template selfadjointView<Upper>();
  33. VERIFY_IS_APPROX(m4, m2+m3);
  34. m3 = m1.template selfadjointView<Lower>();
  35. m4 = m2;
  36. m4 -= m1.template selfadjointView<Lower>();
  37. VERIFY_IS_APPROX(m4, m2-m3);
  38. VERIFY_RAISES_STATIC_ASSERT(m2.template selfadjointView<StrictlyUpper>());
  39. VERIFY_RAISES_STATIC_ASSERT(m2.template selfadjointView<UnitLower>());
  40. }
  41. void bug_159()
  42. {
  43. Matrix3d m = Matrix3d::Random().selfadjointView<Lower>();
  44. EIGEN_UNUSED_VARIABLE(m)
  45. }
  46. void test_selfadjoint()
  47. {
  48. for(int i = 0; i < g_repeat ; i++)
  49. {
  50. int s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
  51. CALL_SUBTEST_1( selfadjoint(Matrix<float, 1, 1>()) );
  52. CALL_SUBTEST_2( selfadjoint(Matrix<float, 2, 2>()) );
  53. CALL_SUBTEST_3( selfadjoint(Matrix3cf()) );
  54. CALL_SUBTEST_4( selfadjoint(MatrixXcd(s,s)) );
  55. CALL_SUBTEST_5( selfadjoint(Matrix<float,Dynamic,Dynamic,RowMajor>(s, s)) );
  56. TEST_SET_BUT_UNUSED_VARIABLE(s)
  57. }
  58. CALL_SUBTEST_1( bug_159() );
  59. }