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.

149 lines
4.6 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Jitse Niesen <jitse@maths.leeds.ac.uk>
  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. #include <unsupported/Eigen/MatrixFunctions>
  11. double binom(int n, int k)
  12. {
  13. double res = 1;
  14. for (int i=0; i<k; i++)
  15. res = res * (n-k+i+1) / (i+1);
  16. return res;
  17. }
  18. template <typename Derived, typename OtherDerived>
  19. double relerr(const MatrixBase<Derived>& A, const MatrixBase<OtherDerived>& B)
  20. {
  21. return std::sqrt((A - B).cwiseAbs2().sum() / (std::min)(A.cwiseAbs2().sum(), B.cwiseAbs2().sum()));
  22. }
  23. template <typename T>
  24. T expfn(T x, int)
  25. {
  26. return std::exp(x);
  27. }
  28. template <typename T>
  29. void test2dRotation(double tol)
  30. {
  31. Matrix<T,2,2> A, B, C;
  32. T angle;
  33. A << 0, 1, -1, 0;
  34. for (int i=0; i<=20; i++)
  35. {
  36. angle = static_cast<T>(pow(10, i / 5. - 2));
  37. B << std::cos(angle), std::sin(angle), -std::sin(angle), std::cos(angle);
  38. C = (angle*A).matrixFunction(expfn);
  39. std::cout << "test2dRotation: i = " << i << " error funm = " << relerr(C, B);
  40. VERIFY(C.isApprox(B, static_cast<T>(tol)));
  41. C = (angle*A).exp();
  42. std::cout << " error expm = " << relerr(C, B) << "\n";
  43. VERIFY(C.isApprox(B, static_cast<T>(tol)));
  44. }
  45. }
  46. template <typename T>
  47. void test2dHyperbolicRotation(double tol)
  48. {
  49. Matrix<std::complex<T>,2,2> A, B, C;
  50. std::complex<T> imagUnit(0,1);
  51. T angle, ch, sh;
  52. for (int i=0; i<=20; i++)
  53. {
  54. angle = static_cast<T>((i-10) / 2.0);
  55. ch = std::cosh(angle);
  56. sh = std::sinh(angle);
  57. A << 0, angle*imagUnit, -angle*imagUnit, 0;
  58. B << ch, sh*imagUnit, -sh*imagUnit, ch;
  59. C = A.matrixFunction(expfn);
  60. std::cout << "test2dHyperbolicRotation: i = " << i << " error funm = " << relerr(C, B);
  61. VERIFY(C.isApprox(B, static_cast<T>(tol)));
  62. C = A.exp();
  63. std::cout << " error expm = " << relerr(C, B) << "\n";
  64. VERIFY(C.isApprox(B, static_cast<T>(tol)));
  65. }
  66. }
  67. template <typename T>
  68. void testPascal(double tol)
  69. {
  70. for (int size=1; size<20; size++)
  71. {
  72. Matrix<T,Dynamic,Dynamic> A(size,size), B(size,size), C(size,size);
  73. A.setZero();
  74. for (int i=0; i<size-1; i++)
  75. A(i+1,i) = static_cast<T>(i+1);
  76. B.setZero();
  77. for (int i=0; i<size; i++)
  78. for (int j=0; j<=i; j++)
  79. B(i,j) = static_cast<T>(binom(i,j));
  80. C = A.matrixFunction(expfn);
  81. std::cout << "testPascal: size = " << size << " error funm = " << relerr(C, B);
  82. VERIFY(C.isApprox(B, static_cast<T>(tol)));
  83. C = A.exp();
  84. std::cout << " error expm = " << relerr(C, B) << "\n";
  85. VERIFY(C.isApprox(B, static_cast<T>(tol)));
  86. }
  87. }
  88. template<typename MatrixType>
  89. void randomTest(const MatrixType& m, double tol)
  90. {
  91. /* this test covers the following files:
  92. Inverse.h
  93. */
  94. typename MatrixType::Index rows = m.rows();
  95. typename MatrixType::Index cols = m.cols();
  96. MatrixType m1(rows, cols), m2(rows, cols), m3(rows, cols),
  97. identity = MatrixType::Identity(rows, rows);
  98. typedef typename NumTraits<typename internal::traits<MatrixType>::Scalar>::Real RealScalar;
  99. for(int i = 0; i < g_repeat; i++) {
  100. m1 = MatrixType::Random(rows, cols);
  101. m2 = m1.matrixFunction(expfn) * (-m1).matrixFunction(expfn);
  102. std::cout << "randomTest: error funm = " << relerr(identity, m2);
  103. VERIFY(identity.isApprox(m2, static_cast<RealScalar>(tol)));
  104. m2 = m1.exp() * (-m1).exp();
  105. std::cout << " error expm = " << relerr(identity, m2) << "\n";
  106. VERIFY(identity.isApprox(m2, static_cast<RealScalar>(tol)));
  107. }
  108. }
  109. void test_matrix_exponential()
  110. {
  111. CALL_SUBTEST_2(test2dRotation<double>(1e-13));
  112. CALL_SUBTEST_1(test2dRotation<float>(2e-5)); // was 1e-5, relaxed for clang 2.8 / linux / x86-64
  113. CALL_SUBTEST_8(test2dRotation<long double>(1e-13));
  114. CALL_SUBTEST_2(test2dHyperbolicRotation<double>(1e-14));
  115. CALL_SUBTEST_1(test2dHyperbolicRotation<float>(1e-5));
  116. CALL_SUBTEST_8(test2dHyperbolicRotation<long double>(1e-14));
  117. CALL_SUBTEST_6(testPascal<float>(1e-6));
  118. CALL_SUBTEST_5(testPascal<double>(1e-15));
  119. CALL_SUBTEST_2(randomTest(Matrix2d(), 1e-13));
  120. CALL_SUBTEST_7(randomTest(Matrix<double,3,3,RowMajor>(), 1e-13));
  121. CALL_SUBTEST_3(randomTest(Matrix4cd(), 1e-13));
  122. CALL_SUBTEST_4(randomTest(MatrixXd(8,8), 1e-13));
  123. CALL_SUBTEST_1(randomTest(Matrix2f(), 1e-4));
  124. CALL_SUBTEST_5(randomTest(Matrix3cf(), 1e-4));
  125. CALL_SUBTEST_1(randomTest(Matrix4f(), 1e-4));
  126. CALL_SUBTEST_6(randomTest(MatrixXf(8,8), 1e-4));
  127. CALL_SUBTEST_9(randomTest(Matrix<long double,Dynamic,Dynamic>(7,7), 1e-13));
  128. }