1 | /*************************************************************************** |
---|
2 | * blitz/applics.h Applicative template classes |
---|
3 | * |
---|
4 | * $Id: applics.h,v 1.5 2003/12/11 03:44:22 julianc Exp $ |
---|
5 | * |
---|
6 | * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org> |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * Suggestions: blitz-dev@oonumerics.org |
---|
19 | * Bugs: blitz-bugs@oonumerics.org |
---|
20 | * |
---|
21 | * For more information, please see the Blitz++ Home Page: |
---|
22 | * http://oonumerics.org/blitz/ |
---|
23 | * |
---|
24 | ***************************************************************************/ |
---|
25 | |
---|
26 | #ifndef BZ_APPLICS_H |
---|
27 | #define BZ_APPLICS_H |
---|
28 | |
---|
29 | #ifndef BZ_BLITZ_H |
---|
30 | #include <blitz/blitz.h> |
---|
31 | #endif |
---|
32 | |
---|
33 | #ifndef BZ_PROMOTE_H |
---|
34 | #include <blitz/promote.h> |
---|
35 | #endif |
---|
36 | |
---|
37 | #ifndef BZ_NUMTRAIT_H |
---|
38 | #include <blitz/numtrait.h> |
---|
39 | #endif |
---|
40 | |
---|
41 | BZ_NAMESPACE(blitz) |
---|
42 | |
---|
43 | // These base classes are included for no other reason than to keep |
---|
44 | // the applicative templates clustered together in a graphical |
---|
45 | // class browser. |
---|
46 | class ApplicativeTemplatesBase { }; |
---|
47 | class TwoOperandApplicativeTemplatesBase : public ApplicativeTemplatesBase { }; |
---|
48 | class OneOperandApplicativeTemplatesBase : public ApplicativeTemplatesBase { }; |
---|
49 | |
---|
50 | template<typename P_numtype1, typename P_numtype2> |
---|
51 | class _bz_Add : public TwoOperandApplicativeTemplatesBase { |
---|
52 | public: |
---|
53 | typedef P_numtype1 T_numtype1; |
---|
54 | typedef P_numtype2 T_numtype2; |
---|
55 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
56 | typedef T_promote T_numtype; |
---|
57 | |
---|
58 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
59 | { return x + y; } |
---|
60 | }; |
---|
61 | |
---|
62 | template<typename P_numtype1, typename P_numtype2> |
---|
63 | class _bz_Subtract : public TwoOperandApplicativeTemplatesBase { |
---|
64 | public: |
---|
65 | typedef P_numtype1 T_numtype1; |
---|
66 | typedef P_numtype2 T_numtype2; |
---|
67 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
68 | typedef T_promote T_numtype; |
---|
69 | |
---|
70 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
71 | { return x - y; } |
---|
72 | }; |
---|
73 | |
---|
74 | template<typename P_numtype1, typename P_numtype2> |
---|
75 | class _bz_Multiply : public TwoOperandApplicativeTemplatesBase { |
---|
76 | public: |
---|
77 | typedef P_numtype1 T_numtype1; |
---|
78 | typedef P_numtype2 T_numtype2; |
---|
79 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
80 | typedef T_promote T_numtype; |
---|
81 | |
---|
82 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
83 | { return x * y; } |
---|
84 | }; |
---|
85 | |
---|
86 | template<typename P_numtype1, typename P_numtype2> |
---|
87 | class _bz_Divide : public TwoOperandApplicativeTemplatesBase { |
---|
88 | public: |
---|
89 | typedef P_numtype1 T_numtype1; |
---|
90 | typedef P_numtype2 T_numtype2; |
---|
91 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
92 | typedef T_promote T_numtype; |
---|
93 | |
---|
94 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
95 | { return x / y; } |
---|
96 | }; |
---|
97 | |
---|
98 | template<typename P_numtype1, typename P_numtype2> |
---|
99 | class _bz_Mod : public TwoOperandApplicativeTemplatesBase { |
---|
100 | public: |
---|
101 | typedef P_numtype1 T_numtype1; |
---|
102 | typedef P_numtype2 T_numtype2; |
---|
103 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
104 | typedef T_promote T_numtype; |
---|
105 | |
---|
106 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
107 | { return x % y; } |
---|
108 | }; |
---|
109 | |
---|
110 | template<typename P_numtype1, typename P_numtype2> |
---|
111 | class _bz_BitwiseXOR : public TwoOperandApplicativeTemplatesBase { |
---|
112 | public: |
---|
113 | typedef P_numtype1 T_numtype1; |
---|
114 | typedef P_numtype2 T_numtype2; |
---|
115 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
116 | typedef T_promote T_numtype; |
---|
117 | |
---|
118 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
119 | { return x ^ y; } |
---|
120 | }; |
---|
121 | |
---|
122 | template<typename P_numtype1, typename P_numtype2> |
---|
123 | class _bz_BitwiseAnd : public TwoOperandApplicativeTemplatesBase { |
---|
124 | public: |
---|
125 | typedef P_numtype1 T_numtype1; |
---|
126 | typedef P_numtype2 T_numtype2; |
---|
127 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
128 | typedef T_promote T_numtype; |
---|
129 | |
---|
130 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
131 | { return x & y; } |
---|
132 | }; |
---|
133 | |
---|
134 | template<typename P_numtype1, typename P_numtype2> |
---|
135 | class _bz_BitwiseOr : public TwoOperandApplicativeTemplatesBase { |
---|
136 | public: |
---|
137 | typedef P_numtype1 T_numtype1; |
---|
138 | typedef P_numtype2 T_numtype2; |
---|
139 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
140 | typedef T_promote T_numtype; |
---|
141 | |
---|
142 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
143 | { return x | y; } |
---|
144 | }; |
---|
145 | |
---|
146 | template<typename P_numtype1, typename P_numtype2> |
---|
147 | class _bz_ShiftRight : public TwoOperandApplicativeTemplatesBase { |
---|
148 | public: |
---|
149 | typedef P_numtype1 T_numtype1; |
---|
150 | typedef P_numtype2 T_numtype2; |
---|
151 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
152 | typedef T_promote T_numtype; |
---|
153 | |
---|
154 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
155 | { return x >> y; } |
---|
156 | }; |
---|
157 | |
---|
158 | template<typename P_numtype1, typename P_numtype2> |
---|
159 | class _bz_ShiftLeft : public TwoOperandApplicativeTemplatesBase { |
---|
160 | public: |
---|
161 | typedef P_numtype1 T_numtype1; |
---|
162 | typedef P_numtype2 T_numtype2; |
---|
163 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
164 | typedef T_promote T_numtype; |
---|
165 | |
---|
166 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
167 | { return x << y; } |
---|
168 | }; |
---|
169 | |
---|
170 | |
---|
171 | template<typename P_numtype1, typename P_numtype2> |
---|
172 | class _bz_Min : public TwoOperandApplicativeTemplatesBase { |
---|
173 | public: |
---|
174 | typedef P_numtype1 T_numtype1; |
---|
175 | typedef P_numtype2 T_numtype2; |
---|
176 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
177 | typedef T_promote T_numtype; |
---|
178 | |
---|
179 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
180 | { return (x < y ? x : y); } |
---|
181 | }; |
---|
182 | |
---|
183 | template<typename P_numtype1, typename P_numtype2> |
---|
184 | class _bz_Max : public TwoOperandApplicativeTemplatesBase { |
---|
185 | public: |
---|
186 | typedef P_numtype1 T_numtype1; |
---|
187 | typedef P_numtype2 T_numtype2; |
---|
188 | typedef BZ_PROMOTE(T_numtype1,T_numtype2) T_promote; |
---|
189 | typedef T_promote T_numtype; |
---|
190 | |
---|
191 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
192 | { return (x > y ? x : y); } |
---|
193 | }; |
---|
194 | |
---|
195 | template<typename P_numtype1, typename P_numtype2> |
---|
196 | class _bz_Greater : public TwoOperandApplicativeTemplatesBase { |
---|
197 | public: |
---|
198 | typedef P_numtype1 T_numtype1; |
---|
199 | typedef P_numtype2 T_numtype2; |
---|
200 | typedef bool T_promote; |
---|
201 | typedef T_promote T_numtype; |
---|
202 | |
---|
203 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
204 | { return x > y; } |
---|
205 | }; |
---|
206 | |
---|
207 | template<typename P_numtype1, typename P_numtype2> |
---|
208 | class _bz_Less : public TwoOperandApplicativeTemplatesBase { |
---|
209 | public: |
---|
210 | typedef P_numtype1 T_numtype1; |
---|
211 | typedef P_numtype2 T_numtype2; |
---|
212 | typedef bool T_promote; |
---|
213 | typedef T_promote T_numtype; |
---|
214 | |
---|
215 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
216 | { return x < y; } |
---|
217 | }; |
---|
218 | |
---|
219 | template<typename P_numtype1, typename P_numtype2> |
---|
220 | class _bz_GreaterOrEqual : public TwoOperandApplicativeTemplatesBase { |
---|
221 | public: |
---|
222 | typedef P_numtype1 T_numtype1; |
---|
223 | typedef P_numtype2 T_numtype2; |
---|
224 | typedef bool T_promote; |
---|
225 | typedef T_promote T_numtype; |
---|
226 | |
---|
227 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
228 | { return x >= y; } |
---|
229 | }; |
---|
230 | |
---|
231 | template<typename P_numtype1, typename P_numtype2> |
---|
232 | class _bz_LessOrEqual : public TwoOperandApplicativeTemplatesBase { |
---|
233 | public: |
---|
234 | typedef P_numtype1 T_numtype1; |
---|
235 | typedef P_numtype2 T_numtype2; |
---|
236 | typedef bool T_promote; |
---|
237 | typedef T_promote T_numtype; |
---|
238 | |
---|
239 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
240 | { return x <= y; } |
---|
241 | }; |
---|
242 | |
---|
243 | template<typename P_numtype1, typename P_numtype2> |
---|
244 | class _bz_Equal : public TwoOperandApplicativeTemplatesBase { |
---|
245 | public: |
---|
246 | typedef P_numtype1 T_numtype1; |
---|
247 | typedef P_numtype2 T_numtype2; |
---|
248 | typedef bool T_promote; |
---|
249 | typedef T_promote T_numtype; |
---|
250 | |
---|
251 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
252 | { return x == y; } |
---|
253 | }; |
---|
254 | |
---|
255 | template<typename P_numtype1, typename P_numtype2> |
---|
256 | class _bz_NotEqual : public TwoOperandApplicativeTemplatesBase { |
---|
257 | public: |
---|
258 | typedef P_numtype1 T_numtype1; |
---|
259 | typedef P_numtype2 T_numtype2; |
---|
260 | typedef bool T_promote; |
---|
261 | typedef T_promote T_numtype; |
---|
262 | |
---|
263 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
264 | { return x != y; } |
---|
265 | }; |
---|
266 | |
---|
267 | template<typename P_numtype1, typename P_numtype2> |
---|
268 | class _bz_LogicalAnd : public TwoOperandApplicativeTemplatesBase { |
---|
269 | public: |
---|
270 | typedef P_numtype1 T_numtype1; |
---|
271 | typedef P_numtype2 T_numtype2; |
---|
272 | typedef bool T_promote; |
---|
273 | typedef T_promote T_numtype; |
---|
274 | |
---|
275 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
276 | { return x && y; } |
---|
277 | }; |
---|
278 | |
---|
279 | template<typename P_numtype1, typename P_numtype2> |
---|
280 | class _bz_LogicalOr : public TwoOperandApplicativeTemplatesBase { |
---|
281 | public: |
---|
282 | typedef P_numtype1 T_numtype1; |
---|
283 | typedef P_numtype2 T_numtype2; |
---|
284 | typedef bool T_promote; |
---|
285 | typedef T_promote T_numtype; |
---|
286 | |
---|
287 | static inline T_promote apply(P_numtype1 x, P_numtype2 y) |
---|
288 | { return x || y; } |
---|
289 | }; |
---|
290 | |
---|
291 | |
---|
292 | template<typename P_numtype_in, typename P_numtype_out> |
---|
293 | class _bz_Cast : public OneOperandApplicativeTemplatesBase { |
---|
294 | public: |
---|
295 | typedef P_numtype_in T_numtype1; |
---|
296 | typedef P_numtype_out T_promote; |
---|
297 | typedef T_promote T_numtype; |
---|
298 | |
---|
299 | static inline P_numtype_out apply(P_numtype_in x) |
---|
300 | { return P_numtype_out(x); } |
---|
301 | }; |
---|
302 | |
---|
303 | template<typename P_numtype> |
---|
304 | class _bz_LogicalNot : public OneOperandApplicativeTemplatesBase { |
---|
305 | public: |
---|
306 | typedef P_numtype T_numtype1; |
---|
307 | typedef bool T_promote; |
---|
308 | typedef T_promote T_numtype; |
---|
309 | |
---|
310 | static inline P_numtype apply(P_numtype x) |
---|
311 | { return !x; } |
---|
312 | }; |
---|
313 | |
---|
314 | template<typename P_numtype> |
---|
315 | class _bz_BitwiseNot : public OneOperandApplicativeTemplatesBase { |
---|
316 | public: |
---|
317 | typedef P_numtype T_numtype1; |
---|
318 | typedef T_numtype1 T_promote; |
---|
319 | typedef T_promote T_numtype; |
---|
320 | |
---|
321 | static inline P_numtype apply(P_numtype x) |
---|
322 | { return ~x; } |
---|
323 | }; |
---|
324 | |
---|
325 | |
---|
326 | |
---|
327 | /***************************************************************************** |
---|
328 | * Math Functions |
---|
329 | *****************************************************************************/ |
---|
330 | |
---|
331 | // Applicative templates for these functions are defined in |
---|
332 | // <blitz/mathfunc.h>, which is included below: |
---|
333 | // |
---|
334 | // abs(i), labs(l) Absolute value |
---|
335 | // acos(d), acols(ld) Inverse cosine |
---|
336 | // acosh(d) Inverse hyperbolic cosine |
---|
337 | // asin(d), asinl(ld) Inverse sine |
---|
338 | // asinh(d) Inverse hyperbolic sine |
---|
339 | // atan(d), atanl(ld) Inverse tangent |
---|
340 | // atan2(d,d), atan2l(ld,ld) Inverse tangent |
---|
341 | // atanh(d) Inverse hyperbolic tangent |
---|
342 | // cbrt(x) Cube root |
---|
343 | // ceil(d), ceill(ld) Smallest f-int not less than x |
---|
344 | // int class(d) Classification of x (FP_XXXXX) |
---|
345 | // cos(d), cosl(ld) Cosine |
---|
346 | // cosh(d), coshl(ld) Hyperbolic cosine |
---|
347 | // copysign(d,d) Return 1st arg with same sign as 2nd |
---|
348 | // drem(x,x) IEEE remainder |
---|
349 | // exp(d), expl(ld) Exponential |
---|
350 | // expm1(d) Exp(x)-1 |
---|
351 | // erf(d), erfl(ld) Error function |
---|
352 | // erfc(d), erfcl(ld) Complementary error function |
---|
353 | // fabs(d), fabsl(ld) Floating point absolute value |
---|
354 | // int finite(d) Nonzero if finite |
---|
355 | // floor(d), floor(ld) Largest f-int not greater than x |
---|
356 | // fmod(d,d), fmodl(ld,ld) Floating point remainder |
---|
357 | // frexp(d, int* e) Break into mantissa/exponent (*) |
---|
358 | // frexpl(ld, int* e) Break into mantissa/exponent (*) |
---|
359 | // gammaFunc(d) Gamma function (** needs special |
---|
360 | // implementation using lgamma) |
---|
361 | // hypot(d,d) Hypotenuse: sqrt(x*x+y*y) |
---|
362 | // int ilogb(d) Integer unbiased exponent |
---|
363 | // int isnan(d) Nonzero if NaNS or NaNQ |
---|
364 | // int itrunc(d) Truncate and convert to integer |
---|
365 | // j0(d) Bessel function first kind, order 0 |
---|
366 | // j1(d) Bessel function first kind, order 1 |
---|
367 | // jn(int, double) Bessel function first kind, order i |
---|
368 | // ldexp(d,i), ldexpl(ld,i) Compute d * 2^i |
---|
369 | // lgamma(d), lgammald(ld) Log absolute gamma |
---|
370 | // log(d), logl(ld) Natural logarithm |
---|
371 | // logb(d) Unbiased exponent (IEEE) |
---|
372 | // log1p(d) Compute log(1 + x) |
---|
373 | // log10(d), log10l(ld) Logarithm base 10 |
---|
374 | // modf(d, int* i), modfl(ld, int* i) Break into integral/fractional part |
---|
375 | // double nearest(double) Nearest floating point integer |
---|
376 | // nextafter(d, d) Next representable neighbor of 1st |
---|
377 | // in direction of 2nd |
---|
378 | // pow(d,d), pow(ld,ld) Computes x ^ y |
---|
379 | // d remainder(d,d) IEEE remainder |
---|
380 | // d rint(d) Round to f-integer (depends on mode) |
---|
381 | // d rsqrt(d) Reciprocal square root |
---|
382 | // d scalb(d,d) Return x * (2^y) |
---|
383 | // sin(d), sinl(ld) Sine |
---|
384 | // sinh(d), sinhl(ld) Hyperbolic sine |
---|
385 | // sqr(x) Return x * x |
---|
386 | // sqrt(d), sqrtl(ld) Square root |
---|
387 | // tan(d), tanl(ld) Tangent |
---|
388 | // tanh(d), tanhl(ld) Hyperbolic tangent |
---|
389 | // trunc(d) Nearest f-int in the direction of 0 |
---|
390 | // unsigned uitrunc(d) Truncate and convert to unsigned |
---|
391 | // int unordered(d,d) Nonzero if comparison is unordered |
---|
392 | // y0(d) Bessel function 2nd kind, order 0 |
---|
393 | // y1(d) Bessel function 2nd kind, order 1 |
---|
394 | // yn(i,d) Bessel function 2nd kind, order d |
---|
395 | |
---|
396 | |
---|
397 | BZ_NAMESPACE_END |
---|
398 | |
---|
399 | #ifndef BZ_MATHFUNC_H |
---|
400 | #include <blitz/mathfunc.h> |
---|
401 | #endif |
---|
402 | |
---|
403 | #ifndef BZ_MATHF2_H |
---|
404 | #include <blitz/mathf2.h> |
---|
405 | #endif |
---|
406 | |
---|
407 | #endif // BZ_APPLICS_H |
---|