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.

101 lines
4.1 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra. Eigen itself is part of the KDE project.
  3. //
  4. // Copyright (C) 2006-2008 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 adjoint(const MatrixType& m)
  11. {
  12. /* this test covers the following files:
  13. Transpose.h Conjugate.h Dot.h
  14. */
  15. typedef typename MatrixType::Scalar Scalar;
  16. typedef typename NumTraits<Scalar>::Real RealScalar;
  17. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  18. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
  19. int rows = m.rows();
  20. int cols = m.cols();
  21. RealScalar largerEps = test_precision<RealScalar>();
  22. if (ei_is_same_type<RealScalar,float>::ret)
  23. largerEps = RealScalar(1e-3f);
  24. MatrixType m1 = MatrixType::Random(rows, cols),
  25. m2 = MatrixType::Random(rows, cols),
  26. m3(rows, cols),
  27. mzero = MatrixType::Zero(rows, cols),
  28. identity = SquareMatrixType::Identity(rows, rows),
  29. square = SquareMatrixType::Random(rows, rows);
  30. VectorType v1 = VectorType::Random(rows),
  31. v2 = VectorType::Random(rows),
  32. v3 = VectorType::Random(rows),
  33. vzero = VectorType::Zero(rows);
  34. Scalar s1 = ei_random<Scalar>(),
  35. s2 = ei_random<Scalar>();
  36. // check basic compatibility of adjoint, transpose, conjugate
  37. VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1);
  38. VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1);
  39. // check multiplicative behavior
  40. VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1);
  41. VERIFY_IS_APPROX((s1 * m1).adjoint(), ei_conj(s1) * m1.adjoint());
  42. // check basic properties of dot, norm, norm2
  43. typedef typename NumTraits<Scalar>::Real RealScalar;
  44. VERIFY(ei_isApprox((s1 * v1 + s2 * v2).eigen2_dot(v3), s1 * v1.eigen2_dot(v3) + s2 * v2.eigen2_dot(v3), largerEps));
  45. VERIFY(ei_isApprox(v3.eigen2_dot(s1 * v1 + s2 * v2), ei_conj(s1)*v3.eigen2_dot(v1)+ei_conj(s2)*v3.eigen2_dot(v2), largerEps));
  46. VERIFY_IS_APPROX(ei_conj(v1.eigen2_dot(v2)), v2.eigen2_dot(v1));
  47. VERIFY_IS_APPROX(ei_real(v1.eigen2_dot(v1)), v1.squaredNorm());
  48. if(NumTraits<Scalar>::HasFloatingPoint)
  49. VERIFY_IS_APPROX(v1.squaredNorm(), v1.norm() * v1.norm());
  50. VERIFY_IS_MUCH_SMALLER_THAN(ei_abs(vzero.eigen2_dot(v1)), static_cast<RealScalar>(1));
  51. if(NumTraits<Scalar>::HasFloatingPoint)
  52. VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1));
  53. // check compatibility of dot and adjoint
  54. VERIFY(ei_isApprox(v1.eigen2_dot(square * v2), (square.adjoint() * v1).eigen2_dot(v2), largerEps));
  55. // like in testBasicStuff, test operator() to check const-qualification
  56. int r = ei_random<int>(0, rows-1),
  57. c = ei_random<int>(0, cols-1);
  58. VERIFY_IS_APPROX(m1.conjugate()(r,c), ei_conj(m1(r,c)));
  59. VERIFY_IS_APPROX(m1.adjoint()(c,r), ei_conj(m1(r,c)));
  60. if(NumTraits<Scalar>::HasFloatingPoint)
  61. {
  62. // check that Random().normalized() works: tricky as the random xpr must be evaluated by
  63. // normalized() in order to produce a consistent result.
  64. VERIFY_IS_APPROX(VectorType::Random(rows).normalized().norm(), RealScalar(1));
  65. }
  66. // check inplace transpose
  67. m3 = m1;
  68. m3.transposeInPlace();
  69. VERIFY_IS_APPROX(m3,m1.transpose());
  70. m3.transposeInPlace();
  71. VERIFY_IS_APPROX(m3,m1);
  72. }
  73. void test_eigen2_adjoint()
  74. {
  75. for(int i = 0; i < g_repeat; i++) {
  76. CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) );
  77. CALL_SUBTEST_2( adjoint(Matrix3d()) );
  78. CALL_SUBTEST_3( adjoint(Matrix4f()) );
  79. CALL_SUBTEST_4( adjoint(MatrixXcf(4, 4)) );
  80. CALL_SUBTEST_5( adjoint(MatrixXi(8, 12)) );
  81. CALL_SUBTEST_6( adjoint(MatrixXf(21, 21)) );
  82. }
  83. // test a large matrix only once
  84. CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) );
  85. }