Skip to content

Refactor to allow containers to be directly used as spaces #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 5, 2022
5 changes: 5 additions & 0 deletions src/CommonRLSpaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ export

include("array.jl")

export
product

include("product.jl")

end
1 change: 1 addition & 0 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ elsize(b::Box) = size(b.lower)
bounds(b::Box) = (b.lower, b.upper)
Base.clamp(x::AbstractArray, b::Box) = clamp.(x, b.lower, b.upper)

Base.convert(t::Type{<:Box}, i::ClosedInterval) = t(SA[minimum(i)], SA[maximum(i)])

struct RepeatedSpace{B, S<:Tuple} <: AbstractArraySpace
base_space::B
Expand Down
23 changes: 23 additions & 0 deletions src/product.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
product(i1::ClosedInterval, i2::ClosedInterval) = Box(SA[minimum(i1), minimum(i2)], SA[maximum(i1), maximum(i2)])

product(b::Box, i::ClosedInterval) = product(b, convert(Box, i))
product(i::ClosedInterval, b::Box) = product(convert(Box, i), b)
product(b1::Box{<:AbstractVector}, b2::Box{<:AbstractVector}) = Box(vcat(b1.lower, b2.lower), vcat(b1.upper, b2.upper))
function product(b1::Box, b2::Box)
if size(b1.lower, 2) == size(b2.lower, 2) # same number of columns
return Box(vcat(b1.lower, b2.lower), vcat(b1.upper, b2.upper))
else
return GenericrSpaceProduct((b1, b2))
end
end

# handle case of 3 or more
product(s1, s2, s3, args...) = product(product(s1, s2), s3, args...)

struct GenericrSpaceProduct{T<:Tuple}
members::T
end

# handle any case not covered above
product(s1, s2) = GenericrSpaceProduct((s1, s2))
product(s1::GenericrSpaceProduct, s2) = GenericrSpaceProduct((s1.members..., s2))
9 changes: 9 additions & 0 deletions test/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
@test @inferred elsize(b) == (2,2)
end

@testset "Box comparison" begin
@test Box([1,2], [3,4]) == Box([1,2], [3,4])
@test Box([1,2], [3,4]) != Box([1,3], [3,4])
end

@testset "Interval to box conversion" begin
@test convert(Box, 1..2) == Box([1], [2])
end

@testset "ArraySpace with Range" begin
s = ArraySpace(1:5, 3, 4)
@test @inferred SpaceStyle(s) == FiniteSpaceStyle()
Expand Down
15 changes: 15 additions & 0 deletions test/product.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@testset "Product of boxes" begin
@test product(Box([1], [2]), Box([3], [4])) == Box([1,3], [2,4])
@test product(Box(ones(2,1), 2*ones(2,1)), Box([3], [4])) == Box(@SMatrix([1;1;3]), @SMatrix([2;2;4]))
end

@testset "Product of intervals" begin
@test @inferred product(1..2, 3..4) == Box([1,3], [2,4])
@test @inferred product(1..2, 3..4, 5..6) == Box([1,3,5], [2,4,6])
@test @inferred product(1..2, 3..4, 5..6, 7..8) == Box([1,3,5,7], [2,4,6,8])
end

@testset "Product of Box and interval" begin
@test @inferred product(Box([1,3], [2,4]), 5..6) == Box([1,3,5], [2,4,6])
@test @inferred product(5..6, Box([1,3], [2,4])) == Box([5,1,3], [6,2,4])
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ using StaticArrays
@testset "CommonRLSpaces.jl" begin
include("basic.jl")
include("array.jl")
include("product.jl")
end