1// This file is part of Eigen, a lightweight C++ template library 2// for linear algebra. 3// 4// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@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 10#include "main.h" 11#include <Eigen/LU> 12#include <algorithm> 13 14template<typename T> std::string type_name() { return "other"; } 15template<> std::string type_name<float>() { return "float"; } 16template<> std::string type_name<double>() { return "double"; } 17template<> std::string type_name<int>() { return "int"; } 18template<> std::string type_name<std::complex<float> >() { return "complex<float>"; } 19template<> std::string type_name<std::complex<double> >() { return "complex<double>"; } 20template<> std::string type_name<std::complex<int> >() { return "complex<int>"; } 21 22#define EIGEN_DEBUG_VAR(x) std::cerr << #x << " = " << x << std::endl; 23 24template<typename T> inline typename NumTraits<T>::Real epsilon() 25{ 26 return std::numeric_limits<typename NumTraits<T>::Real>::epsilon(); 27} 28 29template<typename MatrixType> void inverse_permutation_4x4() 30{ 31 typedef typename MatrixType::Scalar Scalar; 32 typedef typename MatrixType::RealScalar RealScalar; 33 Vector4i indices(0,1,2,3); 34 for(int i = 0; i < 24; ++i) 35 { 36 MatrixType m = MatrixType::Zero(); 37 m(indices(0),0) = 1; 38 m(indices(1),1) = 1; 39 m(indices(2),2) = 1; 40 m(indices(3),3) = 1; 41 MatrixType inv = m.inverse(); 42 double error = double( (m*inv-MatrixType::Identity()).norm() / epsilon<Scalar>() ); 43 VERIFY(error == 0.0); 44 std::next_permutation(indices.data(),indices.data()+4); 45 } 46} 47 48template<typename MatrixType> void inverse_general_4x4(int repeat) 49{ 50 typedef typename MatrixType::Scalar Scalar; 51 typedef typename MatrixType::RealScalar RealScalar; 52 double error_sum = 0., error_max = 0.; 53 for(int i = 0; i < repeat; ++i) 54 { 55 MatrixType m; 56 RealScalar absdet; 57 do { 58 m = MatrixType::Random(); 59 absdet = ei_abs(m.determinant()); 60 } while(absdet < 10 * epsilon<Scalar>()); 61 MatrixType inv = m.inverse(); 62 double error = double( (m*inv-MatrixType::Identity()).norm() * absdet / epsilon<Scalar>() ); 63 error_sum += error; 64 error_max = std::max(error_max, error); 65 } 66 std::cerr << "inverse_general_4x4, Scalar = " << type_name<Scalar>() << std::endl; 67 double error_avg = error_sum / repeat; 68 EIGEN_DEBUG_VAR(error_avg); 69 EIGEN_DEBUG_VAR(error_max); 70 VERIFY(error_avg < (NumTraits<Scalar>::IsComplex ? 8.0 : 1.25)); 71 VERIFY(error_max < (NumTraits<Scalar>::IsComplex ? 64.0 : 20.0)); 72} 73 74void test_eigen2_prec_inverse_4x4() 75{ 76 CALL_SUBTEST_1((inverse_permutation_4x4<Matrix4f>())); 77 CALL_SUBTEST_1(( inverse_general_4x4<Matrix4f>(200000 * g_repeat) )); 78 79 CALL_SUBTEST_2((inverse_permutation_4x4<Matrix<double,4,4,RowMajor> >())); 80 CALL_SUBTEST_2(( inverse_general_4x4<Matrix<double,4,4,RowMajor> >(200000 * g_repeat) )); 81 82 CALL_SUBTEST_3((inverse_permutation_4x4<Matrix4cf>())); 83 CALL_SUBTEST_3((inverse_general_4x4<Matrix4cf>(50000 * g_repeat))); 84} 85