forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.jl
303 lines (256 loc) · 7.43 KB
/
operators.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
## types ##
const (<:) = subtype
super(T::Union(CompositeKind,BitsKind,AbstractKind)) = T.super
## comparison ##
isequal(x,y) = is(x,y)
==(x,y) = isequal(x,y)
!=(x,y) = !(x==y)
< (x,y) = isless(x,y)
> (x,y) = y < x
<=(x,y) = !(y < x)
>=(x,y) = (y <= x)
.> (x,y) = y.<x
.>=(x,y) = y.<=x
# these definitions allow Number types to implement
# == and < instead of isequal and isless, which is more idiomatic:
isequal(x::Number, y::Number) = x==y
isless(x::Real, y::Real) = x<y
max(x,y) = y < x ? x : y
min(x,y) = x < y ? x : y
## definitions providing basic traits of arithmetic operators ##
+() = 0
*() = 1
(&)() = error("zero-argument & is ambiguous")
(|)() = error("zero-argument | is ambiguous")
($)() = error("zero-argument \$ is ambiguous")
+(x::Number) = x
*(x::Number) = x
(&)(x::Integer) = x
(|)(x::Integer) = x
($)(x::Integer) = x
for op = (:+, :*, :&, :|, :$, :min, :max)
@eval begin
($op)(a,b,c) = ($op)(($op)(a,b),c)
($op)(a,b,c,d) = ($op)(($op)(($op)(a,b),c),d)
($op)(a,b,c,d,e) = ($op)(($op)(($op)(($op)(a,b),c),d),e)
function ($op)(a, b, c, xs...)
accum = ($op)(($op)(a,b),c)
for x in xs
accum = ($op)(accum,x)
end
accum
end
end
end
\(x::Number,y::Number) = y/x
# .<op> defaults to <op>
./(x::Number,y::Number) = x/y
.\(x::Number,y::Number) = y./x
.*(x::Number,y::Number) = x*y
.^(x::Number,y::Number) = x^y
.+(x,y) = x+y
.-(x,y) = x-y
.==(x::Number,y::Number) = x==y
.!=(x::Number,y::Number) = x!=y
.< (x::Real,y::Real) = x<y
.<=(x::Real,y::Real) = x<=y
# core << >> and >>> takes Int32 as second arg
<<(x,y::Integer) = x << convert(Int32,y)
<<(x,y::Int32) = no_op_err("<<", typeof(x))
>>(x,y::Integer) = x >> convert(Int32,y)
>>(x,y::Int32) = no_op_err(">>", typeof(x))
>>>(x,y::Integer) = x >>> convert(Int32,y)
>>>(x,y::Int32) = no_op_err(">>>", typeof(x))
# fallback div, fld, rem & mod implementations
div{T<:Real}(x::T, y::T) = convert(T,trunc(x/y))
fld{T<:Real}(x::T, y::T) = convert(T,floor(x/y))
rem{T<:Real}(x::T, y::T) = convert(T,x-y*div(x,y))
mod{T<:Real}(x::T, y::T) = convert(T,x-y*fld(x,y))
# operator alias
const % = rem
# mod returns in [0,y) whereas mod1 returns in (0,y]
mod1{T<:Real}(x::T, y::T) = y-mod(y-x,y)
# cmp returns -1, 0, +1 indicating ordering
cmp{T<:Real}(x::T, y::T) = int(sign(x-y))
# transposed multiply
Ac_mul_B (a,b) = ctranspose(a)*b
A_mul_Bc (a,b) = a*ctranspose(b)
Ac_mul_Bc(a,b) = ctranspose(a)*ctranspose(b)
At_mul_B (a,b) = transpose(a)*b
A_mul_Bt (a,b) = a*transpose(b)
At_mul_Bt(a,b) = transpose(a)*transpose(b)
# transposed divide
Ac_rdiv_B (a,b) = ctranspose(a)/b
A_rdiv_Bc (a,b) = a/ctranspose(b)
Ac_rdiv_Bc(a,b) = ctranspose(a)/ctranspose(b)
At_rdiv_B (a,b) = transpose(a)/b
A_rdiv_Bt (a,b) = a/transpose(b)
At_rdiv_Bt(a,b) = transpose(a)/transpose(b)
Ac_ldiv_B (a,b) = ctranspose(a)\b
A_ldiv_Bc (a,b) = a\ctranspose(b)
Ac_ldiv_Bc(a,b) = ctranspose(a)\ctranspose(b)
At_ldiv_B (a,b) = transpose(a)\b
A_ldiv_Bt (a,b) = a\transpose(b)
At_ldiv_Bt(a,b) = transpose(a)\transpose(b)
oftype{T}(::Type{T},c) = convert(T,c)
oftype{T}(x::T,c) = convert(T,c)
zero(x) = oftype(x,0)
one(x) = oftype(x,1)
sizeof(T::Type) = error(string("size of type ",T," unknown"))
sizeof(T::BitsKind) = div(T.nbits,8)
sizeof(T::CompositeKind) = if isleaftype(T) T.sizeof else error("type does not have a native sizeof") end
sizeof(x) = sizeof(typeof(x))
# copying immutable things
copy(x::Union(Symbol,Number,String,Function,Tuple,LambdaStaticData,
TopNode,QuoteNode,BitsKind,CompositeKind,AbstractKind,
UnionKind)) = x
# function composition & pipelining
one(f::Function) = identity
one(::Type{Function}) = identity
*(f::Function, g::Function) = x->f(g(x))
|(x, f::Function) = f(x)
# currying of map, filter, etc.
map(f::Function) = (x...)->map(f, x...)
filter(f::Function) = (x...)->filter(f, x...)
# array shape rules
function promote_shape(a::(Int,), b::(Int,))
if a[1] != b[1]
error("argument dimensions must match")
end
return a
end
function promote_shape(a::(Int,Int), b::(Int,))
if a[1] != b[1] || a[2] != 1
error("argument dimensions must match")
end
return a
end
promote_shape(a::(Int,), b::(Int,Int)) = promote_shape(b, a)
function promote_shape(a::Dims, b::Dims)
if length(a) < length(b)
return promote_shape(b, a)
end
for i=1:length(b)
if a[i] != b[i]
error("argument dimensions must match")
end
end
for i=length(b)+1:length(a)
if a[i] != 1
error("argument dimensions must match")
end
end
return a
end
# shape of array to create for ref() with indexes I
function ref_shape(I...)
n = length(I)
while n > 0 && isa(I[n],Real); n-=1; end
tuple([length(I[i]) for i=1:n]...)
end
ref_shape(i::Real) = ()
ref_shape(i) = (length(i),)
ref_shape(i::Real,j::Real) = ()
ref_shape(i ,j::Real) = (length(i),)
ref_shape(i ,j) = (length(i),length(j))
ref_shape(i::Real,j::Real,k::Real) = ()
ref_shape(i ,j::Real,k::Real) = (length(i),)
ref_shape(i ,j ,k::Real) = (length(i),length(j))
ref_shape(i ,j ,k ) = (length(i),length(j),length(k))
# check for valid sizes in A[I...] = X where X <: AbstractArray
function assign_shape_check(X::AbstractArray, I...)
nel = 1
for idx in I
nel *= length(idx)
end
if length(X) != nel
error("argument dimensions must match")
end
if ndims(X) > 1
for i = 1:length(I)
if size(X,i) != length(I[i])
error("argument dimensions must match")
end
end
end
end
# convert to integer index
to_index(i) = i
to_index(i::Real) = convert(Int, i)
to_index(i::Int) = i
# vectorization
macro vectorize_1arg(S,f)
S = esc(S); f = esc(f); T = esc(:T)
quote
($f){$T<:$S}(x::AbstractArray{$T,1}) = [ ($f)(x[i]) for i=1:length(x) ]
($f){$T<:$S}(x::AbstractArray{$T,2}) =
[ ($f)(x[i,j]) for i=1:size(x,1), j=1:size(x,2) ]
($f){$T<:$S}(x::AbstractArray{$T}) =
reshape([ ($f)(x[i]) for i=1:length(x) ], size(x))
end
end
macro vectorize_2arg(S,f)
S = esc(S); f = esc(f); T1 = esc(:T1); T2 = esc(:T2)
quote
($f){$T1<:$S, $T2<:$S}(x::($T1), y::AbstractArray{$T2}) =
reshape([ ($f)(x, y[i]) for i=1:length(y) ], size(y))
($f){$T1<:$S, $T2<:$S}(x::AbstractArray{$T1}, y::($T2)) =
reshape([ ($f)(x[i], y) for i=1:length(x) ], size(x))
function ($f){$T1<:$S, $T2<:$S}(x::AbstractArray{$T1}, y::AbstractArray{$T2})
shp = promote_shape(size(x),size(y))
reshape([ ($f)(x[i], y[i]) for i=1:length(x) ], shp)
end
end
end
# some operators not defined yet
global //, .>>, .<<, &>, &>>, &<, &<<
module Operators
export
!,
!=,
$,
%,
&,
*,
+,
-,
.!=,
.+,
.-,
.*,
./,
.<,
.<=,
.==,
.>,
.>=,
.\,
.^,
/,
//,
<,
<:,
<<,
<=,
==,
>,
>=,
>>,
.>>,
.<<,
>>>,
&>,
&>>,
&<,
&<<,
\,
^,
|,
~
import
Base.!, Base.!=, Base.$, Base.%, Base.&, Base.*, Base.+, Base.-, Base..!=,
Base..+, Base..-, Base..*, Base../, Base..<, Base..<=, Base..==, Base..>,
Base..>=, Base..\, Base..^, Base./, Base.//, Base.<, Base.<:, Base.<<,
Base.<=, Base.==, Base.>, Base.>=, Base.>>, Base..>>, Base..<<, Base.>>>,
Base.&>, Base.&>>, Base.&<, Base.&<<, Base.\, Base.^, Base.|, Base.~
end