Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
| 2 | // for linear algebra. |
| 3 | // |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 4 | // Copyright (C) 2010, 2013 Jitse Niesen <jitse@maths.leeds.ac.uk> |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 5 | // |
| 6 | // This Source Code Form is subject to the terms of the Mozilla |
| 7 | // Public License v. 2.0. If a copy of the MPL was not distributed |
| 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | |
| 10 | #ifndef EIGEN_STEM_FUNCTION |
| 11 | #define EIGEN_STEM_FUNCTION |
| 12 | |
| 13 | namespace Eigen { |
| 14 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 15 | namespace internal { |
| 16 | |
| 17 | /** \brief The exponential function (and its derivatives). */ |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 18 | template <typename Scalar> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 19 | Scalar stem_function_exp(Scalar x, int) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 20 | { |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 21 | using std::exp; |
| 22 | return exp(x); |
| 23 | } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 24 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 25 | /** \brief Cosine (and its derivatives). */ |
| 26 | template <typename Scalar> |
| 27 | Scalar stem_function_cos(Scalar x, int n) |
| 28 | { |
| 29 | using std::cos; |
| 30 | using std::sin; |
| 31 | Scalar res; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 32 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 33 | switch (n % 4) { |
| 34 | case 0: |
| 35 | res = std::cos(x); |
| 36 | break; |
| 37 | case 1: |
| 38 | res = -std::sin(x); |
| 39 | break; |
| 40 | case 2: |
| 41 | res = -std::cos(x); |
| 42 | break; |
| 43 | case 3: |
| 44 | res = std::sin(x); |
| 45 | break; |
| 46 | } |
| 47 | return res; |
| 48 | } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 49 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 50 | /** \brief Sine (and its derivatives). */ |
| 51 | template <typename Scalar> |
| 52 | Scalar stem_function_sin(Scalar x, int n) |
| 53 | { |
| 54 | using std::cos; |
| 55 | using std::sin; |
| 56 | Scalar res; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 57 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 58 | switch (n % 4) { |
| 59 | case 0: |
| 60 | res = std::sin(x); |
| 61 | break; |
| 62 | case 1: |
| 63 | res = std::cos(x); |
| 64 | break; |
| 65 | case 2: |
| 66 | res = -std::sin(x); |
| 67 | break; |
| 68 | case 3: |
| 69 | res = -std::cos(x); |
| 70 | break; |
| 71 | } |
| 72 | return res; |
| 73 | } |
| 74 | |
| 75 | /** \brief Hyperbolic cosine (and its derivatives). */ |
| 76 | template <typename Scalar> |
| 77 | Scalar stem_function_cosh(Scalar x, int n) |
| 78 | { |
| 79 | using std::cosh; |
| 80 | using std::sinh; |
| 81 | Scalar res; |
| 82 | |
| 83 | switch (n % 2) { |
| 84 | case 0: |
| 85 | res = std::cosh(x); |
| 86 | break; |
| 87 | case 1: |
| 88 | res = std::sinh(x); |
| 89 | break; |
| 90 | } |
| 91 | return res; |
| 92 | } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 93 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 94 | /** \brief Hyperbolic sine (and its derivatives). */ |
| 95 | template <typename Scalar> |
| 96 | Scalar stem_function_sinh(Scalar x, int n) |
| 97 | { |
| 98 | using std::cosh; |
| 99 | using std::sinh; |
| 100 | Scalar res; |
| 101 | |
| 102 | switch (n % 2) { |
| 103 | case 0: |
| 104 | res = std::sinh(x); |
| 105 | break; |
| 106 | case 1: |
| 107 | res = std::cosh(x); |
| 108 | break; |
| 109 | } |
| 110 | return res; |
| 111 | } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 112 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 113 | } // end namespace internal |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 114 | |
| 115 | } // end namespace Eigen |
| 116 | |
| 117 | #endif // EIGEN_STEM_FUNCTION |