Skip to content
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

Move definition of Domain to DomainSetsCore.jl package #140

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,36 @@ version = "0.6.7"

[deps]
CompositeTypes = "b152e2b5-7a66-4b01-a709-34e65c35f657"
DomainSetsCore = "b5e7cfa8-5ebe-46e7-951c-e7d99cb94c6d"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[weakdeps]
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"

[compat]
julia = "1.6"
CompositeTypes = "0.1.2"
DomainSetsCore = "0.1.0"
GeometryBasics = "0.4"
IntervalSets = "0.7.4"
StaticArrays = "0.12.2, 1"
Intervals = "1.10"
StableRNGs = "1"
julia = "1.6"
StaticArrays = "0.12.2, 1"

[extensions]
DomainSetsIntervalsExt = "Intervals"
DomainSetsGeometryBasicsExt = "GeometryBasics"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "StableRNGs"]
Empty file added docs/src/interface.md
Empty file.
64 changes: 64 additions & 0 deletions ext/DomainSetsGeometryBasicsExt/DomainSetsGeometryBasicsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module DomainSetsGeometryBasicsExt

using DomainSetsCore, DomainSets, GeometryBasics

const GB = GeometryBasics

using StaticArrays

import DomainSets: todomainset

DomainSetsCore.DomainStyle(d::GB.AbstractGeometry) = IsDomain()
DomainSetsCore.domaineltype(d::GB.AbstractGeometry{DIM,T}) where {DIM,T} = SVector{DIM,T}


## The Point type

# repeat because AbstractPoint does not inherit from AbstractGeometry, it is a vector
DomainSetsCore.DomainStyle(d::GB.AbstractPoint) = IsDomain()
DomainSetsCore.domaineltype(d::GB.AbstractPoint{DIM,T}) where {DIM,T} = SVector{DIM,T}

DomainSets.convert_eltype(::Type{SVector{N,T}}, d::GB.Point{N,T}) where {N,T} = d
DomainSets.convert_eltype(::Type{SVector{N,T}}, d::GB.Point{N}) where {N,T} = GB.Point{N,T}(d.data)

todomainset(d::GB.AbstractPoint{N,T}) where {N,T} = DomainSets.Point{SVector{N,T}}(d)

DomainSets.canonicaldomain(::DomainSets.Equal, d::GB.AbstractPoint) = todomainset(d)

DomainSets.canonicaldomain(d::GB.AbstractPoint) = canonicaldomain(todomainset(d))
DomainSets.mapfrom_canonical(d::GB.AbstractPoint) = DomainSets.mapfrom_canonical(todomainset(d))


## The HyperRectangle primitive

DomainSets.convert_eltype(::Type{SVector{N,T}}, d::GB.HyperRectangle{N,T}) where {N,T} = d
DomainSets.convert_eltype(::Type{SVector{N,T}}, d::GB.HyperRectangle{N}) where {N,T} =
GB.HyperRectangle{N,T}(d.origin, d.widths)

todomainset(d::GB.HyperRectangle{N,T}) where {N,T} =
DomainSets.Rectangle{SVector{N,T}}(d.origin, d.origin+d.widths)

DomainSets.canonicaldomain(::DomainSets.Equal, d::GB.HyperRectangle) =
todomainset(d)

DomainSets.canonicaldomain(d::GB.HyperRectangle) =
canonicaldomain(todomainset(d))
DomainSets.mapfrom_canonical(d::GB.HyperRectangle) =
DomainSets.mapfrom_canonical(todomainset(d))


## The HyperSphere primitive

DomainSets.convert_eltype(::Type{SVector{N,T}}, d::GB.HyperSphere{N,T}) where {N,T} = d
DomainSets.convert_eltype(::Type{SVector{N,T}}, d::GB.HyperSphere{N}) where {N,T} =
GB.Sphere{N,T}(d.center, d.r)

todomainset(d::GB.HyperSphere{N,T}) where {N,T} =
DomainSets.Ball{SVector{N,T}}(d.r, d.center)

DomainSets.canonicaldomain(::DomainSets.Equal, d::GB.HyperSphere) = todomainset(d)

DomainSets.canonicaldomain(d::GB.HyperSphere) = canonicaldomain(todomainset(d))
DomainSets.mapfrom_canonical(d::GB.HyperSphere) = DomainSets.mapfrom_canonical(todomainset(d))

