diff --git a/src/DomainSets.jl b/src/DomainSets.jl index d043906..7e980a7 100644 --- a/src/DomainSets.jl +++ b/src/DomainSets.jl @@ -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") diff --git a/src/domains/cube.jl b/src/domains/cube.jl index bace51c..1bc97ce 100644 --- a/src/domains/cube.jl +++ b/src/domains/cube.jl @@ -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)...) @@ -305,7 +305,7 @@ 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} = @@ -313,7 +313,7 @@ Rectangle{T}(domains::ClosedInterval...) where {T} = 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}}) = diff --git a/src/generic/canonical.jl b/src/generic/canonical.jl index b4f9b8a..bee5251 100644 --- a/src/generic/canonical.jl +++ b/src/generic/canonical.jl @@ -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).")) @@ -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)) diff --git a/src/generic/core.jl b/src/generic/interface.jl similarity index 97% rename from src/generic/core.jl rename to src/generic/interface.jl index c759907..abfb5e9 100644 --- a/src/generic/core.jl +++ b/src/generic/interface.jl @@ -69,6 +69,7 @@ DomainStyle(::Type{<:Number}) = IsDomain() DomainStyle(::Type{<:AbstractSet}) = IsDomain() DomainStyle(::Type{<:AbstractArray}) = IsDomain() +BaseDomainType = Union{<:Number,<:AbstractSet,<:AbstractArray} """ domain(d) diff --git a/src/generic/setoperations.jl b/src/generic/setoperations.jl index 1cb1dfa..ddd32df 100644 --- a/src/generic/setoperations.jl +++ b/src/generic/setoperations.jl @@ -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) ############################ @@ -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) = @@ -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) = @@ -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])) diff --git a/test/runtests.jl b/test/runtests.jl index 59f59a7..fcb0420 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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") diff --git a/test/test_canonical.jl b/test/test_canonical.jl index ce98f48..fadddd5 100644 --- a/test/test_canonical.jl +++ b/test/test_canonical.jl @@ -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 diff --git a/test/test_domain_product.jl b/test/test_domain_product.jl index fae35ca..c2d5d78 100644 --- a/test/test_domain_product.jl +++ b/test/test_domain_product.jl @@ -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 @@ -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 diff --git a/test/test_core.jl b/test/test_interface.jl similarity index 61% rename from test/test_core.jl rename to test/test_interface.jl index 9f329a7..c283d12 100644 --- a/test/test_core.jl +++ b/test/test_interface.jl @@ -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} diff --git a/test/test_setoperations.jl b/test/test_setoperations.jl index 90f0d9b..741ddbe 100644 --- a/test/test_setoperations.jl +++ b/test/test_setoperations.jl @@ -66,6 +66,7 @@ @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 @@ -73,7 +74,7 @@ @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 @@ -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())