~ubuntu-branches/ubuntu/raring/blitz++/raring

« back to all changes in this revision

Viewing changes to blitz/funcs.h

  • Committer: Bazaar Package Importer
  • Author(s): Konstantinos Margaritis
  • Date: 2005-02-28 20:25:01 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050228202501-3i4f2sknnprsqfhz
Tags: 1:0.8-4
Added missing build-depends (Closes: #297323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- C++ -*-
 
2
/***************************************************************************
 
3
 * blitz/funcs.h            Function objects for math functions
 
4
 *
 
5
 * $Id: funcs.h,v 1.5 2004/10/06 23:36:43 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
#ifdef BZ_HAVE_COMPLEX_FCNS
 
139
BZ_DEFINE_UNARY_FUNC(Fn_conj,BZ_CMATHFN_SCOPE(conj))
 
140
#endif
 
141
 
 
142
#ifdef BZ_HAVE_COMPLEX
 
143
/* Specialization of unary functor for complex type */
 
144
    
 
145
#define BZ_DEFINE_UNARY_CFUNC(name,fun)                        \
 
146
template<typename T>                                           \
 
147
struct name< complex<T> > {                                    \
 
148
    typedef complex<T> T_numtype1;                             \
 
149
    typedef complex<T> T_numtype;                              \
 
150
                                                               \
 
151
    static inline T_numtype                                    \
 
152
    apply(T_numtype1 a)                                        \
 
153
    { return fun(a); }                                         \
 
154
                                                               \
 
155
    template<typename T1>                                      \
 
156
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,  \
 
157
        prettyPrintFormat& format, const T1& t1)               \
 
158
    {                                                          \
 
159
        str += #fun;                                           \
 
160
        str += "(";                                            \
 
161
        t1.prettyPrint(str, format);                           \
 
162
        str += ")";                                            \
 
163
    }                                                          \
 
164
};
 
165
 
 
166
#ifdef BZ_HAVE_COMPLEX_MATH1
 
167
BZ_DEFINE_UNARY_CFUNC(Fn_cos,BZ_CMATHFN_SCOPE(cos))
 
168
BZ_DEFINE_UNARY_CFUNC(Fn_cosh,BZ_CMATHFN_SCOPE(cosh))
 
169
BZ_DEFINE_UNARY_CFUNC(Fn_exp,BZ_CMATHFN_SCOPE(exp))
 
170
BZ_DEFINE_UNARY_CFUNC(Fn_log,BZ_CMATHFN_SCOPE(log))
 
171
BZ_DEFINE_UNARY_CFUNC(Fn_log10,BZ_CMATHFN_SCOPE(log10))
 
172
BZ_DEFINE_UNARY_CFUNC(Fn_sin,BZ_CMATHFN_SCOPE(sin))
 
173
BZ_DEFINE_UNARY_CFUNC(Fn_sinh,BZ_CMATHFN_SCOPE(sinh))
 
174
BZ_DEFINE_UNARY_CFUNC(Fn_sqrt,BZ_CMATHFN_SCOPE(sqrt))
 
175
BZ_DEFINE_UNARY_CFUNC(Fn_tan,BZ_CMATHFN_SCOPE(tan))
 
176
BZ_DEFINE_UNARY_CFUNC(Fn_tanh,BZ_CMATHFN_SCOPE(tanh))
 
177
#endif // BZ_HAVE_COMPLEX_MATH1
 
178
 
 
179
BZ_DEFINE_UNARY_CFUNC(Fn_sqr,BZ_BLITZ_SCOPE(blitz_sqr))
 
180
BZ_DEFINE_UNARY_CFUNC(Fn_cube,BZ_BLITZ_SCOPE(blitz_cube))
 
181
BZ_DEFINE_UNARY_CFUNC(Fn_pow4,BZ_BLITZ_SCOPE(blitz_pow4))
 
182
BZ_DEFINE_UNARY_CFUNC(Fn_pow5,BZ_BLITZ_SCOPE(blitz_pow5))
 
183
BZ_DEFINE_UNARY_CFUNC(Fn_pow6,BZ_BLITZ_SCOPE(blitz_pow6))
 
