forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast.jl
145 lines (131 loc) · 5.39 KB
/
broadcast.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
# This file is a part of Julia. License is MIT: http://julialang.org/license
function as_sub(x::AbstractVector)
y = similar(x, eltype(x), tuple(([size(x)...]*2)...))
y = sub(y, 2:2:length(y))
y[:] = x[:]
y
end
function as_sub(x::AbstractMatrix)
y = similar(x, eltype(x), tuple(([size(x)...]*2)...))
y = sub(y, 2:2:size(y,1), 2:2:size(y,2))
for j=1:size(x,2)
for i=1:size(x,1)
y[i,j] = x[i,j]
end
end
y
end
function as_sub{T}(x::AbstractArray{T,3})
y = similar(x, eltype(x), tuple(([size(x)...]*2)...))
y = sub(y, 2:2:size(y,1), 2:2:size(y,2), 2:2:size(y,3))
for k=1:size(x,3)
for j=1:size(x,2)
for i=1:size(x,1)
y[i,j,k] = x[i,j,k]
end
end
end
y
end
bittest(f::Function, ewf::Function, a...) = (@test ewf(a...) == BitArray(broadcast(f, a...)))
n1 = 21
n2 = 32
n3 = 17
rb = 1:5
for arr in (identity, as_sub)
@test broadcast(+, arr(eye(2)), arr([1, 4])) == [2 1; 4 5]
@test broadcast(+, arr(eye(2)), arr([1 4])) == [2 4; 1 5]
@test broadcast(+, arr([1 0]), arr([1, 4])) == [2 1; 5 4]
@test broadcast(+, arr([1, 0]), arr([1 4])) == [2 5; 1 4]
@test broadcast(+, arr([1, 0]), arr([1, 4])) == [2, 4]
@test broadcast(+, arr([1, 0]), 2) == [3, 2]
@test @inferred(arr(eye(2)) .+ arr([1, 4])) == arr([2 1; 4 5])
@test arr(eye(2)) .+ arr([1 4]) == arr([2 4; 1 5])
@test arr([1 0]) .+ arr([1, 4]) == arr([2 1; 5 4])
@test arr([1, 0]) .+ arr([1 4]) == arr([2 5; 1 4])
@test arr([1, 0]) .+ arr([1, 4]) == arr([2, 4])
@test arr([1]) .+ arr([]) == arr([])
A = arr(eye(2)); @test broadcast!(+, A, A, arr([1, 4])) == arr([2 1; 4 5])
A = arr(eye(2)); @test broadcast!(+, A, A, arr([1 4])) == arr([2 4; 1 5])
A = arr([1 0]); @test_throws DimensionMismatch broadcast!(+, A, A, arr([1, 4]))
A = arr([1 0]); @test broadcast!(+, A, A, arr([1 4])) == arr([2 4])
A = arr([1 0]); @test broadcast!(+, A, A, 2) == arr([3 2])
@test arr([ 1 2]) .* arr([3, 4]) == [ 3 6; 4 8]
@test arr([24.0 12.0]) ./ arr([2.0, 3.0]) == [12 6; 8 4]
@test arr([1 2]) ./ arr([3, 4]) == [1/3 2/3; 1/4 2/4]
@test arr([1 2]) .\ arr([3, 4]) == [3 1.5; 4 2]
@test arr([3 4]) .^ arr([1, 2]) == [3 4; 9 16]
@test arr(BitArray([true false])) .* arr(BitArray([true, true])) == [true false; true false]
@test arr(BitArray([true false])) .^ arr(BitArray([false, true])) == [true true; true false]
@test arr(BitArray([true false])) .^ arr([0, 3]) == [true true; true false]
M = arr([11 12; 21 22])
@test broadcast_getindex(M, eye(Int, 2).+1,arr([1, 2])) == [21 11; 12 22]
@test_throws BoundsError broadcast_getindex(M, eye(Int, 2).+1,arr([1, -1]))
@test_throws BoundsError broadcast_getindex(M, eye(Int, 2).+1,arr([1, 2]), [2])
@test broadcast_getindex(M, eye(Int, 2).+1,arr([2, 1]), [1]) == [22 12; 11 21]
A = arr(zeros(2,2))
broadcast_setindex!(A, arr([21 11; 12 22]), eye(Int, 2).+1,arr([1, 2]))
@test A == M
broadcast_setindex!(A, 5, [1,2], [2 2])
@test A == [11 5; 21 5]
broadcast_setindex!(A, 7, [1,2], [1 2])
@test A == fill(7, 2, 2)
A = arr(zeros(3,3))
broadcast_setindex!(A, 10:12, 1:3, 1:3)
@test A == diagm(10:12)
@test_throws BoundsError broadcast_setindex!(A, 7, [1,-1], [1 2])
for (f, ewf) in (((==), (.==)),
((<) , (.<) ),
((!=), (.!=)),
((<=), (.<=)))
bittest(f, ewf, arr(eye(2)), arr([1, 4]))
bittest(f, ewf, arr(eye(2)), arr([1 4]))
bittest(f, ewf, arr([0, 1]), arr([1 4]))
bittest(f, ewf, arr([0 1]), arr([1, 4]))
bittest(f, ewf, arr([1, 0]), arr([1, 4]))
bittest(f, ewf, arr(rand(rb, n1, n2, n3)), arr(rand(rb, n1, n2, n3)))
bittest(f, ewf, arr(rand(rb, 1, n2, n3)), arr(rand(rb, n1, 1, n3)))
bittest(f, ewf, arr(rand(rb, 1, n2, 1)), arr(rand(rb, n1, 1, n3)))
bittest(f, ewf, arr(bitrand(n1, n2, n3)), arr(bitrand(n1, n2, n3)))
end
end
r1 = 1:1
r2 = 1:5
ratio = [1,1/2,1/3,1/4,1/5]
@test r1.*r2 == [1:5;]
@test r1./r2 == ratio
m = [1:2;]'
@test m.*r2 == [1:5 2:2:10]
@test_approx_eq m./r2 [ratio 2ratio]
@test_approx_eq m./[r2;] [ratio 2ratio]
@test @inferred([0,1.2].+reshape([0,-2],1,1,2)) == reshape([0 -2; 1.2 -0.8],2,1,2)
rt = Base.return_types(.+, Tuple{Array{Float64, 3}, Array{Int, 1}})
@test length(rt) == 1 && rt[1] == Array{Float64, 3}
rt = Base.return_types(broadcast, Tuple{Function, Array{Float64, 3}, Array{Int, 1}})
@test length(rt) == 1 && rt[1] == Array{Float64, 3}
rt = Base.return_types(broadcast!, Tuple{Function, Array{Float64, 3}, Array{Float64, 3}, Array{Int, 1}})
@test length(rt) == 1 && rt[1] == Array{Float64, 3}
# f.(args...) syntax (#15032)
let x = [1,3.2,4.7], y = [3.5, pi, 1e-4], α = 0.2342
@test sin.(x) == broadcast(sin, x)
@test sin.(α) == broadcast(sin, α)
@test factorial.(3) == broadcast(factorial, 3)
@test atan2.(x, y) == broadcast(atan2, x, y)
@test atan2.(x, y') == broadcast(atan2, x, y')
@test atan2.(x, α) == broadcast(atan2, x, α)
@test atan2.(α, y') == broadcast(atan2, α, y')
end
# issue 14725
let a = Number[2, 2.0, 4//2, 2+0im] / 2
@test eltype(a) == Number
end
let a = Real[2, 2.0, 4//2] / 2
@test eltype(a) == Real
end
let a = Real[2, 2.0, 4//2] / 2.0
@test eltype(a) == Real
end
# issue 16164
let a = broadcast(Float32, [3, 4, 5])
@test eltype(a) == Float32
end