Skip to content

Commit

Permalink
Merge pull request #155 from JuliaApproximation/dh/basedomaintypes
Browse files Browse the repository at this point in the history
better support base domain types
  • Loading branch information
daanhb authored Feb 29, 2024
2 parents 9d9bfa4 + 33b619c commit 7761929
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/DomainSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ include("maps/basic.jl")
include("maps/affine.jl")
include("maps/arithmetics.jl")

include("generic/core.jl")
include("generic/interface.jl")
include("generic/domain.jl")
include("generic/geometry.jl")
include("generic/canonical.jl")
Expand Down
6 changes: 3 additions & 3 deletions src/domains/cube.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ Rectangle(a::T, b::T) where {T} = Rectangle{T}(a, b)
Rectangle(a::NTuple{N,T}, b::NTuple{N,T}) where {N,T} =
Rectangle(SVector{N,T}(a), SVector{N,T}(b))
Rectangle(a::T, b::T) where {T<:Number} =
error("Rectangles have to be constructed from vectors or tuples, not numbers.")
throw(ArgumentError("Rectangles have to be constructed from vectors or tuples, not numbers."))

Rectangle(domains::Tuple) = Rectangle(domains...)
Rectangle(domains::ClosedInterval...) = Rectangle(promote_domains(domains)...)
Expand All @@ -305,15 +305,15 @@ Rectangle(domains::ClosedInterval{T}...) where {T} =
Rectangle(domains::AbstractVector{<:ClosedInterval}) =
Rectangle(map(leftendpoint, domains), map(rightendpoint, domains))
Rectangle(domains::Domain...) =
error("The Rectangle constructor expects two points or a list of intervals (closed).")
throw(ArgumentError("The Rectangle constructor expects two points or a list of intervals (closed)."))

Rectangle{T}(domains::Tuple) where {T} = Rectangle{T}(domains...)
Rectangle{T}(domains::ClosedInterval...) where {T} =
Rectangle{T}(map(leftendpoint, domains), map(rightendpoint, domains))
Rectangle{T}(domains::AbstractVector{<:ClosedInterval}) where {T} =
Rectangle{T}(map(leftendpoint, domains), map(rightendpoint, domains))
Rectangle{T}(domains::Domain...) where {T} =
error("The Rectangle constructor expects two points or a list of intervals (closed).")
throw(ArgumentError("The Rectangle constructor expects two points or a list of intervals (closed)."))

ProductDomain(domains::ClosedInterval{<:Number}...) = Rectangle(domains...)
ProductDomain(domains::AbstractVector{<:ClosedInterval{<:Number}}) =
Expand Down
8 changes: 5 additions & 3 deletions src/generic/canonical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ mapto1(d1, d2) =
hasparameterization(d1) ? mapto(parameterdomain(d1), d2) mapto_parameterdomain(d1) : mapto2(d1, d2)
# simplify the second argument
mapto2(d1, d2) =
hasparameterization(d2) ? mapfrom_parameterdomain(d2) mapto(d1, parameterdomain(d2)) : no_known_mapto(d1,d2)
hasparameterization(d2) ? mapfrom_parameterdomain(d2) mapto(d1, parameterdomain(d2)) : default_mapto(d1,d2)

no_known_mapto(d1, d2) = d1 == d2 ? identitymap(d1) : error("No map known between $(d1) and $(d2).")
default_mapto(d1, d2) = d1 == d2 ? identitymap(d1) : throw(ArgumentError("No map known between $(d1) and $(d2)."))



Expand All @@ -143,7 +143,9 @@ isequaldomain(d1, d2) = isequaldomain1(d1, d2)
isequaldomain1(d1, d2) = simplifies(d1) ? isequaldomain(simplify(d1), d2) : isequaldomain2(d1, d2)
isequaldomain2(d1, d2) = simplifies(d2) ? isequaldomain(d1, simplify(d2)) : default_isequaldomain(d1, d2)
default_isequaldomain(d1, d2) = d1 === d2
isequaldomain(d1::AbstractArray, d2::AbstractArray) = d1 d2 && d2 d1 # Can't use == since order doesnlt matter

# Can't use == for arrays etcetera since order doesn't matter
isequaldomain(d1::BaseDomainType, d2::BaseDomainType) = d1 d2 && d2 d1

==(d1::AnyDomain, d2::AnyDomain) = isequaldomain(domain(d1), domain(d2))

Expand Down
1 change: 1 addition & 0 deletions src/generic/core.jl → src/generic/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ DomainStyle(::Type{<:Number}) = IsDomain()
DomainStyle(::Type{<:AbstractSet}) = IsDomain()
DomainStyle(::Type{<:AbstractArray}) = IsDomain()

BaseDomainType = Union{<:Number,<:AbstractSet,<:AbstractArray}

