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.

136 lines
4.3 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2012 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, static_cast<T>(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, static_cast<T>(tol)));
  43. }
  44. }
  45. template<typename MatrixType>
  46. void testExponentLaws(const MatrixType& m, double tol)
  47. {
  48. typedef typename MatrixType::RealScalar RealScalar;
  49. MatrixType m1, m2, m3, m4, m5;
  50. RealScalar x, y;
  51. for (int i=0; i<g_repeat; ++i) {
  52. generateTestMatrix<MatrixType>::run(m1, m.rows());
  53. MatrixPower<MatrixType> mpow(m1);
  54. x = internal::random<RealScalar>();
  55. y = internal::random<RealScalar>();
  56. m2 = mpow(x);
  57. m3 = mpow(y);
  58. m4 = mpow(x+y);
  59. m5.noalias() = m2 * m3;
  60. VERIFY(m4.isApprox(m5, static_cast<RealScalar>(tol)));
  61. m4 = mpow(x*y);
  62. m5 = m2.pow(y);
  63. VERIFY(m4.isApprox(m5, static_cast<RealScalar>(tol)));
  64. m4 = (std::abs(x) * m1).pow(y);
  65. m5 = std::pow(std::abs(x), y) * m3;
  66. VERIFY(m4.isApprox(m5, static_cast<RealScalar>(tol)));
  67. }
  68. }
  69. template<typename MatrixType, typename VectorType>
  70. void testProduct(const MatrixType& m, const VectorType& v, double tol)
  71. {
  72. typedef typename MatrixType::RealScalar RealScalar;
  73. MatrixType m1;
  74. VectorType v1, v2, v3;
  75. RealScalar p;
  76. for (int i=0; i<g_repeat; ++i) {
  77. generateTestMatrix<MatrixType>::run(m1, m.rows());
  78. MatrixPower<MatrixType> mpow(m1);
  79. v1 = VectorType::Random(v.rows(), v.cols());
  80. p = internal::random<RealScalar>();
  81. v2.noalias() = mpow(p) * v1;
  82. v3.noalias() = mpow(p).eval() * v1;
  83. std::cout << "testMatrixVectorProduct: error powerm = " << relerr(v2, v3) << '\n';
  84. VERIFY(v2.isApprox(v3, static_cast<RealScalar>(tol)));
  85. }
  86. }
  87. template<typename MatrixType, typename VectorType>
  88. void testMatrixVector(const MatrixType& m, const VectorType& v, double tol)
  89. {
  90. testExponentLaws(m,tol);
  91. testProduct(m,v,tol);
  92. }
  93. void test_matrix_power()
  94. {
  95. typedef Matrix<double,3,3,RowMajor> Matrix3dRowMajor;
  96. typedef Matrix<long double,Dynamic,Dynamic> MatrixXe;
  97. typedef Matrix<long double,Dynamic,1> VectorXe;
  98. CALL_SUBTEST_2(test2dRotation<double>(1e-13));
  99. CALL_SUBTEST_1(test2dRotation<float>(2e-5)); // was 1e-5, relaxed for clang 2.8 / linux / x86-64
  100. CALL_SUBTEST_9(test2dRotation<long double>(1e-13));
  101. CALL_SUBTEST_2(test2dHyperbolicRotation<double>(1e-14));
  102. CALL_SUBTEST_1(test2dHyperbolicRotation<float>(1e-5));
  103. CALL_SUBTEST_9(test2dHyperbolicRotation<long double>(1e-14));
  104. CALL_SUBTEST_2(testMatrixVector(Matrix2d(), Vector2d(), 1e-13));
  105. CALL_SUBTEST_7(testMatrixVector(Matrix3dRowMajor(), MatrixXd(3,5), 1e-13));
  106. CALL_SUBTEST_3(testMatrixVector(Matrix4cd(), Vector4cd(), 1e-13));
  107. CALL_SUBTEST_4(testMatrixVector(MatrixXd(8,8), VectorXd(8), 1e-13));
  108. CALL_SUBTEST_1(testMatrixVector(Matrix2f(), Vector2f(), 1e-4));
  109. CALL_SUBTEST_5(testMatrixVector(Matrix3cf(), Vector3cf(), 1e-4));
  110. CALL_SUBTEST_8(testMatrixVector(Matrix4f(), Vector4f(), 1e-4));
  111. CALL_SUBTEST_6(testMatrixVector(MatrixXf(8,8), VectorXf(8), 1e-4));
  112. CALL_SUBTEST_9(testMatrixVector(MatrixXe(7,7), VectorXe(7), 1e-13));
  113. }