1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2009 Benoit Jacob <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li
10*bf2c3715SXin Li #include "main.h"
11*bf2c3715SXin Li #include <Eigen/LU>
12*bf2c3715SXin Li #include <algorithm>
13*bf2c3715SXin Li
inverse_permutation_4x4()14*bf2c3715SXin Li template<typename MatrixType> void inverse_permutation_4x4()
15*bf2c3715SXin Li {
16*bf2c3715SXin Li typedef typename MatrixType::Scalar Scalar;
17*bf2c3715SXin Li Vector4i indices(0,1,2,3);
18*bf2c3715SXin Li for(int i = 0; i < 24; ++i)
19*bf2c3715SXin Li {
20*bf2c3715SXin Li MatrixType m = PermutationMatrix<4>(indices);
21*bf2c3715SXin Li MatrixType inv = m.inverse();
22*bf2c3715SXin Li double error = double( (m*inv-MatrixType::Identity()).norm() / NumTraits<Scalar>::epsilon() );
23*bf2c3715SXin Li EIGEN_DEBUG_VAR(error)
24*bf2c3715SXin Li VERIFY(error == 0.0);
25*bf2c3715SXin Li std::next_permutation(indices.data(),indices.data()+4);
26*bf2c3715SXin Li }
27*bf2c3715SXin Li }
28*bf2c3715SXin Li
inverse_general_4x4(int repeat)29*bf2c3715SXin Li template<typename MatrixType> void inverse_general_4x4(int repeat)
30*bf2c3715SXin Li {
31*bf2c3715SXin Li using std::abs;
32*bf2c3715SXin Li typedef typename MatrixType::Scalar Scalar;
33*bf2c3715SXin Li double error_sum = 0., error_max = 0.;
34*bf2c3715SXin Li for(int i = 0; i < repeat; ++i)
35*bf2c3715SXin Li {
36*bf2c3715SXin Li MatrixType m;
37*bf2c3715SXin Li bool is_invertible;
38*bf2c3715SXin Li do {
39*bf2c3715SXin Li m = MatrixType::Random();
40*bf2c3715SXin Li is_invertible = Eigen::FullPivLU<MatrixType>(m).isInvertible();
41*bf2c3715SXin Li } while(!is_invertible);
42*bf2c3715SXin Li MatrixType inv = m.inverse();
43*bf2c3715SXin Li double error = double( (m*inv-MatrixType::Identity()).norm());
44*bf2c3715SXin Li error_sum += error;
45*bf2c3715SXin Li error_max = (std::max)(error_max, error);
46*bf2c3715SXin Li }
47*bf2c3715SXin Li std::cerr << "inverse_general_4x4, Scalar = " << type_name<Scalar>() << std::endl;
48*bf2c3715SXin Li double error_avg = error_sum / repeat;
49*bf2c3715SXin Li EIGEN_DEBUG_VAR(error_avg);
50*bf2c3715SXin Li EIGEN_DEBUG_VAR(error_max);
51*bf2c3715SXin Li // FIXME that 1.25 used to be a 1.0 until the NumTraits changes on 28 April 2010, what's going wrong??
52*bf2c3715SXin Li // FIXME that 1.25 used to be 1.2 until we tested gcc 4.1 on 30 June 2010 and got 1.21.
53*bf2c3715SXin Li VERIFY(error_avg < (NumTraits<Scalar>::IsComplex ? 8.0 : 1.25));
54*bf2c3715SXin Li VERIFY(error_max < (NumTraits<Scalar>::IsComplex ? 64.0 : 20.0));
55*bf2c3715SXin Li
56*bf2c3715SXin Li {
57*bf2c3715SXin Li int s = 5;//internal::random<int>(4,10);
58*bf2c3715SXin Li int i = 0;//internal::random<int>(0,s-4);
59*bf2c3715SXin Li int j = 0;//internal::random<int>(0,s-4);
60*bf2c3715SXin Li Matrix<Scalar,5,5> mat(s,s);
61*bf2c3715SXin Li mat.setRandom();
62*bf2c3715SXin Li MatrixType submat = mat.template block<4,4>(i,j);
63*bf2c3715SXin Li MatrixType mat_inv = mat.template block<4,4>(i,j).inverse();
64*bf2c3715SXin Li VERIFY_IS_APPROX(mat_inv, submat.inverse());
65*bf2c3715SXin Li mat.template block<4,4>(i,j) = submat.inverse();
66*bf2c3715SXin Li VERIFY_IS_APPROX(mat_inv, (mat.template block<4,4>(i,j)));
67*bf2c3715SXin Li }
68*bf2c3715SXin Li }
69*bf2c3715SXin Li
EIGEN_DECLARE_TEST(prec_inverse_4x4)70*bf2c3715SXin Li EIGEN_DECLARE_TEST(prec_inverse_4x4)
71*bf2c3715SXin Li {
72*bf2c3715SXin Li CALL_SUBTEST_1((inverse_permutation_4x4<Matrix4f>()));
73*bf2c3715SXin Li CALL_SUBTEST_1(( inverse_general_4x4<Matrix4f>(200000 * g_repeat) ));
74*bf2c3715SXin Li CALL_SUBTEST_1(( inverse_general_4x4<Matrix<float,4,4,RowMajor> >(200000 * g_repeat) ));
75*bf2c3715SXin Li
76*bf2c3715SXin Li CALL_SUBTEST_2((inverse_permutation_4x4<Matrix<double,4,4,RowMajor> >()));
77*bf2c3715SXin Li CALL_SUBTEST_2(( inverse_general_4x4<Matrix<double,4,4,ColMajor> >(200000 * g_repeat) ));
78*bf2c3715SXin Li CALL_SUBTEST_2(( inverse_general_4x4<Matrix<double,4,4,RowMajor> >(200000 * g_repeat) ));
79*bf2c3715SXin Li
80*bf2c3715SXin Li CALL_SUBTEST_3((inverse_permutation_4x4<Matrix4cf>()));
81*bf2c3715SXin Li CALL_SUBTEST_3((inverse_general_4x4<Matrix4cf>(50000 * g_repeat)));
82*bf2c3715SXin Li }
83