|
| 1 | +# Essentially, a direct reproduction of Base's reducedim.jl tests. |
| 2 | +# Some small allowances are made for deviations. |
| 3 | + |
| 4 | +@testset "test reductions over region: $region" for region in Any[ |
| 5 | + 1, 2, 3, 4, 5, (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4), |
| 6 | + (1, 2, 3), (1, 3, 4), (2, 3, 4), (1, 2, 3, 4)] |
| 7 | + A = rand(3, 4, 5, 6) |
| 8 | + |
| 9 | + @test sum(A, dims=region) ≈ vvsum(A, dims=region) |
| 10 | + @test prod(A, dims=region) ≈ vvprod(A, dims=region) |
| 11 | + @test maximum(A, dims=region) ≈ vvmaximum(A, dims=region) |
| 12 | + @test minimum(A, dims=region) ≈ vvminimum(A, dims=region) |
| 13 | + |
| 14 | + @test sum(abs2, A, dims=region) ≈ vvsum(abs2, A, dims=region) |
| 15 | + @test prod(abs2, A, dims=region) ≈ vvprod(abs2, A, dims=region) |
| 16 | + @test maximum(abs2, A, dims=region) ≈ vvmaximum(abs2, A, dims=region) |
| 17 | + @test minimum(abs2, A, dims=region) ≈ vvminimum(abs2, A, dims=region) |
| 18 | + |
| 19 | + |
| 20 | + @test count(≥(0.5), A, dims=region) == vcount(≥(0.5), A, dims=region) |
| 21 | + |
| 22 | + # With init=false |
| 23 | + @test sum(A, init=false) ≈ vvsum(A, init=false) |
| 24 | + @test prod(A, init=false) ≈ vvprod(A, init=false) |
| 25 | + @test maximum(A, init=false) ≈ vvmaximum(A, init=false) |
| 26 | + @test minimum(A, init=false) ≈ vvminimum(A, init=false) |
| 27 | + |
| 28 | + @test sum(abs2, A, init=false) ≈ vvsum(abs2, A, init=false) |
| 29 | + @test prod(abs2, A, init=false) ≈ vvprod(abs2, A, init=false) |
| 30 | + @test maximum(abs2, A, init=false) ≈ vvmaximum(abs2, A, init=false) |
| 31 | + @test minimum(abs2, A, init=false) ≈ vvminimum(abs2, A, init=false) |
| 32 | + |
| 33 | + @test @inferred vvsum(A, dims=region) ≈ sum(A, dims=region) |
| 34 | + @test @inferred(vvprod(A, dims=region)) ≈ prod(A, dims=region) |
| 35 | + @test @inferred(vvmaximum(A, dims=region)) ≈ maximum(A, dims=region) |
| 36 | + @test @inferred(vvminimum(A, dims=region)) ≈ minimum(A, dims=region) |
| 37 | + |
| 38 | + @test @inferred(vvsum(abs, A, dims=region)) ≈ sum(abs, A, dims=region) |
| 39 | + @test @inferred(vvsum(abs2, A, dims=region)) ≈ sum(abs2, A, dims=region) |
| 40 | + @test @inferred(vvmaximum(abs, A, dims=region)) ≈ maximum(abs, A, dims=region) |
| 41 | + @test @inferred(vvminimum(abs, A, dims=region)) ≈ minimum(abs, A, dims=region) |
| 42 | + |
| 43 | +end |
| 44 | + |
| 45 | +# Small integers |
| 46 | +@test @inferred(vvsum(Int8[1], dims=1)) == [1] |
| 47 | +@test @inferred(vvsum(UInt8[1], dims=1)) == [1] |
| 48 | + |
| 49 | +A = [1.0 5.0 6.0; |
| 50 | + 5.0 2.0 4.0] |
| 51 | +for (tup, rval, rind) in [((1,), [1.0 2.0 4.0], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(2,3)]), |
| 52 | + ((2,), reshape([1.0,2.0], 2, 1), reshape([CartesianIndex(1,1),CartesianIndex(2,2)], 2, 1)), |
| 53 | + ((1,2), fill(1.0,1,1),fill(CartesianIndex(1,1),1,1))] |
| 54 | + @test vfindmin1(A, dims=tup) == (rval, rind) |
| 55 | + @test isequal(vvminimum(A, dims=tup), rval) |
| 56 | +end |
| 57 | + |
| 58 | +for (tup, rval, rind) in [((1,), [5.0 5.0 6.0], [CartesianIndex(2,1) CartesianIndex(1,2) CartesianIndex(1,3)]), |
| 59 | + ((2,), reshape([6.0,5.0], 2, 1), reshape([CartesianIndex(1,3),CartesianIndex(2,1)], 2, 1)), |
| 60 | + ((1,2), fill(6.0,1,1),fill(CartesianIndex(1,3),1,1))] |
| 61 | + @test vfindmax1(A, dims=tup) == (rval, rind) |
| 62 | + @test isequal(vvmaximum(A, dims=tup), rval) |
| 63 | +end |
| 64 | +@testset "findmin/findmax transformed arguments, numeric values" begin |
| 65 | + A = [1.0 -5.0 -6.0; |
| 66 | + -5.0 2.0 4.0] |
| 67 | + TA = [((1,), [1.0 2.0 4.0], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(2,3)]), |
| 68 | + ((2,), reshape([1.0, 2.0], 2, 1), reshape([CartesianIndex(1,1), CartesianIndex(2,2)], 2, 1)), |
| 69 | + ((1,2), fill(1.0,1,1), fill(CartesianIndex(1,1),1,1))] |
| 70 | + TA2 = [((1,), [1.0 4.0 16.0], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(2,3)]), |
| 71 | + ((2,), reshape([1.0, 4.0], 2, 1), reshape([CartesianIndex(1,1), CartesianIndex(2,2)], 2, 1)), |
| 72 | + ((1,2), fill(1.0,1,1), fill(CartesianIndex(1,1),1,1))] |
| 73 | + TAc = [((1,), [0.28366218546322625 -0.4161468365471424 -0.6536436208636119], [CartesianIndex(2,1) CartesianIndex(2,2) CartesianIndex(2,3)]), |
| 74 | + ((2,), reshape([0.28366218546322625, -0.6536436208636119], 2, 1), reshape([CartesianIndex(1,2), CartesianIndex(2,3)], 2, 1)), |
| 75 | + ((1,2), fill(-0.6536436208636119,1,1), fill(CartesianIndex(2,3),1,1))] |
| 76 | + for (f, At) in ((abs, TA), (abs2, TA2), (cos, TAc)) |
| 77 | + A′ = map(f, A) |
| 78 | + for (tup, rval, rind) in At |
| 79 | + (rval′, rind′) = vfindmin1(f, A, dims=tup) |
| 80 | + @test all(rval′ .≈ rval) |
| 81 | + @test rind′ == rind |
| 82 | + (rval′′, rind′′) = vfindmin1(A′, dims=tup) |
| 83 | + @test all(rval′ .≈ rval′′) |
| 84 | + @test rind′ == rind′′ |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + TA = [((1,), [5.0 5.0 6.0], [CartesianIndex(2,1) CartesianIndex(1,2) CartesianIndex(1,3)]), |
| 89 | + ((2,), reshape([6.0,5.0], 2, 1), reshape([CartesianIndex(1,3), CartesianIndex(2,1)], 2, 1)), |
| 90 | + ((1,2), fill(6.0,1,1),fill(CartesianIndex(1,3),1,1))] |
| 91 | + TA2 = [((1,), [25.0 25.0 36.0], [CartesianIndex(2,1) CartesianIndex(1,2) CartesianIndex(1,3)]), |
| 92 | + ((2,), reshape([36.0, 25.0], 2, 1), reshape([CartesianIndex(1,3), CartesianIndex(2,1)], 2, 1)), |
| 93 | + ((1,2), fill(36.0,1,1), fill(CartesianIndex(1,3),1,1))] |
| 94 | + TAc = [((1,), [0.5403023058681398 0.28366218546322625 0.960170286650366], [CartesianIndex(1,1) CartesianIndex(1,2) CartesianIndex(1,3)]), |
| 95 | + ((2,), reshape([0.960170286650366, 0.28366218546322625], 2, 1), reshape([CartesianIndex(1,3), CartesianIndex(2,1)], 2, 1)), |
| 96 | + ((1,2), fill(0.960170286650366,1,1), fill(CartesianIndex(1,3),1,1))] |
| 97 | + for (f, At) in ((abs, TA), (abs2, TA2), (cos, TAc)) |
| 98 | + A′ = map(f, A) |
| 99 | + for (tup, rval, rind) in At |
| 100 | + (rval′, rind′) = vfindmax1(f, A, dims=tup) |
| 101 | + @test all(rval′ .≈ rval) |
| 102 | + @test rind′ == rind |
| 103 | + (rval′′, rind′′) = vfindmax1(A′, dims=tup) |
| 104 | + @test all(rval′ .≈ rval′′) |
| 105 | + @test rind′ == rind′′ |
| 106 | + end |
| 107 | + end |
| 108 | +end |
| 109 | + |
| 110 | +@testset "NaN in findmin/findmax/minimum/maximum" begin |
| 111 | + A = [1.0 NaN 6.0; |
| 112 | + NaN 2.0 4.0] |
| 113 | + A′ = [-1.0 NaN -6.0; |
| 114 | + NaN -2.0 4.0] |
| 115 | + for (tup, rval, rind) in [((1,), [1.0 2.0 4.0], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(2,3)]), |
| 116 | + ((2,), reshape([1.0, 2.0], 2, 1), reshape([CartesianIndex(1,1),CartesianIndex(2,2)], 2, 1)), |
| 117 | + ((1,2), fill(1.0,1,1),fill(CartesianIndex(1,1),1,1))] |
| 118 | + @test isequal(vfindmin1(A, dims=tup), (rval, rind)) |
| 119 | + @test isequal(vfindmin1(abs, A′, dims=tup), (rval, rind)) |
| 120 | + @test isequal(vvminimum(A, dims=tup), rval) |
| 121 | + @test isequal(vvminimum(abs, A′, dims=tup), rval) |
| 122 | + end |
| 123 | + |
| 124 | + for (tup, rval, rind) in [((1,), [1.0 2.0 6.0], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(1,3)]), |
| 125 | + ((2,), reshape([6.0, 4.0], 2, 1), reshape([CartesianIndex(1,3),CartesianIndex(2,3)], 2, 1)), |
| 126 | + ((1,2), fill(6.0,1,1),fill(CartesianIndex(1,3),1,1))] |
| 127 | + @test isequal(vfindmax1(A, dims=tup), (rval, rind)) |
| 128 | + @test isequal(vfindmax1(abs, A′, dims=tup), (rval, rind)) |
| 129 | + @test isequal(vvmaximum(A, dims=tup), rval) |
| 130 | + @test isequal(vvmaximum(abs, A′, dims=tup), rval) |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +@testset "+/-Inf in findmin/findmax/minimum/maximum" begin |
| 135 | + A = [Inf -Inf Inf -Inf; |
| 136 | + Inf Inf -Inf -Inf] |
| 137 | + A′ = [1 0 1 0; |
| 138 | + 1 1 0 0] |
| 139 | + retinf(x::T) where {T} = ifelse(x == one(T), Inf, -Inf) |
| 140 | + for (tup, rval, rind) in [((1,), [Inf -Inf -Inf -Inf], [CartesianIndex(1,1) CartesianIndex(1,2) CartesianIndex(2,3) CartesianIndex(1,4)]), |
| 141 | + ((2,), reshape([-Inf -Inf], 2, 1), reshape([CartesianIndex(1,2),CartesianIndex(2,3)], 2, 1)), |
| 142 | + ((1,2), fill(-Inf,1,1),fill(CartesianIndex(1,2),1,1))] |
| 143 | + @test isequal(vfindmin1(A, dims=tup), (rval, rind)) |
| 144 | + @test isequal(vfindmin1(retinf, A′, dims=tup), (rval, rind)) |
| 145 | + @test isequal(vvminimum(A, dims=tup), rval) |
| 146 | + @test isequal(vvminimum(retinf, A′, dims=tup), rval) |
| 147 | + end |
| 148 | + |
| 149 | + for (tup, rval, rind) in [((1,), [Inf Inf Inf -Inf], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(1,3) CartesianIndex(1,4)]), |
| 150 | + ((2,), reshape([Inf Inf], 2, 1), reshape([CartesianIndex(1,1),CartesianIndex(2,1)], 2, 1)), |
| 151 | + ((1,2), fill(Inf,1,1),fill(CartesianIndex(1,1),1,1))] |
| 152 | + @test isequal(vfindmax1(A, dims=tup), (rval, rind)) |
| 153 | + @test isequal(vfindmax1(retinf, A′, dims=tup), (rval, rind)) |
| 154 | + @test isequal(vvmaximum(A, dims=tup), rval) |
| 155 | + @test isequal(vvmaximum(retinf, A′, dims=tup), rval) |
| 156 | + end |
| 157 | +end |
| 158 | + |
| 159 | +@testset "region=$region" for region in Any[[0, 1], [0 1; 2 3], "hello"] |
| 160 | + Areduc = rand(3, 4, 5, 6) |
| 161 | + |
| 162 | + @test_throws MethodError vvsum(Areduc, dims=region) |
| 163 | + @test_throws MethodError vvprod(Areduc, dims=region) |
| 164 | + @test_throws MethodError vvmaximum(Areduc, dims=region) |
| 165 | + @test_throws MethodError vvminimum(Areduc, dims=region) |
| 166 | + @test_throws MethodError vvsum(abs, Areduc, dims=region) |
| 167 | + @test_throws MethodError vvsum(abs2, Areduc, dims=region) |
| 168 | + @test_throws MethodError vvmaximum(abs, Areduc, dims=region) |
| 169 | + @test_throws MethodError vvminimum(abs, Areduc, dims=region) |
| 170 | +end |
0 commit comments