diff --git a/Project.toml b/Project.toml index 584ac0ac0..fa2446bf0 100644 --- a/Project.toml +++ b/Project.toml @@ -54,7 +54,6 @@ Interfaces = "0.3" IntervalSets = "0.5, 0.6, 0.7" InvertedIndices = "1" IteratorInterfaceExtensions = "1" -JLArrays = "0.1" LinearAlgebra = "1" Makie = "0.19, 0.20, 0.21" OffsetArrays = "1" @@ -86,7 +85,6 @@ Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" ImageFiltering = "6a3955dd-da59-5b1f-98d4-e7296123deb5" ImageTransformations = "02fcd773-0e25-5acc-982a-7f6622650795" -JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb" Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" @@ -97,4 +95,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [targets] -test = ["Aqua", "ArrayInterface", "BenchmarkTools", "CategoricalArrays", "ColorTypes", "Combinatorics", "CoordinateTransformations", "DataFrames", "Distributions", "Documenter", "ImageFiltering", "ImageTransformations", "JLArrays", "CairoMakie", "OffsetArrays", "Plots", "Random", "SafeTestsets", "StatsPlots", "Test", "Unitful"] +test = ["Aqua", "ArrayInterface", "BenchmarkTools", "CategoricalArrays", "ColorTypes", "Combinatorics", "CoordinateTransformations", "DataFrames", "Distributions", "Documenter", "ImageFiltering", "ImageTransformations", "CairoMakie", "OffsetArrays", "Plots", "Random", "SafeTestsets", "StatsPlots", "Test", "Unitful"] diff --git a/src/array/broadcast.jl b/src/array/broadcast.jl index 72b7fd0d6..de6dec328 100644 --- a/src/array/broadcast.jl +++ b/src/array/broadcast.jl @@ -47,24 +47,26 @@ function Broadcast.copy(bc::Broadcasted{DimensionalStyle{S}}) where S end function Base.copyto!(dest::AbstractArray, bc::Broadcasted{DimensionalStyle{S}}) where S - #TODO: this will cause a comparisson to happen twice. We should avoid that - comparedims(dims(dest), _broadcasted_dims(bc); ignore_length_one=true, order=true) + _dims = comparedims(dims(dest), _broadcasted_dims(bc); ignore_length_one=true, order=true) copyto!(dest, _unwrap_broadcasted(bc)) - return dest + A = _firstdimarray(bc) + if A isa Nothing || _dims isa Nothing + dest + else + rebuild(A, dest, _dims, refdims(A)) + end end - - -@inline function Base.Broadcast.materialize!(dest::AbstractDimArray, bc::Base.Broadcast.Broadcasted{<:Any}) - # needed because we need to check whether the dims are compatible in dest which are already - # stripped when sent to copyto! - comparedims(dims(dest), _broadcasted_dims(bc); ignore_length_one=true, order=true) - style = DimensionalData.DimensionalStyle(Base.Broadcast.combine_styles(parent(dest), bc)) - Base.Broadcast.materialize!(style, parent(dest), bc) - return dest +function Base.copyto!(dest::AbstractDimArray, bc::Broadcasted{DimensionalStyle{S}}) where S + _dims = comparedims(dims(dest), _broadcasted_dims(bc); ignore_length_one=true, order=true) + copyto!(parent(dest), _unwrap_broadcasted(bc)) + A = _firstdimarray(bc) + if A isa Nothing || _dims isa Nothing + dest + else + rebuild(A, parent(dest), _dims, refdims(A)) + end end - - function Base.similar(bc::Broadcast.Broadcasted{DimensionalStyle{S}}, ::Type{T}) where {S,T} A = _firstdimarray(bc) rebuildsliced(A, similar(_unwrap_broadcasted(bc), T, axes(bc)...), axes(bc), Symbol("")) diff --git a/src/array/methods.jl b/src/array/methods.jl index ccbaee150..b6ce99b14 100644 --- a/src/array/methods.jl +++ b/src/array/methods.jl @@ -53,9 +53,25 @@ for (m, f) in ((:Statistics, :median), (:Base, :any), (:Base, :all)) end end -function Base.mapreduce(f, op, A::AbstractDimArray; dims=Base.Colon(), kw...) - dims === Colon() && return mapreduce(f, op, parent(A); kw...) - rebuild(A, mapreduce(f, op, parent(A); dims=dimnum(A, dims), kw...), reducedims(A, dims)) +# These are not exported but it makes a lot of things easier using them +function Base._mapreduce_dim(f, op, nt::NamedTuple{(),<:Tuple}, A::AbstractDimArray, dims) + rebuild(A, Base._mapreduce_dim(f, op, nt, parent(A), dimnum(A, _astuple(dims))), reducedims(A, dims)) +end +function Base._mapreduce_dim(f, op, nt::NamedTuple{(),<:Tuple}, A::AbstractDimArray, dims::Colon) + Base._mapreduce_dim(f, op, nt, parent(A), dims) +end +function Base._mapreduce_dim(f, op, nt, A::AbstractDimArray, dims) + rebuild(A, Base._mapreduce_dim(f, op, nt, parent(A), dimnum(A, dims)), reducedims(A, dims)) +end +function Base._mapreduce_dim(f, op, nt, A::AbstractDimArray, dims::Colon) + rebuild(A, Base._mapreduce_dim(f, op, nt, parent(A), dimnum(A, dims)), reducedims(A, dims)) +end + +function Base._mapreduce_dim(f, op, nt::Base._InitialValue, A::AbstractDimArray, dims) + rebuild(A, Base._mapreduce_dim(f, op, nt, parent(A), dimnum(A, dims)), reducedims(A, dims)) +end +function Base._mapreduce_dim(f, op, nt::Base._InitialValue, A::AbstractDimArray, dims::Colon) + Base._mapreduce_dim(f, op, nt, parent(A), dims) end diff --git a/test/broadcast.jl b/test/broadcast.jl index 92ee0fe4f..fd1586bc6 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -1,11 +1,10 @@ using DimensionalData, Test -using JLArrays + using DimensionalData: NoLookup # Tests taken from NamedDims. Thanks @oxinabox da = ones(X(3)) -dajl = rebuild(da, JLArray(parent(da))); @test Base.BroadcastStyle(typeof(da)) isa DimensionalData.DimensionalStyle @testset "standard case" begin @@ -20,21 +19,11 @@ end @test da2 .* da2[:, 1:1] == [1, 4, 9, 16] * (1:2:8)' end -@testset "JLArray broadcast over length one dimension" begin - da2 = DimArray(JLArray((1:4) * (1:2:8)'), (X, Y)) - @test Array(da2 .* da2[:, 1:1]) == [1, 4, 9, 16] * (1:2:8)' -end - @testset "in place" begin @test parent(da .= 1 .* da .+ 7) == 8 * ones(3) @test dims(da .= 1 .* da .+ 7) == dims(da) end -@testset "JLArray in place" begin - @test Array(parent(dajl .= 1 .* dajl .+ 7)) == 8 * ones(3) - @test dims(dajl .= 1 .* dajl .+ 7) == dims(da) -end - @testset "Dimension disagreement" begin @test_throws DimensionMismatch begin DimArray(zeros(3, 3, 3), (X, Y, Z)) .+ @@ -42,13 +31,6 @@ end end end -@testset "JLArray Dimension disagreement" begin - @test_throws DimensionMismatch begin - DimArray(JLArray(zeros(3, 3, 3)), (X, Y, Z)) .+ - DimArray(JLArray(ones(3, 3, 3)), (Y, Z, X)) - end -end - @testset "dims and regular" begin da = DimArray(ones(3, 3, 3), (X, Y, Z)) left_sum = da .+ ones(3, 3, 3) @@ -59,16 +41,6 @@ end @test dims(right_sum) == dims(da) end -@testset "JLArray dims and regular" begin - da = DimArray(JLArray(ones(3, 3, 3)), (X, Y, Z)) - left_sum = da .+ ones(3, 3, 3) - @test Array(left_sum) == fill(2, 3, 3, 3) - @test dims(left_sum) == dims(da) - right_sum = ones(3, 3, 3) .+ da - @test Array(right_sum) == fill(2, 3, 3, 3) - @test dims(right_sum) == dims(da) -end - @testset "changing type" begin @test (da .> 0) isa DimArray @test (da .* da .> 0) isa DimArray @@ -79,16 +51,6 @@ end @test (rand(3) .> 1 .> 0 .* da) isa DimArray end -@testset "JLArray changing type" begin - @test (dajl .> 0) isa DimArray - @test (dajl .* dajl .> 0) isa DimArray - @test (dajl .> 0 .> rand(3)) isa DimArray - @test (dajl .* rand(3) .> 0.0) isa DimArray - @test (0 .> dajl .> 0 .> rand(3)) isa DimArray - @test (rand(3) .> dajl .> 0 .* rand(3)) isa DimArray - @test (rand(3) .> 1 .> 0 .* dajl) isa DimArray -end - @testset "trailng dimensions" begin @test zeros(X(10), Y(5)) .* zeros(X(10), Y(1)) == zeros(X(10), Y(5)) .* zeros(X(1), Y(1)) == @@ -117,18 +79,6 @@ end @test dims(s .+ v .+ m) == dims(m .+ s .+ v) end -@testset "JLArray broadcasting" begin - v = DimArray(JLArray(zeros(3,)), X) - m = DimArray(JLArray(ones(3, 3)), (X, Y)) - s = 0 - @test Array(v .+ m) == ones(3, 3) == Array(m .+ v) - @test Array(s .+ m) == ones(3, 3) == Array(m .+ s) - @test Array(s .+ v .+ m) == ones(3, 3) == Array(m .+ s .+ v) - @test dims(v .+ m) == dims(m .+ v) - @test dims(s .+ m) == dims(m .+ s) - @test dims(s .+ v .+ m) == dims(m .+ s .+ v) -end - @testset "adjoint broadcasting" begin a = DimArray(reshape(1:12, (4, 3)), (X, Y)) b = DimArray(1:3, Y) @@ -138,17 +88,6 @@ end @test dims(a .* b') == dims(a) end -@testset "JLArray adjoint broadcasting" begin - a = DimArray(JLArray(reshape(1:12, (4, 3))), (X, Y)) - b = DimArray(JLArray(1:3), Y) - @test_throws DimensionMismatch a .* b - @test_throws DimensionMismatch parent(a) .* parent(b) - @test Array(parent(a) .* parent(b)') == Array(parent(a .* b')) - @test dims(a .* b') == dims(a) -end - - - @testset "Mixed array types" begin casts = ( A -> DimArray(A, (X, Y)), # Named Matrix @@ -182,26 +121,13 @@ end @test_throws DimensionMismatch ac .= ab .+ ba # check that dest is written into: - z .= ab .+ ba' + @test dims(z .= ab .+ ba') == dims(ab .+ ba') @test z == (ab.data .+ ba.data') -end -@testset "JLArray in-place assignment .=" begin - ab = DimArray(JLArray(rand(2,2)), (X, Y)) - ba = DimArray(JLArray(rand(2,2)), (Y, X)) - ac = DimArray(JLArray(rand(2,2)), (X, Z)) - a_ = DimArray(JLArray(rand(2,2)), (X(), DimensionalData.AnonDim())) - z = JLArray(zeros(2,2)) - - @test_throws DimensionMismatch z .= ab .+ ba - @test_throws DimensionMismatch z .= ab .+ ac - @test_throws DimensionMismatch a_ .= ab .+ ac - @test_throws DimensionMismatch ab .= a_ .+ ac - @test_throws DimensionMismatch ac .= ab .+ ba - - # check that dest is written into: - z .= ab .+ ba' - @test z == (ab.data .+ ba.data') + @test dims(z .= ab .+ a_) == + (X(NoLookup(Base.OneTo(2))), Y(NoLookup(Base.OneTo(2)))) + @test dims(a_ .= ba' .+ ab) == + (X(NoLookup(Base.OneTo(2))), Y(NoLookup(Base.OneTo(2)))) end @testset "assign using named indexing and dotview" begin @@ -211,13 +137,6 @@ end @test A == [1.0 1.0; 2.0 2.0; 7.0 7.0] end -@testset "JLArray assign using named indexing and dotview" begin - A = DimArray(JLArray(zeros(3,2)), (X, Y)) - A[X=1:2] .= JLArray([1, 2]) - A[X=3] .= 7 - @test Array(A) == [1.0 1.0; 2.0 2.0; 7.0 7.0] -end - @testset "0-dimensional array broadcasting" begin x = DimArray(fill(3), ()) y = DimArray(fill(4), ()) @@ -249,31 +168,6 @@ end @test A[DimSelectors(sub)] == C[DimSelectors(sub)] end -@testset "JLArray DimIndices broadcasting" begin - ds = X(1.0:0.2:2.0), Y(10:2:20) - _A = (rand(ds)) - _B = (zeros(ds)) - _C = (zeros(ds)) - - A = rebuild(_A, JLArray(parent(_A))) - B = rebuild(_B, JLArray(parent(_B))) - C = rebuild(_C, JLArray(parent(_C))) - - B[DimIndices(B)] .+= A - C[DimSelectors(C)] .+= A - @test Array(A) == Array(B) == Array(C) - sub = A[1:4, 1:3] - B .= 0 - C .= 0 - B[DimIndices(sub)] .+= sub - C[DimSelectors(sub)] .+= sub - @test Array(A[DimIndices(sub)]) == Array(B[DimIndices(sub)]) == Array(C[DimIndices(sub)]) - sub = A[2:4, 2:5] - C .= 0 - C[DimSelectors(sub)] .+= sub - @test Array(A[DimSelectors(sub)]) == Array(C[DimSelectors(sub)]) -end - # @testset "Competing Wrappers" begin # da = DimArray(ones(4), X) # ta = TrackedArray(5 * ones(4)) diff --git a/test/methods.jl b/test/methods.jl index 73fa30101..e5b6dff1e 100644 --- a/test/methods.jl +++ b/test/methods.jl @@ -1,6 +1,6 @@ using DimensionalData, Statistics, Test, Unitful, SparseArrays, Dates using DimensionalData.Lookups, DimensionalData.Dimensions -using JLArrays + using LinearAlgebra: Transpose xs = (1, X, X(), :X) @@ -17,17 +17,6 @@ xys = ((1, 2), (X, Y), (X(), Y()), (:X, :Y)) @test map(*, da, da) isa DimArray{Int64,2} end -@testset "JLArray map" begin - a = JLArray([1 2; 3 4]) - dimz = X(143:2:145), Y(Sampled(-38:2:-36; span=Explicit([-38 -36; -36 -34]))) - da = DimArray(a, dimz) - @test Array(map(x -> 2x, da)) == [2 4; 6 8] - @test map(x -> 2x, da) isa DimArray{Int64,2} - @test Array(map(*, da, da)) == [1 4; 9 16] - @test map(*, da, da) isa DimArray{Int64,2} -end - - @testset "dimension reducing methods" begin # Test some reducing methods with Explicit spans @@ -138,117 +127,6 @@ end end end -@testset "JLArray dimension reducing methods" begin - - # Test some reducing methods with Explicit spans - a = JLArray([1 2; 3 4]) - dimz = X(143:2:145), Y(Sampled(-38:2:-36; span=Explicit([-38 -36; -36 -34]))) - da = DimArray(a, dimz) - - # Test all dime combinations with maxium - for dims in xs - @test Array(sum(da; dims)) == [4 6] - @test Array(minimum(da; dims)) == [1 2] - - testdims = (X(Sampled(144.0:2.0:144.0, ForwardOrdered(), Regular(4.0), Points(), NoMetadata())), - Y(Sampled(-38:2:-36, ForwardOrdered(), Explicit([-38 -36; -36 -34]), Intervals(Center()), NoMetadata()))) - @test typeof(DimensionalData.dims(minimum(da; dims))) == typeof(testdims) - # @test val.(span(minimum(da; dims))) == val.(span(testdims)) - end - for dims in ys - @test Array(minimum(da; dims)) == [1 3]' - @test Array(maximum(da; dims)) == [2 4]' - @test Array(maximum(x -> 2x, da; dims)) == [4 8]' - testdims = (X(Sampled(143:2:145, ForwardOrdered(), Regular(2), Points(), NoMetadata())), - Y(Sampled(-37.0:4.0:-37.0, ForwardOrdered(), Explicit(reshape([-38, -34], 2, 1)), Intervals(Center()), NoMetadata()))) - @test typeof(DimensionalData.dims(sum(da; dims))) == typeof(testdims) - @test index(sum(da; dims)) == index.(testdims) - # @test val.(span(sum(da; dims))) == val.(span(testdims)) - end - for dims in xys - @test Array(maximum(da; dims)) == [4]' - @test Array(maximum(x -> 2x, da; dims)) == [8]' - end - - @test minimum(da; dims=:) == 1 - @test maximum(da; dims=:) == 4 - @test sum(da; dims=:) == 10 - @test sum(x -> 2x, da; dims=:) == 20 - - a = JLArray([1 2; 3 4]) - dimz = X(143:2:145), Y(-38:2:-36) - da = DimArray(a, dimz) - - @test reduce(+, da) == reduce(+, a) - @test mapreduce(x -> x > 3, +, da; dims=:) == 1 - @test std(da) === std(a) - - for dims in xs - @test Array(prod(da; dims)) == [3 8] - @test Array(mean(da; dims)) == [2.0 3.0] - @test Array(mean(x -> 2x, da; dims)) == [4.0 6.0] - @test Array(reduce(+, da; dims)) == [4 6] - @test Array(mapreduce(x -> x > 3, +, da; dims)) == [0 1] - @test Array(var(da; dims)) == [2.0 2.0] - @test Array(std(da; dims)) == [1.4142135623730951 1.4142135623730951] - @test Array(extrema(da; dims)) == [(1, 3) (2, 4)] - resultdimz = - (X(Sampled(144.0:2.0:144.0, ForwardOrdered(), Regular(4.0), Points(), NoMetadata())), - Y(Sampled(-38:2:-36, ForwardOrdered(), Regular(2), Points(), NoMetadata()))) - @test typeof(DimensionalData.dims(prod(da; dims))) == typeof(resultdimz) - @test bounds(DimensionalData.dims(prod(da; dims))) == bounds(resultdimz) - end - for dims in ys - @test Array(prod(da; dims=2)) == [2 12]' - @test Array(mean(da; dims)) == [1.5 3.5]' - @test Array(mean(x -> 2x, da; dims)) == [3.0 7.0]' - @test DimensionalData.dims(mean(da; dims)) == - (X(Sampled(143:2:145, ForwardOrdered(), Regular(2), Points(), NoMetadata())), - Y(Sampled(-37.0:4.0:-37.0, ForwardOrdered(), Regular(4.0), Points(), NoMetadata()))) - @test DimensionalData.dims(reduce(+, da; dims)) == - (X(Sampled(143:2:145, ForwardOrdered(), Regular(2), Points(), NoMetadata())), - Y(Sampled(-37.0:2.0:-37.0, ForwardOrdered(), Regular(4.0), Points(), NoMetadata()))) - @test DimensionalData.dims(mapreduce(x -> x > 3, +, da; dims)) == - (X(Sampled(143:2:145, ForwardOrdered(), Regular(2), Points(), NoMetadata())), - Y(Sampled(-37.0:2:-37.0, ForwardOrdered(), Regular(4.0), Points(), NoMetadata()))) - @test Array(std(da; dims)) == [0.7071067811865476 0.7071067811865476]' - @test Array(var(da; dims)) == [0.5 0.5]' - @test DimensionalData.dims(var(da; dims)) == - (X(Sampled(143:2:145, ForwardOrdered(), Regular(2), Points(), NoMetadata())), - Y(Sampled(-37.0:4.0:-37.0, ForwardOrdered(), Regular(4.0), Points(), NoMetadata()))) - @test Array(extrema(da; dims)) == permutedims([(1, 2) (3, 4)]) - end - for dims in xys - @test Array(mean(da; dims=dims)) == [2.5]' - @test Array(mean(x -> 2x, da; dims=dims)) == [5.0]' - @test Array(reduce(+, da; dims)) == [10]' - @test Array(mapreduce(x -> x > 3, +, da; dims)) == [1]' - @test Array(extrema(da; dims)) == reshape([(1, 4)], 1, 1) - end - - a = JLArray([1 2 3; 4 5 6]) - dimz = X(143:2:145), Y(-38:-36) - da = DimArray(a, dimz) - @test @inferred median(da) == 3.5 - # median along a dimension doesn't have a gpu implementation - @test_broken @inferred Array(median(da; dims=X())) == [2.5 3.5 4.5] - @test_broken @inferred Array(median(da; dims=2)) == [2.0 5.0]' - - a = JLArray(Bool[0 1 1; 0 0 0]) - da = DimArray(a, dimz); - @test_broken any(da) === true # This is broken because GPUArrays didn't support any(da, dims=:) only any(da) - @test_broken any(da; dims=Y) == reshape([true, false], 2, 1) - @test_broken all(da) === false - @test_broken all(da; dims=Y) == reshape([false, false], 2, 1) - @test_broken all(da; dims=(X, Y)) == reshape([false], 1, 1) - - @testset "inference" begin - x = DimArray(randn(2, 3, 4), (X, Y, Z)); - foo(x) = maximum(x; dims=(1, 2)) - @inferred foo(x) - end -end - @testset "dimension dropping methods" begin a = [1 2 3; 4 5 6] dimz = X(143:2:145), Y(-38:-36) @@ -264,21 +142,6 @@ end @test length.(dims(dropped[1:2])) == size(dropped[1:2]) end -@testset "JLArray dimension dropping methods" begin - a = JLArray([1 2 3; 4 5 6]) - dimz = X(143:2:145), Y(-38:-36) - da = DimArray(a, dimz); - # Dimensions must have length 1 to be dropped - @test Array(dropdims(da[X(1:1)]; dims=X)) == [1, 2, 3] - @test Array(dropdims(da[2:2, 1:1]; dims=(X(), Y())))[] == 4 - @test typeof(dropdims(da[2:2, 1:1]; dims=(X(), Y()))) <: DimArray{Int,0,Tuple{}} - @test refdims(dropdims(da[X(1:1)]; dims=X)) == - (X(Sampled(143:2:143, ForwardOrdered(), Regular(2), Points(), NoMetadata())),) - dropped = dropdims(da[X(1:1)]; dims=X) - @test Array(dropped[1:2]) == [1, 2] - @test length.(dims(dropped[1:2])) == size(dropped[1:2]) -end - @testset "eachslice" begin a = [1 2 3 4 3 4 5 6 @@ -396,6 +259,7 @@ end @test dims(dsp) == reverse(dims(da)) end + @testset "dimension permuting methods with specified permutation" begin da = DimArray(ones(5, 2, 4), (Y(LinRange(10, 20, 5)), Ti(10:11), X(1:4))) dsp = permutedims(da, [3, 1, 2]) @@ -417,6 +281,7 @@ end @test typeof(dsp2) <: DimArray end + @testset "dimension rotating methods" begin da = DimArray([1 2; 3 4], (X([:a, :b]), Y([1.0, 2.0]))) @@ -844,80 +709,3 @@ end end end end - -@testset "mapreduce" begin - @testset "Array 2D" begin - y = Y(['a', 'b', 'c']) - ti = Ti(DateTime(2021, 1):Month(1):DateTime(2021, 4)) - ys = (1, Y, Y(), :Y, y) - tis = (2, Ti, Ti(), :Ti, ti) - data = [-87 -49 107 -18 - 24 44 -62 124 - 122 -11 48 -7] - A = DimArray(data, (y, ti)) - - - @test mapreduce(identity, +, A) ≈ mapreduce(identity, +, parent(A)) - @test mapreduce(x->x^3+5, +, A) ≈ mapreduce(x->x^3+5, +, parent(A)) - - for dims in ys - @test mapreduce(identity, +, A; dims) ≈ mapreduce(identity, +, parent(A); dims=1) - end - - for dims in tis - @test mapreduce(identity, +, A; dims) ≈ mapreduce(identity, +, parent(A); dims=2) - end - - @test mapreduce(identity, +, A; dims=Y) ≈ mapreduce(identity, +, parent(A); dims=1) - @test mapreduce(identity, +, A; dims=Ti) ≈ mapreduce(identity, +, parent(A); dims=2) - @test mapreduce(identity, +, A; dims=(Y, Ti)) ≈ mapreduce(identity, +, parent(A); dims=(1, 2)) - - init = 5.0 - @test mapreduce(identity, +, A; init) ≈ mapreduce(identity, +, parent(A); init) - @test mapreduce(x->x^3+5, +, A; init) ≈ mapreduce(x->x^3+5, +, parent(A); init) - @test mapreduce(identity, +, A; dims=Y, init) ≈ mapreduce(identity, +, parent(A); dims=1, init) - @test mapreduce(identity, +, A; dims=Ti, init) ≈ mapreduce(identity, +, parent(A); dims=2, init) - @test mapreduce(identity, +, A; dims=(Y, Ti), init) ≈ mapreduce(identity, +, parent(A); dims=(1, 2), init) - end - @testset "Vector" begin - x = DimArray([56, -123, -60, -44, -64, 70, 52, -48, -74, 86], X(2:2:20)) - @test mapreduce(x->x^2, +, x) ≈ mapreduce(x->x^2, +, parent(x)) - @test mapreduce(identity, +, x) ≈ mapreduce(identity, +, parent(x)) - @test mapreduce(identity, +, x; dims=X) ≈ mapreduce(identity, +, parent(x); dims=1) - @test mapreduce(x->x^2, +, x; dims=X) ≈ mapreduce(x->x^2, +, parent(x); dims=1) - @test mapreduce(identity, +, x; init=5.0) ≈ mapreduce(identity, +, parent(x); init=5.0) - end - - @testset "JLArray" begin - y = Y(['a', 'b', 'c']) - ti = Ti(DateTime(2021, 1):Month(1):DateTime(2021, 4)) - ys = (1, Y, Y(), :Y, y) - tis = (2, Ti, Ti(), :Ti, ti) - data = JLArray([-87 -49 107 -18 - 24 44 -62 124 - 122 -11 48 -7]) - A = DimArray(data, (y, ti)) - - @test mapreduce(identity, +, A) ≈ mapreduce(identity, +, parent(A)) - @test mapreduce(x->x^3+5, +, A) ≈ mapreduce(x->x^3+5, +, parent(A)) - # Using parent since JLArray errors - for dims in ys - @test parent(mapreduce(identity, +, A; dims)) ≈ mapreduce(identity, +, parent(A); dims=1) - end - - for dims in tis - @test parent(mapreduce(identity, +, A; dims)) ≈ mapreduce(identity, +, parent(A); dims=2) - end - - @test parent(mapreduce(identity, +, A; dims=Y)) ≈ mapreduce(identity, +, parent(A); dims=1) - @test parent(mapreduce(identity, +, A; dims=Ti)) ≈ mapreduce(identity, +, parent(A); dims=2) - @test parent(mapreduce(identity, +, A; dims=(Y, Ti))) ≈ mapreduce(identity, +, parent(A); dims=(1, 2)) - - init = 5.0 - @test mapreduce(identity, +, A; init) ≈ mapreduce(identity, +, parent(A); init) - @test mapreduce(x->x^3+5, +, A; init) ≈ mapreduce(x->x^3+5, +, parent(A); init) - @test parent(mapreduce(identity, +, A; dims=Y, init)) ≈ mapreduce(identity, +, parent(A); dims=1, init) - @test parent(mapreduce(identity, +, A; dims=Ti, init)) ≈ mapreduce(identity, +, parent(A); dims=2, init) - @test parent(mapreduce(identity, +, A; dims=(Y, Ti), init)) ≈ mapreduce(identity, +, parent(A); dims=(1, 2), init) - end -end