matrix_function.cpp File Reference
#include "main.h"
#include <unsupported/Eigen/MatrixFunctions>

Classes

struct  randomMatrixWithImagEivals< MatrixType, IsComplex >
 
struct  randomMatrixWithImagEivals< MatrixType, 0 >
 
struct  randomMatrixWithImagEivals< MatrixType, 1 >
 

Macros

#define VERIFY_IS_APPROX_ABS(a, b)   VERIFY(test_isApprox_abs(a, b))
 

Functions

template<typename Type1 , typename Type2 >
bool test_isApprox_abs (const Type1 &a, const Type2 &b)
 
template<typename MatrixType >
MatrixType randomMatrixWithRealEivals (const Index size)
 
template<typename MatrixType >
void testMatrixExponential (const MatrixType &A)
 
template<typename MatrixType >
void testMatrixLogarithm (const MatrixType &A)
 
template<typename MatrixType >
void testHyperbolicFunctions (const MatrixType &A)
 
template<typename MatrixType >
void testGonioFunctions (const MatrixType &A)
 
template<typename MatrixType >
void testMatrix (const MatrixType &A)
 
template<typename MatrixType >
void testMatrixType (const MatrixType &m)
 
template<typename MatrixType >
void testMapRef (const MatrixType &A)
 
 EIGEN_DECLARE_TEST (matrix_function)
 

Macro Definition Documentation

◆ VERIFY_IS_APPROX_ABS

#define VERIFY_IS_APPROX_ABS (   a,
  b 
)    VERIFY(test_isApprox_abs(a, b))

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( matrix_function  )
197  {
199  CALL_SUBTEST_2(testMatrixType(Matrix3cf()));
200  CALL_SUBTEST_3(testMatrixType(MatrixXf(8, 8)));
201  CALL_SUBTEST_4(testMatrixType(Matrix2d()));
203  CALL_SUBTEST_6(testMatrixType(Matrix4cd()));
204  CALL_SUBTEST_7(testMatrixType(MatrixXd(13, 13)));
205 
207  CALL_SUBTEST_2(testMapRef(Matrix3cf()));
208  CALL_SUBTEST_3(testMapRef(MatrixXf(8, 8)));
209  CALL_SUBTEST_7(testMapRef(MatrixXd(13, 13)));
210 }
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
void testMatrixType(const MatrixType &m)
Definition: matrix_function.cpp:153
void testMapRef(const MatrixType &A)
Definition: matrix_function.cpp:166
#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_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

References CALL_SUBTEST_1, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, CALL_SUBTEST_6, CALL_SUBTEST_7, testMapRef(), and testMatrixType().

◆ randomMatrixWithRealEivals()

