From e43934d60f20d3f3884493d542bdcb0cb330c39a Mon Sep 17 00:00:00 2001 From: Jitse Niesen Date: Fri, 26 Jul 2013 13:51:10 +0100 Subject: [PATCH] MatrixFunctions: Clean up StemFunction.h --- .../src/MatrixFunctions/MatrixFunction.h | 8 +- .../Eigen/src/MatrixFunctions/StemFunction.h | 170 ++++++++++-------- unsupported/test/matrix_function.cpp | 2 +- 3 files changed, 96 insertions(+), 84 deletions(-) diff --git a/unsupported/Eigen/src/MatrixFunctions/MatrixFunction.h b/unsupported/Eigen/src/MatrixFunctions/MatrixFunction.h index 12e28793d..8a6b84c09 100644 --- a/unsupported/Eigen/src/MatrixFunctions/MatrixFunction.h +++ b/unsupported/Eigen/src/MatrixFunctions/MatrixFunction.h @@ -547,7 +547,7 @@ const MatrixFunctionReturnValue MatrixBase::sin() const { eigen_assert(rows() == cols()); typedef typename internal::stem_function::ComplexScalar ComplexScalar; - return MatrixFunctionReturnValue(derived(), StdStemFunctions::sin); + return MatrixFunctionReturnValue(derived(), internal::stem_function_sin); } template @@ -555,7 +555,7 @@ const MatrixFunctionReturnValue MatrixBase::cos() const { eigen_assert(rows() == cols()); typedef typename internal::stem_function::ComplexScalar ComplexScalar; - return MatrixFunctionReturnValue(derived(), StdStemFunctions::cos); + return MatrixFunctionReturnValue(derived(), internal::stem_function_cos); } template @@ -563,7 +563,7 @@ const MatrixFunctionReturnValue MatrixBase::sinh() const { eigen_assert(rows() == cols()); typedef typename internal::stem_function::ComplexScalar ComplexScalar; - return MatrixFunctionReturnValue(derived(), StdStemFunctions::sinh); + return MatrixFunctionReturnValue(derived(), internal::stem_function_sinh); } template @@ -571,7 +571,7 @@ const MatrixFunctionReturnValue MatrixBase::cosh() const { eigen_assert(rows() == cols()); typedef typename internal::stem_function::ComplexScalar ComplexScalar; - return MatrixFunctionReturnValue(derived(), StdStemFunctions::cosh); + return MatrixFunctionReturnValue(derived(), internal::stem_function_cosh); } } // end namespace Eigen diff --git a/unsupported/Eigen/src/MatrixFunctions/StemFunction.h b/unsupported/Eigen/src/MatrixFunctions/StemFunction.h index 0a815d834..cd45fe5b4 100644 --- a/unsupported/Eigen/src/MatrixFunctions/StemFunction.h +++ b/unsupported/Eigen/src/MatrixFunctions/StemFunction.h @@ -12,93 +12,105 @@ namespace Eigen { -/** \ingroup MatrixFunctions_Module - * \brief Stem functions corresponding to standard mathematical functions. - */ +namespace internal { + +/** \brief The exponential function (and its derivatives). */ template -class StdStemFunctions : internal::noncopyable +Scalar stem_function_exp(Scalar x, int) { - public: + using std::exp; + return exp(x); +} - /** \brief The exponential function (and its derivatives). */ - static Scalar exp(Scalar x, int) - { - return std::exp(x); - } +/** \brief Cosine (and its derivatives). */ +template +Scalar stem_function_cos(Scalar x, int n) +{ + using std::cos; + using std::sin; + Scalar res; - /** \brief Cosine (and its derivatives). */ - static Scalar cos(Scalar x, int n) - { - Scalar res; - switch (n % 4) { - case 0: - res = std::cos(x); - break; - case 1: - res = -std::sin(x); - break; - case 2: - res = -std::cos(x); - break; - case 3: - res = std::sin(x); - break; - } - return res; - } + switch (n % 4) { + case 0: + res = std::cos(x); + break; + case 1: + res = -std::sin(x); + break; + case 2: + res = -std::cos(x); + break; + case 3: + res = std::sin(x); + break; + } + return res; +} - /** \brief Sine (and its derivatives). */ - static Scalar sin(Scalar x, int n) - { - Scalar res; - switch (n % 4) { - case 0: - res = std::sin(x); - break; - case 1: - res = std::cos(x); - break; - case 2: - res = -std::sin(x); - break; - case 3: - res = -std::cos(x); - break; - } - return res; - } +/** \brief Sine (and its derivatives). */ +template +Scalar stem_function_sin(Scalar x, int n) +{ + using std::cos; + using std::sin; + Scalar res; - /** \brief Hyperbolic cosine (and its derivatives). */ - static Scalar cosh(Scalar x, int n) - { - Scalar res; - switch (n % 2) { - case 0: - res = std::cosh(x); - break; - case 1: - res = std::sinh(x); - break; - } - return res; - } + switch (n % 4) { + case 0: + res = std::sin(x); + break; + case 1: + res = std::cos(x); + break; + case 2: + res = -std::sin(x); + break; + case 3: + res = -std::cos(x); + break; + } + return res; +} + +/** \brief Hyperbolic cosine (and its derivatives). */ +template +Scalar stem_function_cosh(Scalar x, int n) +{ + using std::cosh; + using std::sinh; + Scalar res; + + switch (n % 2) { + case 0: + res = std::cosh(x); + break; + case 1: + res = std::sinh(x); + break; + } + return res; +} - /** \brief Hyperbolic sine (and its derivatives). */ - static Scalar sinh(Scalar x, int n) - { - Scalar res; - switch (n % 2) { - case 0: - res = std::sinh(x); - break; - case 1: - res = std::cosh(x); - break; - } - return res; - } +/** \brief Hyperbolic sine (and its derivatives). */ +template +Scalar stem_function_sinh(Scalar x, int n) +{ + using std::cosh; + using std::sinh; + Scalar res; + + switch (n % 2) { + case 0: + res = std::sinh(x); + break; + case 1: + res = std::cosh(x); + break; + } + return res; +} -}; // end of class StdStemFunctions +} // end namespace internal } // end namespace Eigen diff --git a/unsupported/test/matrix_function.cpp b/unsupported/test/matrix_function.cpp index 3c76cfb65..487d5a9b8 100644 --- a/unsupported/test/matrix_function.cpp +++ b/unsupported/test/matrix_function.cpp @@ -102,7 +102,7 @@ void testMatrixExponential(const MatrixType& A) typedef typename NumTraits::Real RealScalar; typedef std::complex ComplexScalar; - VERIFY_IS_APPROX(A.exp(), A.matrixFunction(StdStemFunctions::exp)); + VERIFY_IS_APPROX(A.exp(), A.matrixFunction(internal::stem_function_exp)); } template