00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "main.h"
00026 #include <unsupported/Eigen/MatrixFunctions>
00027
00028 double binom(int n, int k)
00029 {
00030 double res = 1;
00031 for (int i=0; i<k; i++)
00032 res = res * (n-k+i+1) / (i+1);
00033 return res;
00034 }
00035
00036 template <typename Derived, typename OtherDerived>
00037 double relerr(const MatrixBase<Derived>& A, const MatrixBase<OtherDerived>& B)
00038 {
00039 return std::sqrt((A - B).cwiseAbs2().sum() / std::min(A.cwiseAbs2().sum(), B.cwiseAbs2().sum()));
00040 }
00041
00042 template <typename T>
00043 T expfn(T x, int)
00044 {
00045 return std::exp(x);
00046 }
00047
00048 template <typename T>
00049 void test2dRotation(double tol)
00050 {
00051 Matrix<T,2,2> A, B, C;
00052 T angle;
00053
00054 A << 0, 1, -1, 0;
00055 for (int i=0; i<=20; i++)
00056 {
00057 angle = static_cast<T>(pow(10, i / 5. - 2));
00058 B << cos(angle), sin(angle), -sin(angle), cos(angle);
00059
00060 C = (angle*A).matrixFunction(expfn);
00061 std::cout << "test2dRotation: i = " << i << " error funm = " << relerr(C, B);
00062 VERIFY(C.isApprox(B, static_cast<T>(tol)));
00063
00064 C = (angle*A).exp();
00065 std::cout << " error expm = " << relerr(C, B) << "\n";
00066 VERIFY(C.isApprox(B, static_cast<T>(tol)));
00067 }
00068 }
00069
00070 template <typename T>
00071 void test2dHyperbolicRotation(double tol)
00072 {
00073 Matrix<std::complex<T>,2,2> A, B, C;
00074 std::complex<T> imagUnit(0,1);
00075 T angle, ch, sh;
00076
00077 for (int i=0; i<=20; i++)
00078 {
00079 angle = static_cast<T>((i-10) / 2.0);
00080 ch = std::cosh(angle);
00081 sh = std::sinh(angle);
00082 A << 0, angle*imagUnit, -angle*imagUnit, 0;
00083 B << ch, sh*imagUnit, -sh*imagUnit, ch;
00084
00085 C = A.matrixFunction(expfn);
00086 std::cout << "test2dHyperbolicRotation: i = " << i << " error funm = " << relerr(C, B);
00087 VERIFY(C.isApprox(B, static_cast<T>(tol)));
00088
00089 C = A.exp();
00090 std::cout << " error expm = " << relerr(C, B) << "\n";
00091 VERIFY(C.isApprox(B, static_cast<T>(tol)));
00092 }
00093 }
00094
00095 template <typename T>
00096 void testPascal(double tol)
00097 {
00098 for (int size=1; size<20; size++)
00099 {
00100 Matrix<T,Dynamic,Dynamic> A(size,size), B(size,size), C(size,size);
00101 A.setZero();
00102 for (int i=0; i<size-1; i++)
00103 A(i+1,i) = static_cast<T>(i+1);
00104 B.setZero();
00105 for (int i=0; i<size; i++)
00106 for (int j=0; j<=i; j++)
00107 B(i,j) = static_cast<T>(binom(i,j));
00108
00109 C = A.matrixFunction(expfn);
00110 std::cout << "testPascal: size = " << size << " error funm = " << relerr(C, B);
00111 VERIFY(C.isApprox(B, static_cast<T>(tol)));
00112
00113 C = A.exp();
00114 std::cout << " error expm = " << relerr(C, B) << "\n";
00115 VERIFY(C.isApprox(B, static_cast<T>(tol)));
00116 }
00117 }
00118
00119 template<typename MatrixType>
00120 void randomTest(const MatrixType& m, double tol)
00121 {
00122
00123
00124
00125 typename MatrixType::Index rows = m.rows();
00126 typename MatrixType::Index cols = m.cols();
00127 MatrixType m1(rows, cols), m2(rows, cols), m3(rows, cols),
00128 identity = MatrixType::Identity(rows, rows);
00129
00130 typedef typename NumTraits<typename internal::traits<MatrixType>::Scalar>::Real RealScalar;
00131
00132 for(int i = 0; i < g_repeat; i++) {
00133 m1 = MatrixType::Random(rows, cols);
00134
00135 m2 = m1.matrixFunction(expfn) * (-m1).matrixFunction(expfn);
00136 std::cout << "randomTest: error funm = " << relerr(identity, m2);
00137 VERIFY(identity.isApprox(m2, static_cast<RealScalar>(tol)));
00138
00139 m2 = m1.exp() * (-m1).exp();
00140 std::cout << " error expm = " << relerr(identity, m2) << "\n";
00141 VERIFY(identity.isApprox(m2, static_cast<RealScalar>(tol)));
00142 }
00143 }
00144
00145 void test_matrix_exponential()
00146 {
00147 CALL_SUBTEST_2(test2dRotation<double>(1e-13));
00148 CALL_SUBTEST_1(test2dRotation<float>(2e-5));
00149 CALL_SUBTEST_2(test2dHyperbolicRotation<double>(1e-14));
00150 CALL_SUBTEST_1(test2dHyperbolicRotation<float>(1e-5));
00151 CALL_SUBTEST_6(testPascal<float>(1e-6));
00152 CALL_SUBTEST_5(testPascal<double>(1e-15));
00153 CALL_SUBTEST_2(randomTest(Matrix2d(), 1e-13));
00154 CALL_SUBTEST_7(randomTest(Matrix<double,3,3,RowMajor>(), 1e-13));
00155 CALL_SUBTEST_3(randomTest(Matrix4cd(), 1e-13));
00156 CALL_SUBTEST_4(randomTest(MatrixXd(8,8), 1e-13));
00157 CALL_SUBTEST_1(randomTest(Matrix2f(), 1e-4));
00158 CALL_SUBTEST_5(randomTest(Matrix3cf(), 1e-4));
00159 CALL_SUBTEST_1(randomTest(Matrix4f(), 1e-4));
00160 CALL_SUBTEST_6(randomTest(MatrixXf(8,8), 1e-4));
00161 }