From 65cd1c7639e6dab0416ecc440b01e7257554dfc0 Mon Sep 17 00:00:00 2001 From: Jitse Niesen Date: Mon, 11 Jan 2010 18:05:30 +0000 Subject: Add support for matrix sine, cosine, sinh and cosh. --- unsupported/test/matrix_function.cpp | 115 +++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 unsupported/test/matrix_function.cpp (limited to 'unsupported/test/matrix_function.cpp') diff --git a/unsupported/test/matrix_function.cpp b/unsupported/test/matrix_function.cpp new file mode 100644 index 000000000..0eb30eecb --- /dev/null +++ b/unsupported/test/matrix_function.cpp @@ -0,0 +1,115 @@ +// 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 + +template +void testMatrixExponential(const MatrixType& m) +{ + typedef typename ei_traits::Scalar Scalar; + typedef typename NumTraits::Real RealScalar; + typedef std::complex ComplexScalar; + + const int rows = m.rows(); + const int cols = m.cols(); + + for (int i = 0; i < g_repeat; i++) { + MatrixType A = MatrixType::Random(rows, cols); + MatrixType expA1, expA2; + ei_matrix_exponential(A, &expA1); + ei_matrix_function(A, StdStemFunctions::exp, &expA2); + VERIFY_IS_APPROX(expA1, expA2); + } +} + +template +void testHyperbolicFunctions(const MatrixType& m) +{ + const int rows = m.rows(); + const int cols = m.cols(); + + for (int i = 0; i < g_repeat; i++) { + MatrixType A = MatrixType::Random(rows, cols); + MatrixType sinhA, coshA, expA; + ei_matrix_sinh(A, &sinhA); + ei_matrix_cosh(A, &coshA); + ei_matrix_exponential(A, &expA); + VERIFY_IS_APPROX(sinhA, (expA - expA.inverse())/2); + VERIFY_IS_APPROX(coshA, (expA + expA.inverse())/2); + } +} + +template +void testGonioFunctions(const MatrixType& m) +{ + typedef ei_traits Traits; + typedef typename Traits::Scalar Scalar; + typedef typename NumTraits::Real RealScalar; + typedef std::complex ComplexScalar; + typedef Matrix ComplexMatrix; + + const int rows = m.rows(); + const int cols = m.cols(); + ComplexScalar imagUnit(0,1); + ComplexScalar two(2,0); + + for (int i = 0; i < g_repeat; i++) { + MatrixType A = MatrixType::Random(rows, cols); + ComplexMatrix Ac = A.template cast(); + + ComplexMatrix exp_iA; + ei_matrix_exponential(imagUnit * Ac, &exp_iA); + + MatrixType sinA; + ei_matrix_sin(A, &sinA); + ComplexMatrix sinAc = sinA.template cast(); + VERIFY_IS_APPROX(sinAc, (exp_iA - exp_iA.inverse()) / (two*imagUnit)); + + MatrixType cosA; + ei_matrix_cos(A, &cosA); + ComplexMatrix cosAc = cosA.template cast(); + VERIFY_IS_APPROX(cosAc, (exp_iA + exp_iA.inverse()) / 2); + } +} + +template +void testMatrixType(const MatrixType& m) +{ + testMatrixExponential(m); + testHyperbolicFunctions(m); + testGonioFunctions(m); +} + +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))); +} -- cgit v1.2.3