Piecewise functions
Opened this issue · 5 comments
dpsanders commented
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]
.
dpsanders commented
Ah that's because
julia> zero(emptyinterval())
[0.0, 0.0]_com
I have an idea for how to make a default 0 value.
dpsanders commented
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)
dpsanders commented
It would probably be worth having a PiecewiseConstant
too.
dpsanders commented
We also need to add the decoration of course.
OlivierHnt commented