// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2010 Jitse Niesen // // Eigen is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 3 of the License, or (at your option) any later version. // // Alternatively, you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of // the License, or (at your option) any later version. // // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the // GNU General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License and a copy of the GNU General Public License along with // Eigen. If not, see . #include "main.h" #include // Returns a matrix with eigenvalues clustered around 0, 1 and 2. template MatrixType randomMatrixWithRealEivals(const int size) { typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::RealScalar RealScalar; MatrixType diag = MatrixType::Zero(size, size); for (int i = 0; i < size; ++i) { diag(i, i) = Scalar(RealScalar(ei_random(0,2))) + ei_random() * Scalar(RealScalar(0.01)); } MatrixType A = MatrixType::Random(size, size); return A.inverse() * diag * A; } template ::Scalar>::IsComplex> struct randomMatrixWithImagEivals { // Returns a matrix with eigenvalues clustered around 0 and +/- i. static MatrixType run(const int size); }; // Partial specialization for real matrices template struct randomMatrixWithImagEivals { static MatrixType run(const int size) { typedef typename MatrixType::Scalar Scalar; MatrixType diag = MatrixType::Zero(size, size); int i = 0; while (i < size) { int randomInt = ei_random(-1, 1); if (randomInt == 0 || i == size-1) { diag(i, i) = ei_random() * Scalar(0.01); ++i; } else { Scalar alpha = Scalar(randomInt) + ei_random() * Scalar(0.01); diag(i, i+1) = alpha; diag(i+1, i) = -alpha; i += 2; } } MatrixType A = MatrixType::Random(size, size); return A.inverse() * diag * A; } }; // Partial specialization for complex matrices template struct randomMatrixWithImagEivals { static MatrixType run(const int size) { typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::RealScalar RealScalar; const Scalar imagUnit(0, 1); MatrixType diag = MatrixType::Zero(size, size); for (int i = 0; i < size; ++i) { diag(i, i) = Scalar(RealScalar(ei_random(-1, 1))) * imagUnit + ei_random() * Scalar(RealScalar(0.01)); } MatrixType A = MatrixType::Random(size, size); return A.inverse() * diag * A; } }; template void testMatrixExponential(const MatrixType& A) { typedef typename ei_traits::Scalar Scalar; typedef typename NumTraits::Real RealScalar; typedef std::complex ComplexScalar; for (int i = 0; i < g_repeat; i++) { VERIFY_IS_APPROX(ei_matrix_exponential(A), ei_matrix_function(A, StdStemFunctions::exp)); } } template void testHyperbolicFunctions(const MatrixType& A) { for (int i = 0; i < g_repeat; i++) { MatrixType sinhA = ei_matrix_sinh(A); MatrixType coshA = ei_matrix_cosh(A); MatrixType expA = ei_matrix_exponential(A); VERIFY_IS_APPROX(sinhA, (expA - expA.inverse())/2); VERIFY_IS_APPROX(coshA, (expA + expA.inverse())/2); } } template void testGonioFunctions(const MatrixType& A) { typedef ei_traits Traits; typedef typename Traits::Scalar Scalar; typedef typename NumTraits::Real RealScalar; typedef std::complex ComplexScalar; typedef Matrix ComplexMatrix; ComplexScalar imagUnit(0,1); ComplexScalar two(2,0); for (int i = 0; i < g_repeat; i++) { ComplexMatrix Ac = A.template cast(); ComplexMatrix exp_iA = ei_matrix_exponential(imagUnit * Ac); MatrixType sinA = ei_matrix_sin(A); ComplexMatrix sinAc = sinA.template cast(); VERIFY_IS_APPROX(sinAc, (exp_iA - exp_iA.inverse()) / (two*imagUnit)); MatrixType cosA = ei_matrix_cos(A); ComplexMatrix cosAc = cosA.template cast(); VERIFY_IS_APPROX(cosAc, (exp_iA + exp_iA.inverse()) / 2); } } template void testMatrix(const MatrixType& A) { testMatrixExponential(A); testHyperbolicFunctions(A); testGonioFunctions(A); } template void testMatrixType(const MatrixType& m) { // Matrices with clustered eigenvalue lead to different code paths // in MatrixFunction.h and are thus useful for testing. const int size = m.rows(); for (int i = 0; i < g_repeat; i++) { testMatrix(MatrixType::Random(size, size).eval()); testMatrix(randomMatrixWithRealEivals(size)); testMatrix(randomMatrixWithImagEivals::run(size)); } } void test_matrix_function() { CALL_SUBTEST_1(testMatrixType(Matrix())); CALL_SUBTEST_2(testMatrixType(Matrix3cf())); CALL_SUBTEST_3(testMatrixType(MatrixXf(8,8))); CALL_SUBTEST_4(testMatrixType(Matrix2d())); CALL_SUBTEST_5(testMatrixType(Matrix())); CALL_SUBTEST_6(testMatrixType(Matrix4cd())); CALL_SUBTEST_7(testMatrixType(MatrixXd(13,13))); }