JuliaIntervals/IntervalArithmetic.jl

Piecewise functions

Opened this issue · 5 comments

Here's a possible implementation for piecewise functions:

using IntervalArithmetic
using IntervalArithmetic.Symbols # to use the constructor `..`

# represents a piecewise 1D function
struct Piecewise{T <: Tuple}
    pieces::T 
end

Piecewise(pairs...) = Piecewise(pairs)

function (piecewise::Piecewise)(X::Interval)
    return reduce(hull, f(intersect_interval(X, region)) for (region, f)  piecewise.pieces)
end

f = Piecewise(
        0..3 => x -> x + interval(1),
        3..6 => identity,
        6..Inf => x -> x + interval(2)
)

f(2..5)

Then Heaviside should be definable with

H = Piecewise(-Inf..0 => zero, 0..Inf => one)

but that is not working for me right now 🤔 E.g. it gives

julia> H(1..1)
[0.0, 1.0]_trv

whereas it should give [1, 1].

Ah that's because

julia> zero(emptyinterval())
[0.0, 0.0]_com

I have an idea for how to make a default 0 value.

This version works:

function (piecewise::Piecewise)(X::Interval)
    return reduce(hull, begin
                            val = intersect_interval(X, region)
                            isempty_interval(val) ? val : f(val)
                        end
                    for (region, f)  piecewise.pieces)
end

H = Piecewise(-Inf..0 => zero, 0..Inf => one)

It would probably be worth having a PiecewiseConstant too.

We also need to add the decoration of course.

This issue seems to supersede #653 and #654 no?

Also, with such a feature, implementing the Heaviside function would be even simpler than it already is so I am not sure it would warrant being defined inside the library.