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.

204 lines
6.8 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2012, 2013 Chen-Pang He <jdh8@ms63.hinet.net>
  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. template<typename T>
  11. void test2dRotation(double tol)
  12. {
  13. Matrix<T,2,2> A, B, C;
  14. T angle, c, s;
  15. A << 0, 1, -1, 0;
  16. MatrixPower<Matrix<T,2,2> > Apow(A);
  17. for (int i=0; i<=20; ++i) {
  18. angle = pow(10, (i-10) / 5.);
  19. c = std::cos(angle);
  20. s = std::sin(angle);
  21. B << c, s, -s, c;
  22. C = Apow(std::ldexp(angle,1) / M_PI);
  23. std::cout << "test2dRotation: i = " << i << " error powerm = " << relerr(C,B) << '\n';
  24. VERIFY(C.isApprox(B, tol));
  25. }
  26. }
  27. template<typename T>
  28. void test2dHyperbolicRotation(double tol)
  29. {
  30. Matrix<std::complex<T>,2,2> A, B, C;
  31. T angle, ch = std::cosh((T)1);
  32. std::complex<T> ish(0, std::sinh((T)1));
  33. A << ch, ish, -ish, ch;
  34. MatrixPower<Matrix<std::complex<T>,2,2> > Apow(A);
  35. for (int i=0; i<=20; ++i) {
  36. angle = std::ldexp(static_cast<T>(i-10), -1);
  37. ch = std::cosh(angle);
  38. ish = std::complex<T>(0, std::sinh(angle));
  39. B << ch, ish, -ish, ch;
  40. C = Apow(angle);
  41. std::cout << "test2dHyperbolicRotation: i = " << i << " error powerm = " << relerr(C,B) << '\n';
  42. VERIFY(C.isApprox(B, tol));
  43. }
  44. }
  45. template<typename T>
  46. void test3dRotation(double tol)
  47. {
  48. Matrix<T,3,1> v;
  49. T angle;
  50. for (int i=0; i<=20; ++i) {
  51. v = Matrix<T,3,1>::Random();
  52. v.normalize();
  53. angle = pow(10, (i-10) / 5.);
  54. VERIFY(AngleAxis<T>(angle, v).matrix().isApprox(AngleAxis<T>(1,v).matrix().pow(angle), tol));
  55. }
  56. }
  57. template<typename MatrixType>
  58. void testGeneral(const MatrixType& m, double tol)
  59. {
  60. typedef typename MatrixType::RealScalar RealScalar;
  61. MatrixType m1, m2, m3, m4, m5;
  62. RealScalar x, y;
  63. for (int i=0; i < g_repeat; ++i) {
  64. generateTestMatrix<MatrixType>::run(m1, m.rows());
  65. MatrixPower<MatrixType> mpow(m1);
  66. x = internal::random<RealScalar>();
  67. y = internal::random<RealScalar>();
  68. m2 = mpow(x);
  69. m3 = mpow(y);
  70. m4 = mpow(x+y);
  71. m5.noalias() = m2 * m3;
  72. VERIFY(m4.isApprox(m5, tol));
  73. m4 = mpow(x*y);
  74. m5 = m2.pow(y);
  75. VERIFY(m4.isApprox(m5, tol));
  76. m4 = (std::abs(x) * m1).pow(y);
  77. m5 = std::pow(std::abs(x), y) * m3;
  78. VERIFY(m4.isApprox(m5, tol));
  79. }
  80. }
  81. template<typename MatrixType>
  82. void testSingular(const MatrixType& m_const, double tol)
  83. {
  84. // we need to pass by reference in order to prevent errors with
  85. // MSVC for aligned data types ...
  86. MatrixType& m = const_cast<MatrixType&>(m_const);
  87. const int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex;
  88. typedef typename internal::conditional<IsComplex, TriangularView<MatrixType,Upper>, const MatrixType&>::type TriangularType;
  89. typename internal::conditional< IsComplex, ComplexSchur<MatrixType>, RealSchur<MatrixType> >::type schur;
  90. MatrixType T;
  91. for (int i=0; i < g_repeat; ++i) {
  92. m.setRandom();
  93. m.col(0).fill(0);
  94. schur.compute(m);
  95. T = schur.matrixT();
  96. const MatrixType& U = schur.matrixU();
  97. processTriangularMatrix<MatrixType>::run(m, T, U);
  98. MatrixPower<MatrixType> mpow(m);
  99. T = T.sqrt();
  100. VERIFY(mpow(0.5).isApprox(U * (TriangularType(T) * U.adjoint()), tol));
  101. T = T.sqrt();
  102. VERIFY(mpow(0.25).isApprox(U * (TriangularType(T) * U.adjoint()), tol));
  103. T = T.sqrt();
  104. VERIFY(mpow(0.125).isApprox(U * (TriangularType(T) * U.adjoint()), tol));
  105. }
  106. }
  107. template<typename MatrixType>
  108. void testLogThenExp(const MatrixType& m_const, double tol)
  109. {
  110. // we need to pass by reference in order to prevent errors with
  111. // MSVC for aligned data types ...
  112. MatrixType& m = const_cast<MatrixType&>(m_const);
  113. typedef typename MatrixType::Scalar Scalar;
  114. Scalar x;
  115. for (int i=0; i < g_repeat; ++i) {
  116. generateTestMatrix<MatrixType>::run(m, m.rows());
  117. x = internal::random<Scalar>();
  118. VERIFY(m.pow(x).isApprox((x * m.log()).exp(), tol));
  119. }
  120. }
  121. typedef Matrix<double,3,3,RowMajor> Matrix3dRowMajor;
  122. typedef Matrix<long double,3,3> Matrix3e;
  123. typedef Matrix<long double,Dynamic,Dynamic> MatrixXe;
  124. void test_matrix_power()
  125. {
  126. CALL_SUBTEST_2(test2dRotation<double>(1e-13));
  127. CALL_SUBTEST_1(test2dRotation<float>(2e-5)); // was 1e-5, relaxed for clang 2.8 / linux / x86-64
  128. CALL_SUBTEST_9(test2dRotation<long double>(1e-13));
  129. CALL_SUBTEST_2(test2dHyperbolicRotation<double>(1e-14));
  130. CALL_SUBTEST_1(test2dHyperbolicRotation<float>(1e-5));
  131. CALL_SUBTEST_9(test2dHyperbolicRotation<long double>(1e-14));
  132. CALL_SUBTEST_10(test3dRotation<double>(1e-13));
  133. CALL_SUBTEST_11(test3dRotation<float>(1e-5));
  134. CALL_SUBTEST_12(test3dRotation<long double>(1e-13));
  135. CALL_SUBTEST_2(testGeneral(Matrix2d(), 1e-13));
  136. CALL_SUBTEST_7(testGeneral(Matrix3dRowMajor(), 1e-13));
  137. CALL_SUBTEST_3(testGeneral(Matrix4cd(), 1e-13));
  138. CALL_SUBTEST_4(testGeneral(MatrixXd(8,8), 2e-12));
  139. CALL_SUBTEST_1(testGeneral(Matrix2f(), 1e-4));
  140. CALL_SUBTEST_5(testGeneral(Matrix3cf(), 1e-4));
  141. CALL_SUBTEST_8(testGeneral(Matrix4f(), 1e-4));
  142. CALL_SUBTEST_6(testGeneral(MatrixXf(2,2), 1e-3)); // see bug 614
  143. CALL_SUBTEST_9(testGeneral(MatrixXe(7,7), 1e-13));
  144. CALL_SUBTEST_10(testGeneral(Matrix3d(), 1e-13));
  145. CALL_SUBTEST_11(testGeneral(Matrix3f(), 1e-4));
  146. CALL_SUBTEST_12(testGeneral(Matrix3e(), 1e-13));
  147. CALL_SUBTEST_2(testSingular(Matrix2d(), 1e-13));
  148. CALL_SUBTEST_7(testSingular(Matrix3dRowMajor(), 1e-13));
  149. CALL_SUBTEST_3(testSingular(Matrix4cd(), 1e-13));
  150. CALL_SUBTEST_4(testSingular(MatrixXd(8,8), 2e-12));
  151. CALL_SUBTEST_1(testSingular(Matrix2f(), 1e-4));
  152. CALL_SUBTEST_5(testSingular(Matrix3cf(), 1e-4));
  153. CALL_SUBTEST_8(testSingular(Matrix4f(), 1e-4));
  154. CALL_SUBTEST_6(testSingular(MatrixXf(2,2), 1e-3));
  155. CALL_SUBTEST_9(testSingular(MatrixXe(7,7), 1e-13));
  156. CALL_SUBTEST_10(testSingular(Matrix3d(), 1e-13));
  157. CALL_SUBTEST_11(testSingular(Matrix3f(), 1e-4));
  158. CALL_SUBTEST_12(testSingular(Matrix3e(), 1e-13));
  159. CALL_SUBTEST_2(testLogThenExp(Matrix2d(), 1e-13));
  160. CALL_SUBTEST_7(testLogThenExp(Matrix3dRowMajor(), 1e-13));
  161. CALL_SUBTEST_3(testLogThenExp(Matrix4cd(), 1e-13));
  162. CALL_SUBTEST_4(testLogThenExp(MatrixXd(8,8), 2e-12));
  163. CALL_SUBTEST_1(testLogThenExp(Matrix2f(), 1e-4));
  164. CALL_SUBTEST_5(testLogThenExp(Matrix3cf(), 1e-4));
  165. CALL_SUBTEST_8(testLogThenExp(Matrix4f(), 1e-4));
  166. CALL_SUBTEST_6(testLogThenExp(MatrixXf(2,2), 1e-3));
  167. CALL_SUBTEST_9(testLogThenExp(MatrixXe(7,7), 1e-13));
  168. CALL_SUBTEST_10(testLogThenExp(Matrix3d(), 1e-13));
  169. CALL_SUBTEST_11(testLogThenExp(Matrix3f(), 1e-4));
  170. CALL_SUBTEST_12(testLogThenExp(Matrix3e(), 1e-13));
  171. }