| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | // This file is part of Eigen, a lightweight C++ template library
 | 
					
						
							| 
									
										
										
										
											2012-07-15 22:33:40 +08:00
										 |  |  | // for linear algebra.
 | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Copyright (C) 2009 Hauke Heibel <hauke.heibel@gmail.com>
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2012-07-14 02:42:47 +08:00
										 |  |  | // This Source Code Form is subject to the terms of the Mozilla
 | 
					
						
							|  |  |  | // Public License v. 2.0. If a copy of the MPL was not distributed
 | 
					
						
							|  |  |  | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "main.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <Eigen/Core>
 | 
					
						
							|  |  |  | #include <Eigen/Geometry>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <Eigen/LU>   // required for MatrixBase::determinant
 | 
					
						
							|  |  |  | #include <Eigen/SVD>  // required for SVD
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace Eigen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  Constructs a random matrix from the unitary group U(size).
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							|  |  |  | Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> randMatrixUnitary(int size) { | 
					
						
							| 
									
										
										
										
											2009-05-28 05:10:24 +08:00
										 |  |  |   typedef T Scalar; | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  |   typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   MatrixType Q; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   int max_tries = 40; | 
					
						
							| 
									
										
										
										
											2018-11-23 22:12:06 +08:00
										 |  |  |   bool is_unitary = false; | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   while (!is_unitary && max_tries > 0) { | 
					
						
							| 
									
										
										
										
											2009-05-28 05:10:24 +08:00
										 |  |  |     // initialize random matrix
 | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  |     Q = MatrixType::Random(size, size); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-28 05:10:24 +08:00
										 |  |  |     // orthogonalize columns using the Gram-Schmidt algorithm
 | 
					
						
							|  |  |  |     for (int col = 0; col < size; ++col) { | 
					
						
							|  |  |  |       typename MatrixType::ColXpr colVec = Q.col(col); | 
					
						
							|  |  |  |       for (int prevCol = 0; prevCol < col; ++prevCol) { | 
					
						
							|  |  |  |         typename MatrixType::ColXpr prevColVec = Q.col(prevCol); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  |         colVec -= colVec.dot(prevColVec) * prevColVec; | 
					
						
							| 
									
										
										
										
											2009-05-28 05:10:24 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |       Q.col(col) = colVec.normalized(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-17 17:43:46 +08:00
										 |  |  |     // this additional orthogonalization is not necessary in theory but should enhance
 | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  |     // the numerical orthogonality of the matrix
 | 
					
						
							| 
									
										
										
										
											2009-05-28 05:10:24 +08:00
										 |  |  |     for (int row = 0; row < size; ++row) { | 
					
						
							|  |  |  |       typename MatrixType::RowXpr rowVec = Q.row(row); | 
					
						
							|  |  |  |       for (int prevRow = 0; prevRow < row; ++prevRow) { | 
					
						
							|  |  |  |         typename MatrixType::RowXpr prevRowVec = Q.row(prevRow); | 
					
						
							|  |  |  |         rowVec -= rowVec.dot(prevRowVec) * prevRowVec; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       Q.row(row) = rowVec.normalized(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // final check
 | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  |     is_unitary = Q.isUnitary(); | 
					
						
							|  |  |  |     --max_tries; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-25 22:15:22 +08:00
										 |  |  |   if (max_tries == 0) eigen_assert(false && "randMatrixUnitary: Could not construct unitary matrix!"); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-28 05:10:24 +08:00
										 |  |  |   return Q; | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  Constructs a random matrix from the special unitary group SU(size).
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							|  |  |  | Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> randMatrixSpecialUnitary(int size) { | 
					
						
							| 
									
										
										
										
											2009-05-28 05:10:24 +08:00
										 |  |  |   typedef T Scalar; | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixType; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-28 05:10:24 +08:00
										 |  |  |   // initialize unitary matrix
 | 
					
						
							|  |  |  |   MatrixType Q = randMatrixUnitary<Scalar>(size); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-28 05:10:24 +08:00
										 |  |  |   // tweak the first column to make the determinant be 1
 | 
					
						
							| 
									
										
										
										
											2013-06-11 05:40:56 +08:00
										 |  |  |   Q.col(0) *= numext::conj(Q.determinant()); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return Q; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename MatrixType> | 
					
						
							|  |  |  | void run_test(int dim, int num_elements) { | 
					
						
							| 
									
										
										
										
											2012-11-06 22:25:50 +08:00
										 |  |  |   using std::abs; | 
					
						
							| 
									
										
										
										
											2010-10-25 22:15:22 +08:00
										 |  |  |   typedef typename internal::traits<MatrixType>::Scalar Scalar; | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  |   typedef Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixX; | 
					
						
							|  |  |  |   typedef Matrix<Scalar, Eigen::Dynamic, 1> VectorX; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // MUST be positive because in any other case det(cR_t) may become negative for
 | 
					
						
							|  |  |  |   // odd dimensions!
 | 
					
						
							| 
									
										
										
										
											2012-11-06 22:25:50 +08:00
										 |  |  |   const Scalar c = abs(internal::random<Scalar>()); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   MatrixX R = randMatrixSpecialUnitary<Scalar>(dim); | 
					
						
							|  |  |  |   VectorX t = Scalar(50) * VectorX::Random(dim, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   MatrixX cR_t = MatrixX::Identity(dim + 1, dim + 1); | 
					
						
							|  |  |  |   cR_t.block(0, 0, dim, dim) = c * R; | 
					
						
							|  |  |  |   cR_t.block(0, dim, dim, 1) = t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   MatrixX src = MatrixX::Random(dim + 1, num_elements); | 
					
						
							|  |  |  |   src.row(dim) = Matrix<Scalar, 1, Dynamic>::Constant(num_elements, Scalar(1)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 21:34:57 +08:00
										 |  |  |   MatrixX dst = cR_t * src; | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   MatrixX cR_t_umeyama = umeyama(src.block(0, 0, dim, num_elements), dst.block(0, 0, dim, num_elements)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-28 12:20:45 +08:00
										 |  |  |   const Scalar error = (cR_t_umeyama * src - dst).norm() / dst.norm(); | 
					
						
							|  |  |  |   VERIFY(error < Scalar(40) * std::numeric_limits<Scalar>::epsilon()); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename Scalar, int Dimension> | 
					
						
							|  |  |  | void run_fixed_size_test(int num_elements) { | 
					
						
							| 
									
										
										
										
											2012-11-06 22:25:50 +08:00
										 |  |  |   using std::abs; | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  |   typedef Matrix<Scalar, Dimension + 1, Dynamic> MatrixX; | 
					
						
							|  |  |  |   typedef Matrix<Scalar, Dimension + 1, Dimension + 1> HomMatrix; | 
					
						
							|  |  |  |   typedef Matrix<Scalar, Dimension, Dimension> FixedMatrix; | 
					
						
							|  |  |  |   typedef Matrix<Scalar, Dimension, 1> FixedVector; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const int dim = Dimension; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // MUST be positive because in any other case det(cR_t) may become negative for
 | 
					
						
							|  |  |  |   // odd dimensions!
 | 
					
						
							| 
									
										
										
										
											2014-02-17 20:48:00 +08:00
										 |  |  |   // Also if c is to small compared to t.norm(), problem is ill-posed (cf. Bug 744)
 | 
					
						
							|  |  |  |   const Scalar c = internal::random<Scalar>(0.5, 2.0); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   FixedMatrix R = randMatrixSpecialUnitary<Scalar>(dim); | 
					
						
							| 
									
										
										
										
											2014-02-17 20:48:00 +08:00
										 |  |  |   FixedVector t = Scalar(32) * FixedVector::Random(dim, 1); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   HomMatrix cR_t = HomMatrix::Identity(dim + 1, dim + 1); | 
					
						
							|  |  |  |   cR_t.block(0, 0, dim, dim) = c * R; | 
					
						
							|  |  |  |   cR_t.block(0, dim, dim, 1) = t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   MatrixX src = MatrixX::Random(dim + 1, num_elements); | 
					
						
							|  |  |  |   src.row(dim) = Matrix<Scalar, 1, Dynamic>::Constant(num_elements, Scalar(1)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 21:34:57 +08:00
										 |  |  |   MatrixX dst = cR_t * src; | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-28 01:24:05 +08:00
										 |  |  |   Block<MatrixX, Dimension, Dynamic> src_block(src, 0, 0, dim, num_elements); | 
					
						
							|  |  |  |   Block<MatrixX, Dimension, Dynamic> dst_block(dst, 0, 0, dim, num_elements); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   HomMatrix cR_t_umeyama = umeyama(src_block, dst_block); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 20:48:00 +08:00
										 |  |  |   const Scalar error = (cR_t_umeyama * src - dst).squaredNorm(); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 20:48:00 +08:00
										 |  |  |   VERIFY(error < Scalar(16) * std::numeric_limits<Scalar>::epsilon()); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-17 20:46:15 +08:00
										 |  |  | EIGEN_DECLARE_TEST(umeyama) { | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  |   for (int i = 0; i < g_repeat; ++i) { | 
					
						
							| 
									
										
										
										
											2010-10-25 22:15:22 +08:00
										 |  |  |     const int num_elements = internal::random<int>(40, 500); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // works also for dimensions bigger than 3...
 | 
					
						
							|  |  |  |     for (int dim = 2; dim < 8; ++dim) { | 
					
						
							| 
									
										
										
										
											2009-10-29 06:19:29 +08:00
										 |  |  |       CALL_SUBTEST_1(run_test<MatrixXd>(dim, num_elements)); | 
					
						
							|  |  |  |       CALL_SUBTEST_2(run_test<MatrixXf>(dim, num_elements)); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 06:19:29 +08:00
										 |  |  |     CALL_SUBTEST_3((run_fixed_size_test<float, 2>(num_elements))); | 
					
						
							|  |  |  |     CALL_SUBTEST_4((run_fixed_size_test<float, 3>(num_elements))); | 
					
						
							|  |  |  |     CALL_SUBTEST_5((run_fixed_size_test<float, 4>(num_elements))); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 06:19:29 +08:00
										 |  |  |     CALL_SUBTEST_6((run_fixed_size_test<double, 2>(num_elements))); | 
					
						
							|  |  |  |     CALL_SUBTEST_7((run_fixed_size_test<double, 3>(num_elements))); | 
					
						
							|  |  |  |     CALL_SUBTEST_8((run_fixed_size_test<double, 4>(num_elements))); | 
					
						
							| 
									
										
										
										
											2009-05-27 01:22:25 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Those two calls don't compile and result in meaningful error messages!
 | 
					
						
							|  |  |  |   // umeyama(MatrixXcf(),MatrixXcf());
 | 
					
						
							|  |  |  |   // umeyama(MatrixXcd(),MatrixXcd());
 | 
					
						
							|  |  |  | } |