end # module
52 changes: 52 additions & 0 deletions ext/DomainSetsIntervalsExt/DomainSetsIntervalsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module DomainSetsIntervalsExt

using DomainSetsCore, DomainSets, Intervals

import DomainSets: todomainset

## The Intervals.Interval type

DomainSetsCore.DomainStyle(d::Intervals.AbstractInterval) = IsDomain()
DomainSetsCore.domaineltype(d::Intervals.Interval) = eltype(d)

DomainSets.convert_eltype(::Type{T}, d::Intervals.Interval{T}) where {T} = d
DomainSets.convert_eltype(::Type{T}, d::Intervals.Interval) where {T} =
Intervals.Interval(T(d.first), T(d.last))

todomainset(d::Intervals.Interval{T,Closed,Closed}) where T =
DomainSets.Interval{:closed,:closed,T}(d.first, d.last)
todomainset(d::Intervals.Interval{T,Closed,Open}) where T =
DomainSets.Interval{:closed,:open,T}(d.first, d.last)
todomainset(d::Intervals.Interval{T,Open,Closed}) where T =
DomainSets.Interval{:open,:closed,T}(d.first, d.last)
todomainset(d::Intervals.Interval{T,Open,Open}) where T =
DomainSets.Interval{:open,:open,T}(d.first, d.last)

DomainSets.canonicaldomain(::DomainSets.Equal, d::Intervals.Interval) =
todomainset(d)

DomainSets.canonicaldomain(d::Intervals.Interval) =
canonicaldomain(todomainset(d))
DomainSets.mapfrom_canonical(d::Intervals.Interval) =
DomainSets.mapfrom_canonical(todomainset(d))


## The Intervals.IntervalSet type

DomainSetsCore.DomainStyle(d::Intervals.IntervalSet) = IsDomain()
DomainSetsCore.domaineltype(d::Intervals.IntervalSet) = domaineltype(d.items[1])

DomainSets.convert_eltype(::Type{T}, d::Intervals.IntervalSet) where T =
Intervals.IntervalSet(map(d->DomainSets.convert_eltype(T, d), d.items))

todomainset(d::Intervals.IntervalSet) = UnionDomain(map(todomainset, d.items))

DomainSets.canonicaldomain(::DomainSets.Equal, d::Intervals.IntervalSet) =
todomainset(d)

DomainSets.canonicaldomain(d::Intervals.IntervalSet) =
canonicaldomain(todomainset(d))
DomainSets.mapfrom_canonical(d::Intervals.IntervalSet) =
DomainSets.mapfrom_canonical(todomainset(d))

end # module
10 changes: 8 additions & 2 deletions src/DomainSets.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module DomainSets

using DomainSetsCore

using StaticArrays
using LinearAlgebra, Statistics
import LinearAlgebra: cross, ×, pinv
Expand Down Expand Up @@ -33,8 +35,13 @@ import Base: *, +, -, /, \, ^,
# Display
show

# DomainSetsCore
import DomainSetsCore: domain, domaineltype

export DomainStyle, domaineltype

# IntervalSets
import IntervalSets: (..), endpoints, Domain, AbstractInterval, TypedEndpointsInterval,
import IntervalSets: (..), endpoints, AbstractInterval, TypedEndpointsInterval,
leftendpoint, rightendpoint, isleftopen, isleftclosed,
isrightopen, isrightclosed, isopenset, isclosedset,
infimum, supremum
Expand All @@ -43,7 +50,6 @@ export ..

import CompositeTypes: component, components


################################
## Exhaustive list of exports
################################
Expand Down
4 changes: 2 additions & 2 deletions src/domains/ball.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ isequaldomain(d1::Ball, d2::Ball) = isclosedset(d1)==isclosedset(d2) &&
radius(d1)==radius(d2) && center(d1)==center(d2)
hash(d::Ball, h::UInt) = hashrec("Ball", isclosedset(d), radius(d), center(d), h)

function issubset(d1::Ball, d2::Ball)
function issubset_domain(d1::Ball, d2::Ball)
if dimension(d1) == dimension(d2)
if center(d1) == center(d2)
if radius(d1) < radius(d2)
Expand Down Expand Up @@ -119,7 +119,7 @@ approx_indomain(x, d::ClosedUnitBall, tolerance) = norm(x) <= 1+tolerance
isequaldomain(d1::UnitBall, d2::UnitBall) = isclosedset(d1)==isclosedset(d2) &&
dimension(d1) == dimension(d2)

