G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
exprtk_universal_adaptor.h
Go to the documentation of this file.
1
15#define TYPE_TAG_(UNIVERSAL_TYPE) UNIVERSAL_TYPE ## _type_tag
16#define TYPE_TAG(UNIVERSAL_TYPE) TYPE_TAG_(UNIVERSAL_TYPE)
17#define UNIVERSAL_TYPE_TAG TYPE_TAG(UNIVERSAL_TYPE)
18
19namespace exprtk
20{
21namespace details
22{
23namespace numeric
24{
25namespace details
26{
27struct UNIVERSAL_TYPE_TAG {};
28
29template<> struct number_type<UNIVERSAL_TYPE> { typedef UNIVERSAL_TYPE_TAG type; };
30
31template <>
32struct epsilon_type<UNIVERSAL_TYPE>
33{
34 static inline UNIVERSAL_TYPE value()
35 {
36 static const UNIVERSAL_TYPE epsilon =
37 std::numeric_limits<UNIVERSAL_TYPE>::epsilon();
38 return epsilon;
39 }
40};
41
42inline bool is_nan_impl(const UNIVERSAL_TYPE& v, UNIVERSAL_TYPE_TAG)
43{
44 return v.isnar();
45}
46
47template <typename T>
48inline int to_int32_impl(const T& v, UNIVERSAL_TYPE_TAG)
49{
50 return static_cast<int>(v);
51 //return static_cast<int>(v.getValue());
52}
53
54template <typename T>
55inline long long to_int64_impl(const T& v, UNIVERSAL_TYPE_TAG)
56{
57 return static_cast<long long int>(v);
58 //return static_cast<long long int>(v.getValue());
59}
60
61template <typename T>
62inline unsigned long long to_uint64_impl(const T& v, UNIVERSAL_TYPE_TAG)
63{
64 return static_cast<unsigned long long int>(v);
65}
66
67template <typename T> inline T abs_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::abs (v); }
68template <typename T> inline T acos_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::acos (v); }
69template <typename T> inline T acosh_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::acosh(v); }
70template <typename T> inline T asin_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::asin (v); }
71template <typename T> inline T asinh_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::asinh(v); }
72template <typename T> inline T atan_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::atan (v); }
73template <typename T> inline T atanh_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::atanh(v); }
74template <typename T> inline T ceil_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::ceil (v); }
75template <typename T> inline T cos_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::cos (v); }
76template <typename T> inline T cosh_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::cosh (v); }
77template <typename T> inline T exp_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::exp (v); }
78template <typename T> inline T floor_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::floor(v); }
79template <typename T> inline T log_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::log (v); }
80template <typename T> inline T log10_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::log10(v); }
81template <typename T> inline T log2_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::log2 (v); }
82template <typename T> inline T neg_impl(const T& v, UNIVERSAL_TYPE_TAG) { return -v; }
83template <typename T> inline T pos_impl(const T& v, UNIVERSAL_TYPE_TAG) { return v; }
84template <typename T> inline T sin_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::sin (v); }
85template <typename T> inline T sinh_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::sinh (v); }
86template <typename T> inline T sqrt_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::sqrt (v); }
87template <typename T> inline T tan_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::tan (v); }
88template <typename T> inline T tanh_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::tanh (v); }
89template <typename T> inline T cot_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::cot (v); }
90template <typename T> inline T sec_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::sec (v); }
91template <typename T> inline T csc_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::csc (v); }
92template <typename T> inline T r2d_impl(const T& v, UNIVERSAL_TYPE_TAG) { return (v * exprtk::details::constant_universal::_180_pi); }
93template <typename T> inline T d2r_impl(const T& v, UNIVERSAL_TYPE_TAG) { return (v * exprtk::details::constant_universal::pi_180 ); }
94template <typename T> inline T d2g_impl(const T& v, UNIVERSAL_TYPE_TAG) { return (v * UNIVERSAL_TYPE(10.0/9.0)); }
95template <typename T> inline T g2d_impl(const T& v, UNIVERSAL_TYPE_TAG) { return (v * UNIVERSAL_TYPE(9.0/10.0)); }
96template <typename T> inline T notl_impl(const T& v, UNIVERSAL_TYPE_TAG) { return (v != UNIVERSAL_TYPE(0) ? UNIVERSAL_TYPE(0) : UNIVERSAL_TYPE(1)); }
97template <typename T> inline T frac_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::frac (v); }
98template <typename T> inline T trunc_impl(const T& v, UNIVERSAL_TYPE_TAG) { return sw::universal::trunc(v); }
99
100template <typename T> inline T const_pi_impl(UNIVERSAL_TYPE_TAG) { return UNIVERSAL_TYPE(exprtk::details::constant_universal::pi); }
101template <typename T> inline T const_e_impl (UNIVERSAL_TYPE_TAG) { return UNIVERSAL_TYPE(exprtk::details::constant_universal::e ); }
102
103inline bool is_true_impl (const UNIVERSAL_TYPE& v)
104{
105 return true;//0.0 != v;
106 //return 0.0 != v.getValue();
107}
108
109inline bool is_false_impl(const UNIVERSAL_TYPE& v)
110{
111 return !is_true_impl(v);
112}
113
114template <typename T>
115inline T expm1_impl(const T& v, UNIVERSAL_TYPE_TAG)
116{
117 return sw::universal::expm1(v);
118}
119
120template <typename T>
121inline T min_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
122{
123 return sw::universal::min(v0,v1);
124}
125
126template <typename T>
127inline T max_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
128{
129 return sw::universal::max(v0,v1);
130}
131
132template <typename T>
133inline T nequal_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
134{
135 const T epsilon = epsilon_type<T>::value();
136 const T eps_norm = (sw::universal::max(T(1),sw::universal::max(sw::universal::abs(v0),sw::universal::abs(v1))) * epsilon);
137 return (sw::universal::abs(T(v0 - v1)) > eps_norm) ? T(1) : T(0);
138}
139
140template <typename T>
141inline T sgn_impl(const T& v, UNIVERSAL_TYPE_TAG)
142{
143 if (v > T(0)) return T(+1);
144 else if (v < T(0)) return T(-1);
145 else return T( 0);
146}
147
148template <typename T>
149inline T log1p_impl(const T& v, UNIVERSAL_TYPE_TAG)
150{
151 return sw::universal::log1p(v);
152}
153
154template <typename T>
155inline T erf_impl(const T& v, UNIVERSAL_TYPE_TAG)
156{
157 return sw::universal::erf(v);
158}
159
160template <typename T>
161inline T erfc_impl(const T& v, UNIVERSAL_TYPE_TAG)
162{
163 return sw::universal::erfc(v);
164}
165
166template <typename T>
167inline T ncdf_impl(const T& v, UNIVERSAL_TYPE_TAG)
168{
169 T cnd = T(0.5) * (T(1) + erf_impl(
170 sw::universal::abs(v) /
171 T(exprtk::details::constant_universal::sqrt2), UNIVERSAL_TYPE_TAG()));
172 return (v < T(0)) ? (T(1) - cnd) : cnd;
173}
174
175template <typename T>
176inline T modulus_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
177{
178 std::cerr<<"modulus is not derivated\n";
179 return T(sw::universal::fmod(v0,v1));
180 //return T(std::fmod(v0.getValue(),v1.getValue()));
181}
182
183template <typename T>
184inline T pow_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
185{
186 return sw::universal::pow(v0, v1 );// note: exponent considered constant
187 //return pow(v0, v1.getValue() );// note: exponent considered constant
188}
189
190template <typename T>
191inline T logn_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
192{
193 return sw::universal::log(v0) / sw::universal::log(v1);
194}
195
196template <typename T>
197inline T sinc_impl(const T& v, UNIVERSAL_TYPE_TAG)
198{
199 if (sw::universal::abs(v) >= epsilon_type<T>::value())
200 return(sw::universal::sin(v) / v);
201 else
202 return T(1);
203}
204
205template <typename T>
206inline T xor_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
207{
208 return (is_false_impl(v0) != is_false_impl(v1)) ? T(1) : T(0);
209}
210
211template <typename T>
212inline T xnor_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
213{
214 const bool v0_true = is_true_impl(v0);
215 const bool v1_true = is_true_impl(v1);
216 if ((v0_true && v1_true) || (!v0_true && !v1_true))
217 return T(1);
218 else
219 return T(0);
220}
221
222template <typename T>
223inline T equal_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
224{
225 const T epsilon = epsilon_type<T>::value();
226 const T eps_norm = (sw::universal::max(T(1),sw::universal::max(sw::universal::abs(v0),sw::universal::abs(v1))) * epsilon);
227 return (sw::universal::abs(T(v0-v1)) <= eps_norm) ? T(1) : T(0);
228}
229
230template <typename T>
231inline T round_impl(const T& v, UNIVERSAL_TYPE_TAG)
232{
233 return sw::universal::round(v);
234}
235
236template <typename T>
237inline T roundn_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
238{
239 const T p10 = pow(T(10),floor(v1));
240 if (v0 < T(0))
241 return T(sw::universal::ceil (T((v0 * p10) - T(0.5))) / p10);
242 else
243 return T(sw::universal::floor(T((v0 * p10) + T(0.5))) / p10);
244}
245
246template <typename T>
247inline bool is_integer_impl(const T& v, UNIVERSAL_TYPE_TAG)
248{
249 return sw::universal::ceil(v) == v;
250 //return std::ceil(v.getValue()) == v.getValue();
251}
252
253template <typename T>
254inline T root_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
255{
256 return sw::universal::pow(v0,T(1) / v1);
257}
258
259template <typename T>
260inline T hypot_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
261{
262 return sw::universal::hypot(v0,v1);
263}
264
265template <typename T>
266inline T atan2_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
267{
268 return sw::universal::atan2(v0,v1);
269}
270
271template <typename T>
272inline T shr_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
273{
274 return v0 * (T(1) / sw::universal::pow(T(2.0),v1));
275}
276
277template <typename T>
278inline T shl_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
279{
280 return v0 * sw::universal::pow(T(2.0),v1);
281}
282
283template <typename T>
284inline T and_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
285{
286 return (is_true_impl(v0) && is_true_impl(v1)) ? T(1) : T(0);
287}
288
289template <typename T>
290inline T nand_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
291{
292 return (is_false_impl(v0) || is_false_impl(v1)) ? T(1) : T(0);
293}
294
295template <typename T>
296inline T or_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
297{
298 return (is_true_impl(v0) || is_true_impl(v1)) ? T(1) : T(0);
299}
300
301template <typename T>
302inline T nor_impl(const T& v0, const T& v1, UNIVERSAL_TYPE_TAG)
303{
304 return (is_false_impl(v0) && is_false_impl(v1)) ? T(1) : T(0);
305}
306}
307}
308
309template <typename Iterator>
310inline bool string_to_real(Iterator& itr_external,
311 const Iterator end, UNIVERSAL_TYPE& t,
312 numeric::details::UNIVERSAL_TYPE_TAG)
313{
314 const std::string num(itr_external,end);
315 t = ::atof(num.c_str());
316 return true;
317}
318
319inline bool is_true (const UNIVERSAL_TYPE& v)
320{ return details::numeric::details::is_true_impl (v); }
321
322inline bool is_false(const UNIVERSAL_TYPE& v)
323{ return details::numeric::details::is_false_impl(v); }
324}
325
326namespace helper
327{
328namespace details
329{
330inline void print_type(const std::string&, const UNIVERSAL_TYPE& v,
331 exprtk::details::numeric::details::UNIVERSAL_TYPE_TAG)
332{
333 std::cout << pretty_print(v) << "\n";
334 //printf("%f",v.getValue());
335}
336}
337}
338}