NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
muParserTemplateMagic.h
Go to the documentation of this file.
1#ifndef MU_PARSER_TEMPLATE_MAGIC_H
2#define MU_PARSER_TEMPLATE_MAGIC_H
3
4#include <cmath>
5#include "muParserError.h"
6
7std::complex<double> intPower(const std::complex<double>&, int);
8
9namespace mu
10{
11 //-----------------------------------------------------------------------------------------------
12 //
13 // Compile time type detection
14 //
15 //-----------------------------------------------------------------------------------------------
16
20 template<typename T>
21 struct TypeInfo
22 {
23 static bool IsInteger() { return false; }
24 };
25
26 template<>
27 struct TypeInfo<char>
28 {
29 static bool IsInteger() { return true; }
30 };
31
32 template<>
33 struct TypeInfo<short>
34 {
35 static bool IsInteger() { return true; }
36 };
37
38 template<>
39 struct TypeInfo<int>
40 {
41 static bool IsInteger() { return true; }
42 };
43
44 template<>
45 struct TypeInfo<long>
46 {
47 static bool IsInteger() { return true; }
48 };
49
50 template<>
51 struct TypeInfo<unsigned char>
52 {
53 static bool IsInteger() { return true; }
54 };
55
56 template<>
57 struct TypeInfo<unsigned short>
58 {
59 static bool IsInteger() { return true; }
60 };
61
62 template<>
63 struct TypeInfo<unsigned int>
64 {
65 static bool IsInteger() { return true; }
66 };
67
68 template<>
69 struct TypeInfo<unsigned long>
70 {
71 static bool IsInteger() { return true; }
72 };
73
74
75 //-----------------------------------------------------------------------------------------------
76 //
77 // Standard math functions with dummy overload for integer types
78 //
79 //-----------------------------------------------------------------------------------------------
80
86 template<typename T>
87 struct MathImpl
88 {
89 inline static T Sin(const T& v)
90 {
91 return v.imag() == 0.0 ? std::sin(v.real()) : std::sin(v);
92 }
93
94
95 inline static T Cos(const T& v)
96 {
97 return v.imag() == 0.0 ? std::cos(v.real()) : std::cos(v);
98 }
99
100
101 inline static T Tan(const T& v)
102 {
103 return v.imag() == 0.0 ? std::tan(v.real()) : std::tan(v);
104 }
105
106 inline static T ASin(const T& v)
107 {
108 return v.imag() == 0.0 ? std::asin(v.real()) : std::asin(v);
109 }
110
111
112 inline static T ACos(const T& v)
113 {
114 return v.imag() == 0.0 ? std::acos(v.real()) : std::acos(v);
115 }
116
117
118 inline static T ATan(const T& v)
119 {
120 return std::isnan(v.real()) || std::isnan(v.imag())
121 ? NAN
122 : (v.imag() == 0.0 ? std::atan(v.real()) : std::atan(v));
123 }
124
125
126 inline static T ATan2(const T& v1, const T& v2)
127 {
128 return std::atan2(v1, v2); // only available for doubles
129 }
130
131
132 inline static T Sinh(const T& v)
133 {
134 return v.imag() == 0.0 ? std::sinh(v.real()) : std::sinh(v);
135 }
136
137
138 inline static T Cosh(const T& v)
139 {
140 return v.imag() == 0.0 ? std::cosh(v.real()) : std::cosh(v);
141 }
142
143
144 inline static T Tanh(const T& v)
145 {
146 return v.imag() == 0.0 ? std::tanh(v.real()) : std::tanh(v);
147 }
148
149
150 inline static T Sqrt(const T& v)
151 {
152 return v.imag() == 0.0 ? std::sqrt(v.real()) : std::sqrt(v);
153 }
154
155
156 inline static T Log(const T& v)
157 {
158 return v.imag() == 0.0 ? std::log(v.real()) : std::log(v);
159 }
160
161
162 inline static T Log2(const T& v)
163 {
164 return (v.imag() == 0.0 ? std::log(v.real()) : std::log(v))/std::log(2.0); // Logarithm base 2
165 }
166
167
168 inline static T Log10(const T& v)
169 {
170 return v.imag() == 0.0 ? std::log10(v.real()) : std::log10(v); // Logarithm base 10
171 }
172
173
174 inline static T ASinh(const T& v)
175 {
176 return Log(v + Sqrt(v * v + 1.0));
177 }
178
179
180 inline static T ACosh(const T& v)
181 {
182 return Log(v + Sqrt(v * v - 1.0));
183 }
184
185
186 inline static T ATanh(const T& v)
187 {
188 return (0.5 * Log((1.0 + v) / (1.0 - v)));
189 }
190
191
192 inline static T Exp(const T& v)
193 {
194 return v.imag() == 0 ? std::exp(v.real()) : std::exp(v);
195 }
196
197
198 inline static T Rint(const T& v)
199 {
200 return std::floor(v + (T)0.5);
201 }
202
203
204 inline static T Sign(const T& v)
205 {
206 return (T)((v<0.0) ? -1.0 : (v>0.0) ? 1.0 : 0.0);
207 }
208
209
210 inline static T Pow(const T& v1, const T& v2)
211 {
212 return v2.imag() == 0.0 && v2.real() == (int)v2.real()
213 ? intPower(v1, v2.real())
214 : (v1.imag() == 0.0 && v2.imag() == 0.0 ? std::pow(v1.real(), v2.real()) : std::pow(v1, v2));
215 }
216 };
217}
218
219#endif
This file defines the error class used by the parser.
std::complex< double > intPower(const std::complex< double > &, int)
This function calculates the power of a value with the specialization that the exponent is an integer...
Definition: tools.cpp:3640
CONSTCD14 std::enable_if< detail::no_overflow< Period, typenameTo::period >::value, To >::type floor(const std::chrono::duration< Rep, Period > &d)
Definition: date.h:1251
Namespace for mathematical applications.
Definition: muParser.cpp:53
bool isnan(const value_type &v)
Definition: muParserDef.h:379
A template class for providing wrappers for essential math functions.
static T ACos(const T &v)
static T ASinh(const T &v)
static T Log10(const T &v)
static T Sign(const T &v)
static T Tanh(const T &v)
static T ATan(const T &v)
static T Tan(const T &v)
static T ATan2(const T &v1, const T &v2)
static T Sqrt(const T &v)
static T Pow(const T &v1, const T &v2)
static T Sin(const T &v)
static T ATanh(const T &v)
static T Cosh(const T &v)
static T Log(const T &v)
static T Rint(const T &v)
static T Log2(const T &v)
static T Sinh(const T &v)
static T Cos(const T &v)
static T ACosh(const T &v)
static T ASin(const T &v)
static T Exp(const T &v)
A class singling out integer types at compile time using template meta programming.
static bool IsInteger()