JuliaIntervals / IntervalArithmetic.jl

Library for validated numerics using interval arithmetic
https://juliaintervals.github.io/IntervalArithmetic.jl/
Other
297 stars 71 forks source link

No mechanism to define functions restricted to a domain #654

Closed dpsanders closed 4 months ago

dpsanders commented 4 months ago

Suppose I want to define a function

f(x) = "sqrt(x) for x > 0,  and 0 for x < 0". 

I believe that currently we have no mechanism for a user to do this in terms of interval objects. (They would have to manually define it using inf, sup, if etc.)

A natural way would be to define Heaviside as in https://github.com/JuliaIntervals/IntervalArithmetic.jl/issues/653 and then

f(x) = Heaviside(x) * sqrt(x)

But this will give empty for x < 0, instead of 0, since sqrt(x) gives empty.

We either need a "strong 0" that gives 0 when multiplied by empty, or a general function

restrict_to_domain(f, D)

that does this in the interval context.

dpsanders commented 4 months ago

This can be implemented using piecewise from this comment:

julia> f(x) = piecewise((-Inf..0 => x -> 0..0, 0..Inf => x -> sqrt(x)), x)
f (generic function with 1 method)

julia> f(1..2)
[0.0, 1.41422]_trv

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

julia> f(-1..(-1))
[0.0, 0.0]_trv
dpsanders commented 4 months ago

Superseded by https://github.com/JuliaIntervals/IntervalArithmetic.jl/issues/655