Skip to content

Commit

Permalink
Add vcat and hcat for AbstractDimArray (#456)
Browse files Browse the repository at this point in the history
* Add vcat and hcat

* Unify hcat and vcat

* Add vcat and hcat tests

* Increment patch version number
  • Loading branch information
sethaxen authored Feb 19, 2023
1 parent 6ef9e5f commit 11be8da
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DimensionalData"
uuid = "0703355e-b756-11e9-17c0-8b28908087d0"
authors = ["Rafael Schouten <rafaelschouten@gmail.com>"]
version = "0.24.2"
version = "0.24.3"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
17 changes: 17 additions & 0 deletions src/array/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,23 @@ function _cat(catdims::Tuple, A1::AbstractDimArray, As::AbstractDimArray...)
rebuild(A1, newA, format(newdims, newA), newrefdims)
end

function Base.vcat(As::Union{AbstractDimVector,AbstractDimMatrix}...)
return _horvcat(Base.splat(vcat), As, Val(1))
end

function Base.hcat(As::AbstractDimMatrix...)
return _horvcat(Base.splat(hcat), As, Val(2))
end

function _horvcat(f, As, ::Val{I}) where {I}
A1 = first(As)
catdim = vcat(map(Base.Fix2(dims, I), As)...)
noncatdim = only(otherdims(dims(A1), catdim))
newdims = Base.setindex((noncatdim, noncatdim), catdim, I)
newA = f(map(parent, As))
rebuild(A1, newA, format(newdims, newA))
end

function Base.vcat(d1::Dimension, ds::Dimension...)
newlookup = _vcat_lookups(lookup((d1, ds...))...)
rebuild(d1, newlookup)
Expand Down
26 changes: 26 additions & 0 deletions test/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,32 @@ end
end
end

@testset "vcat" begin
a = [1 2 3; 4 5 6]
da = DimArray(a, (X(4.0:5.0), Y(6.0:8.0)))
b = [7 8 9; 10 11 12]
db = DimArray(b, (X(6.0:7.0), Y(6.0:8.0)))
c = [13 14 15; 16 17 18]
dc = DimArray(c, (X(8.0:9.0), Y(6.0:8.0)))

@test @inferred(vcat(da)) == da
@test @inferred(vcat(da, db)) == cat(da, db; dims=1)
@test @inferred(vcat(da, db, dc)) == cat(da, db, dc; dims=1)
end

@testset "hcat" begin
a = [1 2 3; 4 5 6]
da = DimArray(a, (X(4.0:5.0), Y(6.0:8.0)))
b = [7 8 9; 10 11 12]
db = DimArray(b, (X(4.0:5.0), Y(9.0:11.0)))
c = [13 14 15; 16 17 18]
dc = DimArray(c, (X(4.0:5.0), Y(12.0:14.0)))

@test @inferred(hcat(da)) == da
@test @inferred(hcat(da, db)) == cat(da, db; dims=2)
@test @inferred(hcat(da, db, dc)) == cat(da, db, dc; dims=2)
end

@testset "unique" begin
a = [1 1 6; 1 1 6]
xs = (X, X(), :X)
Expand Down

2 comments on commit 11be8da

@sethaxen
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/78022

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.24.3 -m "<description of version>" 11be8da01d0c23ef82d6d4b4e7ae5f7d2edab232
git push origin v0.24.3

Please sign in to comment.