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

Functions

double binom (int n, int k)
 
template<typename T >
T expfn (T x, int)
 
template<typename T >
void test2dRotation (double tol)
 
template<typename T >
void test2dHyperbolicRotation (double tol)
 
template<typename T >
void testPascal (double tol)
 
template<typename MatrixType >
void randomTest (const MatrixType &m, double tol)
 
 EIGEN_DECLARE_TEST (matrix_exponential)
 

Function Documentation

◆ binom()

double binom ( int  n,
int  k 
)
12  {
13  double res = 1;
14  for (int i = 0; i < k; i++) res = res * (n - k + i + 1) / (i + 1);
15  return res;
16 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
char char char int int * k
Definition: level2_impl.h:374

References i, k, n, and res.

Referenced by testPascal().

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( matrix_exponential  )
110  {
111  CALL_SUBTEST_2(test2dRotation<double>(1e-13));
112  CALL_SUBTEST_1(test2dRotation<float>(2e-5)); // was 1e-5, relaxed for clang 2.8 / linux / x86-64
113  CALL_SUBTEST_8(test2dRotation<long double>(1e-13));
114  CALL_SUBTEST_2(test2dHyperbolicRotation<double>(1e-14));
115  CALL_SUBTEST_1(test2dHyperbolicRotation<float>(1e-5));
116  CALL_SUBTEST_8(test2dHyperbolicRotation<long double>(1e-14));
117  CALL_SUBTEST_6(testPascal<float>(1e-6));
118  CALL_SUBTEST_5(testPascal<double>(1e-15));
119  CALL_SUBTEST_2(randomTest(Matrix2d(), 1e-13));
121  CALL_SUBTEST_3(randomTest(Matrix4cd(), 1e-13));
122  CALL_SUBTEST_4(randomTest(MatrixXd(8, 8), 1e-13));
123  CALL_SUBTEST_1(randomTest(Matrix2f(), 1e-4));
124  CALL_SUBTEST_5(randomTest(Matrix3cf(), 1e-4));
125  CALL_SUBTEST_1(randomTest(Matrix4f(), 1e-4));
126  CALL_SUBTEST_6(randomTest(MatrixXf(8, 8), 1e-4));
128 }
Array< double, 1, 3 > e(1./3., 0.5, 2.)
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
void randomTest(const MatrixType &m, double tol)
Definition: matrix_exponential.cpp:87
#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_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

References CALL_SUBTEST_1, 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(), and randomTest().

◆ expfn()

template<typename T >
T expfn ( T  x,
int   
)
19  {
20  return std::exp(x);
21 }
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 exp(const bfloat16 &a)
Definition: BFloat16.h:615
list x
Definition: plotDoE.py:28

References Eigen::bfloat16_impl::exp(), and plotDoE::x.

Referenced by randomTest(), test2dHyperbolicRotation(), test2dRotation(), and testPascal().

◆ randomTest()

template<typename MatrixType >
void randomTest ( const MatrixType m,
double  tol 
)
87  {
88  /* this test covers the following files:
89  Inverse.h
90  */
91  typename MatrixType::Index rows = m.rows();
92  typename MatrixType::Index cols = m.cols();
93  MatrixType m1(rows, cols), m2(rows, cols), identity = MatrixType::Identity(rows, cols);
94 
96 
97  for (int i = 0; i < g_repeat; i++) {
98  m1 = MatrixType::Random(rows, cols);
99 
100  m2 = m1.matrixFunction(expfn) * (-m1).matrixFunction(expfn);
101  std::cout << "randomTest: error funm = " << relerr(identity, m2);
102  VERIFY(identity.isApprox(m2, static_cast<RealScalar>(tol)));
103 
104  m2 = m1.exp() * (-m1).exp();
105  std::cout << " error expm = " << relerr(identity, m2) << "\n";
106  VERIFY(identity.isApprox(m2, static_cast<RealScalar>(tol)));
107  }
108 }
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixType m2(n_dims)
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 100 >, boost::multiprecision::et_on > Real
Definition: boostmultiprec.cpp:77
int * m
Definition: level2_cplx_impl.h:294
#define VERIFY(a)
Definition: main.h:362
T expfn(T x, int)
Definition: matrix_exponential.cpp:19
Derived::RealScalar relerr(const MatrixBase< Derived > &A, const MatrixBase< OtherDerived > &B)
Definition: matrix_functions.h:54
static int g_repeat
Definition: main.h:191
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217

References cols, Eigen::bfloat16_impl::exp(), expfn(), Eigen::g_repeat, i, m, m1, m2(), relerr(), rows, and VERIFY.

Referenced by EIGEN_DECLARE_TEST().

◆ test2dHyperbolicRotation()

template<typename T >
void test2dHyperbolicRotation ( double  tol)
44  {
45  Matrix<std::complex<T>, 2, 2> A, B, C;
46  std::complex<T> imagUnit(0, 1);
47  T angle, ch, sh;
48 
49  for (int i = 0; i <= 20; i++) {
50  angle = static_cast<T>((i - 10) / 2.0);
51  ch = std::cosh(angle);
52  sh = std::sinh(angle);
53  A << 0, angle * imagUnit, -angle * imagUnit, 0;
54  B << ch, sh * imagUnit, -sh * imagUnit, ch;
55 
56  C = A.matrixFunction(expfn);
57  std::cout << "test2dHyperbolicRotation: i = " << i << " error funm = " << relerr(C, B);
58  VERIFY(C.isApprox(B, static_cast<T>(tol)));
59 
60  C = A.exp();
61  std::cout << " error expm = " << relerr(C, B) << "\n";
62  VERIFY(C.isApprox(B, static_cast<T>(tol)));
63  }
64 }
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
Definition: matrices.h:74
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(), expfn(), i, relerr(), Eigen::bfloat16_impl::sinh(), and VERIFY.

◆ test2dRotation()

template<typename T >
void test2dRotation ( double  tol)
24  {
25  Matrix<T, 2, 2> A, B, C;
26  T angle;
27 
28  A << 0, 1, -1, 0;
29  for (int i = 0; i <= 20; i++) {
30  angle = static_cast<T>(pow(10, i / 5. - 2));
32 
33  C = (angle * A).matrixFunction(expfn);
34  std::cout << "test2dRotation: i = " << i << " error funm = " << relerr(C, B);
35  VERIFY(C.isApprox(B, static_cast<T>(tol)));
36 
37  C = (angle * A).exp();
38  std::cout << " error expm = " << relerr(C, B) << "\n";
39  VERIFY(C.isApprox(B, static_cast<T>(tol)));
40  }
41 }
AnnoyingScalar cos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:136
AnnoyingScalar sin(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:137
EIGEN_DEVICE_FUNC const GlobalUnaryPowReturnType< Derived, ScalarExponent > pow(const Eigen::ArrayBase< Derived > &x, const ScalarExponent &exponent)
Definition: GlobalFunctions.h:137

References Jeffery_Solution::angle(), cos(), Eigen::bfloat16_impl::exp(), expfn(), i, Eigen::ArrayBase< Derived >::pow(), relerr(), sin(), and VERIFY.

◆ testPascal()

template<typename T >
void testPascal ( double  tol)
67  {
68  for (int size = 1; size < 20; size++) {
70  A.setZero();
71  for (int i = 0; i < size - 1; i++) A(i + 1, i) = static_cast<T>(i + 1);
72  B.setZero();
73  for (int i = 0; i < size; i++)
74  for (int j = 0; j <= i; j++) B(i, j) = static_cast<T>(binom(i, j));
75 
76  C = A.matrixFunction(expfn);
77  std::cout << "testPascal: size = " << size << " error funm = " << relerr(C, B);
78  VERIFY(C.isApprox(B, static_cast<T>(tol)));
79 
80  C = A.exp();
81  std::cout << " error expm = " << relerr(C, B) << "\n";
82  VERIFY(C.isApprox(B, static_cast<T>(tol)));
83  }
84 }
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:569
double binom(int n, int k)
Definition: matrix_exponential.cpp:12
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References binom(), expfn(), i, j, relerr(), Eigen::PlainObjectBase< Derived >::setZero(), size, and VERIFY.