matrix_power.cpp File Reference
#include "matrix_functions.h"

Typedefs

typedef Matrix< double, 3, 3, RowMajor > Matrix3dRowMajor
 
typedef Matrix< long double, 3, 3 > Matrix3e
 
typedef Matrix< long double, Dynamic, Dynamic > MatrixXe
 

Functions

template<typename T >
void test2dRotation (const T &tol)
 
template<typename T >
void test2dHyperbolicRotation (const T &tol)
 
template<typename T >
void test3dRotation (const T &tol)
 
template<typename MatrixType >
void testGeneral (const MatrixType &m, const typename MatrixType::RealScalar &tol)
 
template<typename MatrixType >
void testSingular (const MatrixType &m_const, const typename MatrixType::RealScalar &tol)
 
template<typename MatrixType >
void testLogThenExp (const MatrixType &m_const, const typename MatrixType::RealScalar &tol)
 
 EIGEN_DECLARE_TEST (matrix_power)
 

Typedef Documentation

◆ Matrix3dRowMajor

typedef Matrix<double, 3, 3, RowMajor> Matrix3dRowMajor

◆ Matrix3e

typedef Matrix<long double, 3, 3> Matrix3e

◆ MatrixXe

typedef Matrix<long double, Dynamic, Dynamic> MatrixXe

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( matrix_power  )
147  {
148  CALL_SUBTEST_2(test2dRotation<double>(1e-13));
149  CALL_SUBTEST_1(test2dRotation<float>(2e-5f)); // was 1e-5, relaxed for clang 2.8 / linux / x86-64
150  CALL_SUBTEST_9(test2dRotation<long double>(1e-13L));
151  CALL_SUBTEST_2(test2dHyperbolicRotation<double>(1e-14));
152  CALL_SUBTEST_1(test2dHyperbolicRotation<float>(1e-5f));
153  CALL_SUBTEST_9(test2dHyperbolicRotation<long double>(1e-14L));
154 
155  CALL_SUBTEST_10(test3dRotation<double>(1e-13));
156  CALL_SUBTEST_11(test3dRotation<float>(1e-5f));
157  CALL_SUBTEST_12(test3dRotation<long double>(1e-13L));
158 
159  CALL_SUBTEST_2(testGeneral(Matrix2d(), 1e-13));
161  CALL_SUBTEST_3(testGeneral(Matrix4cd(), 1e-13));
162  CALL_SUBTEST_4(testGeneral(MatrixXd(8, 8), 2e-12));
163  CALL_SUBTEST_1(testGeneral(Matrix2f(), 1e-4f));
164  CALL_SUBTEST_5(testGeneral(Matrix3cf(), 1e-4f));
165  CALL_SUBTEST_8(testGeneral(Matrix4f(), 1e-4f));
166  CALL_SUBTEST_6(testGeneral(MatrixXf(2, 2), 1e-3f)); // see bug 614
167  CALL_SUBTEST_9(testGeneral(MatrixXe(7, 7), 1e-12L));
168  CALL_SUBTEST_10(testGeneral(Matrix3d(), 1e-13));
169  CALL_SUBTEST_11(testGeneral(Matrix3f(), 1e-4f));
171 
172  CALL_SUBTEST_2(testSingular(Matrix2d(), 1e-13));
174  CALL_SUBTEST_3(testSingular(Matrix4cd(), 1e-13));
175  CALL_SUBTEST_4(testSingular(MatrixXd(8, 8), 2e-12));
176  CALL_SUBTEST_1(testSingular(Matrix2f(), 1e-4f));
177  CALL_SUBTEST_5(testSingular(Matrix3cf(), 1e-4f));
178  CALL_SUBTEST_8(testSingular(Matrix4f(), 1e-4f));
179  CALL_SUBTEST_6(testSingular(MatrixXf(2, 2), 1e-3f));
180  CALL_SUBTEST_9(testSingular(MatrixXe(7, 7), 1e-12L));
181  CALL_SUBTEST_10(testSingular(Matrix3d(), 1e-13));
182  CALL_SUBTEST_11(testSingular(Matrix3f(), 1e-4f));
184 
185  CALL_SUBTEST_2(testLogThenExp(Matrix2d(), 1e-13));
187  CALL_SUBTEST_3(testLogThenExp(Matrix4cd(), 1e-13));
188  CALL_SUBTEST_4(testLogThenExp(MatrixXd(8, 8), 2e-12));
189  CALL_SUBTEST_1(testLogThenExp(Matrix2f(), 1e-4f));
190  CALL_SUBTEST_5(testLogThenExp(Matrix3cf(), 1e-4f));
191  CALL_SUBTEST_8(testLogThenExp(Matrix4f(), 1e-4f));
192  CALL_SUBTEST_6(testLogThenExp(MatrixXf(2, 2), 1e-3f));
193  CALL_SUBTEST_9(testLogThenExp(MatrixXe(7, 7), 1e-12L));
194  CALL_SUBTEST_10(testLogThenExp(Matrix3d(), 1e-13));
195  CALL_SUBTEST_11(testLogThenExp(Matrix3f(), 1e-4f));
197 }
Array< double, 1, 3 > e(1./3., 0.5, 2.)
void testSingular(const MatrixType &m_const, const typename MatrixType::RealScalar &tol)
Definition: matrix_power.cpp:96
Matrix< long double, 3, 3 > Matrix3e
Definition: matrix_power.cpp:144
Matrix< long double, Dynamic, Dynamic > MatrixXe
Definition: matrix_power.cpp:145
void testLogThenExp(const MatrixType &m_const, const typename MatrixType::RealScalar &tol)
Definition: matrix_power.cpp:128
Matrix< double, 3, 3, RowMajor > Matrix3dRowMajor
Definition: matrix_power.cpp:143
void testGeneral(const MatrixType &m, const typename MatrixType::RealScalar &tol)
Definition: matrix_power.cpp:67
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
#define CALL_SUBTEST_11(FUNC)
Definition: split_test_helper.h:64
#define CALL_SUBTEST_12(FUNC)
Definition: split_test_helper.h:70
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
#define CALL_SUBTEST_10(FUNC)
Definition: split_test_helper.h:58