184
BZ_DEFINE_UNARY_CFUNC(Fn_pow7,BZ_BLITZ_SCOPE(blitz_pow7))
 
185
BZ_DEFINE_UNARY_CFUNC(Fn_pow8,BZ_BLITZ_SCOPE(blitz_pow8))
 
186
 
 
187
/* Unary functions that apply only to complex<T> and return T */
 
188
    
 
189
#define BZ_DEFINE_UNARY_CFUNC2(name,fun)                       \
 
190
template<typename T_numtype1>                                  \
 
191
struct name;                                                   \
 
192
                                                               \
 
193
template<typename T>                                           \
 
194
struct name< complex<T> > {                                    \
 
195
    typedef complex<T> T_numtype1;                             \
 
196
    typedef T T_numtype;                                       \
 
197
                                                               \
 
198
    static inline T_numtype                                    \
 
199
    apply(T_numtype1 a)                                        \
 
200
    { return fun(a); }                                         \
 
201
                                                               \
 
202
    template<typename T1>                                      \
 
203
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,  \
 
204
        prettyPrintFormat& format, const T1& t1)               \
 
205
    {                                                          \
 
206
        str += #fun;                                           \
 
207
        str += "(";                                            \
 
208
        t1.prettyPrint(str, format);                           \
 
209
        str += ")";                                            \
 
210
    }                                                          \
 
211
};
 
212
 
 
213
#ifdef BZ_HAVE_COMPLEX_FCNS
 
214
BZ_DEFINE_UNARY_CFUNC2(Fn_arg,BZ_CMATHFN_SCOPE(arg))
 
215
BZ_DEFINE_UNARY_CFUNC2(Fn_imag,BZ_CMATHFN_SCOPE(imag))
 
216
BZ_DEFINE_UNARY_CFUNC2(Fn_norm,BZ_CMATHFN_SCOPE(norm))
 
217
BZ_DEFINE_UNARY_CFUNC2(Fn_real,BZ_CMATHFN_SCOPE(real))
 
218
#endif // BZ_HAVE_COMPLEX_FCNS
 
219
    
 
220
#endif // BZ_HAVE_COMPLEX
 
221
    
 
222
/* Unary functions that return a specified type */
 
223
    
 
224
#define BZ_DEFINE_UNARY_FUNC_RET(name,fun,ret)                 \
 
225
template<typename T_numtype1>                                  \
 
226
struct name {                                                  \
 
227
    typedef ret T_numtype;                                     \
 
228
                                                               \
 
229
    static inline T_numtype                                    \
 
230
    apply(T_numtype1 a)                                        \
 
231
    { return fun(a); }                                         \
 
232
                                                               \
 
233
    template<typename T1>                                      \
 
234
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,  \
 
235
        prettyPrintFormat& format, const T1& t1)               \
 
236
    {                                                          \
 
237
        str += #fun;                                           \
 
238
        str += "(";                                            \
 
239
        t1.prettyPrint(str, format);                           \
 
240
        str += ")";                                            \
 
241
    }                                                          \
 
242
};
 
243
 
 
244
#ifdef BZ_HAVE_IEEE_MATH
 
245
BZ_DEFINE_UNARY_FUNC_RET(Fn_ilogb,BZ_IEEEMATHFN_SCOPE(ilogb),int)
 
246
#endif
 
247
    
 
248
#ifdef BZ_HAVE_SYSTEM_V_MATH
 
249
BZ_DEFINE_UNARY_FUNC_RET(Fn_itrunc,BZ_IEEEMATHFN_SCOPE(itrunc),int)
 
250
BZ_DEFINE_UNARY_FUNC_RET(Fn_uitrunc,BZ_IEEEMATHFN_SCOPE(uitrunc),unsigned int)
 
251
#endif
 
252
    
 
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
#ifdef BZ_HAVE_COMPLEX
 
293
/* Specialization of binary functor for complex type */
 
294
    
 
295
#define BZ_DEFINE_BINARY_CFUNC(name,fun)                       \
 
296
template<typename T>                                           \
 
