1 | // -*- C++ -*- |
---|
2 | /*************************************************************************** |
---|
3 | * blitz/funcs.h Function objects for math functions |
---|
4 | * |
---|
5 | * $Id: funcs.h,v 1.7 2005/10/08 01:22:55 julianc Exp $ |
---|
6 | * |
---|
7 | * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org> |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * Suggestions: blitz-dev@oonumerics.org |
---|
20 | * Bugs: blitz-bugs@oonumerics.org |
---|
21 | * |
---|
22 | * For more information, please see the Blitz++ Home Page: |
---|
23 | * http://oonumerics.org/blitz/ |
---|
24 | * |
---|
25 | *************************************************************************/ |
---|
26 | |
---|
27 | #ifndef BZ_FUNCS_H |
---|
28 | #define BZ_FUNCS_H |
---|
29 | |
---|
30 | #include <blitz/blitz.h> |
---|
31 | #include <blitz/promote.h> |
---|
32 | #include <blitz/prettyprint.h> |
---|
33 | |
---|
34 | BZ_NAMESPACE(blitz) |
---|
35 | |
---|
36 | /* Helper functions */ |
---|
37 | |
---|
38 | template <typename T> |
---|
39 | inline T blitz_sqr(T x) |
---|
40 | { return x*x; } |
---|
41 | |
---|
42 | template <typename T> |
---|
43 | inline T blitz_cube(T x) |
---|
44 | { return x*x*x; } |
---|
45 | |
---|
46 | template <typename T> |
---|
47 | inline T blitz_pow4(T x) |
---|
48 | { return x*x*x*x; } |
---|
49 | |
---|
50 | template <typename T> |
---|
51 | inline T blitz_pow5(T x) |
---|
52 | { return x*x*x*x*x; } |
---|
53 | |
---|
54 | template <typename T> |
---|
55 | inline T blitz_pow6(T x) |
---|
56 | { return x*x*x*x*x*x; } |
---|
57 | |
---|
58 | template <typename T> |
---|
59 | inline T blitz_pow7(T x) |
---|
60 | { return x*x*x*x*x*x*x; } |
---|
61 | |
---|
62 | template <typename T> |
---|
63 | inline T blitz_pow8(T x) |
---|
64 | { return x*x*x*x*x*x*x*x; } |
---|
65 | |
---|
66 | |
---|
67 | /* Unary functions that return same type as argument */ |
---|
68 | |
---|
69 | #define BZ_DEFINE_UNARY_FUNC(name,fun) \ |
---|
70 | template<typename T_numtype1> \ |
---|
71 | struct name { \ |
---|
72 | typedef T_numtype1 T_numtype; \ |
---|
73 | \ |
---|
74 | static inline T_numtype \ |
---|
75 | apply(T_numtype1 a) \ |
---|
76 | { return fun(a); } \ |
---|
77 | \ |
---|
78 | template<typename T1> \ |
---|
79 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
80 | prettyPrintFormat& format, const T1& t1) \ |
---|
81 | { \ |
---|
82 | str += #fun; \ |
---|
83 | str += "("; \ |
---|
84 | t1.prettyPrint(str, format); \ |
---|
85 | str += ")"; \ |
---|
86 | } \ |
---|
87 | }; |
---|
88 | |
---|
89 | BZ_DEFINE_UNARY_FUNC(Fn_acos,BZ_MATHFN_SCOPE(acos)) |
---|
90 | BZ_DEFINE_UNARY_FUNC(Fn_asin,BZ_MATHFN_SCOPE(asin)) |
---|
91 | BZ_DEFINE_UNARY_FUNC(Fn_atan,BZ_MATHFN_SCOPE(atan)) |
---|
92 | BZ_DEFINE_UNARY_FUNC(Fn_ceil,BZ_MATHFN_SCOPE(ceil)) |
---|
93 | BZ_DEFINE_UNARY_FUNC(Fn_cos,BZ_MATHFN_SCOPE(cos)) |
---|
94 | BZ_DEFINE_UNARY_FUNC(Fn_cosh,BZ_MATHFN_SCOPE(cosh)) |
---|
95 | BZ_DEFINE_UNARY_FUNC(Fn_exp,BZ_MATHFN_SCOPE(exp)) |
---|
96 | BZ_DEFINE_UNARY_FUNC(Fn_fabs,BZ_MATHFN_SCOPE(fabs)) |
---|
97 | BZ_DEFINE_UNARY_FUNC(Fn_floor,BZ_MATHFN_SCOPE(floor)) |
---|
98 | BZ_DEFINE_UNARY_FUNC(Fn_log,BZ_MATHFN_SCOPE(log)) |
---|
99 | BZ_DEFINE_UNARY_FUNC(Fn_log10,BZ_MATHFN_SCOPE(log10)) |
---|
100 | BZ_DEFINE_UNARY_FUNC(Fn_sin,BZ_MATHFN_SCOPE(sin)) |
---|
101 | BZ_DEFINE_UNARY_FUNC(Fn_sinh,BZ_MATHFN_SCOPE(sinh)) |
---|
102 | BZ_DEFINE_UNARY_FUNC(Fn_sqrt,BZ_MATHFN_SCOPE(sqrt)) |
---|
103 | BZ_DEFINE_UNARY_FUNC(Fn_tan,BZ_MATHFN_SCOPE(tan)) |
---|
104 | BZ_DEFINE_UNARY_FUNC(Fn_tanh,BZ_MATHFN_SCOPE(tanh)) |
---|
105 | |
---|
106 | #ifdef BZ_HAVE_IEEE_MATH |
---|
107 | BZ_DEFINE_UNARY_FUNC(Fn_acosh,BZ_IEEEMATHFN_SCOPE(acosh)) |
---|
108 | BZ_DEFINE_UNARY_FUNC(Fn_asinh,BZ_IEEEMATHFN_SCOPE(asinh)) |
---|
109 | BZ_DEFINE_UNARY_FUNC(Fn_atanh,BZ_IEEEMATHFN_SCOPE(atanh)) |
---|
110 | BZ_DEFINE_UNARY_FUNC(Fn_cbrt,BZ_IEEEMATHFN_SCOPE(cbrt)) |
---|
111 | BZ_DEFINE_UNARY_FUNC(Fn_erf,BZ_IEEEMATHFN_SCOPE(erf)) |
---|
112 | BZ_DEFINE_UNARY_FUNC(Fn_erfc,BZ_IEEEMATHFN_SCOPE(erfc)) |
---|
113 | BZ_DEFINE_UNARY_FUNC(Fn_expm1,BZ_IEEEMATHFN_SCOPE(expm1)) |
---|
114 | BZ_DEFINE_UNARY_FUNC(Fn_j0,BZ_IEEEMATHFN_SCOPE(j0)) |
---|
115 | BZ_DEFINE_UNARY_FUNC(Fn_j1,BZ_IEEEMATHFN_SCOPE(j1)) |
---|
116 | BZ_DEFINE_UNARY_FUNC(Fn_lgamma,BZ_IEEEMATHFN_SCOPE(lgamma)) |
---|
117 | BZ_DEFINE_UNARY_FUNC(Fn_logb,BZ_IEEEMATHFN_SCOPE(logb)) |
---|
118 | BZ_DEFINE_UNARY_FUNC(Fn_log1p,BZ_IEEEMATHFN_SCOPE(log1p)) |
---|
119 | BZ_DEFINE_UNARY_FUNC(Fn_rint,BZ_IEEEMATHFN_SCOPE(rint)) |
---|
120 | BZ_DEFINE_UNARY_FUNC(Fn_y0,BZ_IEEEMATHFN_SCOPE(y0)) |
---|
121 | BZ_DEFINE_UNARY_FUNC(Fn_y1,BZ_IEEEMATHFN_SCOPE(y1)) |
---|
122 | #endif |
---|
123 | |
---|
124 | #ifdef BZ_HAVE_SYSTEM_V_MATH |
---|
125 | BZ_DEFINE_UNARY_FUNC(Fn__class,BZ_IEEEMATHFN_SCOPE(_class)) |
---|
126 | BZ_DEFINE_UNARY_FUNC(Fn_nearest,BZ_IEEEMATHFN_SCOPE(nearest)) |
---|
127 | BZ_DEFINE_UNARY_FUNC(Fn_rsqrt,BZ_IEEEMATHFN_SCOPE(rsqrt)) |
---|
128 | #endif |
---|
129 | |
---|
130 | BZ_DEFINE_UNARY_FUNC(Fn_sqr,BZ_BLITZ_SCOPE(blitz_sqr)) |
---|
131 | BZ_DEFINE_UNARY_FUNC(Fn_cube,BZ_BLITZ_SCOPE(blitz_cube)) |
---|
132 | BZ_DEFINE_UNARY_FUNC(Fn_pow4,BZ_BLITZ_SCOPE(blitz_pow4)) |
---|
133 | BZ_DEFINE_UNARY_FUNC(Fn_pow5,BZ_BLITZ_SCOPE(blitz_pow5)) |
---|
134 | BZ_DEFINE_UNARY_FUNC(Fn_pow6,BZ_BLITZ_SCOPE(blitz_pow6)) |
---|
135 | BZ_DEFINE_UNARY_FUNC(Fn_pow7,BZ_BLITZ_SCOPE(blitz_pow7)) |
---|
136 | BZ_DEFINE_UNARY_FUNC(Fn_pow8,BZ_BLITZ_SCOPE(blitz_pow8)) |
---|
137 | |
---|
138 | /* Unary functions that return a specified type */ |
---|
139 | |
---|
140 | #define BZ_DEFINE_UNARY_FUNC_RET(name,fun,ret) \ |
---|
141 | template<typename T_numtype1> \ |
---|
142 | struct name { \ |
---|
143 | typedef ret T_numtype; \ |
---|
144 | \ |
---|
145 | static inline T_numtype \ |
---|
146 | apply(T_numtype1 a) \ |
---|
147 | { return fun(a); } \ |
---|
148 | \ |
---|
149 | template<typename T1> \ |
---|
150 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
151 | prettyPrintFormat& format, const T1& t1) \ |
---|
152 | { \ |
---|
153 | str += #fun; \ |
---|
154 | str += "("; \ |
---|
155 | t1.prettyPrint(str, format); \ |
---|
156 | str += ")"; \ |
---|
157 | } \ |
---|
158 | }; |
---|
159 | |
---|
160 | #ifdef BZ_HAVE_IEEE_MATH |
---|
161 | BZ_DEFINE_UNARY_FUNC_RET(Fn_ilogb,BZ_IEEEMATHFN_SCOPE(ilogb),int) |
---|
162 | #endif |
---|
163 | |
---|
164 | #ifdef BZ_HAVE_SYSTEM_V_MATH |
---|
165 | BZ_DEFINE_UNARY_FUNC_RET(Fn_itrunc,BZ_IEEEMATHFN_SCOPE(itrunc),int) |
---|
166 | BZ_DEFINE_UNARY_FUNC_RET(Fn_uitrunc,BZ_IEEEMATHFN_SCOPE(uitrunc),unsigned int) |
---|
167 | #endif |
---|
168 | |
---|
169 | |
---|
170 | #ifdef BZ_HAVE_COMPLEX |
---|
171 | /* Specialization of unary functor for complex type */ |
---|
172 | |
---|
173 | #define BZ_DEFINE_UNARY_CFUNC(name,fun) \ |
---|
174 | template<typename T> \ |
---|
175 | struct name< complex<T> > { \ |
---|
176 | typedef complex<T> T_numtype1; \ |
---|
177 | typedef complex<T> T_numtype; \ |
---|
178 | \ |
---|
179 | static inline T_numtype \ |
---|
180 | apply(T_numtype1 a) \ |
---|
181 | { return fun(a); } \ |
---|
182 | \ |
---|
183 | template<typename T1> \ |
---|
184 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
185 | prettyPrintFormat& format, const T1& t1) \ |
---|
186 | { \ |
---|
187 | str += #fun; \ |
---|
188 | str += "("; \ |
---|
189 | t1.prettyPrint(str, format); \ |
---|
190 | str += ")"; \ |
---|
191 | } \ |
---|
192 | }; |
---|
193 | |
---|
194 | #ifdef BZ_HAVE_COMPLEX_FCNS |
---|
195 | BZ_DEFINE_UNARY_FUNC(Fn_conj,BZ_CMATHFN_SCOPE(conj)) |
---|
196 | #endif |
---|
197 | |
---|
198 | #ifdef BZ_HAVE_COMPLEX_MATH1 |
---|
199 | BZ_DEFINE_UNARY_CFUNC(Fn_cos,BZ_CMATHFN_SCOPE(cos)) |
---|
200 | BZ_DEFINE_UNARY_CFUNC(Fn_cosh,BZ_CMATHFN_SCOPE(cosh)) |
---|
201 | BZ_DEFINE_UNARY_CFUNC(Fn_exp,BZ_CMATHFN_SCOPE(exp)) |
---|
202 | BZ_DEFINE_UNARY_CFUNC(Fn_log,BZ_CMATHFN_SCOPE(log)) |
---|
203 | BZ_DEFINE_UNARY_CFUNC(Fn_log10,BZ_CMATHFN_SCOPE(log10)) |
---|
204 | BZ_DEFINE_UNARY_CFUNC(Fn_sin,BZ_CMATHFN_SCOPE(sin)) |
---|
205 | BZ_DEFINE_UNARY_CFUNC(Fn_sinh,BZ_CMATHFN_SCOPE(sinh)) |
---|
206 | BZ_DEFINE_UNARY_CFUNC(Fn_sqrt,BZ_CMATHFN_SCOPE(sqrt)) |
---|
207 | BZ_DEFINE_UNARY_CFUNC(Fn_tan,BZ_CMATHFN_SCOPE(tan)) |
---|
208 | BZ_DEFINE_UNARY_CFUNC(Fn_tanh,BZ_CMATHFN_SCOPE(tanh)) |
---|
209 | #endif // BZ_HAVE_COMPLEX_MATH1 |
---|
210 | |
---|
211 | BZ_DEFINE_UNARY_CFUNC(Fn_sqr,BZ_BLITZ_SCOPE(blitz_sqr)) |
---|
212 | BZ_DEFINE_UNARY_CFUNC(Fn_cube,BZ_BLITZ_SCOPE(blitz_cube)) |
---|
213 | BZ_DEFINE_UNARY_CFUNC(Fn_pow4,BZ_BLITZ_SCOPE(blitz_pow4)) |
---|
214 | BZ_DEFINE_UNARY_CFUNC(Fn_pow5,BZ_BLITZ_SCOPE(blitz_pow5)) |
---|
215 | BZ_DEFINE_UNARY_CFUNC(Fn_pow6,BZ_BLITZ_SCOPE(blitz_pow6)) |
---|
216 | BZ_DEFINE_UNARY_CFUNC(Fn_pow7,BZ_BLITZ_SCOPE(blitz_pow7)) |
---|
217 | BZ_DEFINE_UNARY_CFUNC(Fn_pow8,BZ_BLITZ_SCOPE(blitz_pow8)) |
---|
218 | |
---|
219 | /* Unary functions that apply only to complex<T> and return T */ |
---|
220 | |
---|
221 | #define BZ_DEFINE_UNARY_CFUNC2(name,fun) \ |
---|
222 | template<typename T_numtype1> \ |
---|
223 | struct name; \ |
---|
224 | \ |
---|
225 | template<typename T> \ |
---|
226 | struct name< complex<T> > { \ |
---|
227 | typedef complex<T> T_numtype1; \ |
---|
228 | typedef T T_numtype; \ |
---|
229 | \ |
---|
230 | static inline T_numtype \ |
---|
231 | apply(T_numtype1 a) \ |
---|
232 | { return fun(a); } \ |
---|
233 | \ |
---|
234 | template<typename T1> \ |
---|
235 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
236 | prettyPrintFormat& format, const T1& t1) \ |
---|
237 | { \ |
---|
238 | str += #fun; \ |
---|
239 | str += "("; \ |
---|
240 | t1.prettyPrint(str, format); \ |
---|
241 | str += ")"; \ |
---|
242 | } \ |
---|
243 | }; |
---|
244 | |
---|
245 | #ifdef BZ_HAVE_COMPLEX_FCNS |
---|
246 | BZ_DEFINE_UNARY_CFUNC2(Fn_arg,BZ_CMATHFN_SCOPE(arg)) |
---|
247 | BZ_DEFINE_UNARY_CFUNC2(Fn_imag,BZ_CMATHFN_SCOPE(imag)) |
---|
248 | BZ_DEFINE_UNARY_CFUNC2(Fn_norm,BZ_CMATHFN_SCOPE(norm)) |
---|
249 | BZ_DEFINE_UNARY_CFUNC2(Fn_real,BZ_CMATHFN_SCOPE(real)) |
---|
250 | #endif // BZ_HAVE_COMPLEX_FCNS |
---|
251 | |
---|
252 | #endif // BZ_HAVE_COMPLEX |
---|
253 | |
---|
254 | /* Binary functions that return type based on type promotion */ |
---|
255 | |
---|
256 | #define BZ_DEFINE_BINARY_FUNC(name,fun) \ |
---|
257 | template<typename T_numtype1, typename T_numtype2> \ |
---|
258 | struct name { \ |
---|
259 | typedef BZ_PROMOTE(T_numtype1, T_numtype2) T_numtype; \ |
---|
260 | \ |
---|
261 | static inline T_numtype \ |
---|
262 | apply(T_numtype1 a, T_numtype2 b) \ |
---|
263 | { return fun(a,b); } \ |
---|
264 | \ |
---|
265 | template<typename T1, typename T2> \ |
---|
266 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
267 | prettyPrintFormat& format, const T1& t1, \ |
---|
268 | const T2& t2) \ |
---|
269 | { \ |
---|
270 | str += #fun; \ |
---|
271 | str += "("; \ |
---|
272 | t1.prettyPrint(str, format); \ |
---|
273 | str += ","; \ |
---|
274 | t2.prettyPrint(str, format); \ |
---|
275 | str += ")"; \ |
---|
276 | } \ |
---|
277 | }; |
---|
278 | |
---|
279 | BZ_DEFINE_BINARY_FUNC(Fn_atan2,BZ_MATHFN_SCOPE(atan2)) |
---|
280 | BZ_DEFINE_BINARY_FUNC(Fn_fmod,BZ_MATHFN_SCOPE(fmod)) |
---|
281 | BZ_DEFINE_BINARY_FUNC(Fn_pow,BZ_MATHFN_SCOPE(pow)) |
---|
282 | |
---|
283 | #ifdef BZ_HAVE_SYSTEM_V_MATH |
---|
284 | BZ_DEFINE_BINARY_FUNC(Fn_copysign,BZ_IEEEMATHFN_SCOPE(copysign)) |
---|
285 | BZ_DEFINE_BINARY_FUNC(Fn_drem,BZ_IEEEMATHFN_SCOPE(drem)) |
---|
286 | BZ_DEFINE_BINARY_FUNC(Fn_hypot,BZ_IEEEMATHFN_SCOPE(hypot)) |
---|
287 | BZ_DEFINE_BINARY_FUNC(Fn_nextafter,BZ_IEEEMATHFN_SCOPE(nextafter)) |
---|
288 | BZ_DEFINE_BINARY_FUNC(Fn_remainder,BZ_IEEEMATHFN_SCOPE(remainder)) |
---|
289 | BZ_DEFINE_BINARY_FUNC(Fn_scalb,BZ_IEEEMATHFN_SCOPE(scalb)) |
---|
290 | #endif |
---|
291 | |
---|
292 | /* Binary functions that return a specified type */ |
---|
293 | |
---|
294 | #define BZ_DEFINE_BINARY_FUNC_RET(name,fun,ret) \ |
---|
295 | template<typename T_numtype1, typename T_numtype2> \ |
---|
296 | struct name { \ |
---|
297 | typedef ret T_numtype; \ |
---|
298 | \ |
---|
299 | static inline T_numtype \ |
---|
300 | apply(T_numtype1 a, T_numtype2 b) \ |
---|
301 | { return fun(a,b); } \ |
---|
302 | \ |
---|
303 | template<typename T1, typename T2> \ |
---|
304 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
305 | prettyPrintFormat& format, const T1& t1, \ |
---|
306 | const T2& t2) \ |
---|
307 | { \ |
---|
308 | str += #fun; \ |
---|
309 | str += "("; \ |
---|
310 | t1.prettyPrint(str, format); \ |
---|
311 | str += ","; \ |
---|
312 | t2.prettyPrint(str, format); \ |
---|
313 | str += ")"; \ |
---|
314 | } \ |
---|
315 | }; |
---|
316 | |
---|
317 | #ifdef BZ_HAVE_SYSTEM_V_MATH |
---|
318 | BZ_DEFINE_BINARY_FUNC_RET(Fn_unordered,BZ_IEEEMATHFN_SCOPE(unordered),int) |
---|
319 | #endif |
---|
320 | |
---|
321 | #ifdef BZ_HAVE_COMPLEX |
---|
322 | /* Specialization of binary functor for complex type */ |
---|
323 | |
---|
324 | #define BZ_DEFINE_BINARY_CFUNC(name,fun) \ |
---|
325 | template<typename T> \ |
---|
326 | struct name< complex<T>, complex<T> > { \ |
---|
327 | typedef complex<T> T_numtype1; \ |
---|
328 | typedef complex<T> T_numtype2; \ |
---|
329 | typedef complex<T> T_numtype; \ |
---|
330 | \ |
---|
331 | static inline T_numtype \ |
---|
332 | apply(T_numtype1 a, T_numtype2 b) \ |
---|
333 | { return fun(a,b); } \ |
---|
334 | \ |
---|
335 | template<typename T1, typename T2> \ |
---|
336 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
337 | prettyPrintFormat& format, const T1& t1, \ |
---|
338 | const T2& t2) \ |
---|
339 | { \ |
---|
340 | str += #fun; \ |
---|
341 | str += "("; \ |
---|
342 | t1.prettyPrint(str, format); \ |
---|
343 | str += ","; \ |
---|
344 | t2.prettyPrint(str, format); \ |
---|
345 | str += ")"; \ |
---|
346 | } \ |
---|
347 | }; \ |
---|
348 | \ |
---|
349 | template<typename T> \ |
---|
350 | struct name< complex<T>, T > { \ |
---|
351 | typedef complex<T> T_numtype1; \ |
---|
352 | typedef T T_numtype2; \ |
---|
353 | typedef complex<T> T_numtype; \ |
---|
354 | \ |
---|
355 | static inline T_numtype \ |
---|
356 | apply(T_numtype1 a, T_numtype2 b) \ |
---|
357 | { return fun(a,b); } \ |
---|
358 | \ |
---|
359 | template<typename T1, typename T2> \ |
---|
360 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
361 | prettyPrintFormat& format, const T1& t1, \ |
---|
362 | const T2& t2) \ |
---|
363 | { \ |
---|
364 | str += #fun; \ |
---|
365 | str += "("; \ |
---|
366 | t1.prettyPrint(str, format); \ |
---|
367 | str += ","; \ |
---|
368 | t2.prettyPrint(str, format); \ |
---|
369 | str += ")"; \ |
---|
370 | } \ |
---|
371 | }; \ |
---|
372 | \ |
---|
373 | template<typename T> \ |
---|
374 | struct name< T, complex<T> > { \ |
---|
375 | typedef T T_numtype1; \ |
---|
376 | typedef complex<T> T_numtype2; \ |
---|
377 | typedef complex<T> T_numtype; \ |
---|
378 | \ |
---|
379 | static inline T_numtype \ |
---|
380 | apply(T_numtype1 a, T_numtype2 b) \ |
---|
381 | { return fun(a,b); } \ |
---|
382 | \ |
---|
383 | template<typename T1, typename T2> \ |
---|
384 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
385 | prettyPrintFormat& format, const T1& t1, \ |
---|
386 | const T2& t2) \ |
---|
387 | { \ |
---|
388 | str += #fun; \ |
---|
389 | str += "("; \ |
---|
390 | t1.prettyPrint(str, format); \ |
---|
391 | str += ","; \ |
---|
392 | t2.prettyPrint(str, format); \ |
---|
393 | str += ")"; \ |
---|
394 | } \ |
---|
395 | }; |
---|
396 | |
---|
397 | #ifdef BZ_HAVE_COMPLEX_MATH1 |
---|
398 | BZ_DEFINE_BINARY_CFUNC(Fn_pow,BZ_CMATHFN_SCOPE(pow)) |
---|
399 | #endif |
---|
400 | |
---|
401 | /* Binary functions that apply only to T and return complex<T> */ |
---|
402 | |
---|
403 | #define BZ_DEFINE_BINARY_FUNC_CRET(name,fun) \ |
---|
404 | template<typename T_numtype1, typename T_numtype2> \ |
---|
405 | struct name; \ |
---|
406 | \ |
---|
407 | template<typename T> \ |
---|
408 | struct name<T, T> { \ |
---|
409 | typedef T T_numtype1; \ |
---|
410 | typedef T T_numtype2; \ |
---|
411 | typedef complex<T> T_numtype; \ |
---|
412 | \ |
---|
413 | static inline T_numtype \ |
---|
414 | apply(T_numtype1 a, T_numtype2 b) \ |
---|
415 | { return fun(a,b); } \ |
---|
416 | \ |
---|
417 | template<typename T1, typename T2> \ |
---|
418 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
419 | prettyPrintFormat& format, const T1& t1, \ |
---|
420 | const T2& t2) \ |
---|
421 | { \ |
---|
422 | str += #fun; \ |
---|
423 | str += "("; \ |
---|
424 | t1.prettyPrint(str, format); \ |
---|
425 | str += ","; \ |
---|
426 | t2.prettyPrint(str, format); \ |
---|
427 | str += ")"; \ |
---|
428 | } \ |
---|
429 | }; |
---|
430 | |
---|
431 | #ifdef BZ_HAVE_COMPLEX_FCNS |
---|
432 | BZ_DEFINE_BINARY_FUNC_CRET(Fn_polar,BZ_CMATHFN_SCOPE(polar)) |
---|
433 | #endif |
---|
434 | |
---|
435 | #endif // BZ_HAVE_COMPLEX |
---|
436 | |
---|
437 | /* Ternary functions that return type based on type promotion */ |
---|
438 | |
---|
439 | #define BZ_DEFINE_TERNARY_FUNC(name,fun) \ |
---|
440 | template <typename P_numtype1, typename P_numtype2, \ |
---|
441 | typename P_numtype3> \ |
---|
442 | struct name { \ |
---|
443 | typedef BZ_PROMOTE(P_numtype1, \ |
---|
444 | BZ_PROMOTE(P_numtype2,P_numtype3)) T_numtype; \ |
---|
445 | \ |
---|
446 | static inline T_numtype \ |
---|
447 | apply(P_numtype1 x, P_numtype2 y, P_numtype3 z) \ |
---|
448 | { return fun(x,y,z); } \ |
---|
449 | \ |
---|
450 | template <typename T1, typename T2, typename T3> \ |
---|
451 | static void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
452 | prettyPrintFormat& format, \ |
---|
453 | const T1& a,const T2& b, const T3& c) \ |
---|
454 | { \ |
---|
455 | str += #fun; \ |
---|
456 | str += "("; \ |
---|
457 | a.prettyPrint(str,format); \ |
---|
458 | str += ","; \ |
---|
459 | b.prettyPrint(str,format); \ |
---|
460 | str += ","; \ |
---|
461 | c.prettyPrint(str,format); \ |
---|
462 | str += ")"; \ |
---|
463 | } \ |
---|
464 | }; |
---|
465 | |
---|
466 | /* Ternary functions that return a specified type */ |
---|
467 | |
---|
468 | #define BZ_DEFINE_TERNARY_FUNC_RET(name,fun,ret) \ |
---|
469 | template <typename P_numtype1, typename P_numtype2, \ |
---|
470 | typename P_numtype3> \ |
---|
471 | struct name { \ |
---|
472 | typedef ret T_numtype; \ |
---|
473 | \ |
---|
474 | static inline T_numtype \ |
---|
475 | apply(P_numtype1 x, P_numtype2 y, P_numtype3 z) \ |
---|
476 | { return fun(x,y,z); } \ |
---|
477 | \ |
---|
478 | template <typename T1, typename T2, typename T3> \ |
---|
479 | static void prettyPrint(BZ_STD_SCOPE(string) &str, \ |
---|
480 | prettyPrintFormat& format, \ |
---|
481 | const T1& a,const T2& b, const T3& c) \ |
---|
482 | { \ |
---|
483 | str += #fun; \ |
---|
484 | str += "("; \ |
---|
485 | a.prettyPrint(str,format); \ |
---|
486 | str += ","; \ |
---|
487 | b.prettyPrint(str,format); \ |
---|
488 | str += ","; \ |
---|
489 | c.prettyPrint(str,format); \ |
---|
490 | str += ")"; \ |
---|
491 | } \ |
---|
492 | }; |
---|
493 | |
---|
494 | |
---|
495 | /* These functions don't quite fit the usual patterns */ |
---|
496 | |
---|
497 | // abs() Absolute value |
---|
498 | template<typename T_numtype1> |
---|
499 | struct Fn_abs; |
---|
500 | |
---|
501 | // abs(int) |
---|
502 | template<> |
---|
503 | struct Fn_abs< int > { |
---|
504 | typedef int T_numtype1; |
---|
505 | typedef int T_numtype; |
---|
506 | |
---|
507 | static inline T_numtype |
---|
508 | apply(T_numtype1 a) |
---|
509 | { return BZ_MATHFN_SCOPE(abs)(a); } |
---|
510 | |
---|
511 | template<typename T1> |
---|
512 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
513 | prettyPrintFormat& format, const T1& t1) |
---|
514 | { |
---|
515 | str += "abs"; |
---|
516 | str += "("; |
---|
517 | t1.prettyPrint(str, format); |
---|
518 | str += ")"; |
---|
519 | } |
---|
520 | }; |
---|
521 | |
---|
522 | // abs(long) |
---|
523 | template<> |
---|
524 | struct Fn_abs< long int > { |
---|
525 | typedef long int T_numtype1; |
---|
526 | typedef long int T_numtype; |
---|
527 | |
---|
528 | static inline T_numtype |
---|
529 | apply(T_numtype1 a) |
---|
530 | { return BZ_MATHFN_SCOPE(labs)(a); } |
---|
531 | |
---|
532 | template<typename T1> |
---|
533 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
534 | prettyPrintFormat& format, const T1& t1) |
---|
535 | { |
---|
536 | str += "abs"; |
---|
537 | str += "("; |
---|
538 | t1.prettyPrint(str, format); |
---|
539 | str += ")"; |
---|
540 | } |
---|
541 | }; |
---|
542 | |
---|
543 | // abs(float) |
---|
544 | template<> |
---|
545 | struct Fn_abs< float > { |
---|
546 | typedef float T_numtype1; |
---|
547 | typedef float T_numtype; |
---|
548 | |
---|
549 | static inline T_numtype |
---|
550 | apply(T_numtype1 a) |
---|
551 | { return BZ_MATHFN_SCOPE(fabs)(a); } |
---|
552 | |
---|
553 | template<typename T1> |
---|
554 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
555 | prettyPrintFormat& format, const T1& t1) |
---|
556 | { |
---|
557 | str += "abs"; |
---|
558 | str += "("; |
---|
559 | t1.prettyPrint(str, format); |
---|
560 | str += ")"; |
---|
561 | } |
---|
562 | }; |
---|
563 | |
---|
564 | // abs(double) |
---|
565 | template<> |
---|
566 | struct Fn_abs< double > { |
---|
567 | typedef double T_numtype1; |
---|
568 | typedef double T_numtype; |
---|
569 | |
---|
570 | static inline T_numtype |
---|
571 | apply(T_numtype1 a) |
---|
572 | { return BZ_MATHFN_SCOPE(fabs)(a); } |
---|
573 | |
---|
574 | template<typename T1> |
---|
575 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
576 | prettyPrintFormat& format, const T1& t1) |
---|
577 | { |
---|
578 | str += "abs"; |
---|
579 | str += "("; |
---|
580 | t1.prettyPrint(str, format); |
---|
581 | str += ")"; |
---|
582 | } |
---|
583 | }; |
---|
584 | |
---|
585 | // abs(long double) |
---|
586 | template<> |
---|
587 | struct Fn_abs< long double > { |
---|
588 | typedef long double T_numtype1; |
---|
589 | typedef long double T_numtype; |
---|
590 | |
---|
591 | static inline T_numtype |
---|
592 | apply(T_numtype1 a) |
---|
593 | { return BZ_MATHFN_SCOPE(fabs)(a); } |
---|
594 | |
---|
595 | template<typename T1> |
---|
596 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
597 | prettyPrintFormat& format, const T1& t1) |
---|
598 | { |
---|
599 | str += "abs"; |
---|
600 | str += "("; |
---|
601 | t1.prettyPrint(str, format); |
---|
602 | str += ")"; |
---|
603 | } |
---|
604 | }; |
---|
605 | |
---|
606 | #ifdef BZ_HAVE_COMPLEX_FCNS |
---|
607 | // abs(complex<T>) |
---|
608 | template<typename T> |
---|
609 | struct Fn_abs< complex<T> > { |
---|
610 | typedef complex<T> T_numtype1; |
---|
611 | typedef T T_numtype; |
---|
612 | |
---|
613 | static inline T_numtype |
---|
614 | apply(T_numtype1 a) |
---|
615 | { return BZ_CMATHFN_SCOPE(abs)(a); } |
---|
616 | |
---|
617 | template<typename T1> |
---|
618 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
619 | prettyPrintFormat& format, const T1& t1) |
---|
620 | { |
---|
621 | str += "abs"; |
---|
622 | str += "("; |
---|
623 | t1.prettyPrint(str, format); |
---|
624 | str += ")"; |
---|
625 | } |
---|
626 | }; |
---|
627 | #endif // BZ_HAVE_COMPLEX_FCNS |
---|
628 | |
---|
629 | |
---|
630 | #ifdef BZ_HAVE_IEEE_MATH |
---|
631 | // isnan() Nonzero if NaNS or NaNQ |
---|
632 | template<typename T_numtype1> |
---|
633 | struct Fn_isnan { |
---|
634 | typedef int T_numtype; |
---|
635 | |
---|
636 | static inline T_numtype |
---|
637 | apply(T_numtype1 a) |
---|
638 | { |
---|
639 | #ifdef BZ_ISNAN_IN_NAMESPACE_STD |
---|
640 | return BZ_STD_SCOPE(isnan)(a); |
---|
641 | #else |
---|
642 | return BZ_IEEEMATHFN_SCOPE(isnan)(a); |
---|
643 | #endif |
---|
644 | } |
---|
645 | |
---|
646 | template<typename T1> |
---|
647 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
648 | prettyPrintFormat& format, const T1& t1) |
---|
649 | { |
---|
650 | str += "isnan"; |
---|
651 | str += "("; |
---|
652 | t1.prettyPrint(str, format); |
---|
653 | str += ")"; |
---|
654 | } |
---|
655 | }; |
---|
656 | #endif // BZ_HAVE_IEEE_MATH |
---|
657 | |
---|
658 | |
---|
659 | // Blitz cast() function |
---|
660 | template<typename T_numtype1, typename T_cast> |
---|
661 | struct Cast { |
---|
662 | typedef T_cast T_numtype; |
---|
663 | |
---|
664 | static inline T_numtype |
---|
665 | apply(T_numtype1 a) |
---|
666 | { return T_numtype(a); } |
---|
667 | |
---|
668 | template<typename T1> |
---|
669 | static inline void prettyPrint(BZ_STD_SCOPE(string) &str, |
---|
670 | prettyPrintFormat& format, const T1& t1) |
---|
671 | { |
---|
672 | str += BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_cast); |
---|
673 | str += "("; |
---|
674 | t1.prettyPrint(str, format); |
---|
675 | str += ")"; |
---|
676 | } |
---|
677 | }; |
---|
678 | |
---|
679 | |
---|
680 | BZ_NAMESPACE_END |
---|
681 | |
---|
682 | #endif // BZ_FUNCS_H |
---|