mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Clang-format tests, examples, libraries, benchmarks, etc.
This commit is contained in:
committed by
Rasmus Munk Larsen
parent
3252ecc7a4
commit
46e9cdb7fe
@@ -12,15 +12,14 @@
|
||||
#include <Eigen/Core>
|
||||
#include <Eigen/Geometry>
|
||||
|
||||
#include <Eigen/LU> // required for MatrixBase::determinant
|
||||
#include <Eigen/SVD> // required for SVD
|
||||
#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)
|
||||
{
|
||||
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> randMatrixUnitary(int size) {
|
||||
typedef T Scalar;
|
||||
typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixType;
|
||||
|
||||
@@ -29,32 +28,27 @@ Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> randMatrixUnitary(int size)
|
||||
int max_tries = 40;
|
||||
bool is_unitary = false;
|
||||
|
||||
while (!is_unitary && max_tries > 0)
|
||||
{
|
||||
while (!is_unitary && max_tries > 0) {
|
||||
// initialize random matrix
|
||||
Q = MatrixType::Random(size, size);
|
||||
|
||||
// orthogonalize columns using the Gram-Schmidt algorithm
|
||||
for (int col = 0; col < size; ++col)
|
||||
{
|
||||
for (int col = 0; col < size; ++col) {
|
||||
typename MatrixType::ColXpr colVec = Q.col(col);
|
||||
for (int prevCol = 0; prevCol < col; ++prevCol)
|
||||
{
|
||||
for (int prevCol = 0; prevCol < col; ++prevCol) {
|
||||
typename MatrixType::ColXpr prevColVec = Q.col(prevCol);
|
||||
colVec -= colVec.dot(prevColVec)*prevColVec;
|
||||
colVec -= colVec.dot(prevColVec) * prevColVec;
|
||||
}
|
||||
Q.col(col) = colVec.normalized();
|
||||
}
|
||||
|
||||
// this additional orthogonalization is not necessary in theory but should enhance
|
||||
// the numerical orthogonality of the matrix
|
||||
for (int row = 0; row < size; ++row)
|
||||
{
|
||||
for (int row = 0; row < size; ++row) {
|
||||
typename MatrixType::RowXpr rowVec = Q.row(row);
|
||||
for (int prevRow = 0; prevRow < row; ++prevRow)
|
||||
{
|
||||
for (int prevRow = 0; prevRow < row; ++prevRow) {
|
||||
typename MatrixType::RowXpr prevRowVec = Q.row(prevRow);
|
||||
rowVec -= rowVec.dot(prevRowVec)*prevRowVec;
|
||||
rowVec -= rowVec.dot(prevRowVec) * prevRowVec;
|
||||
}
|
||||
Q.row(row) = rowVec.normalized();
|
||||
}
|
||||
@@ -64,16 +58,14 @@ Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> randMatrixUnitary(int size)
|
||||
--max_tries;
|
||||
}
|
||||
|
||||
if (max_tries == 0)
|
||||
eigen_assert(false && "randMatrixUnitary: Could not construct unitary matrix!");
|
||||
if (max_tries == 0) eigen_assert(false && "randMatrixUnitary: Could not construct unitary matrix!");
|
||||
|
||||
return Q;
|
||||
}
|
||||
|
||||
// Constructs a random matrix from the special unitary group SU(size).
|
||||
template <typename T>
|
||||
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> randMatrixSpecialUnitary(int size)
|
||||
{
|
||||
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> randMatrixSpecialUnitary(int size) {
|
||||
typedef T Scalar;
|
||||
|
||||
typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixType;
|
||||
@@ -88,8 +80,7 @@ Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> randMatrixSpecialUnitary(int si
|
||||
}
|
||||
|
||||
template <typename MatrixType>
|
||||
void run_test(int dim, int num_elements)
|
||||
{
|
||||
void run_test(int dim, int num_elements) {
|
||||
using std::abs;
|
||||
typedef typename internal::traits<MatrixType>::Scalar Scalar;
|
||||
typedef Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixX;
|
||||
@@ -100,29 +91,28 @@ void run_test(int dim, int num_elements)
|
||||
const Scalar c = abs(internal::random<Scalar>());
|
||||
|
||||
MatrixX R = randMatrixSpecialUnitary<Scalar>(dim);
|
||||
VectorX t = Scalar(50)*VectorX::Random(dim,1);
|
||||
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 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);
|
||||
MatrixX src = MatrixX::Random(dim + 1, num_elements);
|
||||
src.row(dim) = Matrix<Scalar, 1, Dynamic>::Constant(num_elements, Scalar(1));
|
||||
|
||||
MatrixX dst = cR_t*src;
|
||||
MatrixX dst = cR_t * src;
|
||||
|
||||
MatrixX cR_t_umeyama = umeyama(src.block(0,0,dim,num_elements), dst.block(0,0,dim,num_elements));
|
||||
MatrixX cR_t_umeyama = umeyama(src.block(0, 0, dim, num_elements), dst.block(0, 0, dim, num_elements));
|
||||
|
||||
const Scalar error = ( cR_t_umeyama*src - dst ).norm() / dst.norm();
|
||||
VERIFY(error < Scalar(40)*std::numeric_limits<Scalar>::epsilon());
|
||||
const Scalar error = (cR_t_umeyama * src - dst).norm() / dst.norm();
|
||||
VERIFY(error < Scalar(40) * std::numeric_limits<Scalar>::epsilon());
|
||||
}
|
||||
|
||||
template<typename Scalar, int Dimension>
|
||||
void run_fixed_size_test(int num_elements)
|
||||
{
|
||||
template <typename Scalar, int Dimension>
|
||||
void run_fixed_size_test(int num_elements) {
|
||||
using std::abs;
|
||||
typedef Matrix<Scalar, Dimension+1, Dynamic> MatrixX;
|
||||
typedef Matrix<Scalar, Dimension+1, Dimension+1> HomMatrix;
|
||||
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;
|
||||
|
||||
@@ -134,36 +124,33 @@ void run_fixed_size_test(int num_elements)
|
||||
const Scalar c = internal::random<Scalar>(0.5, 2.0);
|
||||
|
||||
FixedMatrix R = randMatrixSpecialUnitary<Scalar>(dim);
|
||||
FixedVector t = Scalar(32)*FixedVector::Random(dim,1);
|
||||
FixedVector t = Scalar(32) * FixedVector::Random(dim, 1);
|
||||
|
||||
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;
|
||||
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);
|
||||
MatrixX src = MatrixX::Random(dim + 1, num_elements);
|
||||
src.row(dim) = Matrix<Scalar, 1, Dynamic>::Constant(num_elements, Scalar(1));
|
||||
|
||||
MatrixX dst = cR_t*src;
|
||||
MatrixX dst = cR_t * src;
|
||||
|
||||
Block<MatrixX, Dimension, Dynamic> src_block(src,0,0,dim,num_elements);
|
||||
Block<MatrixX, Dimension, Dynamic> dst_block(dst,0,0,dim,num_elements);
|
||||
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);
|
||||
|
||||
const Scalar error = ( cR_t_umeyama*src - dst ).squaredNorm();
|
||||
const Scalar error = (cR_t_umeyama * src - dst).squaredNorm();
|
||||
|
||||
VERIFY(error < Scalar(16)*std::numeric_limits<Scalar>::epsilon());
|
||||
VERIFY(error < Scalar(16) * std::numeric_limits<Scalar>::epsilon());
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(umeyama)
|
||||
{
|
||||
for (int i=0; i<g_repeat; ++i)
|
||||
{
|
||||
const int num_elements = internal::random<int>(40,500);
|
||||
EIGEN_DECLARE_TEST(umeyama) {
|
||||
for (int i = 0; i < g_repeat; ++i) {
|
||||
const int num_elements = internal::random<int>(40, 500);
|
||||
|
||||
// works also for dimensions bigger than 3...
|
||||
for (int dim=2; dim<8; ++dim)
|
||||
{
|
||||
for (int dim = 2; dim < 8; ++dim) {
|
||||
CALL_SUBTEST_1(run_test<MatrixXd>(dim, num_elements));
|
||||
CALL_SUBTEST_2(run_test<MatrixXf>(dim, num_elements));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user