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.

183 lines
5.7 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Hauke Heibel <hauke.heibel@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. #include "main.h"
  10. #include <Eigen/Core>
  11. #include <Eigen/Geometry>
  12. #include <Eigen/LU> // required for MatrixBase::determinant
  13. #include <Eigen/SVD> // required for SVD
  14. using namespace Eigen;
  15. // Constructs a random matrix from the unitary group U(size).
  16. template <typename T>
  17. Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> randMatrixUnitary(int size)
  18. {
  19. typedef T Scalar;
  20. typedef typename NumTraits<Scalar>::Real RealScalar;
  21. typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixType;
  22. MatrixType Q;
  23. int max_tries = 40;
  24. double is_unitary = false;
  25. while (!is_unitary && max_tries > 0)
  26. {
  27. // initialize random matrix
  28. Q = MatrixType::Random(size, size);
  29. // orthogonalize columns using the Gram-Schmidt algorithm
  30. for (int col = 0; col < size; ++col)
  31. {
  32. typename MatrixType::ColXpr colVec = Q.col(col);
  33. for (int prevCol = 0; prevCol < col; ++prevCol)
  34. {
  35. typename MatrixType::ColXpr prevColVec = Q.col(prevCol);
  36. colVec -= colVec.dot(prevColVec)*prevColVec;
  37. }
  38. Q.col(col) = colVec.normalized();
  39. }
  40. // this additional orthogonalization is not necessary in theory but should enhance
  41. // the numerical orthogonality of the matrix
  42. for (int row = 0; row < size; ++row)
  43. {
  44. typename MatrixType::RowXpr rowVec = Q.row(row);
  45. for (int prevRow = 0; prevRow < row; ++prevRow)
  46. {
  47. typename MatrixType::RowXpr prevRowVec = Q.row(prevRow);
  48. rowVec -= rowVec.dot(prevRowVec)*prevRowVec;
  49. }
  50. Q.row(row) = rowVec.normalized();
  51. }
  52. // final check
  53. is_unitary = Q.isUnitary();
  54. --max_tries;
  55. }
  56. if (max_tries == 0)
  57. eigen_assert(false && "randMatrixUnitary: Could not construct unitary matrix!");
  58. return Q;
  59. }
  60. // Constructs a random matrix from the special unitary group SU(size).
  61. template <typename T>
  62. Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> randMatrixSpecialUnitary(int size)
  63. {
  64. typedef T Scalar;
  65. typedef typename NumTraits<Scalar>::Real RealScalar;
  66. typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixType;
  67. // initialize unitary matrix
  68. MatrixType Q = randMatrixUnitary<Scalar>(size);
  69. // tweak the first column to make the determinant be 1
  70. Q.col(0) *= internal::conj(Q.determinant());
  71. return Q;
  72. }
  73. template <typename MatrixType>
  74. void run_test(int dim, int num_elements)
  75. {
  76. typedef typename internal::traits<MatrixType>::Scalar Scalar;
  77. typedef Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixX;
  78. typedef Matrix<Scalar, Eigen::Dynamic, 1> VectorX;
  79. // MUST be positive because in any other case det(cR_t) may become negative for
  80. // odd dimensions!
  81. const Scalar c = internal::abs(internal::random<Scalar>());
  82. MatrixX R = randMatrixSpecialUnitary<Scalar>(dim);
  83. VectorX t = Scalar(50)*VectorX::Random(dim,1);
  84. MatrixX cR_t = MatrixX::Identity(dim+1,dim+1);
  85. cR_t.block(0,0,dim,dim) = c*R;
  86. cR_t.block(0,dim,dim,1) = t;
  87. MatrixX src = MatrixX::Random(dim+1, num_elements);
  88. src.row(dim) = Matrix<Scalar, 1, Dynamic>::Constant(num_elements, Scalar(1));
  89. MatrixX dst = cR_t*src;
  90. MatrixX cR_t_umeyama = umeyama(src.block(0,0,dim,num_elements), dst.block(0,0,dim,num_elements));
  91. const Scalar error = ( cR_t_umeyama*src - dst ).norm() / dst.norm();
  92. VERIFY(error < Scalar(40)*std::numeric_limits<Scalar>::epsilon());
  93. }
  94. template<typename Scalar, int Dimension>
  95. void run_fixed_size_test(int num_elements)
  96. {
  97. typedef Matrix<Scalar, Dimension+1, Dynamic> MatrixX;
  98. typedef Matrix<Scalar, Dimension+1, Dimension+1> HomMatrix;
  99. typedef Matrix<Scalar, Dimension, Dimension> FixedMatrix;
  100. typedef Matrix<Scalar, Dimension, 1> FixedVector;
  101. const int dim = Dimension;
  102. // MUST be positive because in any other case det(cR_t) may become negative for
  103. // odd dimensions!
  104. const Scalar c = internal::abs(internal::random<Scalar>());
  105. FixedMatrix R = randMatrixSpecialUnitary<Scalar>(dim);
  106. FixedVector t = Scalar(50)*FixedVector::Random(dim,1);
  107. HomMatrix cR_t = HomMatrix::Identity(dim+1,dim+1);
  108. cR_t.block(0,0,dim,dim) = c*R;
  109. cR_t.block(0,dim,dim,1) = t;
  110. MatrixX src = MatrixX::Random(dim+1, num_elements);
  111. src.row(dim) = Matrix<Scalar, 1, Dynamic>::Constant(num_elements, Scalar(1));
  112. MatrixX dst = cR_t*src;
  113. Block<MatrixX, Dimension, Dynamic> src_block(src,0,0,dim,num_elements);
  114. Block<MatrixX, Dimension, Dynamic> dst_block(dst,0,0,dim,num_elements);
  115. HomMatrix cR_t_umeyama = umeyama(src_block, dst_block);
  116. const Scalar error = ( cR_t_umeyama*src - dst ).array().square().sum();
  117. VERIFY(error < Scalar(10)*std::numeric_limits<Scalar>::epsilon());
  118. }
  119. void test_umeyama()
  120. {
  121. for (int i=0; i<g_repeat; ++i)
  122. {
  123. const int num_elements = internal::random<int>(40,500);
  124. // works also for dimensions bigger than 3...
  125. for (int dim=2; dim<8; ++dim)
  126. {
  127. CALL_SUBTEST_1(run_test<MatrixXd>(dim, num_elements));
  128. CALL_SUBTEST_2(run_test<MatrixXf>(dim, num_elements));
  129. }
  130. CALL_SUBTEST_3((run_fixed_size_test<float, 2>(num_elements)));
  131. CALL_SUBTEST_4((run_fixed_size_test<float, 3>(num_elements)));
  132. CALL_SUBTEST_5((run_fixed_size_test<float, 4>(num_elements)));
  133. CALL_SUBTEST_6((run_fixed_size_test<double, 2>(num_elements)));
  134. CALL_SUBTEST_7((run_fixed_size_test<double, 3>(num_elements)));
  135. CALL_SUBTEST_8((run_fixed_size_test<double, 4>(num_elements)));
  136. }
  137. // Those two calls don't compile and result in meaningful error messages!
  138. // umeyama(MatrixXcf(),MatrixXcf());
  139. // umeyama(MatrixXcd(),MatrixXcd());
  140. }