Skip to content

Commit

Permalink
Renaming of functions (#572)
Browse files Browse the repository at this point in the history
* Rework multidim and set/boolean functions

* Change function names

* Fix tests

* Change `interior` back to `isinterior`

* Rename `strictless` back to `isstrictless`

* Add explicit error messages for boolean functions

* Change "weakly" to "weak" in function names

* Add error message for `issetequal`

* More renaming

* `isequalinterval(::Interval, ::Real)` -> `isthin`

* Minor cleanup

* Fix doctests

* Renaming again

* `setdiffinterval` -> `setdiff_interval`

* `precedes_interval` -> `precedes`

* Throw errors for set operations
  • Loading branch information
OlivierHnt authored Oct 7, 2023
1 parent 4757c9e commit b0d9686
Show file tree
Hide file tree
Showing 64 changed files with 9,326 additions and 9,331 deletions.
4 changes: 3 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Documenter, IntervalArithmetic

makedocs(
DocMeta.setdocmeta!(IntervalArithmetic, :DocTestSetup, :(using IntervalArithmetic))

makedocs(;
modules = [IntervalArithmetic],
authors = "David P. Sanders and Luis Benet",
sitename = "IntervalArithmetic.jl",
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ If you use the IntervalArithmetic library in your publication, research, teachin
This project began in 2014 during a masters' course in the postgraduate programs of Mathematics and Physics at the Universidad Nacional Autónoma de México. It was initially written in Python, then reinitiated in 2015 and rewritten in Julia. We thank the participants of the courses for their contribution, energy and ideas.


### Support
## Support

Financial support is acknowledged from DGAPA-UNAM PAPIME grants PE-105911 and PE-107114, and DGAPA-UNAM PAPIIT grant IN-117214.

Expand Down
51 changes: 9 additions & 42 deletions docs/src/manual/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,48 +93,15 @@ sin(Y)



## Multi-dimensional intervals
## Comparisons and set operations

Multi-dimensional (hyper-)boxes are implemented in the
`IntervalBox` type.
These represent Cartesian products of intervals, i.e. rectangles (in 2D),
cuboids (in 3D), etc.
All comparisons and set operations for `Real` have been purposely disallowed to prevent silent errors.

`IntervalBox`es are constructed from an array of `Interval`s; it is
often convenient to use the `..` notation:

```
julia> using IntervalArithmetic
julia> X = IntervalBox(1..3, 2..4)
[1, 3] × [2, 4]
julia> Y = IntervalBox(2.1..2.9, 3.1..4.9)
[2.09999, 2.90001] × [3.09999, 4.90001]
```

Several operations are defined on `IntervalBox`es, for example:

```
julia> X ∩ Y
[2.09999, 2.90001] × [3.09999, 4]
julia> X ⊆ Y
false
```

Given a multi-dimensional function taking several inputs, and interval box can be constructed as follows:

```
julia> f(x, y) = (x + y, x - y)
f (generic function with 1 method)
julia> X = IntervalBox(1..1, 2..2)
[1, 1] × [2, 2]
julia> f(X...)
([3, 3], [-1, -1])
julia> IntervalBox(f(X...))
[3, 3] × [-1, -1]
```@repl usage
interval(1) < interval(2)
precedes(interval(1), interval(2))
issubset(interval(1, 2), interval(2))
issubset_interval(interval(1, 2), interval(2))
intersect(interval(1, 2), interval(2))
intersect_interval(interval(1, 2), interval(2))
```
59 changes: 30 additions & 29 deletions src/IntervalArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,62 @@ import Base:
rad2deg, deg2rad,
sin, cos, tan, cot, csc, sec, asin, acos, atan, acot, sinpi, cospi, sincospi,
sinh, cosh, tanh, coth, csch, sech, asinh, acosh, atanh, acoth,
in, union, intersect, issubset, isempty, isdisjoint,
convert,
BigFloat, float, big,
float, big,
floor, ceil, trunc, sign, round, copysign, flipsign, signbit,
expm1, log1p,
precision,
isfinite, isinteger, isnan, isinf, iszero,
abs, abs2,
show,
parse, hash

import Base.MPFR: MPFRRoundingMode
import Base.MPFR: MPFRRoundUp, MPFRRoundDown, MPFRRoundNearest, MPFRRoundToZero, MPFRRoundFromZero

export
Interval, interval, ±, @I_str,
diam, radius, mid, scaled_mid, mag, mig, hull,
emptyinterval, isempty, isinterior,
precedes, strictprecedes, , , , contains_zero, isthinzero,
isweaklyless, isstrictless, overlap, Overlap,
, setdiffinterval,
entireinterval, isentire, nai, isnai, isthin, iscommon, isatomic,
inf, sup, bounds, bisect, mince,
dist,
midpoint_radius,
RoundTiesToEven, RoundTiesToAway,
IntervalRounding,
cancelminus, cancelplus, isbounded, isunbounded,
pow, extended_div, nthroot,
setformat

## Decorations
export
decoration, DecoratedInterval,
com, dac, def, trv, ill

function __init__()
setrounding(BigFloat, RoundNearest)
end

function Base.setrounding(f::Function, ::Type{Rational{T}},
rounding_mode::RoundingMode) where T
function Base.setrounding(f::Function, ::Type{Rational{T}}, rounding_mode::RoundingMode) where {T<:Integer}
return setrounding(f, float(Rational{T}), rounding_mode)
end

## Includes
#

include("intervals/intervals.jl")
export Interval, interval, ±, @I_str,
diam, radius, mid, midradius, scaled_mid, mag, mig, hull,
emptyinterval, isempty_interval,
isequal_interval,
in_interval,
issubset_interval, isstrictsubset_interval,
precedes, strictprecedes,
isweakless, isstrictless,
contains_zero,
isthinzero, isthin,
isbounded, isunbounded,
isdisjoint_interval, intersect_interval, setdiff_interval,
entireinterval, isentire_interval, nai, isnai, iscommon, isatomic,
inf, sup, bounds, mince,
dist,
RoundTiesToEven, RoundTiesToAway,
IntervalRounding,
cancelminus, cancelplus,
pow, extended_div, nthroot,
overlap, Overlap

include("bisect.jl")
export bisect

include("decorations/decorations.jl")
export decoration, DecoratedInterval, com, dac, def, trv, ill

include("rand.jl")

include("parsing.jl")

include("display.jl")
export setformat

include("symbols.jl")

end
4 changes: 1 addition & 3 deletions src/bisect.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const where_bisect = 0.49609375

"""
bisect(X::Interval, α=0.49609375)
Split the interval `X` at position α; α=0.5 corresponds to the midpoint.
Returns a tuple of the new intervals.
"""
function bisect(X::Interval{T}, α=where_bisect) where {T<:NumTypes}
function bisect(X::Interval{T}, α=0.49609375) where {T<:NumTypes}
@assert 0 α 1

m = scaled_mid(X, α)
Expand Down
1 change: 0 additions & 1 deletion src/decorations/decorations.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
include("intervals.jl")
include("functions.jl")

isnan(::Interval) = false # NaI is always decorated

"""`NaI` not-an-interval: [NaN, NaN]."""
nai(::Type{Interval{T}}) where {T<:NumTypes} = nai(T)
Expand Down
49 changes: 21 additions & 28 deletions src/decorations/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ one(::Type{DecoratedInterval{T}}) where {T<:NumTypes} = DecoratedInterval(one(T)
## Bool functions

Base.:(==)(::DecoratedInterval, ::DecoratedInterval) =
throw(ArgumentError("== is purposely not supported, use instead"))
throw(ArgumentError("`==` is purposely not supported, use `isequal_interval` instead"))

const bool_functions = (
:isempty, :isentire, :isunbounded,
:isfinite, :isnan,
:isempty_interval, :isentire_interval, :isunbounded, :isbounded,
:isthin, :iscommon
)

const bool_binary_functions = (
:issubset,
:isinterior, :isdisjoint, :precedes, :strictprecedes, :isstrictless, :isweaklyless,
:, :overlap
:issubset_interval, :isstrictsubset_interval, :isdisjoint_interval,
:precedes, :strictprecedes, :isweakless, :isstrictless,
:isequal_interval, :overlap
)

for f in bool_functions
Expand All @@ -37,16 +36,16 @@ for f in bool_binary_functions
end
end

in(x::T, a::DecoratedInterval) where T<:Real = in(x, interval(a))
in_interval(x::Real, a::DecoratedInterval) = in_interval(x, interval(a))


## scalar functions: mig, mag and friends
scalar_functions = (
:mig, :mag, :inf, :sup, :mid, :diam, :radius, :eps, :midpoint_radius
:mig, :mag, :inf, :sup, :mid, :diam, :radius, :eps, :midradius
)

for f in scalar_functions
@eval $(f)(xx::DecoratedInterval{T}) where T = $f(interval(xx))
@eval $(f)(xx::DecoratedInterval)= $f(interval(xx))
end

dist(xx::DecoratedInterval, yy::DecoratedInterval) = dist(interval(xx), interval(yy))
Expand All @@ -72,7 +71,7 @@ end
function inv(xx::DecoratedInterval{T}) where T
x = interval(xx)
dx = decoration(xx)
dx = zero(T) x ? min(dx,trv) : dx
dx = in_interval(zero(T), x) ? min(dx, trv) : dx
r = inv(x)
dx = min(decoration(r), dx)
DecoratedInterval( r, dx )
Expand All @@ -84,7 +83,7 @@ function /(xx::DecoratedInterval{T}, yy::DecoratedInterval{T}) where T
y = interval(yy)
r = x / y
dy = decoration(yy)
dy = zero(T) y ? min(dy, trv) : dy
dy = in_interval(zero(T), y) ? min(dy, trv) : dy
dy = min(decoration(xx), dy, decoration(r))
DecoratedInterval(r, dy)
end
Expand All @@ -103,7 +102,7 @@ function ^(xx::DecoratedInterval{T}, n::Integer) where T
x = interval(xx)
r = x^n
d = min(decoration(xx), decoration(r))
n < 0 && zero(T) x && return DecoratedInterval(r, trv)
n < 0 && in_interval(zero(T), x) && return DecoratedInterval(r, trv)
DecoratedInterval(r, d)
end

Expand All @@ -112,7 +111,7 @@ function ^(xx::DecoratedInterval{T}, q::AbstractFloat) where T
r = x^q
d = min(decoration(xx), decoration(r))
if inf(x) > zero(T) || (inf(x) zero(T) && q > zero(T)) ||
(isinteger(q) && q > zero(q)) || (isinteger(q) && zero(T) x)
(isinteger(q) && q > zero(q)) || (isinteger(q) && !in_interval(zero(T), x))
return DecoratedInterval(r, d)
end
DecoratedInterval(r, trv)
Expand All @@ -123,7 +122,7 @@ function ^(xx::DecoratedInterval{T}, q::Rational{S}) where {T, S<:Integer}
r = x^q
d = min(decoration(xx), decoration(r))
if inf(x) > zero(T) || (inf(x) zero(T) && q > zero(T)) ||
(isinteger(q) && q > zero(q)) || (isinteger(q) && zero(T) x)
(isinteger(q) && q > zero(q)) || (isinteger(q) && !in_interval(zero(T), x))
return DecoratedInterval(r, d)
end
DecoratedInterval(r, trv)
Expand All @@ -136,7 +135,7 @@ function ^(xx::DecoratedInterval{T}, qq::DecoratedInterval{S}) where {T,S}
d = min(decoration(xx), decoration(qq), decoration(r))
if inf(x) > zero(T) || (inf(x) zero(T) && inf(q) > zero(T)) ||
(isthin(q) && isinteger(inf(q)) && inf(q) > zero(T)) ||
(isthin(q) && isinteger(inf(q)) && zero(T) x)
(isthin(q) && isinteger(inf(q)) && !in_interval(zero(T), x))
return DecoratedInterval(r, d)
end
DecoratedInterval(r, trv)
Expand Down Expand Up @@ -226,7 +225,7 @@ abs(xx::DecoratedInterval{T}) where T =


## Other (cancel and set) functions
other_functions = ( :cancelplus, :cancelminus, :intersect, :hull, :union )
other_functions = ( :cancelplus, :cancelminus, :intersect_interval, :hull )

for f in other_functions
@eval $(f)(xx::DecoratedInterval{T}, yy::DecoratedInterval{T}) where T =
Expand All @@ -248,11 +247,11 @@ following the IEEE-1788 Standard (see Sect. 11.7.1, pp 47).
""" cancelminus

@doc """
intersect(xx, yy)
intersect_interval(xx, yy)
Decorated interval extension; the result is decorated as `trv`,
following the IEEE-1788 Standard (see Sect. 11.7.1, pp 47).
""" intersect
""" intersect_interval

@doc """
hull(xx, yy)
Expand All @@ -261,12 +260,6 @@ Decorated interval extension; the result is decorated as `trv`,
following the IEEE-1788 Standard (see Sect. 11.7.1, pp 47).
""" hull

@doc """
union(xx, yy)
Decorated interval extension; the result is decorated as `trv`,
following the IEEE-1788 Standard (see Sect. 11.7.1, pp 47).
""" union


## Functions on unrestricted domains; tan and atan are treated separately
Expand Down Expand Up @@ -311,8 +304,8 @@ function atan(yy::DecoratedInterval{T}, xx::DecoratedInterval{T}) where T
d = decoration(r)
d = min(d, decoration(xx), decoration(yy))
# Check cases when decoration is trv and decays (from com or dac)
if zero(T) y
zero(T) x && return DecoratedInterval(r, trv)
if in_interval(zero(T), y)
in_interval(zero(T), x) && return DecoratedInterval(r, trv)
if sup(x) < zero(T)
inf(y) < zero(T) && return DecoratedInterval(r, min(d, def))
return DecoratedInterval(r, min(d, dac))
Expand Down Expand Up @@ -347,7 +340,7 @@ for (f, domain) in restricted_functions1
x = interval(xx)
r = $(f)(x)
d = min(decoration(xx), decoration(r))
isinterior(x, $(domain)) && return DecoratedInterval(r, d)
isstrictsubset_interval(x, $domain) && return DecoratedInterval(r, d)
DecoratedInterval(r, trv)
end
end
Expand All @@ -357,7 +350,7 @@ for (f, domain) in restricted_functions2
x = interval(xx)
r = $(f)(x)
d = min(decoration(xx), decoration(r))
x $(domain) && return DecoratedInterval(r, d)
issubset_interval(x, $domain) && return DecoratedInterval(r, d)
DecoratedInterval(r, trv)
end
end
4 changes: 2 additions & 2 deletions src/decorations/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ follows Section 11.2 of the IEEE Standard 1788-2015:
- `ill -> 0`: not an interval (ill-formed)
"""
@enum DECORATION ill=0 trv=1 def=2 dac=3 com=4
# Note that `isweaklyless`, and hence `<` and `min`, are automatically defined for enums
# Note that `isweakless`, and hence `<` and `min`, are automatically defined for enums

"""
DecoratedInterval{T<:NumTypes}
Expand Down Expand Up @@ -68,7 +68,7 @@ decoration(x::DecoratedInterval) = x.decoration

function decoration(x::Interval)
isnai(x) && return ill # nai()
isempty(x) && return trv # emptyinterval
isempty_interval(x) && return trv # emptyinterval
isunbounded(x) && return dac # unbounded
return com # common
end
Expand Down
6 changes: 3 additions & 3 deletions src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ end
# `String` representation of an `Interval`

function basic_representation(a::Interval{T}, format::Symbol) where {T<:AbstractFloat}
isempty(a) && return ""
isempty_interval(a) && return ""
sigdigits = display_params.sigdigits
if format === :full
# Do not use `inf(a)` to avoid -0.0
Expand All @@ -150,7 +150,7 @@ function basic_representation(a::Interval{T}, format::Symbol) where {T<:Abstract
end

function basic_representation(a::Interval{Float32}, format::Symbol)
isempty(a) && return ""
isempty_interval(a) && return ""
sigdigits = display_params.sigdigits
if format === :full
# Do not use `inf(a)` to avoid -0.0
Expand All @@ -171,7 +171,7 @@ function basic_representation(a::Interval{Float32}, format::Symbol)
end

function basic_representation(a::Interval{T}, format::Symbol) where {T<:Rational}
isempty(a) && return ""
isempty_interval(a) && return ""
if format === :full
# Do not use `inf(a)` to avoid -0.0
return string("Interval{", T, "}(", a.lo, ", ", sup(a), ")")
Expand Down
Loading

0 comments on commit b0d9686

Please sign in to comment.