issubset(d1::UnitBall, d2::UnitBall) =
issubset_domain(d1::UnitBall, d2::UnitBall) =
dimension(d1) == dimension(d2) && (isclosedset(d2) || isopenset(d1))

convert(::Type{SublevelSet}, d::UnitBall{T,C}) where {T,C} =
Expand Down
34 changes: 18 additions & 16 deletions src/domains/boundingbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,54 @@ boundingbox(d::Vector{T}) where {T <: Number} = minimum(d)..maximum(d)
boundingbox(d::Set{T}) where {T<:Number} = minimum(d)..maximum(d)

"Return the bounding box of the union of two or more bounding boxes."
unionbox(d::Domain) = d
unionbox(d1::Domain, d2::Domain) = unionbox(promote_domains(d1, d2)...)
unionbox(d1::Domain, d2::Domain, domains...) =
unionbox(d) = d
unionbox(d1, d2) = unionbox1(promote_domains(d1, d2)...)
unionbox(d1, d2, domains...) =
unionbox(unionbox(d1,d2), domains...)

unionbox(d1::Domain{T}, d2::Domain{T}) where {T} = unionbox1(d1, d2)
unionbox1(d1, d2) = unionbox2(d1, d2)
unionbox2(d1, d2) = fullspace(d1)
unionbox1(d1::EmptySpace, d2) = d2
unionbox1(d1::FullSpace, d2) = d1
unionbox2(d1, d2::EmptySpace) = d1
unionbox2(d1, d2::FullSpace) = d2

unionbox(d1::D, d2::D) where {D<:FixedInterval} = d1
unionbox1(d1::D, d2::D) where {D<:FixedInterval} = d1

function unionbox(d1::AbstractInterval{T}, d2::AbstractInterval{T}) where {T}
function unionbox1(d1::AbstractInterval, d2::AbstractInterval)
a, b = endpoints(d1)
c, d = endpoints(d2)
A = min(a, c)
B = max(b, d)
isinf(A) && isinf(B) ? fullspace(T) : A..B
isinf(A) && isinf(B) ? fullspace(d1) : A..B
end

unionbox(d1::HyperRectangle{T}, d2::HyperRectangle{T}) where {T} =
function unionbox(d1::HyperRectangle, d2::HyperRectangle)
@assert dimension(d1) == dimension(d2)
T = promote_type(eltype(d1),eltype(d2))
Rectangle{T}(map(unionbox, components(d1), components(d2)))
end

"Return the bounding box of the intersection of two or more bounding boxes."
intersectbox(d::Domain) = d
intersectbox(d1::Domain, d2::Domain) = intersectbox(promote_domains(d1, d2)...)
intersectbox(d1::Domain, d2::Domain, domains...) =
intersectbox(d) = d
intersectbox(d1, d2) = intersectbox1(promote_domains(d1, d2)...)
intersectbox(d1, d2, domains...) =
intersectbox(intersectbox(d1,d2), domains...)

intersectbox(d1::Domain{T}, d2::Domain{T}) where {T} = intersectbox1(d1, d2)
intersectbox1(d1, d2) = intersectbox2(d1, d2)
intersectbox2(d1, d2) = fullspace(d1)
intersectbox1(d1::EmptySpace, d2) = d1
intersectbox1(d1::FullSpace, d2) = d2
intersectbox2(d1, d2::EmptySpace) = d2
intersectbox2(d1, d2::FullSpace) = d1

intersectbox(d1::D, d2::D) where {D<:FixedInterval} = d1
intersectbox1(d1::D, d2::D) where {D<:FixedInterval} = d1

intersectbox(d1::AbstractInterval{T}, d2::AbstractInterval{T}) where {T} =
intersectbox1(d1::AbstractInterval, d2::AbstractInterval) =
intersectdomain(d1, d2)

