diff --git a/Project.toml b/Project.toml index d9f66b8..138fb2f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "DomainSets" uuid = "5b8099bc-c8ec-5219-889f-1d9e522a28bf" -version = "0.8.1" +version = "0.8.2" [deps] CompositeTypes = "b152e2b5-7a66-4b01-a709-34e65c35f657" diff --git a/src/domains/numbers.jl b/src/domains/numbers.jl index f49d7ab..003efb8 100644 --- a/src/domains/numbers.jl +++ b/src/domains/numbers.jl @@ -50,6 +50,8 @@ in(x::Complex{T}, d::ComplexNumbers) where {T<:Real} = true in(x, d::ComplexNumbers) = x ∈ RealNumbers() +choice(d::Union{RealNumbers,Rationals,ComplexNumbers,NaturalNumbers,Integers}) = zero(eltype(d)) + "The set of natural numbers." const ℕ = NaturalNumbers() "The set of integers." diff --git a/src/generic/domain.jl b/src/generic/domain.jl index 177a915..509a34e 100644 --- a/src/generic/domain.jl +++ b/src/generic/domain.jl @@ -144,7 +144,8 @@ isapprox(d1::Domain, d2::Domain; kwds...) = d1 == d2 isrealdomain(d::Domain) = isrealtype(domaineltype(d)) "Return a point from the given domain." -function choice(d) end +choice(d) = error("Overload choice(::$(typeof(d)))") choice(d::AbstractSet) = first(d) choice(d::AbstractArray) = first(d) +choice(d::Number) = d \ No newline at end of file diff --git a/test/test_applications.jl b/test/test_applications.jl index 782254d..63d7b3e 100644 --- a/test/test_applications.jl +++ b/test/test_applications.jl @@ -149,5 +149,5 @@ end m2 = CartToPolarMap() cmap = m1 ∘ m2 ∘ m1 x = rand(2) - @test cmap(x) == m1(m2(m1(x))) + @test cmap(x) ≈ m1(m2(m1(x))) end diff --git a/test/test_domain_product.jl b/test/test_domain_product.jl index a1a8807..92b7053 100644 --- a/test/test_domain_product.jl +++ b/test/test_domain_product.jl @@ -3,6 +3,7 @@ using DomainSets: unionbox, intersectbox function test_product_domains() @test productdomain() == () @test productdomain(2) == 2 + @test choice(productdomain(2)) ≡ 2 @testset "VcatDomain" begin d1 = VcatDomain(-1.0..1.0, -1.0..1.0) diff --git a/test/test_interface.jl b/test/test_interface.jl index bdcf639..8cd192a 100644 --- a/test/test_interface.jl +++ b/test/test_interface.jl @@ -32,6 +32,7 @@ struct NonDomain end @test !(d3 isa Domain) @test DomainStyle(d3) == NotDomain() @test_throws ErrorException checkdomain(d3) + @test_throws ErrorException choice(d3) @test DomainStyle(2.0) == IsDomain() @test DomainStyle([1,2]) == IsDomain() diff --git a/test/test_specific_domains.jl b/test/test_specific_domains.jl index ec6162b..3906304 100644 --- a/test/test_specific_domains.jl +++ b/test/test_specific_domains.jl @@ -697,6 +697,13 @@ include("test_domain_simplex.jl") @test (SVector(0.1,0.2),0.3) ∉ d7 @test (SVector(1,2),3) ∉ d7 end + + @testset "choice" begin + @test choice(ℕ) ≡ 0 + @test choice(ℝ) ≡ 0.0 + @test choice(ℚ) ≡ 0//1 + @test choice(ℂ) ≡ 0.0+0.0im + end end @testset "cartesian product" begin