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.

141 lines
5.6 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  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. #define EIGEN_NO_STATIC_ASSERT
  10. #include "main.h"
  11. template<typename MatrixType> void adjoint(const MatrixType& m)
  12. {
  13. /* this test covers the following files:
  14. Transpose.h Conjugate.h Dot.h
  15. */
  16. typedef typename MatrixType::Index Index;
  17. typedef typename MatrixType::Scalar Scalar;
  18. typedef typename NumTraits<Scalar>::Real RealScalar;
  19. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  20. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
  21. Index rows = m.rows();
  22. Index cols = m.cols();
  23. MatrixType m1 = MatrixType::Random(rows, cols),
  24. m2 = MatrixType::Random(rows, cols),
  25. m3(rows, cols),
  26. square = SquareMatrixType::Random(rows, rows);
  27. VectorType v1 = VectorType::Random(rows),
  28. v2 = VectorType::Random(rows),
  29. v3 = VectorType::Random(rows),
  30. vzero = VectorType::Zero(rows);
  31. Scalar s1 = internal::random<Scalar>(),
  32. s2 = internal::random<Scalar>();
  33. // check basic compatibility of adjoint, transpose, conjugate
  34. VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1);
  35. VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1);
  36. // check multiplicative behavior
  37. VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1);
  38. VERIFY_IS_APPROX((s1 * m1).adjoint(), internal::conj(s1) * m1.adjoint());
  39. // check basic properties of dot, norm, norm2
  40. typedef typename NumTraits<Scalar>::Real RealScalar;
  41. RealScalar ref = NumTraits<Scalar>::IsInteger ? RealScalar(0) : (std::max)((s1 * v1 + s2 * v2).norm(),v3.norm());
  42. VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3), internal::conj(s1) * v1.dot(v3) + internal::conj(s2) * v2.dot(v3), ref));
  43. VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2), s1*v3.dot(v1)+s2*v3.dot(v2), ref));
  44. VERIFY_IS_APPROX(internal::conj(v1.dot(v2)), v2.dot(v1));
  45. VERIFY_IS_APPROX(internal::real(v1.dot(v1)), v1.squaredNorm());
  46. if(!NumTraits<Scalar>::IsInteger) {
  47. VERIFY_IS_APPROX(v1.squaredNorm(), v1.norm() * v1.norm());
  48. // check normalized() and normalize()
  49. VERIFY_IS_APPROX(v1, v1.norm() * v1.normalized());
  50. v3 = v1;
  51. v3.normalize();
  52. VERIFY_IS_APPROX(v1, v1.norm() * v3);
  53. VERIFY_IS_APPROX(v3, v1.normalized());
  54. VERIFY_IS_APPROX(v3.norm(), RealScalar(1));
  55. }
  56. VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(vzero.dot(v1)), static_cast<RealScalar>(1));
  57. // check compatibility of dot and adjoint
  58. ref = NumTraits<Scalar>::IsInteger ? 0 : (std::max)((std::max)(v1.norm(),v2.norm()),(std::max)((square * v2).norm(),(square.adjoint() * v1).norm()));
  59. VERIFY(test_isApproxWithRef(v1.dot(square * v2), (square.adjoint() * v1).dot(v2), ref));
  60. // like in testBasicStuff, test operator() to check const-qualification
  61. Index r = internal::random<Index>(0, rows-1),
  62. c = internal::random<Index>(0, cols-1);
  63. VERIFY_IS_APPROX(m1.conjugate()(r,c), internal::conj(m1(r,c)));
  64. VERIFY_IS_APPROX(m1.adjoint()(c,r), internal::conj(m1(r,c)));
  65. if(!NumTraits<Scalar>::IsInteger)
  66. {
  67. // check that Random().normalized() works: tricky as the random xpr must be evaluated by
  68. // normalized() in order to produce a consistent result.
  69. VERIFY_IS_APPROX(VectorType::Random(rows).normalized().norm(), RealScalar(1));
  70. }
  71. // check inplace transpose
  72. m3 = m1;
  73. m3.transposeInPlace();
  74. VERIFY_IS_APPROX(m3,m1.transpose());
  75. m3.transposeInPlace();
  76. VERIFY_IS_APPROX(m3,m1);
  77. // check inplace adjoint
  78. m3 = m1;
  79. m3.adjointInPlace();
  80. VERIFY_IS_APPROX(m3,m1.adjoint());
  81. m3.transposeInPlace();
  82. VERIFY_IS_APPROX(m3,m1.conjugate());
  83. // check mixed dot product
  84. typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
  85. RealVectorType rv1 = RealVectorType::Random(rows);
  86. VERIFY_IS_APPROX(v1.dot(rv1.template cast<Scalar>()), v1.dot(rv1));
  87. VERIFY_IS_APPROX(rv1.template cast<Scalar>().dot(v1), rv1.dot(v1));
  88. }
  89. void test_adjoint()
  90. {
  91. for(int i = 0; i < g_repeat; i++) {
  92. CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) );
  93. CALL_SUBTEST_2( adjoint(Matrix3d()) );
  94. CALL_SUBTEST_3( adjoint(Matrix4f()) );
  95. CALL_SUBTEST_4( adjoint(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  96. CALL_SUBTEST_5( adjoint(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  97. CALL_SUBTEST_6( adjoint(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  98. }
  99. // test a large static matrix only once
  100. CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) );
  101. #ifdef EIGEN_TEST_PART_4
  102. {
  103. MatrixXcf a(10,10), b(10,10);
  104. VERIFY_RAISES_ASSERT(a = a.transpose());
  105. VERIFY_RAISES_ASSERT(a = a.transpose() + b);
  106. VERIFY_RAISES_ASSERT(a = b + a.transpose());
  107. VERIFY_RAISES_ASSERT(a = a.conjugate().transpose());
  108. VERIFY_RAISES_ASSERT(a = a.adjoint());
  109. VERIFY_RAISES_ASSERT(a = a.adjoint() + b);
  110. VERIFY_RAISES_ASSERT(a = b + a.adjoint());
  111. // no assertion should be triggered for these cases:
  112. a.transpose() = a.transpose();
  113. a.transpose() += a.transpose();
  114. a.transpose() += a.transpose() + b;
  115. a.transpose() = a.adjoint();
  116. a.transpose() += a.adjoint();
  117. a.transpose() += a.adjoint() + b;
  118. }
  119. #endif
  120. }