References CALL_SUBTEST_1, CALL_SUBTEST_10, CALL_SUBTEST_11, CALL_SUBTEST_12, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, CALL_SUBTEST_6, CALL_SUBTEST_7, CALL_SUBTEST_8, CALL_SUBTEST_9, e(), testGeneral(), testLogThenExp(), and testSingular().

◆ test2dHyperbolicRotation()

template<typename T >
void test2dHyperbolicRotation ( const T tol)
33  {
34  Matrix<std::complex<T>, 2, 2> A, B, C;
35  T angle, ch = std::cosh((T)1);
36  std::complex<T> ish(0, std::sinh((T)1));
37 
38  A << ch, ish, -ish, ch;
40 
41  for (int i = 0; i <= 20; ++i) {
42  angle = std::ldexp(static_cast<T>(i - 10), -1);
43  ch = std::cosh(angle);
44  ish = std::complex<T>(0, std::sinh(angle));
45  B << ch, ish, -ish, ch;
46 
47  C = Apow(angle);
48  std::cout << "test2dHyperbolicRotation: i = " << i << " error powerm = " << relerr(C, B) << '\n';
49  VERIFY(C.isApprox(B, tol));
50  }
51 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:49
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:48
Class for computing matrix powers.
Definition: MatrixPower.h:340
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
Definition: matrices.h:74
#define VERIFY(a)
Definition: main.h:362
Derived::RealScalar relerr(const MatrixBase< Derived > &A, const MatrixBase< OtherDerived > &B)
Definition: matrix_functions.h:54
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 cosh(const bfloat16 &a)
Definition: BFloat16.h:638
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 sinh(const bfloat16 &a)
Definition: BFloat16.h:637
double angle(const double &t)
Angular position as a function of time t.
Definition: jeffery_orbit.cc:98

References Jeffery_Solution::angle(), Eigen::bfloat16_impl::cosh(), i, relerr(), Eigen::bfloat16_impl::sinh(), and VERIFY.

◆ test2dRotation()

template<typename T >
void test2dRotation ( const T tol)
13  {
14  Matrix<T, 2, 2> A, B, C;
15  T angle, c, s;
16 
17  A << 0, 1, -1, 0;
19 
20  for (int i = 0; i <= 20; ++i) {
21  angle = std::pow(T(10), T(i - 10) / T(5.));
22  c = std::cos(angle);
23  s = std::sin(angle);
24  B << c, s, -s, c;
25 
26  C = Apow(std::ldexp(angle, 1) / T(EIGEN_PI));
27  std::cout << "test2dRotation: i = " << i << " error powerm = " << relerr(C, B) << '\n';
28  VERIFY(C.isApprox(B, tol));
29  }
30 }
AnnoyingScalar cos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:136
AnnoyingScalar sin(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:137
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
#define EIGEN_PI
Definition: MathFunctions.h:16
RealScalar s
Definition: level1_cplx_impl.h:130
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 pow(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:625
int c
Definition: calibrate.py:100

References Jeffery_Solution::angle(), calibrate::c, cos(), EIGEN_PI, i, Eigen::bfloat16_impl::pow(), relerr(), s, sin(), and VERIFY.

◆ test3dRotation()

template<typename T >
void test3dRotation ( const T tol)
54  {
56  T angle;
57 
58  for (int i = 0; i <= 20; ++i) {
60  v.normalize();
61  angle = std::pow(T(10), T(i - 10) / T(5.));
63  }
64 }
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Represents a 3D rotation as a rotation angle around an arbitrary 3D axis.
Definition: AngleAxis.h:52
EIGEN_DEVICE_FUNC const GlobalUnaryPowReturnType< Derived, ScalarExponent > pow(const Eigen::ArrayBase< Derived > &x, const ScalarExponent &exponent)
Definition: GlobalFunctions.h:137
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
EIGEN_DEVICE_FUNC bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1923

References Jeffery_Solution::angle(), i, Eigen::internal::isApprox(), matrix(), Eigen::bfloat16_impl::pow(), Eigen::ArrayBase< Derived >::pow(), v, and VERIFY.

◆ testGeneral()

template<typename MatrixType >
void testGeneral ( const MatrixType m,
const typename MatrixType::RealScalar tol 
)
67  {
68  typedef typename MatrixType::RealScalar RealScalar;
69  MatrixType m1, m2, m3, m4, m5;
70  RealScalar x, y;
71 
72  for (int i = 0; i < g_repeat; ++i) {
75 
76  x = internal::random<RealScalar>();
77  y = internal::random<RealScalar>();
78  m2 = mpow(x);
79  m3 = mpow(y);
80 
81  m4 = mpow(x + y);
82  m5.noalias() = m2 * m3;
83  VERIFY(m4.isApprox(m5, tol));
84 
85  m4 = mpow(x * y);
86  m5 = m2.pow(y);
87  VERIFY(m4.isApprox(m5, tol));
88 
89  m4 = (std::abs(x) * m1).pow(y);
90  m5 = std::pow(std::abs(x), y) * m3;
91  VERIFY(m4.isApprox(m5, tol));
92  }
93 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixType m2(n_dims)
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Scalar * y
Definition: level1_cplx_impl.h:128
int * m
Definition: level2_cplx_impl.h:294
static int g_repeat
Definition: main.h:191
list x
Definition: plotDoE.py:28
void run(const string &dir_name, LinearSolver *linear_solver_pt, const unsigned nel_1d, bool mess_up_order)
Definition: two_d_poisson_compare_solvers.cc:317

References abs(), Eigen::g_repeat, i, m, m1, m2(), Eigen::bfloat16_impl::pow(), Eigen::ArrayBase< Derived >::pow(), run(), VERIFY, plotDoE::x, and y.

Referenced by EIGEN_DECLARE_TEST().

◆ testLogThenExp()

template<typename MatrixType >
void testLogThenExp ( const MatrixType m_const,
const typename MatrixType::RealScalar tol 
)
128  {
129  // we need to pass by reference in order to prevent errors with
130  // MSVC for aligned data types ...
131  MatrixType& m = const_cast<MatrixType&>(m_const);
132 
133  typedef typename MatrixType::Scalar Scalar;
134  Scalar x;
135 
136  for (int i = 0; i < g_repeat; ++i) {
138  x = internal::random<Scalar>();
139  VERIFY(m.pow(x).isApprox((x * m.log()).exp(), tol));
140  }
141 }
SCALAR Scalar
Definition: bench_gemm.cpp:45

References Eigen::g_repeat, i, m, run(), VERIFY, and plotDoE::x.

Referenced by EIGEN_DECLARE_TEST().

◆ testSingular()

template<typename MatrixType >
void testSingular ( const MatrixType m_const,
const typename MatrixType::RealScalar tol 
)
96  {
97  // we need to pass by reference in order to prevent errors with
98  // MSVC for aligned data types ...
99  MatrixType& m = const_cast<MatrixType&>(m_const);
100 
102  typedef std::conditional_t<IsComplex, TriangularView<MatrixType, Upper>, const MatrixType&> TriangularType;
103  std::conditional_t<IsComplex, ComplexSchur<MatrixType>, RealSchur<MatrixType> > schur;
104  MatrixType T;
105 
106  for (int i = 0; i < g_repeat; ++i) {
107  m.setRandom();
108  m.col(0).fill(0);
109 
110  schur.compute(m);
111  T = schur.matrixT();
112  const MatrixType& U = schur.matrixU();
115 
116  T = T.sqrt();
117  VERIFY(mpow(0.5L).isApprox(U * (TriangularType(T) * U.adjoint()), tol));
118 
119  T = T.sqrt();
120  VERIFY(mpow(0.25L).isApprox(U * (TriangularType(T) * U.adjoint()), tol));
121 
122  T = T.sqrt();
123  VERIFY(mpow(0.125L).isApprox(U * (TriangularType(T) * U.adjoint()), tol));
124  }
125 }
ComplexSchur< MatrixXcf > schur(4)
@ IsComplex
Definition: common.h:73
double U
Swimming speed.
Definition: two_d_variable_diff_adapt.cc:53
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
static void run(MatrixType &, MatrixType &, const MatrixType &)
Definition: matrix_functions.h:16

References Eigen::g_repeat, i, Eigen::internal::isApprox(), IsComplex, m, processTriangularMatrix< MatrixType, IsComplex >::run(), schur(), RachelsAdvectionDiffusion::U, and VERIFY.

Referenced by EIGEN_DECLARE_TEST().