297
struct name< complex<T>, complex<T> > {                        \
 
298
    typedef complex<T> T_numtype1;                             \
 
299
    typedef complex<T> T_numtype2;                             \
 
300
    typedef complex<T> T_numtype;                              \
 
301
                                                               \
 
302
    static inline T_numtype                                    \
 
303
    apply(T_numtype1 a, T_numtype2 b)                          \
 
304
    { return fun(a,b); }                                       \
 
305
                                                               \
 
306
    template<typename T1, typename T2>                         \
 
307
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,  \
 
308
        prettyPrintFormat& format, const T1& t1,               \
 
309
        const T2& t2)                                          \
 
310
    {                                                          \
 
311
        str += #fun;                                           \
 
312
        str += "(";                                            \
 
313
        t1.prettyPrint(str, format);                           \
 
314
        str += ",";                                            \
 
315
        t2.prettyPrint(str, format);                           \
 
316
        str += ")";                                            \
 
317
    }                                                          \
 
318
};                                                             \
 
319
                                                               \
 
320
template<typename T>                                           \
 
321
struct name< complex<T>, T > {                                 \
 
322
    typedef complex<T> T_numtype1;                             \
 
323
    typedef T T_numtype2;                                      \
 
324
    typedef complex<T> T_numtype;                              \
 
325
                                                               \
 
326
    static inline T_numtype                                    \
 
327
    apply(T_numtype1 a, T_numtype2 b)                          \
 
328
    { return fun(a,b); }                                       \
 
329
                                                               \
 
330
    template<typename T1, typename T2>                         \
 
331
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,  \
 
332
        prettyPrintFormat& format, const T1& t1,               \
 
333
        const T2& t2)                                          \
 
334
    {                                                          \
 
335
        str += #fun;                                           \
 
336
        str += "(";                                            \
 
337
        t1.prettyPrint(str, format);                           \
 
338
        str += ",";                                            \
 
339
        t2.prettyPrint(str, format);                           \
 
340
        str += ")";                                            \
 
341
    }                                                          \
 
342
};                                                             \
 
343
                                                               \
 
344
template<typename T>                                           \
 
345
struct name< T, complex<T> > {                                 \
 
346
    typedef T T_numtype1;                                      \
 
347
    typedef complex<T> T_numtype2;                             \
 
348
    typedef complex<T> T_numtype;                              \
 
349
                                                               \
 
350
    static inline T_numtype                                    \
 
351
    apply(T_numtype1 a, T_numtype2 b)                          \
 
352
    { return fun(a,b); }                                       \
 
353
                                                               \
 
354
    template<typename T1, typename T2>                         \
 
355
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,  \
 
356
        prettyPrintFormat& format, const T1& t1,               \
 
357
        const T2& t2)                                          \
 
358
    {                                                          \
 
359
        str += #fun;                                           \
 
360
        str += "(";                                            \
 
361
        t1.prettyPrint(str, format);                           \
 
362
        str += ",";                                            \
 
363
        t2.prettyPrint(str, format);                           \
 
364
        str += ")";                                            \
 
365
    }                                                          \
 
366
};
 
367
 
 
368
#ifdef BZ_HAVE_COMPLEX_MATH1
 
369
BZ_DEFINE_BINARY_CFUNC(Fn_pow,BZ_CMATHFN_SCOPE(pow))
 
370
#endif
 
371
 
 
372
/* Binary functions that apply only to T and return complex<T> */
 
373
    
 
374
#define BZ_DEFINE_BINARY_CFUNC2(name,fun)                         \
 
375
template<typename T_numtype1, typename T_numtype2>                \
 
376
struct name;                                                      \
 
377
                                                                  \
 
378
template<typename T>                                              \
 
379
struct name<T, T> {                                               \
 
380
    typedef T T_numtype1;                                         \
 
381
    typedef T T_numtype2;                                         \
 
382
    typedef complex<T> T_numtype;                                 \
 
383
                                                                  \
 
384
    static inline T_numtype                                       \
 
385
    apply(T_numtype1 a, T_numtype2 b)                             \
 
386
    { return fun(a,b); }                                          \
 
387
                                                                  \
 
388
    template<typename T1, typename T2>                            \
 
389
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,     \
 
390
        prettyPrintFormat& format, const T1& t1,                  \
 
391
        const T2& t2)                                             \
 
