eigen2_prec_inverse_4x4.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "main.h"
00026 #include <Eigen/LU>
00027 #include <algorithm>
00028 
00029 template<typename T> std::string type_name() { return "other"; }
00030 template<> std::string type_name<float>() { return "float"; }
00031 template<> std::string type_name<double>() { return "double"; }
00032 template<> std::string type_name<int>() { return "int"; }
00033 template<> std::string type_name<std::complex<float> >() { return "complex<float>"; }
00034 template<> std::string type_name<std::complex<double> >() { return "complex<double>"; }
00035 template<> std::string type_name<std::complex<int> >() { return "complex<int>"; }
00036 
00037 #define EIGEN_DEBUG_VAR(x) std::cerr << #x << " = " << x << std::endl;
00038 
00039 template<typename T> inline typename NumTraits<T>::Real epsilon()
00040 {
00041  return std::numeric_limits<typename NumTraits<T>::Real>::epsilon();
00042 }
00043 
00044 template<typename MatrixType> void inverse_permutation_4x4()
00045 {
00046   typedef typename MatrixType::Scalar Scalar;
00047   typedef typename MatrixType::RealScalar RealScalar;
00048   Vector4i indices(0,1,2,3);
00049   for(int i = 0; i < 24; ++i)
00050   {
00051     MatrixType m = MatrixType::Zero();
00052     m(indices(0),0) = 1;
00053     m(indices(1),1) = 1;
00054     m(indices(2),2) = 1;
00055     m(indices(3),3) = 1;
00056     MatrixType inv = m.inverse();
00057     double error = double( (m*inv-MatrixType::Identity()).norm() / epsilon<Scalar>() );
00058     VERIFY(error == 0.0);
00059     std::next_permutation(indices.data(),indices.data()+4);
00060   }
00061 }
00062 
00063 template<typename MatrixType> void inverse_general_4x4(int repeat)
00064 {
00065   typedef typename MatrixType::Scalar Scalar;
00066   typedef typename MatrixType::RealScalar RealScalar;
00067   double error_sum = 0., error_max = 0.;
00068   for(int i = 0; i < repeat; ++i)
00069   {
00070     MatrixType m;
00071     RealScalar absdet;
00072     do {
00073       m = MatrixType::Random();
00074       absdet = ei_abs(m.determinant());
00075     } while(absdet < 10 * epsilon<Scalar>());
00076     MatrixType inv = m.inverse();
00077     double error = double( (m*inv-MatrixType::Identity()).norm() * absdet / epsilon<Scalar>() );
00078     error_sum += error;
00079     error_max = std::max(error_max, error);
00080   }
00081   std::cerr << "inverse_general_4x4, Scalar = " << type_name<Scalar>() << std::endl;
00082   double error_avg = error_sum / repeat;
00083   EIGEN_DEBUG_VAR(error_avg);
00084   EIGEN_DEBUG_VAR(error_max);
00085   VERIFY(error_avg < (NumTraits<Scalar>::IsComplex ? 8.0 : 1.25));
00086   VERIFY(error_max < (NumTraits<Scalar>::IsComplex ? 64.0 : 20.0));
00087 }
00088 
00089 void test_eigen2_prec_inverse_4x4()
00090 {
00091   CALL_SUBTEST_1((inverse_permutation_4x4<Matrix4f>()));
00092   CALL_SUBTEST_1(( inverse_general_4x4<Matrix4f>(200000 * g_repeat) ));
00093 
00094   CALL_SUBTEST_2((inverse_permutation_4x4<Matrix<double,4,4,RowMajor> >()));
00095   CALL_SUBTEST_2(( inverse_general_4x4<Matrix<double,4,4,RowMajor> >(200000 * g_repeat) ));
00096 
00097   CALL_SUBTEST_3((inverse_permutation_4x4<Matrix4cf>()));
00098   CALL_SUBTEST_3((inverse_general_4x4<Matrix4cf>(50000 * g_repeat)));
00099 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:04