function intersectbox(d1::HyperRectangle{T}, d2::HyperRectangle{T}) where {T}
function intersectbox1(d1::HyperRectangle, d2::HyperRectangle)
@assert dimension(d1) == dimension(d2)
T = eltype(d1)
d = Rectangle{T}(map(intersectbox, components(d1), components(d2)))
isempty(d) ? emptyspace(d) : d
end
Expand Down
4 changes: 2 additions & 2 deletions src/domains/cube.jl
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ ProductDomain(domains::NTuple{N,D}) where {N,D <: FixedInterval} =
ProductDomain(domains::SVector{N,<:FixedInterval}) where {N} =
FixedIntervalProduct(domains)
ProductDomain{T}(domains::D...) where {N,S,T<:SVector{N,S},D <: FixedInterval} =
FixedIntervalProduct(convert.(Ref(Domain{S}), domains))
FixedIntervalProduct(convert_eltype.(Ref(S), domains))
ProductDomain{T}(domains::NTuple{N,D}) where {N,S,T<:SVector{N,S},D <: FixedInterval} =
FixedIntervalProduct(convert.(Ref(Domain{S}), domains))
FixedIntervalProduct(convert_eltype.(Ref(S), domains))
ProductDomain{T}(domains::SVector{N,<:FixedInterval}) where {N,T<:SVector{N}} =
FixedIntervalProduct(domains)
25 changes: 13 additions & 12 deletions src/domains/indicator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ implementing `in` directly.
abstract type AbstractIndicatorFunction{T} <: Domain{T} end

"The indicator function of a domain is the function `f(x) = x ∈ D`."
indicatorfunction(d::Domain) = x -> x ∈ d
indicatorfunction(d) = x -> x ∈ d

indomain(x, d::AbstractIndicatorFunction) = _indomain(x, d, indicatorfunction(d))
_indomain(x, d::AbstractIndicatorFunction, f) = f(x)
Expand All @@ -35,8 +35,8 @@ indicatorfunction(d::IndicatorFunction) = d.f
similardomain(d::IndicatorFunction, ::Type{T}) where {T} = IndicatorFunction{T}(d.f)

convert(::Type{IndicatorFunction}, d::AbstractIndicatorFunction) = d
convert(::Type{IndicatorFunction}, d::Domain{T}) where {T} =
IndicatorFunction{T}(indicatorfunction(d))
convert(::Type{IndicatorFunction}, d) =
IndicatorFunction{deltype(d)}(indicatorfunction(checkdomain(d)))

isequaldomain(d1::IndicatorFunction, d2::IndicatorFunction) =
indicatorfunction(d1)==indicatorfunction(d2)
Expand All @@ -45,18 +45,19 @@ intersectdomain1(d1::IndicatorFunction, d2) = BoundedIndicatorFunction(d1.f, d2)
intersectdomain2(d1, d2::IndicatorFunction) = BoundedIndicatorFunction(d2.f, d1)

"An indicator function with a known bounding domain."
struct BoundedIndicatorFunction{F,D,T} <: AbstractIndicatorFunction{T}
struct BoundedIndicatorFunction{T,F,D} <: AbstractIndicatorFunction{T}
f :: F
domain :: D
end

BoundedIndicatorFunction(f, domain::Domain{T}) where {T} =
BoundedIndicatorFunction{typeof(f),typeof(domain),T}(f, domain)

function BoundedIndicatorFunction(f, domain)
T = eltype(domain)
BoundedIndicatorFunction{typeof(f),typeof(domain),T}(f, domain)
end
BoundedIndicatorFunction(f, domain) =
BoundedIndicatorFunction{deltype(domain)}(f, domain)
BoundedIndicatorFunction{T}(f, domain::Domain{T}) where {T} =
BoundedIndicatorFunction{T,typeof(f),typeof(domain)}(f, domain)
BoundedIndicatorFunction{T}(f, domain) where {T} =
_BoundedIndicatorFunction(T, f, checkdomain(domain))
_BoundedIndicatorFunction(::Type{T}, f, domain) where {T} =
BoundedIndicatorFunction{T,typeof(f),typeof(domain)}(f, domain)

indicatorfunction(d::BoundedIndicatorFunction) = d.f

Expand All @@ -70,7 +71,7 @@ hash(d::BoundedIndicatorFunction, h::UInt) =
hashrec(indicatorfunction(d), boundingdomain(d), h)

similardomain(d::BoundedIndicatorFunction, ::Type{T}) where {T} =
BoundedIndicatorFunction(d.f, convert(Domain{T}, d.domain))
BoundedIndicatorFunction(d.f, convert_eltype(T, d.domain))

boundingbox(d::BoundedIndicatorFunction) = boundingbox(boundingdomain(d))

Expand Down
Loading