392
    {                                                             \
 
393
        str += #fun;                                              \
 
394
        str += "(";                                               \
 
395
        t1.prettyPrint(str, format);                              \
 
396
        str += ",";                                               \
 
397
        t2.prettyPrint(str, format);                              \
 
398
        str += ")";                                               \
 
399
    }                                                             \
 
400
};
 
401
 
 
402
#ifdef BZ_HAVE_COMPLEX_FCNS
 
403
BZ_DEFINE_BINARY_CFUNC2(Fn_polar,BZ_CMATHFN_SCOPE(polar))
 
404
#endif
 
405
    
 
406
#endif // BZ_HAVE_COMPLEX
 
407
    
 
408
/* Binary functions that return a specified type */
 
409
    
 
410
#define BZ_DEFINE_BINARY_FUNC_RET(name,fun,ret)                   \
 
411
template<typename T_numtype1, typename T_numtype2>                \
 
412
struct name {                                                     \
 
413
    typedef ret T_numtype;                                        \
 
414
                                                                  \
 
415
    static inline T_numtype                                       \
 
416
    apply(T_numtype1 a, T_numtype2 b)                             \
 
417
    { return fun(a,b); }                                          \
 
418
                                                                  \
 
419
    template<typename T1, typename T2>                            \
 
420
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,     \
 
421
        prettyPrintFormat& format, const T1& t1,                  \
 
422
        const T2& t2)                                             \
 
423
    {                                                             \
 
424
        str += #fun;                                              \
 
425
        str += "(";                                               \
 
426
        t1.prettyPrint(str, format);                              \
 
427
        str += ",";                                               \
 
428
        t2.prettyPrint(str, format);                              \
 
429
        str += ")";                                               \
 
430
    }                                                             \
 
431
};
 
432
 
 
433
#ifdef BZ_HAVE_SYSTEM_V_MATH
 
434
BZ_DEFINE_BINARY_FUNC_RET(Fn_unordered,BZ_IEEEMATHFN_SCOPE(unordered),int)
 
435
#endif
 
436
    
 
437
    
 
438
/* These functions don't quite fit the usual patterns */
 
439
    
 
440
// abs()    Absolute value
 
441
template<typename T_numtype1>
 
442
struct Fn_abs;
 
443
 
 
444
// abs(int)
 
445
template<>
 
446
struct Fn_abs< int > {
 
447
    typedef int T_numtype1;
 
448
    typedef int T_numtype;
 
449
    
 
450
    static inline T_numtype
 
451
    apply(T_numtype1 a)
 
452
    { return BZ_MATHFN_SCOPE(abs)(a); }
 
453
    
 
454
    template<typename T1>
 
455
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,
 
456
        prettyPrintFormat& format, const T1& t1)
 
457
    {
 
458
        str += "abs";
 
459
        str += "(";
 
460
        t1.prettyPrint(str, format);
 
461
        str += ")";
 
462
    }
 
463
};
 
464
 
 
465
// abs(long)
 
466
template<>
 
467
struct Fn_abs< long int > {
 
468
    typedef long int T_numtype1;
 
469
    typedef long int T_numtype;
 
470
    
 
471
    static inline T_numtype
 
472
    apply(T_numtype1 a)
 
473
    { return BZ_MATHFN_SCOPE(labs)(a); }
 
474
    
 
475
    template<typename T1>
 
476
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,
 
477
        prettyPrintFormat& format, const T1& t1)
 
478
    {
 
479
        str += "abs";
 
480
        str += "(";
 
481
        t1.prettyPrint(str, format);
 
482
        str += ")";
 
483
    }
 
484
};
 
485
 
 
486
// abs(float)
 
487
template<>
 
488
struct Fn_abs< float > {
 
489
    typedef float T_numtype1;
 
490
    typedef float T_numtype;
 
491
    
 
492
    static inline T_numtype
 
493
    apply(T_numtype1 a)
 
494
    { return BZ_MATHFN_SCOPE(fabs)(a); }
 
495
    
 
496
    template<typename T1>
 
497
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,
 
498
        prettyPrintFormat& format, const T1& t1)
 
499
    {
 
500
        str += "abs";
 
501
        str += "(";
 
502
        t1.prettyPrint(str, format);
 
503
        str += ")";
 
504
    }
 
505
};
 
506
 
 
507
// abs(double)
 
