-
Notifications
You must be signed in to change notification settings - Fork 9
/
functors.jl
344 lines (269 loc) · 7.47 KB
/
functors.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#################################################
#
# Abstract types
#
#################################################
abstract Functor{N} # N is the number of arguments
typealias UnaryFunctor Functor{1}
typealias BinaryFunctor Functor{2}
typealias TernaryFunctor Functor{3}
## macros for defining functors
# On 0.4 we can do `function evaluate end` (althought it's probably better)
# to use the call overload when we require julia 0.4
function evaluate() end
macro functor1(F, fun, T)
eF = esc(F)
efun = esc(fun)
eT = esc(T)
quote
type $eF <: Functor{1} end
NumericFuns.evaluate(::$eF, x::$eT) = $efun(x)
end
end
macro functor2(F, fun, T)
eF = esc(F)
efun = esc(fun)
eT = esc(T)
quote
type $eF <: Functor{2} end
NumericFuns.evaluate(::$eF, x::$eT, y::$eT) = $efun(x, y)
end
end
default_functorsym(f::Symbol) =
(fstr = string(f); symbol(string(uppercase(fstr[1]), fstr[2:end], "Fun")))
macro functor1a(fun, T)
F = default_functorsym(fun)
eF = esc(F)
efun = esc(fun)
eT = esc(T)
quote
type $eF <: Functor{1} end
NumericFuns.evaluate(::$eF, x::$eT) = $efun(x)
end
end
macro functor2a(fun, T)
F = default_functorsym(fun)
eF = esc(F)
efun = esc(fun)
eT = esc(T)
quote
type $eF <: Functor{2} end
NumericFuns.evaluate(::$eF, x::$eT, y::$eT) = $efun(x, y)
end
end
macro functor1a_ord(fun, T)
F = default_functorsym(fun)
eF = esc(F)
efun = esc(fun)
eT = esc(T)
quote
immutable $eF{OT<:Real} <: Functor{1}
order::OT
end
$eF{OT<:Real}(ord::OT) = $eF{OT}(ord)
NumericFuns.evaluate(f::$eF, x::$eT) = $efun(f.order, x)
end
end
#################################################
#
# Functors
#
#################################################
## arithmetic operators
export Negate, Add, Subtract, Multiply, Divide, RDivide, Pow
export FixPow, FixAbsPow
@functor1(Negate, -, Number)
@functor2(Add, +, Number)
@functor2(Subtract, -, Number)
@functor2(Multiply, *, Number)
@functor2(Divide, /, Number)
@functor2(RDivide, \, Number)
@functor2(Pow, ^, Number)
immutable FixPow{T<:Real} <: Functor{1}
p::T
end
evaluate(f::FixPow, x::Real) = x ^ f.p
immutable FixAbsPow{T<:Real} <: Functor{1}
p::T
end
evaluate(f::FixAbsPow, x::Real) = abs(x) ^ f.p
## comparison operators
export LT, GT, LE, GE, EQ, NE
@functor2(LT, <, Real)
@functor2(GT, >, Real)
@functor2(LE, <=, Real)
@functor2(GE, >=, Real)
@functor2(EQ, ==, Number)
@functor2(NE, !=, Number)
## logical & bitwise operators
export Not, And, Or
export BitwiseNot, BitwiseAnd, BitwiseOr, BitwiseXor
@functor1(Not, !, Bool)
@functor2(And, &, Bool)
@functor2(Or, |, Bool)
@functor1(BitwiseNot, ~, Integer)
@functor2(BitwiseAnd, &, Integer)
@functor2(BitwiseOr, |, Integer)
@functor2(BitwiseXor, $, Integer)
## arithmetic functions
export DivFun, FldFun, RemFun, ModFun
export AbsFun, Abs2Fun, RealFun, ImagFun, MaxFun, MinFun
export SqrFun, RcpFun, SignFun, SignbitFun
@functor2a div Real
@functor2a fld Real
@functor2a rem Real
@functor2a mod Real
@functor1a abs Number
@functor1a abs2 Number
@functor1a real Number
@functor1a imag Number
@functor2a max Real
@functor2a min Real
@functor1a sqr Number
@functor1a rcp Number
@functor1a sign Real
@functor1a signbit Real
## rounding functions
export FloorFun, CeilFun, TruncFun, RoundFun
export IfloorFun, IceilFun, ItruncFun, IroundFun
@functor1a floor Real
@functor1a ceil Real
@functor1a trunc Real
@functor1a round Real
_ifloor(x) = floor(Integer, x)
_iceil(x) = ceil(Integer, x)
_itrunc(x) = trunc(Integer, x)
_iround(x) = round(Integer, x)
@functor1 IfloorFun _ifloor Real
@functor1 IceilFun _iceil Real
@functor1 ItruncFun _itrunc Real
@functor1 IroundFun _iround Real
## number classification
export IsnanFun, IsinfFun, IsfiniteFun
@functor1a isnan Real
@functor1a isinf Real
@functor1a isfinite Real
## algebraic functions
export SqrtFun, CbrtFun, HypotFun
export RsqrtFun, RcbrtFun
@functor1a sqrt Number
@functor1a cbrt Real
@functor2a hypot Real
@functor1a rsqrt Number
@functor1a rcbrt Real
## exponential & logarithm
export ExpFun, Exp2Fun, Exp10Fun, Expm1Fun
export LogFun, Log2Fun, Log10Fun, Log1pFun
export XlogxFun, XlogyFun, LogisticFun, LogitFun
export SoftplusFun, InvsoftplusFun, LogsumexpFun
@functor1a exp Number
@functor1a exp2 Number
@functor1a exp10 Number
@functor1a expm1 Real
@functor1a log Number
@functor1a log2 Number
@functor1a log10 Number
@functor1a log1p Real
@functor1a xlogx Real
@functor2a xlogy Real
@functor1a logistic Real
@functor1a logit Real
@functor1a softplus Real
@functor1a invsoftplus Real
@functor2a logsumexp Real
## trigonometric functions
export SinFun, CosFun, TanFun, CotFun, SecFun, CscFun
export AsinFun, AcosFun, AtanFun, AcotFun, AsecFun, AcscFun, Atan2Fun
export SincFun, CoscFun, SinpiFun, CospiFun
export SindFun, CosdFun, TandFun, CotdFun, SecdFun, CscdFun
export AsindFun, AcosdFun, AtandFun, AcotdFun, AsecdFun, AcscdFun
@functor1a sin Number
@functor1a cos Number
@functor1a tan Number
@functor1a cot Number
@functor1a sec Number
@functor1a csc Number
@functor1a asin Number
@functor1a acos Number
@functor1a atan Number
@functor1a acot Number
@functor1a asec Number
@functor1a acsc Number
@functor2a atan2 Real
@functor1a sinc Number
@functor1a cosc Number
@functor1a sinpi Number
@functor1a cospi Number
@functor1a sind Real
@functor1a cosd Real
@functor1a tand Real
@functor1a cotd Real
@functor1a secd Real
@functor1a cscd Real
@functor1a asind Real
@functor1a acosd Real
@functor1a atand Real
@functor1a acotd Real
@functor1a asecd Real
@functor1a acscd Real
## hyperbolic functions
export SinhFun, CoshFun, TanhFun, CothFun, SechFun, CschFun
export AsinhFun, AcoshFun, AtanhFun, AcothFun, AsechFun, AcschFun
@functor1a sinh Number
@functor1a cosh Number
@functor1a tanh Number
@functor1a coth Number
@functor1a sech Number
@functor1a csch Number
@functor1a asinh Number
@functor1a acosh Number
@functor1a atanh Number
@functor1a acoth Number
@functor1a asech Number
@functor1a acsch Number
## special functions
export ErfFun, ErfcFun, ErfinvFun, ErfcinvFun, ErfiFun, ErfcxFun
export GammaFun, LgammaFun, DigammaFun, EtaFun, ZetaFun, BetaFun, LbetaFun
export AiryFun, AiryprimeFun, AiryaiFun, AiryaiprimeFun, AirybiFun, AirybiprimeFun
export Besselj0Fun, Besselj1Fun, Bessely0Fun, Bessely1Fun
export BesseliFun, BesseljFun, BesselkFun, BesselyFun
export Hankelh1Fun, Hankelh2Fun
@functor1a erf Number
@functor1a erfc Number
@functor1a erfinv Real
@functor1a erfcinv Real
@functor1a erfi Number
@functor1a erfcx Number
@functor1a gamma Number
@functor1a lgamma Number
@functor1a digamma Real
@functor1a eta Number
@functor1a zeta Number
@functor2a beta Real
@functor2a lbeta Real
@functor1a airy Number
@functor1a airyprime Number
@functor1a airyai Number
@functor1a airyaiprime Number
@functor1a airybi Number
@functor1a airybiprime Number
@functor1a besselj0 Number
@functor1a besselj1 Number
@functor1a bessely0 Number
@functor1a bessely1 Number
@functor1a_ord besseli Number
@functor1a_ord besselj Number
@functor1a_ord besselk Number
@functor1a_ord bessely Number
@functor1a_ord hankelh1 Number
@functor1a_ord hankelh2 Number
#######################################
#
# Ternary functors
#
#######################################
export FMA, IfelseFun
type FMA <: Functor{3} end
evaluate(::FMA, x::Number, y::Number, z::Number) = (x + y * z)
type IfelseFun <: Functor{3} end
evaluate{T<:Number}(::IfelseFun, c::Bool, x::T, y::T) = ifelse(c, x, y)