muParser API -  1.35
muParserTemplateMagic.h
1 /*
2 
3  _____ __ _____________ _______ ______ ___________
4  / \| | \____ \__ \\_ __ \/ ___// __ \_ __ \
5  | Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
6  |__|_| /____/| __(____ /__| /____ >\___ >__|
7  \/ |__| \/ \/ \/
8  Copyright (C) 2004 - 2022 Ingo Berg
9 
10  Redistribution and use in source and binary forms, with or without modification, are permitted
11  provided that the following conditions are met:
12 
13  * Redistributions of source code must retain the above copyright notice, this list of
14  conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright notice, this list of
16  conditions and the following disclaimer in the documentation and/or other materials provided
17  with the distribution.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef MU_PARSER_TEMPLATE_MAGIC_H
30 #define MU_PARSER_TEMPLATE_MAGIC_H
31 
32 #include <cmath>
33 #include "muParserError.h"
34 
35 
36 namespace mu
37 {
38  //-----------------------------------------------------------------------------------------------
39  //
40  // Compile time type detection
41  //
42  //-----------------------------------------------------------------------------------------------
43 
44  /** \brief A class singling out integer types at compile time using
45  template meta programming.
46  */
47  template<typename T>
48  struct TypeInfo
49  {
50  static bool IsInteger() { return false; }
51  };
52 
53  template<>
54  struct TypeInfo<char>
55  {
56  static bool IsInteger() { return true; }
57  };
58 
59  template<>
60  struct TypeInfo<short>
61  {
62  static bool IsInteger() { return true; }
63  };
64 
65  template<>
66  struct TypeInfo<int>
67  {
68  static bool IsInteger() { return true; }
69  };
70 
71  template<>
72  struct TypeInfo<long>
73  {
74  static bool IsInteger() { return true; }
75  };
76 
77  template<>
78  struct TypeInfo<unsigned char>
79  {
80  static bool IsInteger() { return true; }
81  };
82 
83  template<>
84  struct TypeInfo<unsigned short>
85  {
86  static bool IsInteger() { return true; }
87  };
88 
89  template<>
90  struct TypeInfo<unsigned int>
91  {
92  static bool IsInteger() { return true; }
93  };
94 
95  template<>
96  struct TypeInfo<unsigned long>
97  {
98  static bool IsInteger() { return true; }
99  };
100 
101 
102  //-----------------------------------------------------------------------------------------------
103  //
104  // Standard math functions with dummy overload for integer types
105  //
106  //-----------------------------------------------------------------------------------------------
107 
108  /** \brief A template class for providing wrappers for essential math functions.
109 
110  This template is spezialized for several types in order to provide a unified interface
111  for parser internal math function calls regardless of the data type.
112  */
113  template<typename T>
114  struct MathImpl
115  {
116  static T Sin(T v) { return sin(v); }
117  static T Cos(T v) { return cos(v); }
118  static T Tan(T v) { return tan(v); }
119  static T ASin(T v) { return asin(v); }
120  static T ACos(T v) { return acos(v); }
121  static T ATan(T v) { return atan(v); }
122  static T ATan2(T v1, T v2) { return atan2(v1, v2); }
123  static T Sinh(T v) { return sinh(v); }
124  static T Cosh(T v) { return cosh(v); }
125  static T Tanh(T v) { return tanh(v); }
126  static T ASinh(T v) { return log(v + sqrt(v * v + 1)); }
127  static T ACosh(T v) { return log(v + sqrt(v * v - 1)); }
128  static T ATanh(T v) { return ((T)0.5 * log((1 + v) / (1 - v))); }
129  static T Log(T v) { return log(v); }
130  static T Log2(T v) { return log(v) / log((T)2); } // Logarithm base 2
131  static T Log10(T v) { return log10(v); } // Logarithm base 10
132  static T Exp(T v) { return exp(v); }
133  static T Abs(T v) { return (v >= 0) ? v : -v; }
134  static T Sqrt(T v) { return sqrt(v); }
135  static T Rint(T v) { return floor(v + (T)0.5); }
136  static T Sign(T v) { return (T)((v < 0) ? -1 : (v > 0) ? 1 : 0); }
137  static T Pow(T v1, T v2) { return std::pow(v1, v2); }
138 
139  static T UnaryMinus(T v) { return -v; }
140  static T UnaryPlus(T v) { return v; }
141 
142  static T Sum(const T *a_afArg, int a_iArgc)
143  {
144  if (!a_iArgc)
145  throw ParserError(_T("too few arguments for function sum."));
146 
147  T fRes = 0;
148  for (int i = 0; i < a_iArgc; ++i) fRes += a_afArg[i];
149  return fRes;
150  }
151 
152  static T Avg(const T *a_afArg, int a_iArgc)
153  {
154  if (!a_iArgc)
155  throw ParserError(_T("too few arguments for function avg."));
156 
157  T fRes = 0;
158  for (int i = 0; i < a_iArgc; ++i) fRes += a_afArg[i];
159  return fRes / (T)a_iArgc;
160  }
161 
162  static T Min(const T *a_afArg, int a_iArgc)
163  {
164  if (!a_iArgc)
165  throw ParserError(_T("too few arguments for function min."));
166 
167  T fRes = a_afArg[0];
168  for (int i = 0; i < a_iArgc; ++i)
169  fRes = std::min(fRes, a_afArg[i]);
170 
171  return fRes;
172  }
173 
174  static T Max(const T *a_afArg, int a_iArgc)
175  {
176  if (!a_iArgc)
177  throw ParserError(_T("too few arguments for function max."));
178 
179  T fRes = a_afArg[0];
180  for (int i = 0; i < a_iArgc; ++i) fRes = std::max(fRes, a_afArg[i]);
181 
182  return fRes;
183  }
184 
185 
186 #if defined (__GNUG__)
187  // Bei zu genauer definition von pi kann die Berechnung von
188  // sin(pi*a) mit a=1 10 x langsamer sein!
189  static constexpr T CONST_PI = (T)3.141592653589;
190 #else
191  static constexpr T CONST_PI = (T)3.141592653589793238462643;
192 #endif
193 
194  static constexpr T CONST_E = (T)2.718281828459045235360287;
195  };
196 }
197 
198 #endif
#define _T(x)
Activate this option in order to compile with OpenMP support.
Definition: muParserDef.h:69
A class singling out integer types at compile time using template meta programming.
Error class of the parser.
Definition: muParserError.h:74
A template class for providing wrappers for essential math functions.
Namespace for mathematical applications.
Definition: muParser.cpp:46
This file defines the error class used by the parser.