508
template<>
 
509
struct Fn_abs< double > {
 
510
    typedef double T_numtype1;
 
511
    typedef double T_numtype;
 
512
    
 
513
    static inline T_numtype
 
514
    apply(T_numtype1 a)
 
515
    { return BZ_MATHFN_SCOPE(fabs)(a); }
 
516
    
 
517
    template<typename T1>
 
518
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,
 
519
        prettyPrintFormat& format, const T1& t1)
 
520
    {
 
521
        str += "abs";
 
522
        str += "(";
 
523
        t1.prettyPrint(str, format);
 
524
        str += ")";
 
525
    }
 
526
};
 
527
 
 
528
// abs(long double)
 
529
template<>
 
530
struct Fn_abs< long double > {
 
531
    typedef long double T_numtype1;
 
532
    typedef long double T_numtype;
 
533
    
 
534
    static inline T_numtype
 
535
    apply(T_numtype1 a)
 
536
    { return BZ_MATHFN_SCOPE(fabs)(a); }
 
537
    
 
538
    template<typename T1>
 
539
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,
 
540
        prettyPrintFormat& format, const T1& t1)
 
541
    {
 
542
        str += "abs";
 
543
        str += "(";
 
544
        t1.prettyPrint(str, format);
 
545
        str += ")";
 
546
    }
 
547
};
 
548
 
 
549
#ifdef BZ_HAVE_COMPLEX_FCNS
 
550
// abs(complex<T>)
 
551
template<typename T>
 
552
struct Fn_abs< complex<T> > {
 
553
    typedef complex<T> T_numtype1;
 
554
    typedef T T_numtype;
 
555
    
 
556
    static inline T_numtype
 
557
    apply(T_numtype1 a)
 
558
    { return BZ_CMATHFN_SCOPE(abs)(a); }
 
559
    
 
560
    template<typename T1>
 
561
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,
 
562
        prettyPrintFormat& format, const T1& t1)
 
563
    {
 
564
        str += "abs";
 
565
        str += "(";
 
566
        t1.prettyPrint(str, format);
 
567
        str += ")";
 
568
    }
 
569
};
 
570
#endif // BZ_HAVE_COMPLEX_FCNS
 
571
 
 
572
 
 
573
#ifdef BZ_HAVE_IEEE_MATH
 
574
// isnan()    Nonzero if NaNS or NaNQ
 
575
template<typename T_numtype1>
 
576
struct Fn_isnan {
 
577
    typedef int T_numtype;
 
578
    
 
579
    static inline T_numtype
 
580
    apply(T_numtype1 a)
 
581
    {
 
582
#ifdef isnan
 
583
        // Some platforms define isnan as a macro, which causes the
 
584
        // BZ_IEEEMATHFN_SCOPE macro to break.
 
585
        return isnan(a); 
 
586
#else
 
587
        return BZ_IEEEMATHFN_SCOPE(isnan)(a);
 
588
#endif
 
589
    }
 
590
    
 
591
    template<typename T1>
 
592
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,
 
593
        prettyPrintFormat& format, const T1& t1)
 
594
    {
 
595
        str += "isnan";
 
596
        str += "(";
 
597
        t1.prettyPrint(str, format);
 
598
        str += ")";
 
599
    }
 
600
};
 
601
#endif // BZ_HAVE_IEEE_MATH
 
602
 
 
603
 
 
604
// Blitz cast() function
 
605
template<typename T_numtype1, typename T_cast>
 
606
struct Cast {
 
607
    typedef T_cast T_numtype;
 
608
    
 
609
    static inline T_numtype
 
610
    apply(T_numtype1 a)
 
611
    { return T_numtype(a); }
 
612
 
 
613
    template<typename T1>
 
614
    static inline void prettyPrint(BZ_STD_SCOPE(string) &str,
 
615
        prettyPrintFormat& format, const T1& t1)
 
616
    {
 
617
        str += BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_cast);
 
618
        str += "(";
 
619
        t1.prettyPrint(str, format);
 
620
        str += ")";
 
621
    }
 
622
};
 
623
 
 
624
 
 
625
BZ_NAMESPACE_END
 
626
 
 
627
#endif // BZ_FUNCS_H