xref: /aosp_15_r20/external/eigen/unsupported/test/matrix_power.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
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) 2012, 2013 Chen-Pang He <[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 "matrix_functions.h"
11*bf2c3715SXin Li 
12*bf2c3715SXin Li template<typename T>
test2dRotation(const T & tol)13*bf2c3715SXin Li void test2dRotation(const T& tol)
14*bf2c3715SXin Li {
15*bf2c3715SXin Li   Matrix<T,2,2> A, B, C;
16*bf2c3715SXin Li   T angle, c, s;
17*bf2c3715SXin Li 
18*bf2c3715SXin Li   A << 0, 1, -1, 0;
19*bf2c3715SXin Li   MatrixPower<Matrix<T,2,2> > Apow(A);
20*bf2c3715SXin Li 
21*bf2c3715SXin Li   for (int i=0; i<=20; ++i) {
22*bf2c3715SXin Li     angle = std::pow(T(10), T(i-10) / T(5.));
23*bf2c3715SXin Li     c = std::cos(angle);
24*bf2c3715SXin Li     s = std::sin(angle);
25*bf2c3715SXin Li     B << c, s, -s, c;
26*bf2c3715SXin Li 
27*bf2c3715SXin Li     C = Apow(std::ldexp(angle,1) / T(EIGEN_PI));
28*bf2c3715SXin Li     std::cout << "test2dRotation: i = " << i << "   error powerm = " << relerr(C,B) << '\n';
29*bf2c3715SXin Li     VERIFY(C.isApprox(B, tol));
30*bf2c3715SXin Li   }
31*bf2c3715SXin Li }
32*bf2c3715SXin Li 
33*bf2c3715SXin Li template<typename T>
test2dHyperbolicRotation(const T & tol)34*bf2c3715SXin Li void test2dHyperbolicRotation(const T& tol)
35*bf2c3715SXin Li {
36*bf2c3715SXin Li   Matrix<std::complex<T>,2,2> A, B, C;
37*bf2c3715SXin Li   T angle, ch = std::cosh((T)1);
38*bf2c3715SXin Li   std::complex<T> ish(0, std::sinh((T)1));
39*bf2c3715SXin Li 
40*bf2c3715SXin Li   A << ch, ish, -ish, ch;
41*bf2c3715SXin Li   MatrixPower<Matrix<std::complex<T>,2,2> > Apow(A);
42*bf2c3715SXin Li 
43*bf2c3715SXin Li   for (int i=0; i<=20; ++i) {
44*bf2c3715SXin Li     angle = std::ldexp(static_cast<T>(i-10), -1);
45*bf2c3715SXin Li     ch = std::cosh(angle);
46*bf2c3715SXin Li     ish = std::complex<T>(0, std::sinh(angle));
47*bf2c3715SXin Li     B << ch, ish, -ish, ch;
48*bf2c3715SXin Li 
49*bf2c3715SXin Li     C = Apow(angle);
50*bf2c3715SXin Li     std::cout << "test2dHyperbolicRotation: i = " << i << "   error powerm = " << relerr(C,B) << '\n';
51*bf2c3715SXin Li     VERIFY(C.isApprox(B, tol));
52*bf2c3715SXin Li   }
53*bf2c3715SXin Li }
54*bf2c3715SXin Li 
55*bf2c3715SXin Li template<typename T>
test3dRotation(const T & tol)56*bf2c3715SXin Li void test3dRotation(const T& tol)
57*bf2c3715SXin Li {
58*bf2c3715SXin Li   Matrix<T,3,1> v;
59*bf2c3715SXin Li   T angle;
60*bf2c3715SXin Li 
61*bf2c3715SXin Li   for (int i=0; i<=20; ++i) {
62*bf2c3715SXin Li     v = Matrix<T,3,1>::Random();
63*bf2c3715SXin Li     v.normalize();
64*bf2c3715SXin Li     angle = std::pow(T(10), T(i-10) / T(5.));
65*bf2c3715SXin Li     VERIFY(AngleAxis<T>(angle, v).matrix().isApprox(AngleAxis<T>(1,v).matrix().pow(angle), tol));
66*bf2c3715SXin Li   }
67*bf2c3715SXin Li }
68*bf2c3715SXin Li 
69*bf2c3715SXin Li template<typename MatrixType>
testGeneral(const MatrixType & m,const typename MatrixType::RealScalar & tol)70*bf2c3715SXin Li void testGeneral(const MatrixType& m, const typename MatrixType::RealScalar& tol)
71*bf2c3715SXin Li {
72*bf2c3715SXin Li   typedef typename MatrixType::RealScalar RealScalar;
73*bf2c3715SXin Li   MatrixType m1, m2, m3, m4, m5;
74*bf2c3715SXin Li   RealScalar x, y;
75*bf2c3715SXin Li 
76*bf2c3715SXin Li   for (int i=0; i < g_repeat; ++i) {
77*bf2c3715SXin Li     generateTestMatrix<MatrixType>::run(m1, m.rows());
78*bf2c3715SXin Li     MatrixPower<MatrixType> mpow(m1);
79*bf2c3715SXin Li 
80*bf2c3715SXin Li     x = internal::random<RealScalar>();
81*bf2c3715SXin Li     y = internal::random<RealScalar>();
82*bf2c3715SXin Li     m2 = mpow(x);
83*bf2c3715SXin Li     m3 = mpow(y);
84*bf2c3715SXin Li 
85*bf2c3715SXin Li     m4 = mpow(x+y);
86*bf2c3715SXin Li     m5.noalias() = m2 * m3;
87*bf2c3715SXin Li     VERIFY(m4.isApprox(m5, tol));
88*bf2c3715SXin Li 
89*bf2c3715SXin Li     m4 = mpow(x*y);
90*bf2c3715SXin Li     m5 = m2.pow(y);
91*bf2c3715SXin Li     VERIFY(m4.isApprox(m5, tol));
92*bf2c3715SXin Li 
93*bf2c3715SXin Li     m4 = (std::abs(x) * m1).pow(y);
94*bf2c3715SXin Li     m5 = std::pow(std::abs(x), y) * m3;
95*bf2c3715SXin Li     VERIFY(m4.isApprox(m5, tol));
96*bf2c3715SXin Li   }
97*bf2c3715SXin Li }
98*bf2c3715SXin Li 
99*bf2c3715SXin Li template<typename MatrixType>
testSingular(const MatrixType & m_const,const typename MatrixType::RealScalar & tol)100*bf2c3715SXin Li void testSingular(const MatrixType& m_const, const typename MatrixType::RealScalar& tol)
101*bf2c3715SXin Li {
102*bf2c3715SXin Li   // we need to pass by reference in order to prevent errors with
103*bf2c3715SXin Li   // MSVC for aligned data types ...
104*bf2c3715SXin Li   MatrixType& m = const_cast<MatrixType&>(m_const);
105*bf2c3715SXin Li 
106*bf2c3715SXin Li   const int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex;
107*bf2c3715SXin Li   typedef typename internal::conditional<IsComplex, TriangularView<MatrixType,Upper>, const MatrixType&>::type TriangularType;
108*bf2c3715SXin Li   typename internal::conditional< IsComplex, ComplexSchur<MatrixType>, RealSchur<MatrixType> >::type schur;
109*bf2c3715SXin Li   MatrixType T;
110*bf2c3715SXin Li 
111*bf2c3715SXin Li   for (int i=0; i < g_repeat; ++i) {
112*bf2c3715SXin Li     m.setRandom();
113*bf2c3715SXin Li     m.col(0).fill(0);
114*bf2c3715SXin Li 
115*bf2c3715SXin Li     schur.compute(m);
116*bf2c3715SXin Li     T = schur.matrixT();
117*bf2c3715SXin Li     const MatrixType& U = schur.matrixU();
118*bf2c3715SXin Li     processTriangularMatrix<MatrixType>::run(m, T, U);
119*bf2c3715SXin Li     MatrixPower<MatrixType> mpow(m);
120*bf2c3715SXin Li 
121*bf2c3715SXin Li     T = T.sqrt();
122*bf2c3715SXin Li     VERIFY(mpow(0.5L).isApprox(U * (TriangularType(T) * U.adjoint()), tol));
123*bf2c3715SXin Li 
124*bf2c3715SXin Li     T = T.sqrt();
125*bf2c3715SXin Li     VERIFY(mpow(0.25L).isApprox(U * (TriangularType(T) * U.adjoint()), tol));
126*bf2c3715SXin Li 
127*bf2c3715SXin Li     T = T.sqrt();
128*bf2c3715SXin Li     VERIFY(mpow(0.125L).isApprox(U * (TriangularType(T) * U.adjoint()), tol));
129*bf2c3715SXin Li   }
130*bf2c3715SXin Li }
131*bf2c3715SXin Li 
132*bf2c3715SXin Li template<typename MatrixType>
testLogThenExp(const MatrixType & m_const,const typename MatrixType::RealScalar & tol)133*bf2c3715SXin Li void testLogThenExp(const MatrixType& m_const, const typename MatrixType::RealScalar& tol)
134*bf2c3715SXin Li {
135*bf2c3715SXin Li   // we need to pass by reference in order to prevent errors with
136*bf2c3715SXin Li   // MSVC for aligned data types ...
137*bf2c3715SXin Li   MatrixType& m = const_cast<MatrixType&>(m_const);
138*bf2c3715SXin Li 
139*bf2c3715SXin Li   typedef typename MatrixType::Scalar Scalar;
140*bf2c3715SXin Li   Scalar x;
141*bf2c3715SXin Li 
142*bf2c3715SXin Li   for (int i=0; i < g_repeat; ++i) {
143*bf2c3715SXin Li     generateTestMatrix<MatrixType>::run(m, m.rows());
144*bf2c3715SXin Li     x = internal::random<Scalar>();
145*bf2c3715SXin Li     VERIFY(m.pow(x).isApprox((x * m.log()).exp(), tol));
146*bf2c3715SXin Li   }
147*bf2c3715SXin Li }
148*bf2c3715SXin Li 
149*bf2c3715SXin Li typedef Matrix<double,3,3,RowMajor>         Matrix3dRowMajor;
150*bf2c3715SXin Li typedef Matrix<long double,3,3>             Matrix3e;
151*bf2c3715SXin Li typedef Matrix<long double,Dynamic,Dynamic> MatrixXe;
152*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(matrix_power)153*bf2c3715SXin Li EIGEN_DECLARE_TEST(matrix_power)
154*bf2c3715SXin Li {
155*bf2c3715SXin Li   CALL_SUBTEST_2(test2dRotation<double>(1e-13));
156*bf2c3715SXin Li   CALL_SUBTEST_1(test2dRotation<float>(2e-5f));  // was 1e-5, relaxed for clang 2.8 / linux / x86-64
157*bf2c3715SXin Li   CALL_SUBTEST_9(test2dRotation<long double>(1e-13L));
158*bf2c3715SXin Li   CALL_SUBTEST_2(test2dHyperbolicRotation<double>(1e-14));
159*bf2c3715SXin Li   CALL_SUBTEST_1(test2dHyperbolicRotation<float>(1e-5f));
160*bf2c3715SXin Li   CALL_SUBTEST_9(test2dHyperbolicRotation<long double>(1e-14L));
161*bf2c3715SXin Li 
162*bf2c3715SXin Li   CALL_SUBTEST_10(test3dRotation<double>(1e-13));
163*bf2c3715SXin Li   CALL_SUBTEST_11(test3dRotation<float>(1e-5f));
164*bf2c3715SXin Li   CALL_SUBTEST_12(test3dRotation<long double>(1e-13L));
165*bf2c3715SXin Li 
166*bf2c3715SXin Li   CALL_SUBTEST_2(testGeneral(Matrix2d(),         1e-13));
167*bf2c3715SXin Li   CALL_SUBTEST_7(testGeneral(Matrix3dRowMajor(), 1e-13));
168*bf2c3715SXin Li   CALL_SUBTEST_3(testGeneral(Matrix4cd(),        1e-13));
169*bf2c3715SXin Li   CALL_SUBTEST_4(testGeneral(MatrixXd(8,8),      2e-12));
170*bf2c3715SXin Li   CALL_SUBTEST_1(testGeneral(Matrix2f(),         1e-4f));
171*bf2c3715SXin Li   CALL_SUBTEST_5(testGeneral(Matrix3cf(),        1e-4f));
172*bf2c3715SXin Li   CALL_SUBTEST_8(testGeneral(Matrix4f(),         1e-4f));
173*bf2c3715SXin Li   CALL_SUBTEST_6(testGeneral(MatrixXf(2,2),      1e-3f)); // see bug 614
174*bf2c3715SXin Li   CALL_SUBTEST_9(testGeneral(MatrixXe(7,7),      1e-13L));
175*bf2c3715SXin Li   CALL_SUBTEST_10(testGeneral(Matrix3d(),        1e-13));
176*bf2c3715SXin Li   CALL_SUBTEST_11(testGeneral(Matrix3f(),        1e-4f));
177*bf2c3715SXin Li   CALL_SUBTEST_12(testGeneral(Matrix3e(),        1e-13L));
178*bf2c3715SXin Li 
179*bf2c3715SXin Li   CALL_SUBTEST_2(testSingular(Matrix2d(),         1e-13));
180*bf2c3715SXin Li   CALL_SUBTEST_7(testSingular(Matrix3dRowMajor(), 1e-13));
181*bf2c3715SXin Li   CALL_SUBTEST_3(testSingular(Matrix4cd(),        1e-13));
182*bf2c3715SXin Li   CALL_SUBTEST_4(testSingular(MatrixXd(8,8),      2e-12));
183*bf2c3715SXin Li   CALL_SUBTEST_1(testSingular(Matrix2f(),         1e-4f));
184*bf2c3715SXin Li   CALL_SUBTEST_5(testSingular(Matrix3cf(),        1e-4f));
185*bf2c3715SXin Li   CALL_SUBTEST_8(testSingular(Matrix4f(),         1e-4f));
186*bf2c3715SXin Li   CALL_SUBTEST_6(testSingular(MatrixXf(2,2),      1e-3f));
187*bf2c3715SXin Li   CALL_SUBTEST_9(testSingular(MatrixXe(7,7),      1e-13L));
188*bf2c3715SXin Li   CALL_SUBTEST_10(testSingular(Matrix3d(),        1e-13));
189*bf2c3715SXin Li   CALL_SUBTEST_11(testSingular(Matrix3f(),        1e-4f));
190*bf2c3715SXin Li   CALL_SUBTEST_12(testSingular(Matrix3e(),        1e-13L));
191*bf2c3715SXin Li 
192*bf2c3715SXin Li   CALL_SUBTEST_2(testLogThenExp(Matrix2d(),         1e-13));
193*bf2c3715SXin Li   CALL_SUBTEST_7(testLogThenExp(Matrix3dRowMajor(), 1e-13));
194*bf2c3715SXin Li   CALL_SUBTEST_3(testLogThenExp(Matrix4cd(),        1e-13));
195*bf2c3715SXin Li   CALL_SUBTEST_4(testLogThenExp(MatrixXd(8,8),      2e-12));
196*bf2c3715SXin Li   CALL_SUBTEST_1(testLogThenExp(Matrix2f(),         1e-4f));
197*bf2c3715SXin Li   CALL_SUBTEST_5(testLogThenExp(Matrix3cf(),        1e-4f));
198*bf2c3715SXin Li   CALL_SUBTEST_8(testLogThenExp(Matrix4f(),         1e-4f));
199*bf2c3715SXin Li   CALL_SUBTEST_6(testLogThenExp(MatrixXf(2,2),      1e-3f));
200*bf2c3715SXin Li   CALL_SUBTEST_9(testLogThenExp(MatrixXe(7,7),      1e-13L));
201*bf2c3715SXin Li   CALL_SUBTEST_10(testLogThenExp(Matrix3d(),        1e-13));
202*bf2c3715SXin Li   CALL_SUBTEST_11(testLogThenExp(Matrix3f(),        1e-4f));
203*bf2c3715SXin Li   CALL_SUBTEST_12(testLogThenExp(Matrix3e(),        1e-13L));
204*bf2c3715SXin Li }
205