template<typename MatrixType >
MatrixType randomMatrixWithRealEivals ( const Index  size)
24  {
25  typedef typename MatrixType::Scalar Scalar;
26  typedef typename MatrixType::RealScalar RealScalar;
28  for (Index i = 0; i < size; ++i) {
29  diag(i, i) =
30  Scalar(RealScalar(internal::random<int>(0, 2))) + internal::random<Scalar>() * Scalar(RealScalar(0.01));
31  }
32  MatrixType A = MatrixType::Random(size, size);
34  return QRofA.householderQ().inverse() * diag * QRofA.householderQ();
35 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
const char const char const char * diag
Definition: level2_impl.h:86
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
double Zero
Definition: pseudosolid_node_update_elements.cc:35

References diag, Eigen::HouseholderQR< MatrixType_ >::householderQ(), i, Eigen::HouseholderSequence< VectorsType, CoeffsType, Side >::inverse(), size, and oomph::PseudoSolidHelper::Zero.

◆ test_isApprox_abs()

template<typename Type1 , typename Type2 >
bool test_isApprox_abs ( const Type1 &  a,
const Type2 &  b 
)
inline
18  {
19  return ((a - b).array().abs() < test_precision<typename Type1::RealScalar>()).all();
20 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
Scalar * b
Definition: benchVecAdd.cpp:17
const Scalar * a
Definition: level2_cplx_impl.h:32
std::array< T, N > array
Definition: EmulateArray.h:231

References a, abs(), and b.

◆ testGonioFunctions()

template<typename MatrixType >
void testGonioFunctions ( const MatrixType A)
122  {
123  typedef typename MatrixType::Scalar Scalar;
124  typedef typename NumTraits<Scalar>::Real RealScalar;
125  typedef std::complex<RealScalar> ComplexScalar;
127  ComplexMatrix;
128 
129  ComplexScalar imagUnit(0, 1);
130  ComplexScalar two(2, 0);
131 
132  ComplexMatrix Ac = A.template cast<ComplexScalar>();
133 
134  ComplexMatrix exp_iA = (imagUnit * Ac).exp();
135  ComplexMatrix exp_miA = (-imagUnit * Ac).exp();
136 
137  ComplexMatrix sinAc = A.sin().template cast<ComplexScalar>();
138  VERIFY_IS_APPROX_ABS(sinAc, (exp_iA - exp_miA) / (two * imagUnit));
139 
140  ComplexMatrix cosAc = A.cos().template cast<ComplexScalar>();
141  VERIFY_IS_APPROX_ABS(cosAc, (exp_iA + exp_miA) / 2);
142 }
#define VERIFY_IS_APPROX_ABS(a, b)
Definition: matrix_function.cpp:15
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 exp(const bfloat16 &a)
Definition: BFloat16.h:615
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217

References Eigen::bfloat16_impl::exp(), and VERIFY_IS_APPROX_ABS.

Referenced by testMatrix().

◆ testHyperbolicFunctions()

template<typename MatrixType >
void testHyperbolicFunctions ( const MatrixType A)
114  {
115  // Need to use absolute error because of possible cancellation when
116  // adding/subtracting expA and expmA.
117  VERIFY_IS_APPROX_ABS(A.sinh(), (A.exp() - (-A).exp()) / 2);
118  VERIFY_IS_APPROX_ABS(A.cosh(), (A.exp() + (-A).exp()) / 2);
119 }

References VERIFY_IS_APPROX_ABS.

Referenced by testMatrix().

◆ testMapRef()

template<typename MatrixType >
void testMapRef ( const MatrixType A)
166  {
167  // Test if passing Ref and Map objects is possible
168  // (Regression test for Bug #1796)
169  Index size = A.rows();
170  MatrixType X;
171  X.setRandom(size, size);
172  MatrixType Y(size, size);
175  Map<MatrixType> M(Y.data(), size, size);
176  Map<const MatrixType> Mc(X.data(), size, size);
177 
178  X = X * X; // make sure sqrt is possible
179  Y = X.sqrt();
180  R = Rc.sqrt();
181  M = Mc.sqrt();
182  Y = X.exp();
183  R = Rc.exp();
184  M = Mc.exp();
185  X = Y; // make sure log is possible
186  Y = X.log();
187  R = Rc.log();
188  M = Mc.log();
189 
190  Y = X.cos() + Rc.cos() + Mc.cos();
191  Y = X.sin() + Rc.sin() + Mc.sin();
192 
193  Y = X.cosh() + Rc.cosh() + Mc.cosh();
194  Y = X.sinh() + Rc.sinh() + Mc.sinh();
195 }
@ R
Definition: StatisticsVector.h:21
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:50
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:191
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:264
#define X
Definition: icosphere.cpp:20
const char Y
Definition: test/EulerAngles.cpp:32

References R, Eigen::PlainObjectBase< Derived >::rows(), size, X, and Y.

Referenced by EIGEN_DECLARE_TEST().

◆ testMatrix()

template<typename MatrixType >
void testMatrix ( const MatrixType A)
145  {
150 }
void testMatrixLogarithm(const MatrixType &A)
Definition: matrix_function.cpp:96
void testHyperbolicFunctions(const MatrixType &A)
Definition: matrix_function.cpp:114
void testMatrixExponential(const MatrixType &A)
Definition: matrix_function.cpp:87
void testGonioFunctions(const MatrixType &A)
Definition: matrix_function.cpp:122

References testGonioFunctions(), testHyperbolicFunctions(), testMatrixExponential(), and testMatrixLogarithm().

Referenced by testMatrixType().

◆ testMatrixExponential()

template<typename MatrixType >
void testMatrixExponential ( const MatrixType A)
87  {
89  typedef typename NumTraits<Scalar>::Real RealScalar;
90  typedef std::complex<RealScalar> ComplexScalar;
91 
92  VERIFY_IS_APPROX(A.exp(), A.matrixFunction(internal::stem_function_exp<ComplexScalar>));
93 }
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13

References VERIFY_IS_APPROX.

Referenced by testMatrix().

◆ testMatrixLogarithm()

template<typename MatrixType >
void testMatrixLogarithm ( const MatrixType A)
96  {
98  typedef typename NumTraits<Scalar>::Real RealScalar;
99 
100  MatrixType scaledA;
101  RealScalar maxImagPartOfSpectrum = A.eigenvalues().imag().cwiseAbs().maxCoeff();
102  if (maxImagPartOfSpectrum >= RealScalar(0.9L * EIGEN_PI))
103  scaledA = A * RealScalar(0.9L * EIGEN_PI) / maxImagPartOfSpectrum;
104  else
105  scaledA = A;
106 
107  // identity X.exp().log() = X only holds if Im(lambda) < pi for all eigenvalues of X
108  MatrixType expA = scaledA.exp();
109  MatrixType logExpA = expA.log();
110  VERIFY_IS_APPROX(logExpA, scaledA);
111 }
#define EIGEN_PI
Definition: MathFunctions.h:16
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47

References EIGEN_PI, and VERIFY_IS_APPROX.

Referenced by testMatrix().

◆ testMatrixType()

template<typename MatrixType >
void testMatrixType ( const MatrixType m)
153  {
154  // Matrices with clustered eigenvalue lead to different code paths
155  // in MatrixFunction.h and are thus useful for testing.
156 
157  const Index size = m.rows();
158  for (int i = 0; i < g_repeat; i++) {
159  testMatrix(MatrixType::Random(size, size).eval());
160  testMatrix(randomMatrixWithRealEivals<MatrixType>(size));
162  }
163 }
int * m
Definition: level2_cplx_impl.h:294
void testMatrix(const MatrixType &A)
Definition: matrix_function.cpp:145
static int g_repeat
Definition: main.h:191
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Definition: sparse_permutations.cpp:47
Definition: matrix_function.cpp:38

References eval(), Eigen::g_repeat, i, m, size, and testMatrix().

Referenced by EIGEN_DECLARE_TEST().