2010-01-12 02:05:30 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
|
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2010-01-12 02:05:30 +08:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include <unsupported/Eigen/MatrixFunctions>
|
|
|
|
|
2010-03-01 20:05:57 +08:00
|
|
|
// Variant of VERIFY_IS_APPROX which uses absolute error instead of
|
|
|
|
// relative error.
|
|
|
|
#define VERIFY_IS_APPROX_ABS(a, b) VERIFY(test_isApprox_abs(a, b))
|
|
|
|
|
|
|
|
template<typename Type1, typename Type2>
|
|
|
|
inline bool test_isApprox_abs(const Type1& a, const Type2& b)
|
|
|
|
{
|
|
|
|
return ((a-b).array().abs() < test_precision<typename Type1::RealScalar>()).all();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-14 06:38:27 +08:00
|
|
|
// Returns a matrix with eigenvalues clustered around 0, 1 and 2.
|
2010-01-12 02:05:30 +08:00
|
|
|
template<typename MatrixType>
|
2010-06-21 05:13:24 +08:00
|
|
|
MatrixType randomMatrixWithRealEivals(const typename MatrixType::Index size)
|
2010-01-25 06:56:28 +08:00
|
|
|
{
|
2010-06-21 05:13:24 +08:00
|
|
|
typedef typename MatrixType::Index Index;
|
2010-01-25 06:56:28 +08:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
2010-02-04 02:20:25 +08:00
|
|
|
typedef typename MatrixType::RealScalar RealScalar;
|
2010-02-14 06:38:27 +08:00
|
|
|
MatrixType diag = MatrixType::Zero(size, size);
|
2010-06-21 05:13:24 +08:00
|
|
|
for (Index i = 0; i < size; ++i) {
|
2010-10-25 22:15:22 +08:00
|
|
|
diag(i, i) = Scalar(RealScalar(internal::random<int>(0,2)))
|
|
|
|
+ internal::random<Scalar>() * Scalar(RealScalar(0.01));
|
2010-01-25 06:56:28 +08:00
|
|
|
}
|
2010-02-14 06:38:27 +08:00
|
|
|
MatrixType A = MatrixType::Random(size, size);
|
2010-03-01 20:05:57 +08:00
|
|
|
HouseholderQR<MatrixType> QRofA(A);
|
|
|
|
return QRofA.householderQ().inverse() * diag * QRofA.householderQ();
|
2010-01-25 06:56:28 +08:00
|
|
|
}
|
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
template <typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
|
2010-02-14 06:49:01 +08:00
|
|
|
struct randomMatrixWithImagEivals
|
|
|
|
{
|
|
|
|
// Returns a matrix with eigenvalues clustered around 0 and +/- i.
|
2010-06-21 05:13:24 +08:00
|
|
|
static MatrixType run(const typename MatrixType::Index size);
|
2010-02-14 06:49:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Partial specialization for real matrices
|
|
|
|
template<typename MatrixType>
|
|
|
|
struct randomMatrixWithImagEivals<MatrixType, 0>
|
|
|
|
{
|
2010-06-21 05:13:24 +08:00
|
|
|
static MatrixType run(const typename MatrixType::Index size)
|
2010-02-14 06:49:01 +08:00
|
|
|
{
|
2010-06-21 05:13:24 +08:00
|
|
|
typedef typename MatrixType::Index Index;
|
2010-02-14 06:49:01 +08:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
MatrixType diag = MatrixType::Zero(size, size);
|
2010-06-21 05:13:24 +08:00
|
|
|
Index i = 0;
|
2010-02-14 06:49:01 +08:00
|
|
|
while (i < size) {
|
2010-10-25 22:15:22 +08:00
|
|
|
Index randomInt = internal::random<Index>(-1, 1);
|
2010-02-14 06:49:01 +08:00
|
|
|
if (randomInt == 0 || i == size-1) {
|
2010-10-25 22:15:22 +08:00
|
|
|
diag(i, i) = internal::random<Scalar>() * Scalar(0.01);
|
2010-02-14 06:49:01 +08:00
|
|
|
++i;
|
|
|
|
} else {
|
2010-10-25 22:15:22 +08:00
|
|
|
Scalar alpha = Scalar(randomInt) + internal::random<Scalar>() * Scalar(0.01);
|
2010-02-14 06:49:01 +08:00
|
|
|
diag(i, i+1) = alpha;
|
|
|
|
diag(i+1, i) = -alpha;
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MatrixType A = MatrixType::Random(size, size);
|
2010-03-01 20:05:57 +08:00
|
|
|
HouseholderQR<MatrixType> QRofA(A);
|
|
|
|
return QRofA.householderQ().inverse() * diag * QRofA.householderQ();
|
2010-02-14 06:49:01 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Partial specialization for complex matrices
|
|
|
|
template<typename MatrixType>
|
|
|
|
struct randomMatrixWithImagEivals<MatrixType, 1>
|
|
|
|
{
|
2010-06-21 05:13:24 +08:00
|
|
|
static MatrixType run(const typename MatrixType::Index size)
|
2010-02-14 06:49:01 +08:00
|
|
|
{
|
2010-06-21 13:37:59 +08:00
|
|
|
typedef typename MatrixType::Index Index;
|
2010-02-14 06:49:01 +08:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
typedef typename MatrixType::RealScalar RealScalar;
|
|
|
|
const Scalar imagUnit(0, 1);
|
|
|
|
MatrixType diag = MatrixType::Zero(size, size);
|
2010-06-21 05:13:24 +08:00
|
|
|
for (Index i = 0; i < size; ++i) {
|
2010-10-25 22:15:22 +08:00
|
|
|
diag(i, i) = Scalar(RealScalar(internal::random<Index>(-1, 1))) * imagUnit
|
|
|
|
+ internal::random<Scalar>() * Scalar(RealScalar(0.01));
|
2010-02-14 06:49:01 +08:00
|
|
|
}
|
|
|
|
MatrixType A = MatrixType::Random(size, size);
|
2010-03-01 20:05:57 +08:00
|
|
|
HouseholderQR<MatrixType> QRofA(A);
|
|
|
|
return QRofA.householderQ().inverse() * diag * QRofA.householderQ();
|
2010-02-14 06:49:01 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-03-01 20:05:57 +08:00
|
|
|
|
2010-01-25 06:56:28 +08:00
|
|
|
template<typename MatrixType>
|
|
|
|
void testMatrixExponential(const MatrixType& A)
|
2010-01-12 02:05:30 +08:00
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
typedef typename internal::traits<MatrixType>::Scalar Scalar;
|
2010-01-12 02:05:30 +08:00
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
|
|
|
typedef std::complex<RealScalar> ComplexScalar;
|
|
|
|
|
2013-07-26 20:51:10 +08:00
|
|
|
VERIFY_IS_APPROX(A.exp(), A.matrixFunction(internal::stem_function_exp<ComplexScalar>));
|
2010-01-12 02:05:30 +08:00
|
|
|
}
|
|
|
|
|
2011-06-07 21:44:43 +08:00
|
|
|
template<typename MatrixType>
|
|
|
|
void testMatrixLogarithm(const MatrixType& A)
|
|
|
|
{
|
|
|
|
typedef typename internal::traits<MatrixType>::Scalar Scalar;
|
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
|
|
|
|
|
|
|
MatrixType scaledA;
|
|
|
|
RealScalar maxImagPartOfSpectrum = A.eigenvalues().imag().cwiseAbs().maxCoeff();
|
|
|
|
if (maxImagPartOfSpectrum >= 0.9 * M_PI)
|
|
|
|
scaledA = A * 0.9 * M_PI / maxImagPartOfSpectrum;
|
|
|
|
else
|
|
|
|
scaledA = A;
|
|
|
|
|
|
|
|
// identity X.exp().log() = X only holds if Im(lambda) < pi for all eigenvalues of X
|
|
|
|
MatrixType expA = scaledA.exp();
|
|
|
|
MatrixType logExpA = expA.log();
|
|
|
|
VERIFY_IS_APPROX(logExpA, scaledA);
|
|
|
|
}
|
|
|
|
|
2010-01-12 02:05:30 +08:00
|
|
|
template<typename MatrixType>
|
2010-01-25 06:56:28 +08:00
|
|
|
void testHyperbolicFunctions(const MatrixType& A)
|
2010-01-12 02:05:30 +08:00
|
|
|
{
|
2010-03-01 20:05:57 +08:00
|
|
|
// Need to use absolute error because of possible cancellation when
|
|
|
|
// adding/subtracting expA and expmA.
|
2010-03-17 01:26:55 +08:00
|
|
|
VERIFY_IS_APPROX_ABS(A.sinh(), (A.exp() - (-A).exp()) / 2);
|
|
|
|
VERIFY_IS_APPROX_ABS(A.cosh(), (A.exp() + (-A).exp()) / 2);
|
2010-01-12 02:05:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename MatrixType>
|
2010-01-25 06:56:28 +08:00
|
|
|
void testGonioFunctions(const MatrixType& A)
|
2010-01-12 02:05:30 +08:00
|
|
|
{
|
2010-03-01 20:05:57 +08:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
2010-01-12 02:05:30 +08:00
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
|
|
|
typedef std::complex<RealScalar> ComplexScalar;
|
2010-03-01 20:05:57 +08:00
|
|
|
typedef Matrix<ComplexScalar, MatrixType::RowsAtCompileTime,
|
|
|
|
MatrixType::ColsAtCompileTime, MatrixType::Options> ComplexMatrix;
|
2010-01-12 02:05:30 +08:00
|
|
|
|
|
|
|
ComplexScalar imagUnit(0,1);
|
|
|
|
ComplexScalar two(2,0);
|
|
|
|
|
2010-03-01 20:05:57 +08:00
|
|
|
ComplexMatrix Ac = A.template cast<ComplexScalar>();
|
|
|
|
|
2010-03-17 01:26:55 +08:00
|
|
|
ComplexMatrix exp_iA = (imagUnit * Ac).exp();
|
|
|
|
ComplexMatrix exp_miA = (-imagUnit * Ac).exp();
|
2010-03-01 20:05:57 +08:00
|
|
|
|
2010-03-17 01:26:55 +08:00
|
|
|
ComplexMatrix sinAc = A.sin().template cast<ComplexScalar>();
|
2010-03-01 20:05:57 +08:00
|
|
|
VERIFY_IS_APPROX_ABS(sinAc, (exp_iA - exp_miA) / (two*imagUnit));
|
|
|
|
|
2010-03-17 01:26:55 +08:00
|
|
|
ComplexMatrix cosAc = A.cos().template cast<ComplexScalar>();
|
2010-03-01 20:05:57 +08:00
|
|
|
VERIFY_IS_APPROX_ABS(cosAc, (exp_iA + exp_miA) / 2);
|
2010-01-12 02:05:30 +08:00
|
|
|
}
|
|
|
|
|
2010-02-14 06:38:27 +08:00
|
|
|
template<typename MatrixType>
|
|
|
|
void testMatrix(const MatrixType& A)
|
|
|
|
{
|
|
|
|
testMatrixExponential(A);
|
2011-06-07 21:44:43 +08:00
|
|
|
testMatrixLogarithm(A);
|
2010-02-14 06:38:27 +08:00
|
|
|
testHyperbolicFunctions(A);
|
|
|
|
testGonioFunctions(A);
|
|
|
|
}
|
|
|
|
|
2010-01-12 02:05:30 +08:00
|
|
|
template<typename MatrixType>
|
|
|
|
void testMatrixType(const MatrixType& m)
|
|
|
|
{
|
2010-02-14 06:38:27 +08:00
|
|
|
// Matrices with clustered eigenvalue lead to different code paths
|
|
|
|
// in MatrixFunction.h and are thus useful for testing.
|
2010-06-21 05:13:24 +08:00
|
|
|
typedef typename MatrixType::Index Index;
|
2010-02-14 06:38:27 +08:00
|
|
|
|
2010-06-21 05:13:24 +08:00
|
|
|
const Index size = m.rows();
|
2010-01-25 06:56:28 +08:00
|
|
|
for (int i = 0; i < g_repeat; i++) {
|
2010-02-14 06:38:27 +08:00
|
|
|
testMatrix(MatrixType::Random(size, size).eval());
|
|
|
|
testMatrix(randomMatrixWithRealEivals<MatrixType>(size));
|
2010-02-14 06:49:01 +08:00
|
|
|
testMatrix(randomMatrixWithImagEivals<MatrixType>::run(size));
|
2010-01-25 06:56:28 +08:00
|
|
|
}
|
2010-01-12 02:05:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_matrix_function()
|
|
|
|
{
|
|
|
|
CALL_SUBTEST_1(testMatrixType(Matrix<float,1,1>()));
|
|
|
|
CALL_SUBTEST_2(testMatrixType(Matrix3cf()));
|
|
|
|
CALL_SUBTEST_3(testMatrixType(MatrixXf(8,8)));
|
|
|
|
CALL_SUBTEST_4(testMatrixType(Matrix2d()));
|
|
|
|
CALL_SUBTEST_5(testMatrixType(Matrix<double,5,5,RowMajor>()));
|
|
|
|
CALL_SUBTEST_6(testMatrixType(Matrix4cd()));
|
|
|
|
CALL_SUBTEST_7(testMatrixType(MatrixXd(13,13)));
|
|
|
|
}
|