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