@@ -115,4 +115,116 @@ using Test
115
115
val2, ind2 = vfindmax (h, B1, B2, B3, dims= (2 ,4 ))
116
116
@test ind1 == ind2 && val1 ≈ val2
117
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)
228
+ end
229
+ end
118
230
end
0 commit comments