This repository was archived by the owner on Dec 16, 2022. It is now read-only.
forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbroadcast.jl
114 lines (104 loc) · 4.41 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
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...) == bitpack(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 @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 broadcast!(+, A, A, arr([1, 4]))
A = arr([1 0]); @test broadcast!(+, A, A, arr([1 4])) == arr([2 4])
@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(bitpack([true false])) .* arr(bitpack([true, true])) == [true false; true false]
@test arr(bitpack([true false])) .^ arr(bitpack([false, true])) == [true true; true false]
@test arr(bitpack([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 broadcast_getindex(M, eye(Int, 2).+1,arr([1, -1]))
@test_throws 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 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(randbool(n1, n2, n3)), arr(randbool(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(.+, (Array{Float64, 3}, Array{Int, 1}))
@test length(rt) == 1 && rt[1] == Array{Float64, 3}
rt = Base.return_types(broadcast, (Function, Array{Float64, 3}, Array{Int, 1}))
@test length(rt) == 1 && rt[1] == Array{Float64, 3}
rt = Base.return_types(broadcast!, (Function, Array{Float64, 3}, Array{Float64, 3}, Array{Int, 1}))
@test length(rt) == 1 && rt[1] == Array{Float64, 3}