StemFunction.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_STEM_FUNCTION
00026 #define EIGEN_STEM_FUNCTION
00027 
00028 namespace Eigen { 
00029 
00033 template <typename Scalar>
00034 class StdStemFunctions
00035 {
00036   public:
00037 
00039     static Scalar exp(Scalar x, int)
00040     {
00041       return std::exp(x);
00042     }
00043 
00045     static Scalar cos(Scalar x, int n)
00046     {
00047       Scalar res;
00048       switch (n % 4) {
00049       case 0: 
00050         res = std::cos(x);
00051         break;
00052       case 1:
00053         res = -std::sin(x);
00054         break;
00055       case 2:
00056         res = -std::cos(x);
00057         break;
00058       case 3:
00059         res = std::sin(x);
00060         break;
00061       }
00062       return res;
00063     }
00064 
00066     static Scalar sin(Scalar x, int n)
00067     {
00068       Scalar res;
00069       switch (n % 4) {
00070       case 0:
00071         res = std::sin(x);
00072         break;
00073       case 1:
00074         res = std::cos(x);
00075         break;
00076       case 2:
00077         res = -std::sin(x);
00078         break;
00079       case 3:
00080         res = -std::cos(x);
00081         break;
00082       }
00083       return res;
00084     }
00085 
00087     static Scalar cosh(Scalar x, int n)
00088     {
00089       Scalar res;
00090       switch (n % 2) {
00091       case 0:
00092         res = std::cosh(x);
00093         break;
00094       case 1:
00095         res = std::sinh(x);
00096         break;
00097       }
00098       return res;
00099     }
00100         
00102     static Scalar sinh(Scalar x, int n)
00103     {
00104       Scalar res;
00105       switch (n % 2) {
00106       case 0:
00107         res = std::sinh(x);
00108         break;
00109       case 1:
00110         res = std::cosh(x);
00111         break;
00112       }
00113       return res;
00114     }
00115 
00116 }; // end of class StdStemFunctions
00117 
00118 } // end namespace Eigen
00119 
00120 #endif // EIGEN_STEM_FUNCTION