@@ -2,229 +2,16 @@ using VectorizedReduction
2
2
using Test
3
3
4
4
@testset " VectorizedReduction.jl" begin
5
- @testset " vvmapreduce" begin
6
- A = rand (5 ,5 ,5 ,5 )
7
- @test vvmapreduce (abs2, + , A, dims= (1 ,3 )) ≈ mapreduce (abs2, + , A, dims= (1 ,3 ))
8
- @test vvmapreduce (cos, * , A, dims= (2 ,4 )) ≈ mapreduce (cos, * , A, dims= (2 ,4 ))
9
- @test vvprod (log, A, dims= 1 ) ≈ prod (log, A, dims= 1 )
10
- @test vvminimum (sin, A, dims= (3 ,4 )) ≈ minimum (sin, A, dims= (3 ,4 ))
11
- @test vvmaximum (sin, A, dims= (3 ,4 )) ≈ maximum (sin, A, dims= (3 ,4 ))
12
- end
13
- @testset " vvmapreduce_vararg" begin
14
- A1 = rand (5 ,5 ,5 ,5 )
15
- A2 = rand (5 ,5 ,5 ,5 )
16
- A3 = rand (5 ,5 ,5 ,5 )
17
- A4 = rand (1 : 10 , 5 ,5 ,5 ,5 )
18
- as = (A1, A2, A3)
19
- @test vvmapreduce (+ , + , as, dims= (1 ,2 ,4 )) ≈ mapreduce (+ , + , A1, A2, A3, dims= (1 ,2 ,4 ))
20
- # Tests of variably typed arrays
21
- @test vvmapreduce (+ , + , A1, A2, dims= (2 ,3 ,4 )) ≈ mapreduce (+ , + , A1, A2, dims= (2 ,3 ,4 ))
22
- @test vvmapreduce (+ , + , A1, A4, dims= (2 ,3 ,4 )) ≈ mapreduce (+ , + , A1, A4, dims= (2 ,3 ,4 ))
23
- # interface tests
24
- r = mapreduce (* , + , A1, A2, A3)
25
- @test r ≈ vvmapreduce (* , + , zero, A1, A2, A3)
26
- @test r ≈ vvmapreduce (* , + , A1, A2, A3)
27
- @test r ≈ vvmapreduce (* , + , A1, A2, A3, dims= :)
28
- @test r ≈ vvmapreduce (* , + , A1, A2, A3, dims= :, init= 0 )
29
- @test r ≈ vvmapreduce (* , + , A1, A2, A3, dims= :, init= zero)
30
- @test r ≈ vvmapreduce (* , + , as)
31
- # # And for really strange stuff (e.g. posterior predictive transformations)
32
- # @benchmark vvmapreduce((x,y,z) -> ifelse(x*y+z ≥ 1, 1, 0), +, A1, A2, A3)
33
- # @benchmark vvmapreduce((x,y,z) -> ifelse(x*y+z ≥ 1, 1, 0), +, A1, A2, A3, dims=(2,3,4))
34
- # # using ifelse for just a boolean is quite slow, but the above is just for demonstration
35
- # @benchmark vvmapreduce(≥, +, A1, A2)
36
- # @benchmark vvmapreduce((x,y,z) -> ≥(x*y+z, 1), +, A1, A2, A3)
37
- # @benchmark vvmapreduce((x,y,z) -> ≥(x*y+z, 1), +, A1, A2, A3, dims=(2,3,4))
38
- # @benchmark mapreduce((x,y,z) -> ≥(x*y+z, 1), +, A1, A2, A3)
39
- # # What I mean by posterior predictive transformation? Well, one might encounter
40
- # # this in Bayesian model checking, which provides a convenient example.
41
- # # If one wishes to compute the Pr = ∫∫𝕀(T(yʳᵉᵖ, θ) ≥ T(y, θ))p(yʳᵉᵖ|θ)p(θ|y)dyʳᵉᵖdθ
42
- # # Let's imagine that A1 represents T(yʳᵉᵖ, θ) and A2 represents T(y, θ)
43
- # # i.e. the test variable samples computed as a functional of the Markov chain (samples of θ)
44
- # Then, Pr is computed as
45
- vvmapreduce (≥ , + , A1, A2) / length (A1)
46
- # Or, if only the probability is of interest, and we do not wish to use the functionals
47
- # for any other purpose, we could compute it as:
48
- # vvmapreduce((x, y) -> ≥(f(x), f(y)), +, A1, A2)
49
- # where `f` is the functional of interest, e.g.
50
- f (x, y) = ≥ (abs2 (x), abs2 (y))
51
- # r = mapreduce((x, y) -> ≥(abs2(x), abs2(y)), +, A1, A2)
52
- r = mapreduce (f, + , A1, A2)
53
- # @test r ≈ vvmapreduce((x, y) -> ≥(abs2(x), abs2(y)), +, A1, A2)
54
- @test r ≈ vvmapreduce (f, + , A1, A2)
55
- # R = mapreduce((x, y) -> ≥(abs2(x), abs2(y)), +, A1, A2, dims=(2,3,4))
56
- # @test R ≈ vvmapreduce((x, y) -> ≥(abs2(x), abs2(y)), +, A1, A2, dims=(2,3,4))
57
- R = mapreduce (f, + , A1, A2, dims= (2 ,3 ,4 ))
58
- @test R ≈ vvmapreduce (f, + , A1, A2, dims= (2 ,3 ,4 ))
59
- # One can also express commonly encountered reductions with ease;
60
- # these will be fused once a post-reduction operator can be specified
61
- # MSE
62
- sqdiff (x, y) = abs2 (x - y)
63
- # B = vvmapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)) ./ (size(A1, 2) * size(A1, 4) )
64
- # @test B ≈ mapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)) ./ (size(A1, 2) * size(A1, 4) )
65
- B = vvmapreduce (sqdiff, + , A1, A2, dims= (2 ,4 )) ./ (size (A1, 2 ) * size (A1, 4 ))
66
- @test B ≈ mapreduce (sqdiff, + , A1, A2, dims= (2 ,4 )) ./ (size (A1, 2 ) * size (A1, 4 ))
67
- # Euclidean distance
68
- # B = (√).(vvmapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)))
69
- # @test B ≈ (√).(mapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)))
70
- B = (√ ). (vvmapreduce (sqdiff, + , A1, A2, dims= (2 ,4 )))
71
- @test B ≈ (√ ). (mapreduce (sqdiff, + , A1, A2, dims= (2 ,4 )))
72
- end
73
- @testset " vfindminmax" begin
74
- # Simple
75
- A1 = rand (5 ,5 )
76
- A2 = rand (5 ,5 )
77
- A3 = rand (5 ,5 )
78
- A′ = @. A1 + A2 + A3
79
- @test findmin (A′) == vfindmin (+ , A1, A2, A3)
80
- @test findmin (A′, dims= 2 ) == vfindmin (+ , A1, A2, A3, dims= 2 )
81
- #
82
- v1 = rand (5 )
83
- v2 = rand (5 )
84
- v3 = rand (5 )
85
- v′ = @. v1 + v2 + v3;
86
- @test findmin (v′) == vfindmin (+ , v1, v2, v3)
87
- #
88
- A = rand (5 ,5 ,5 ,5 )
89
- A′ = cos .(A)
90
- val1, ind1 = findmax (A′, dims= (2 ,4 ))
91
- val2, ind2 = vfindmax (cos, A, dims= (2 ,4 ))
92
- @test ind1 == ind2 && val1 ≈ val2
93
- #
94
- g (x) = ifelse (abs (x) ≥ 2 , 100 , 1 )
95
- A = randn (5 ,5 ,5 ,5 )
96
- A′ = g .(A)
97
- val1, ind1 = findmax (A′, dims= (2 ,4 ))
98
- val2, ind2 = vfindmax (g, A, dims= (2 ,4 ))
99
- @test ind1 == ind2 && val1 ≈ val2
100
- end
101
- @testset " vfindminmax_vararg" begin
102
- A1 = rand (5 ,5 )
103
- A2 = rand (5 ,5 )
104
- A3 = rand (5 ,5 )
105
- A′ = @. A1 * A2 + A3;
106
- @test findmin (A′) == vfindmin ((x, y, z) -> x * y + z, A1, A2, A3)
107
- val1, ind1 = findmin (A′, dims= 2 )
108
- val2, ind2 = vfindmin ((x, y, z) -> x * y + z, A1, A2, A3, dims= 2 )
109
- @test ind1 == ind2 && val1 ≈ val2
110
- #
111
- B1, B2, B3 = randn (5 ,5 ,5 ,5 ), randn (5 ,5 ,5 ,5 ), randn (5 ,5 ,5 ,5 );
112
- h (x, y, z) = ifelse (x ≥ .5 , 100 , 1 ) * y + √ abs (z)
113
- B′ = h .(B1, B2, B3)
114
- val1, ind1 = findmax (B′, dims= (2 ,4 ))
115
- val2, ind2 = vfindmax (h, B1, B2, B3, dims= (2 ,4 ))
116
- @test ind1 == ind2 && val1 ≈ val2
117
- end
118
- # ###########################################################################################
119
- @testset " from reducedim.jl tests" begin
120
- A = [1.0 5.0 6.0 ;
121
- 5.0 2.0 4.0 ]
122
- for (tup, rval, rind) in [((1 ,), [1.0 2.0 4.0 ], [CartesianIndex (1 ,1 ) CartesianIndex (2 ,2 ) CartesianIndex (2 ,3 )]),
123
- ((2 ,), reshape ([1.0 ,2.0 ], 2 , 1 ), reshape ([CartesianIndex (1 ,1 ),CartesianIndex (2 ,2 )], 2 , 1 )),
124
- ((1 ,2 ), fill (1.0 ,1 ,1 ),fill (CartesianIndex (1 ,1 ),1 ,1 ))]
125
- @test vfindmin1 (A, dims= tup) == (rval, rind)
126
- @test isequal (vvminimum (A, dims= tup), rval)
127
- end
128
-
129
- for (tup, rval, rind) in [((1 ,), [5.0 5.0 6.0 ], [CartesianIndex (2 ,1 ) CartesianIndex (1 ,2 ) CartesianIndex (1 ,3 )]),
130
- ((2 ,), reshape ([6.0 ,5.0 ], 2 , 1 ), reshape ([CartesianIndex (1 ,3 ),CartesianIndex (2 ,1 )], 2 , 1 )),
131
- ((1 ,2 ), fill (6.0 ,1 ,1 ),fill (CartesianIndex (1 ,3 ),1 ,1 ))]
132
- @test vfindmax1 (A, dims= tup) == (rval, rind)
133
- @test isequal (vvmaximum (A, dims= tup), rval)
134
- end
135
- end
136
- @testset " findmin/findmax transformed arguments, numeric values" begin
137
- A = [1.0 - 5.0 - 6.0 ;
138
- - 5.0 2.0 4.0 ]
139
- TA = [((1 ,), [1.0 2.0 4.0 ], [CartesianIndex (1 ,1 ) CartesianIndex (2 ,2 ) CartesianIndex (2 ,3 )]),
140
- ((2 ,), reshape ([1.0 , 2.0 ], 2 , 1 ), reshape ([CartesianIndex (1 ,1 ), CartesianIndex (2 ,2 )], 2 , 1 )),
141
- ((1 ,2 ), fill (1.0 ,1 ,1 ), fill (CartesianIndex (1 ,1 ),1 ,1 ))]
142
- TA2 = [((1 ,), [1.0 4.0 16.0 ], [CartesianIndex (1 ,1 ) CartesianIndex (2 ,2 ) CartesianIndex (2 ,3 )]),
143
- ((2 ,), reshape ([1.0 , 4.0 ], 2 , 1 ), reshape ([CartesianIndex (1 ,1 ), CartesianIndex (2 ,2 )], 2 , 1 )),
144
- ((1 ,2 ), fill (1.0 ,1 ,1 ), fill (CartesianIndex (1 ,1 ),1 ,1 ))]
145
- TAc = [((1 ,), [0.28366218546322625 - 0.4161468365471424 - 0.6536436208636119 ], [CartesianIndex (2 ,1 ) CartesianIndex (2 ,2 ) CartesianIndex (2 ,3 )]),
146
- ((2 ,), reshape ([0.28366218546322625 , - 0.6536436208636119 ], 2 , 1 ), reshape ([CartesianIndex (1 ,2 ), CartesianIndex (2 ,3 )], 2 , 1 )),
147
- ((1 ,2 ), fill (- 0.6536436208636119 ,1 ,1 ), fill (CartesianIndex (2 ,3 ),1 ,1 ))]
148
- for (f, At) in ((abs, TA), (abs2, TA2), (cos, TAc))
149
- A′ = map (f, A)
150
- for (tup, rval, rind) in At
151
- (rval′, rind′) = vfindmin1 (f, A, dims= tup)
152
- @test all (rval′ .≈ rval)
153
- @test rind′ == rind
154
- (rval′′, rind′′) = vfindmin1 (A′, dims= tup)
155
- @test all (rval′ .≈ rval′′)
156
- @test rind′ == rind′′
157
- end
158
- end
159
-
160
- TA = [((1 ,), [5.0 5.0 6.0 ], [CartesianIndex (2 ,1 ) CartesianIndex (1 ,2 ) CartesianIndex (1 ,3 )]),
161
- ((2 ,), reshape ([6.0 ,5.0 ], 2 , 1 ), reshape ([CartesianIndex (1 ,3 ), CartesianIndex (2 ,1 )], 2 , 1 )),
162
- ((1 ,2 ), fill (6.0 ,1 ,1 ),fill (CartesianIndex (1 ,3 ),1 ,1 ))]
163
- TA2 = [((1 ,), [25.0 25.0 36.0 ], [CartesianIndex (2 ,1 ) CartesianIndex (1 ,2 ) CartesianIndex (1 ,3 )]),
164
- ((2 ,), reshape ([36.0 , 25.0 ], 2 , 1 ), reshape ([CartesianIndex (1 ,3 ), CartesianIndex (2 ,1 )], 2 , 1 )),
165
- ((1 ,2 ), fill (36.0 ,1 ,1 ), fill (CartesianIndex (1 ,3 ),1 ,1 ))]
166
- TAc = [((1 ,), [0.5403023058681398 0.28366218546322625 0.960170286650366 ], [CartesianIndex (1 ,1 ) CartesianIndex (1 ,2 ) CartesianIndex (1 ,3 )]),
167
- ((2 ,), reshape ([0.960170286650366 , 0.28366218546322625 ], 2 , 1 ), reshape ([CartesianIndex (1 ,3 ), CartesianIndex (2 ,1 )], 2 , 1 )),
168
- ((1 ,2 ), fill (0.960170286650366 ,1 ,1 ), fill (CartesianIndex (1 ,3 ),1 ,1 ))]
169
- for (f, At) in ((abs, TA), (abs2, TA2), (cos, TAc))
170
- A′ = map (f, A)
171
- for (tup, rval, rind) in At
172
- (rval′, rind′) = vfindmax1 (f, A, dims= tup)
173
- @test all (rval′ .≈ rval)
174
- @test rind′ == rind
175
- (rval′′, rind′′) = vfindmax1 (A′, dims= tup)
176
- @test all (rval′ .≈ rval′′)
177
- @test rind′ == rind′′
178
- end
179
- end
180
- end
181
-
182
- @testset " NaN in findmin/findmax/minimum/maximum" begin
183
- A = [1.0 NaN 6.0 ;
184
- NaN 2.0 4.0 ]
185
- A′ = [- 1.0 NaN - 6.0 ;
186
- NaN - 2.0 4.0 ]
187
- for (tup, rval, rind) in [((1 ,), [1.0 2.0 4.0 ], [CartesianIndex (1 ,1 ) CartesianIndex (2 ,2 ) CartesianIndex (2 ,3 )]),
188
- ((2 ,), reshape ([1.0 , 2.0 ], 2 , 1 ), reshape ([CartesianIndex (1 ,1 ),CartesianIndex (2 ,2 )], 2 , 1 )),
189
- ((1 ,2 ), fill (1.0 ,1 ,1 ),fill (CartesianIndex (1 ,1 ),1 ,1 ))]
190
- @test isequal (vfindmin1 (A, dims= tup), (rval, rind))
191
- @test isequal (vfindmin1 (abs, A′, dims= tup), (rval, rind))
192
- @test isequal (vvminimum (A, dims= tup), rval)
193
- @test isequal (vvminimum (abs, A′, dims= tup), rval)
194
- end
195
-
196
- for (tup, rval, rind) in [((1 ,), [1.0 2.0 6.0 ], [CartesianIndex (1 ,1 ) CartesianIndex (2 ,2 ) CartesianIndex (1 ,3 )]),
197
- ((2 ,), reshape ([6.0 , 4.0 ], 2 , 1 ), reshape ([CartesianIndex (1 ,3 ),CartesianIndex (2 ,3 )], 2 , 1 )),
198
- ((1 ,2 ), fill (6.0 ,1 ,1 ),fill (CartesianIndex (1 ,3 ),1 ,1 ))]
199
- @test isequal (vfindmax1 (A, dims= tup), (rval, rind))
200
- @test isequal (vfindmax1 (abs, A′, dims= tup), (rval, rind))
201
- @test isequal (vvmaximum (A, dims= tup), rval)
202
- @test isequal (vvmaximum (abs, A′, dims= tup), rval)
203
- end
204
- end
205
-
206
- @testset " +/-Inf in findmin/findmax/minimum/maximum" begin
207
- A = [Inf - Inf Inf - Inf ;
208
- Inf Inf - Inf - Inf ]
209
- A′ = [1 0 1 0 ;
210
- 1 1 0 0 ]
211
- retinf (x:: T ) where {T} = ifelse (x == one (T), Inf , - Inf )
212
- for (tup, rval, rind) in [((1 ,), [Inf - Inf - Inf - Inf ], [CartesianIndex (1 ,1 ) CartesianIndex (1 ,2 ) CartesianIndex (2 ,3 ) CartesianIndex (1 ,4 )]),
213
- ((2 ,), reshape ([- Inf - Inf ], 2 , 1 ), reshape ([CartesianIndex (1 ,2 ),CartesianIndex (2 ,3 )], 2 , 1 )),
214
- ((1 ,2 ), fill (- Inf ,1 ,1 ),fill (CartesianIndex (1 ,2 ),1 ,1 ))]
215
- @test isequal (vfindmin1 (A, dims= tup), (rval, rind))
216
- @test isequal (vfindmin1 (retinf, A′, dims= tup), (rval, rind))
217
- @test isequal (vvminimum (A, dims= tup), rval)
218
- @test isequal (vvminimum (retinf, A′, dims= tup), rval)
219
- end
220
-
221
- for (tup, rval, rind) in [((1 ,), [Inf Inf Inf - Inf ], [CartesianIndex (1 ,1 ) CartesianIndex (2 ,2 ) CartesianIndex (1 ,3 ) CartesianIndex (1 ,4 )]),
222
- ((2 ,), reshape ([Inf Inf ], 2 , 1 ), reshape ([CartesianIndex (1 ,1 ),CartesianIndex (2 ,1 )], 2 , 1 )),
223
- ((1 ,2 ), fill (Inf ,1 ,1 ),fill (CartesianIndex (1 ,1 ),1 ,1 ))]
224
- @test isequal (vfindmax1 (A, dims= tup), (rval, rind))
225
- @test isequal (vfindmax1 (retinf, A′, dims= tup), (rval, rind))
226
- @test isequal (vvmaximum (A, dims= tup), rval)
227
- @test isequal (vvmaximum (retinf, A′, dims= tup), rval)
5
+ const tests = [
6
+ " vrspecials.jl"
7
+ " reduce.jl" ,
8
+ " reducedim.jl" ,
9
+ " treduce.jl" ,
10
+ " treducedim.jl"
11
+ ]
12
+ for t in tests
13
+ @testset " Test $t " begin
14
+ include (t)
228
15
end
229
16
end
230
17
end
0 commit comments