mirror of
https://gitlab.com/libeigen/eigen.git
synced 2024-12-03 06:50:57 +08:00
Add support for matrix exponential of floats and complex numbers.
This commit is contained in:
parent
309d540d4a
commit
f71f878bab
@ -25,11 +25,7 @@
|
|||||||
#ifndef EIGEN_MATRIX_EXPONENTIAL
|
#ifndef EIGEN_MATRIX_EXPONENTIAL
|
||||||
#define EIGEN_MATRIX_EXPONENTIAL
|
#define EIGEN_MATRIX_EXPONENTIAL
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
/** \brief Compute the matrix exponential.
|
||||||
template <typename Scalar> Scalar log2(Scalar v) { return std::log(v)/std::log(Scalar(2)); }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/** Compute the matrix exponential.
|
|
||||||
*
|
*
|
||||||
* \param M matrix whose exponential is to be computed.
|
* \param M matrix whose exponential is to be computed.
|
||||||
* \param result pointer to the matrix in which to store the result.
|
* \param result pointer to the matrix in which to store the result.
|
||||||
@ -58,103 +54,264 @@ template <typename Scalar> Scalar log2(Scalar v) { return std::log(v)/std::log(S
|
|||||||
* <em>SIAM J. %Matrix Anal. Applic.</em>, <b>26</b>:1179–1193,
|
* <em>SIAM J. %Matrix Anal. Applic.</em>, <b>26</b>:1179–1193,
|
||||||
* 2005.
|
* 2005.
|
||||||
*
|
*
|
||||||
* \note Currently, \p M has to be a matrix of \c double .
|
* \note \p M has to be a matrix of \c float, \c double,
|
||||||
|
* \c complex<float> or \c complex<double> .
|
||||||
*/
|
*/
|
||||||
template <typename Derived>
|
template <typename Derived>
|
||||||
void ei_matrix_exponential(const MatrixBase<Derived> &M, typename ei_plain_matrix_type<Derived>::type* result)
|
EIGEN_STRONG_INLINE void ei_matrix_exponential(const MatrixBase<Derived> &M,
|
||||||
{
|
typename MatrixBase<Derived>::PlainMatrixType* result);
|
||||||
typedef typename ei_traits<Derived>::Scalar Scalar;
|
|
||||||
typedef typename NumTraits<Scalar>::Real RealScalar;
|
|
||||||
typedef typename ei_plain_matrix_type<Derived>::type PlainMatrixType;
|
|
||||||
|
|
||||||
ei_assert(M.rows() == M.cols());
|
|
||||||
EIGEN_STATIC_ASSERT(NumTraits<Scalar>::HasFloatingPoint,NUMERIC_TYPE_MUST_BE_FLOATING_POINT)
|
|
||||||
|
|
||||||
PlainMatrixType num, den, U, V;
|
/** \internal \brief Internal helper functions for computing the
|
||||||
PlainMatrixType Id = PlainMatrixType::Identity(M.rows(), M.cols());
|
* matrix exponential.
|
||||||
typename ei_eval<Derived>::type Meval = M.eval();
|
*/
|
||||||
RealScalar l1norm = Meval.cwise().abs().colwise().sum().maxCoeff();
|
namespace MatrixExponentialInternal {
|
||||||
int squarings = 0;
|
|
||||||
|
|
||||||
// Choose degree of Pade approximant, depending on norm of M
|
#ifdef _MSC_VER
|
||||||
if (l1norm < 1.495585217958292e-002) {
|
template <typename Scalar> Scalar log2(Scalar v) { return std::log(v)/std::log(Scalar(2)); }
|
||||||
|
#endif
|
||||||
|
|
||||||
// Use (3,3)-Pade
|
/** \internal \brief Compute the (3,3)-Padé approximant to
|
||||||
|
* the exponential.
|
||||||
|
*
|
||||||
|
* After exit, \f$ (V+U)(V-U)^{-1} \f$ is the Padé
|
||||||
|
* approximant of \f$ \exp(M) \f$ around \f$ M = 0 \f$.
|
||||||
|
*
|
||||||
|
* \param M Argument of matrix exponential
|
||||||
|
* \param Id Identity matrix of same size as M
|
||||||
|
* \param tmp Temporary storage, to be provided by the caller
|
||||||
|
* \param M2 Temporary storage, to be provided by the caller
|
||||||
|
* \param U Even-degree terms in numerator of Padé approximant
|
||||||
|
* \param V Odd-degree terms in numerator of Padé approximant
|
||||||
|
*/
|
||||||
|
template <typename MatrixType>
|
||||||
|
EIGEN_STRONG_INLINE void pade3(const MatrixType &M, const MatrixType& Id, MatrixType& tmp,
|
||||||
|
MatrixType& M2, MatrixType& U, MatrixType& V)
|
||||||
|
{
|
||||||
|
typedef typename ei_traits<MatrixType>::Scalar Scalar;
|
||||||
const Scalar b[] = {120., 60., 12., 1.};
|
const Scalar b[] = {120., 60., 12., 1.};
|
||||||
PlainMatrixType M2;
|
M2 = (M * M).lazy();
|
||||||
M2 = (Meval * Meval).lazy();
|
tmp = b[3]*M2 + b[1]*Id;
|
||||||
num = b[3]*M2 + b[1]*Id;
|
U = (M * tmp).lazy();
|
||||||
U = (Meval * num).lazy();
|
|
||||||
V = b[2]*M2 + b[0]*Id;
|
V = b[2]*M2 + b[0]*Id;
|
||||||
|
|
||||||
} else if (l1norm < 2.539398330063230e-001) {
|
|
||||||
|
|
||||||
// Use (5,5)-Pade
|
|
||||||
const Scalar b[] = {30240., 15120., 3360., 420., 30., 1.};
|
|
||||||
PlainMatrixType M2, M4;
|
|
||||||
M2 = (Meval * Meval).lazy();
|
|
||||||
M4 = (M2 * M2).lazy();
|
|
||||||
num = b[5]*M4 + b[3]*M2 + b[1]*Id;
|
|
||||||
U = (Meval * num).lazy();
|
|
||||||
V = b[4]*M4 + b[2]*M2 + b[0]*Id;
|
|
||||||
|
|
||||||
} else if (l1norm < 9.504178996162932e-001) {
|
|
||||||
|
|
||||||
// Use (7,7)-Pade
|
|
||||||
const Scalar b[] = {17297280., 8648640., 1995840., 277200., 25200., 1512., 56., 1.};
|
|
||||||
PlainMatrixType M2, M4, M6;
|
|
||||||
M2 = (Meval * Meval).lazy();
|
|
||||||
M4 = (M2 * M2).lazy();
|
|
||||||
M6 = (M4 * M2).lazy();
|
|
||||||
num = b[7]*M6 + b[5]*M4 + b[3]*M2 + b[1]*Id;
|
|
||||||
U = (Meval * num).lazy();
|
|
||||||
V = b[6]*M6 + b[4]*M4 + b[2]*M2 + b[0]*Id;
|
|
||||||
|
|
||||||
} else if (l1norm < 2.097847961257068e+000) {
|
|
||||||
|
|
||||||
// Use (9,9)-Pade
|
|
||||||
const Scalar b[] = {17643225600., 8821612800., 2075673600., 302702400., 30270240.,
|
|
||||||
2162160., 110880., 3960., 90., 1.};
|
|
||||||
PlainMatrixType M2, M4, M6, M8;
|
|
||||||
M2 = (Meval * Meval).lazy();
|
|
||||||
M4 = (M2 * M2).lazy();
|
|
||||||
M6 = (M4 * M2).lazy();
|
|
||||||
M8 = (M6 * M2).lazy();
|
|
||||||
num = b[9]*M8 + b[7]*M6 + b[5]*M4 + b[3]*M2 + b[1]*Id;
|
|
||||||
U = (Meval * num).lazy();
|
|
||||||
V = b[8]*M8 + b[6]*M6 + b[4]*M4 + b[2]*M2 + b[0]*Id;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// Use (13,13)-Pade; scale matrix by power of 2 so that its norm
|
|
||||||
// is small enough
|
|
||||||
|
|
||||||
const Scalar maxnorm = 5.371920351148152;
|
|
||||||
const Scalar b[] = {64764752532480000., 32382376266240000., 7771770303897600.,
|
|
||||||
1187353796428800., 129060195264000., 10559470521600., 670442572800.,
|
|
||||||
33522128640., 1323241920., 40840800., 960960., 16380., 182., 1.};
|
|
||||||
|
|
||||||
squarings = std::max(0, (int)ceil(log2(l1norm / maxnorm)));
|
|
||||||
PlainMatrixType A, A2, A4, A6;
|
|
||||||
A = Meval / pow(Scalar(2), squarings);
|
|
||||||
A2 = (A * A).lazy();
|
|
||||||
A4 = (A2 * A2).lazy();
|
|
||||||
A6 = (A4 * A2).lazy();
|
|
||||||
num = b[13]*A6 + b[11]*A4 + b[9]*A2;
|
|
||||||
V = (A6 * num).lazy();
|
|
||||||
num = V + b[7]*A6 + b[5]*A4 + b[3]*A2 + b[1]*Id;
|
|
||||||
U = (A * num).lazy();
|
|
||||||
num = b[12]*A6 + b[10]*A4 + b[8]*A2;
|
|
||||||
V = (A6 * num).lazy() + b[6]*A6 + b[4]*A4 + b[2]*A2 + b[0]*Id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
num = U + V; // numerator of Pade approximant
|
/** \internal \brief Compute the (5,5)-Padé approximant to
|
||||||
den = -U + V; // denominator of Pade approximant
|
* the exponential.
|
||||||
den.lu().solve(num, result);
|
*
|
||||||
|
* After exit, \f$ (V+U)(V-U)^{-1} \f$ is the Padé
|
||||||
|
* approximant of \f$ \exp(M) \f$ around \f$ M = 0 \f$.
|
||||||
|
*
|
||||||
|
* \param M Argument of matrix exponential
|
||||||
|
* \param Id Identity matrix of same size as M
|
||||||
|
* \param tmp Temporary storage, to be provided by the caller
|
||||||
|
* \param M2 Temporary storage, to be provided by the caller
|
||||||
|
* \param U Even-degree terms in numerator of Padé approximant
|
||||||
|
* \param V Odd-degree terms in numerator of Padé approximant
|
||||||
|
*/
|
||||||
|
template <typename MatrixType>
|
||||||
|
EIGEN_STRONG_INLINE void pade5(const MatrixType &M, const MatrixType& Id, MatrixType& tmp,
|
||||||
|
MatrixType& M2, MatrixType& U, MatrixType& V)
|
||||||
|
{
|
||||||
|
typedef typename ei_traits<MatrixType>::Scalar Scalar;
|
||||||
|
const Scalar b[] = {30240., 15120., 3360., 420., 30., 1.};
|
||||||
|
M2 = (M * M).lazy();
|
||||||
|
MatrixType M4 = (M2 * M2).lazy();
|
||||||
|
tmp = b[5]*M4 + b[3]*M2 + b[1]*Id;
|
||||||
|
U = (M * tmp).lazy();
|
||||||
|
V = b[4]*M4 + b[2]*M2 + b[0]*Id;
|
||||||
|
}
|
||||||
|
|
||||||
// Undo scaling by repeated squaring
|
/** \internal \brief Compute the (7,7)-Padé approximant to
|
||||||
for (int i=0; i<squarings; i++)
|
* the exponential.
|
||||||
*result *= *result;
|
*
|
||||||
|
* After exit, \f$ (V+U)(V-U)^{-1} \f$ is the Padé
|
||||||
|
* approximant of \f$ \exp(M) \f$ around \f$ M = 0 \f$.
|
||||||
|
*
|
||||||
|
* \param M Argument of matrix exponential
|
||||||
|
* \param Id Identity matrix of same size as M
|
||||||
|
* \param tmp Temporary storage, to be provided by the caller
|
||||||
|
* \param M2 Temporary storage, to be provided by the caller
|
||||||
|
* \param U Even-degree terms in numerator of Padé approximant
|
||||||
|
* \param V Odd-degree terms in numerator of Padé approximant
|
||||||
|
*/
|
||||||
|
template <typename MatrixType>
|
||||||
|
EIGEN_STRONG_INLINE void pade7(const MatrixType &M, const MatrixType& Id, MatrixType& tmp,
|
||||||
|
MatrixType& M2, MatrixType& U, MatrixType& V)
|
||||||
|
{
|
||||||
|
typedef typename ei_traits<MatrixType>::Scalar Scalar;
|
||||||
|
const Scalar b[] = {17297280., 8648640., 1995840., 277200., 25200., 1512., 56., 1.};
|
||||||
|
M2 = (M * M).lazy();
|
||||||
|
MatrixType M4 = (M2 * M2).lazy();
|
||||||
|
MatrixType M6 = (M4 * M2).lazy();
|
||||||
|
tmp = b[7]*M6 + b[5]*M4 + b[3]*M2 + b[1]*Id;
|
||||||
|
U = (M * tmp).lazy();
|
||||||
|
V = b[6]*M6 + b[4]*M4 + b[2]*M2 + b[0]*Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** \internal \brief Compute the (9,9)-Padé approximant to
|
||||||
|
* the exponential.
|
||||||
|
*
|
||||||
|
* After exit, \f$ (V+U)(V-U)^{-1} \f$ is the Padé
|
||||||
|
* approximant of \f$ \exp(M) \f$ around \f$ M = 0 \f$.
|
||||||
|
*
|
||||||
|
* \param M Argument of matrix exponential
|
||||||
|
* \param Id Identity matrix of same size as M
|
||||||
|
* \param tmp Temporary storage, to be provided by the caller
|
||||||
|
* \param M2 Temporary storage, to be provided by the caller
|
||||||
|
* \param U Even-degree terms in numerator of Padé approximant
|
||||||
|
* \param V Odd-degree terms in numerator of Padé approximant
|
||||||
|
*/
|
||||||
|
template <typename MatrixType>
|
||||||
|
EIGEN_STRONG_INLINE void pade9(const MatrixType &M, const MatrixType& Id, MatrixType& tmp,
|
||||||
|
MatrixType& M2, MatrixType& U, MatrixType& V)
|
||||||
|
{
|
||||||
|
typedef typename ei_traits<MatrixType>::Scalar Scalar;
|
||||||
|
const Scalar b[] = {17643225600., 8821612800., 2075673600., 302702400., 30270240.,
|
||||||
|
2162160., 110880., 3960., 90., 1.};
|
||||||
|
M2 = (M * M).lazy();
|
||||||
|
MatrixType M4 = (M2 * M2).lazy();
|
||||||
|
MatrixType M6 = (M4 * M2).lazy();
|
||||||
|
MatrixType M8 = (M6 * M2).lazy();
|
||||||
|
tmp = b[9]*M8 + b[7]*M6 + b[5]*M4 + b[3]*M2 + b[1]*Id;
|
||||||
|
U = (M * tmp).lazy();
|
||||||
|
V = b[8]*M8 + b[6]*M6 + b[4]*M4 + b[2]*M2 + b[0]*Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** \internal \brief Compute the (13,13)-Padé approximant to
|
||||||
|
* the exponential.
|
||||||
|
*
|
||||||
|
* After exit, \f$ (V+U)(V-U)^{-1} \f$ is the Padé
|
||||||
|
* approximant of \f$ \exp(M) \f$ around \f$ M = 0 \f$.
|
||||||
|
*
|
||||||
|
* \param M Argument of matrix exponential
|
||||||
|
* \param Id Identity matrix of same size as M
|
||||||
|
* \param tmp Temporary storage, to be provided by the caller
|
||||||
|
* \param M2 Temporary storage, to be provided by the caller
|
||||||
|
* \param U Even-degree terms in numerator of Padé approximant
|
||||||
|
* \param V Odd-degree terms in numerator of Padé approximant
|
||||||
|
*/
|
||||||
|
template <typename MatrixType>
|
||||||
|
EIGEN_STRONG_INLINE void pade13(const MatrixType &M, const MatrixType& Id, MatrixType& tmp,
|
||||||
|
MatrixType& M2, MatrixType& U, MatrixType& V)
|
||||||
|
{
|
||||||
|
typedef typename ei_traits<MatrixType>::Scalar Scalar;
|
||||||
|
const Scalar b[] = {64764752532480000., 32382376266240000., 7771770303897600.,
|
||||||
|
1187353796428800., 129060195264000., 10559470521600., 670442572800.,
|
||||||
|
33522128640., 1323241920., 40840800., 960960., 16380., 182., 1.};
|
||||||
|
M2 = (M * M).lazy();
|
||||||
|
MatrixType M4 = (M2 * M2).lazy();
|
||||||
|
MatrixType M6 = (M4 * M2).lazy();
|
||||||
|
V = b[13]*M6 + b[11]*M4 + b[9]*M2;
|
||||||
|
tmp = (M6 * V).lazy();
|
||||||
|
tmp += b[7]*M6 + b[5]*M4 + b[3]*M2 + b[1]*Id;
|
||||||
|
U = (M * tmp).lazy();
|
||||||
|
tmp = b[12]*M6 + b[10]*M4 + b[8]*M2;
|
||||||
|
V = (M6 * tmp).lazy();
|
||||||
|
V += b[6]*M6 + b[4]*M4 + b[2]*M2 + b[0]*Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** \internal \brief Helper class for computing Padé
|
||||||
|
* approximants to the exponential.
|
||||||
|
*/
|
||||||
|
template <typename MatrixType, typename RealScalar = typename NumTraits<typename ei_traits<MatrixType>::Scalar>::Real>
|
||||||
|
struct computeUV_selector
|
||||||
|
{
|
||||||
|
/** \internal \brief Compute Padé approximant to the exponential.
|
||||||
|
*
|
||||||
|
* Computes \p U, \p V and \p squarings such that \f$ (V+U)(V-U)^{-1} \f$
|
||||||
|
* is a Padé of \f$ \exp(2^{-\mbox{squarings}}M) \f$
|
||||||
|
* around \f$ M = 0 \f$. The degree of the Padé
|
||||||
|
* approximant and the value of squarings are chosen such that
|
||||||
|
* the approximation error is no more than the round-off error.
|
||||||
|
*
|
||||||
|
* \param M Argument of matrix exponential
|
||||||
|
* \param Id Identity matrix of same size as M
|
||||||
|
* \param tmp1 Temporary storage, to be provided by the caller
|
||||||
|
* \param tmp2 Temporary storage, to be provided by the caller
|
||||||
|
* \param U Even-degree terms in numerator of Padé approximant
|
||||||
|
* \param V Odd-degree terms in numerator of Padé approximant
|
||||||
|
* \param l1norm L<sub>1</sub> norm of M
|
||||||
|
* \param squarings Pointer to integer containing number of times
|
||||||
|
* that the result needs to be squared to find the
|
||||||
|
* matrix exponential
|
||||||
|
*/
|
||||||
|
static void run(const MatrixType &M, const MatrixType& Id, MatrixType& tmp1, MatrixType& tmp2,
|
||||||
|
MatrixType& U, MatrixType& V, float l1norm, int* squarings);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename MatrixType>
|
||||||
|
struct computeUV_selector<MatrixType, float>
|
||||||
|
{
|
||||||
|
static void run(const MatrixType &M, const MatrixType& Id, MatrixType& tmp1, MatrixType& tmp2,
|
||||||
|
MatrixType& U, MatrixType& V, float l1norm, int* squarings)
|
||||||
|
{
|
||||||
|
*squarings = 0;
|
||||||
|
if (l1norm < 4.258730016922831e-001) {
|
||||||
|
pade3(M, Id, tmp1, tmp2, U, V);
|
||||||
|
} else if (l1norm < 1.880152677804762e+000) {
|
||||||
|
pade5(M, Id, tmp1, tmp2, U, V);
|
||||||
|
} else {
|
||||||
|
const float maxnorm = 3.925724783138660;
|
||||||
|
*squarings = std::max(0, (int)ceil(log2(l1norm / maxnorm)));
|
||||||
|
MatrixType A = M / std::pow(typename ei_traits<MatrixType>::Scalar(2), *squarings);
|
||||||
|
pade7(A, Id, tmp1, tmp2, U, V);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename MatrixType>
|
||||||
|
struct computeUV_selector<MatrixType, double>
|
||||||
|
{
|
||||||
|
static void run(const MatrixType &M, const MatrixType& Id, MatrixType& tmp1, MatrixType& tmp2,
|
||||||
|
MatrixType& U, MatrixType& V, float l1norm, int* squarings)
|
||||||
|
{
|
||||||
|
*squarings = 0;
|
||||||
|
if (l1norm < 1.495585217958292e-002) {
|
||||||
|
pade3(M, Id, tmp1, tmp2, U, V);
|
||||||
|
} else if (l1norm < 2.539398330063230e-001) {
|
||||||
|
pade5(M, Id, tmp1, tmp2, U, V);
|
||||||
|
} else if (l1norm < 9.504178996162932e-001) {
|
||||||
|
pade7(M, Id, tmp1, tmp2, U, V);
|
||||||
|
} else if (l1norm < 2.097847961257068e+000) {
|
||||||
|
pade9(M, Id, tmp1, tmp2, U, V);
|
||||||
|
} else {
|
||||||
|
const double maxnorm = 5.371920351148152;
|
||||||
|
*squarings = std::max(0, (int)ceil(log2(l1norm / maxnorm)));
|
||||||
|
MatrixType A = M / std::pow(typename ei_traits<MatrixType>::Scalar(2), *squarings);
|
||||||
|
pade13(A, Id, tmp1, tmp2, U, V);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** \internal \brief Compute the matrix exponential.
|
||||||
|
*
|
||||||
|
* \param M matrix whose exponential is to be computed.
|
||||||
|
* \param result pointer to the matrix in which to store the result.
|
||||||
|
*/
|
||||||
|
template <typename MatrixType>
|
||||||
|
void compute(const MatrixType &M, MatrixType* result)
|
||||||
|
{
|
||||||
|
MatrixType num, den, U, V;
|
||||||
|
MatrixType Id = MatrixType::Identity(M.rows(), M.cols());
|
||||||
|
float l1norm = M.cwise().abs().colwise().sum().maxCoeff();
|
||||||
|
int squarings;
|
||||||
|
computeUV_selector<MatrixType>::run(M, Id, num, den, U, V, l1norm, &squarings);
|
||||||
|
num = U + V; // numerator of Pade approximant
|
||||||
|
den = -U + V; // denominator of Pade approximant
|
||||||
|
den.partialLu().solve(num, result);
|
||||||
|
for (int i=0; i<squarings; i++)
|
||||||
|
*result *= *result; // undo scaling by repeated squaring
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end of namespace MatrixExponentialInternal
|
||||||
|
|
||||||
|
template <typename Derived>
|
||||||
|
EIGEN_STRONG_INLINE void ei_matrix_exponential(const MatrixBase<Derived> &M,
|
||||||
|
typename MatrixBase<Derived>::PlainMatrixType* result)
|
||||||
|
{
|
||||||
|
ei_assert(M.rows() == M.cols());
|
||||||
|
MatrixExponentialInternal::compute(M.eval(), result);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // EIGEN_MATRIX_EXPONENTIAL
|
#endif // EIGEN_MATRIX_EXPONENTIAL
|
||||||
|
@ -34,26 +34,47 @@ double binom(int n, int k)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test2dRotation()
|
template <typename T>
|
||||||
|
void test2dRotation(double tol)
|
||||||
{
|
{
|
||||||
Matrix2d A, B, C;
|
Matrix<T,2,2> A, B, C;
|
||||||
double angle;
|
T angle;
|
||||||
|
|
||||||
|
A << 0, 1, -1, 0;
|
||||||
for (int i=0; i<=20; i++)
|
for (int i=0; i<=20; i++)
|
||||||
{
|
{
|
||||||
angle = pow(10, i / 5. - 2);
|
angle = pow(10, i / 5. - 2);
|
||||||
A << 0, angle, -angle, 0;
|
|
||||||
B << cos(angle), sin(angle), -sin(angle), cos(angle);
|
B << cos(angle), sin(angle), -sin(angle), cos(angle);
|
||||||
ei_matrix_exponential(A, &C);
|
ei_matrix_exponential(angle*A, &C);
|
||||||
VERIFY(C.isApprox(B, 1e-14));
|
VERIFY(C.isApprox(B, tol));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void testPascal()
|
template <typename T>
|
||||||
|
void test2dHyperbolicRotation(double tol)
|
||||||
|
{
|
||||||
|
Matrix<std::complex<T>,2,2> A, B, C;
|
||||||
|
std::complex<T> imagUnit(0,1);
|
||||||
|
T angle, ch, sh;
|
||||||
|
|
||||||
|
for (int i=0; i<=20; i++)
|
||||||
|
{
|
||||||
|
angle = (i-10) / 2.0;
|
||||||
|
ch = std::cosh(angle);
|
||||||
|
sh = std::sinh(angle);
|
||||||
|
A << 0, angle*imagUnit, -angle*imagUnit, 0;
|
||||||
|
B << ch, sh*imagUnit, -sh*imagUnit, ch;
|
||||||
|
ei_matrix_exponential(A, &C);
|
||||||
|
VERIFY(C.isApprox(B, tol));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void testPascal(double tol)
|
||||||
{
|
{
|
||||||
for (int size=1; size<20; size++)
|
for (int size=1; size<20; size++)
|
||||||
{
|
{
|
||||||
MatrixXd A(size,size), B(size,size), C(size,size);
|
Matrix<T,Dynamic,Dynamic> A(size,size), B(size,size), C(size,size);
|
||||||
A.setZero();
|
A.setZero();
|
||||||
for (int i=0; i<size-1; i++)
|
for (int i=0; i<size-1; i++)
|
||||||
A(i+1,i) = i+1;
|
A(i+1,i) = i+1;
|
||||||
@ -62,11 +83,12 @@ void testPascal()
|
|||||||
for (int j=0; j<=i; j++)
|
for (int j=0; j<=i; j++)
|
||||||
B(i,j) = binom(i,j);
|
B(i,j) = binom(i,j);
|
||||||
ei_matrix_exponential(A, &C);
|
ei_matrix_exponential(A, &C);
|
||||||
VERIFY(C.isApprox(B, 1e-14));
|
VERIFY(C.isApprox(B, tol));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename MatrixType> void randomTest(const MatrixType& m)
|
template<typename MatrixType>
|
||||||
|
void randomTest(const MatrixType& m, double tol)
|
||||||
{
|
{
|
||||||
/* this test covers the following files:
|
/* this test covers the following files:
|
||||||
Inverse.h
|
Inverse.h
|
||||||
@ -80,16 +102,24 @@ template<typename MatrixType> void randomTest(const MatrixType& m)
|
|||||||
m1 = MatrixType::Random(rows, cols);
|
m1 = MatrixType::Random(rows, cols);
|
||||||
ei_matrix_exponential(m1, &m2);
|
ei_matrix_exponential(m1, &m2);
|
||||||
ei_matrix_exponential(-m1, &m3);
|
ei_matrix_exponential(-m1, &m3);
|
||||||
VERIFY(identity.isApprox(m2 * m3, 1e-13));
|
VERIFY(identity.isApprox(m2 * m3, tol));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_matrixExponential()
|
void test_matrixExponential()
|
||||||
{
|
{
|
||||||
CALL_SUBTEST(test2dRotation());
|
CALL_SUBTEST(test2dRotation<double>(1e-14));
|
||||||
CALL_SUBTEST(testPascal());
|
CALL_SUBTEST(test2dRotation<float>(1e-5));
|
||||||
CALL_SUBTEST(randomTest(Matrix2d()));
|
CALL_SUBTEST(test2dHyperbolicRotation<double>(1e-14));
|
||||||
CALL_SUBTEST(randomTest(Matrix3d()));
|
CALL_SUBTEST(test2dHyperbolicRotation<float>(1e-5));
|
||||||
CALL_SUBTEST(randomTest(Matrix4d()));
|
CALL_SUBTEST(testPascal<float>(1e-5));
|
||||||
CALL_SUBTEST(randomTest(MatrixXd(8,8)));
|
CALL_SUBTEST(testPascal<double>(1e-14));
|
||||||
|
CALL_SUBTEST(randomTest(Matrix2d(), 1e-13));
|
||||||
|
CALL_SUBTEST(randomTest(Matrix<double,3,3,RowMajor>(), 1e-13));
|
||||||
|
CALL_SUBTEST(randomTest(Matrix4cd(), 1e-13));
|
||||||
|
CALL_SUBTEST(randomTest(MatrixXd(8,8), 1e-13));
|
||||||
|
CALL_SUBTEST(randomTest(Matrix2f(), 1e-4));
|
||||||
|
CALL_SUBTEST(randomTest(Matrix3cf(), 1e-4));
|
||||||
|
CALL_SUBTEST(randomTest(Matrix4f(), 1e-4));
|
||||||
|
CALL_SUBTEST(randomTest(MatrixXf(8,8), 1e-4));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user