"""
domain(d)
Expand Down
5 changes: 5 additions & 0 deletions src/generic/setoperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ default_issubset_domain(d1, d2) = d1 == d2
issubset1(d1::Number, d2) = in(d1, d2)
issubset1(d1::AbstractArray, d2) = all(in(d2), d1)
issubset1(d1::AbstractSet, d2) = all(in(d2), d1)
# use Julia's implementation for Julia types
issubset_domain(d1::BaseDomainType, d2::BaseDomainType) = issubset(d1, d2)


############################
Expand Down Expand Up @@ -70,6 +72,7 @@ function default_uniondomain(d1, d2)
UnionDomain(d1, d2)
end
end
uniondomain(d1::BaseDomainType, d2::BaseDomainType) = union(d1, d2)

uniondomain(d1, d2, d3) = _ud3(promote_domains(d1, d2, d3)...)
_ud3(d1, d2, d3) =
Expand Down Expand Up @@ -216,6 +219,7 @@ function default_intersectdomain(d1, d2)
IntersectDomain(d1, d2)
end
end
intersectdomain(d1::BaseDomainType, d2::BaseDomainType) = intersect(d1, d2)

intersectdomain(d1, d2, d3) = _id3(promote_domains(d1, d2, d3)...)
_id3(d1, d2, d3) =
Expand Down Expand Up @@ -328,6 +332,7 @@ function default_setdiffdomain(d1, d2)
SetdiffDomain(d1, d2)
end
end
setdiffdomain(d1::BaseDomainType, d2::BaseDomainType) = setdiff(d1, d2)

# avoid nested difference domains
setdiffdomain1(d1::SetdiffDomain, d2) = setdiffdomain(d1.domains[1], uniondomain(d2, d1.domains[2]))
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ include("test_generic_domain.jl")
include("test_specific_domains.jl")
include("test_canonical.jl")
include("test_setoperations.jl")
include("test_core.jl")
include("test_interface.jl")
include("test_applications.jl")
include("test_readme.jl")
2 changes: 1 addition & 1 deletion test/test_canonical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end
@test mapfrom_canonical(iso, d2) isa DomainSets.NumberToVector
@test mapto_canonical(iso, d2) isa DomainSets.VectorToNumber

@test_throws ErrorException mapto(UnitCircle(), UnitDisk())
@test_throws ArgumentError mapto(UnitCircle(), UnitDisk())
@test mapto(UnitInterval(), UnitInterval()) isa IdentityMap
@test mapto(UnitInterval(), ChebyshevInterval()) isa AffineMap

Expand Down
10 changes: 6 additions & 4 deletions test/test_domain_product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ function test_product_domains()
@test Rectangle((0..1, 2..3)) isa Rectangle{SVector{2,Int}}
@test Rectangle{SVector{2,Float64}}((0..1, 2..3)) isa Rectangle{SVector{2,Float64}}

@test_throws ErrorException Rectangle(UnitCircle(), UnitDisk())
@test_throws ErrorException Rectangle(OpenInterval(1,2), 3..4)
@test_throws ErrorException Rectangle{SVector{2,Float64}}(UnitCircle(), UnitDisk())
@test_throws ErrorException Rectangle(0.0, 1.0)
@test_throws ArgumentError Rectangle(UnitCircle(), UnitDisk())
@test_throws ArgumentError Rectangle(OpenInterval(1,2), 3..4)
@test_throws ArgumentError Rectangle{SVector{2,Float64}}(UnitCircle(), UnitDisk())
@test_throws ArgumentError Rectangle(0.0, 1.0)

bnd = boundary(Rectangle([1,2],[3,4]))
@test [1,3] bnd
Expand Down Expand Up @@ -372,5 +372,7 @@ function test_product_domains()
x = VcatDomain(0.0:0.5:2.0, [1,3,4])
y = VcatDomain(0.0:0.5:2.0, [1,3,4])
@test x == y
@test isequaldomain(5, [5.0])
@test isequaldomain(Set([5.0]), [5.0])
end
end
6 changes: 3 additions & 3 deletions test/test_core.jl → test/test_interface.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

@testset "DomainSetsCore" begin
@testset "Domain interface" begin
p = Point(0.5)
@test !(p == 0.5)
@test p == DomainRef(0.5)

@test union(0.5,0.7) isa Vector{Float64}
@test uniondomain(0.5, 0.7) isa UnionDomain{Float64,Tuple{Float64,Float64}}
@test uniondomain(0.5, 1) isa UnionDomain{Float64,Tuple{Float64,Float64}}
@test uniondomain(0.5, 0.7) == [0.5, 0.7]
@test uniondomain(0.5, 1) == [0.5, 1]
@test_throws MethodError union(Point(0.5), 0.5)
@test union(Point(0.5), DomainRef(0.5)) == Point(0.5)
@test union(Point(0.5), DomainRef(0.7)) isa UnionDomain{Float64}
Expand Down
6 changes: 4 additions & 2 deletions test/test_setoperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@
@test !issubset_domain([0,1,2], 0..1)
@test issubset_domain(Set([0,1]), 0..1)
@test !issubset_domain(Set([0,2]), 0..1)
@test issubset_domain([0,1], 0:5)

@test uniondomain() == EmptySpace{Any}()
@test uniondomain(0..1) == 0..1
@test uniondomain(0..1, [0,1]) == 0..1
@test uniondomain([0,1], 0..1) == 0..1

@test uniondomain([0,1], [0.0,1.0]) == [0,1]
@test uniondomain([0,2], [0.0,1.0]) isa UnionDomain
@test isequaldomain(uniondomain([0,2], [0.0,1.0]), [0.0,2.0,1.0])

# larger union expressions
@test uniondomain(0..1, 1..3, Point(0.4), 2..5, FullSpace(), Point(-0.2)) isa FullSpace
Expand Down Expand Up @@ -155,7 +156,8 @@
@test intersectdomain(0..1, [0,1]) == [0,1]
@test intersectdomain([0,1], 0..1) == [0,1]
@test intersectdomain([0,1], [0.0,1.0]) == [0,1]
@test intersectdomain([0,2], [0.0,1.0]) isa IntersectDomain
@test intersectdomain([0,2], [0.0,1.0]) == [0.0]
@test isempty(intersectdomain([0,2], Set(3:7)))

@test IntersectDomain(UnitDisk(), UnitSquare()) == IntersectDomain(UnitSquare(), UnitDisk())

Expand Down

2 comments on commit 7761929

@daanhb
Copy link
Member Author

@daanhb daanhb commented on 7761929 Feb 29, 2024

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/102030

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.7.7 -m "<description of version>" 77619299e4274437d3a57e89c601eacd9a8a14f8
git push origin v0.7.7

Please sign in to comment.