-
Notifications
You must be signed in to change notification settings - Fork 17
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
Interval arithmetic through functions with branches #191
Comments
Essentially you have to create a method of Something like this could be helpful, which uses your definition of function f(x::Interval)
x.lo > 2 && return Interval(f(x.lo), f(x.hi))
x.hi ≤ 2 && return Interval(f(x.hi), f(x.lo))
return Interval( f(2), max(f(x.lo),f(x.hi)))
end Now, using your examples: julia> f(0..1)
[-2, -0]
julia> f(3..4)
[6, 8]
julia> f(0..1)
[-2, -0]
julia> f(1..3)
[-4, 6]
julia> f(2..2)
[-4, -4] For this kind of things, it is worth taking a look on how |
It would be nice, but seems hard, to find a way to automatize the generation of the interval version with the correct behaviour. |
If you write the function as
then it has the correct behaviour, i.e. gives an inclusion of the result: julia> f(x) = 2x * sign(x - 2)
f (generic function with 1 method)
julia> f(3..4)
[6, 8]
julia> f(-1..1)
[-2, 2]
julia> f(-1)
2
julia> f(1)
-2
julia> f(-1..4)
[-8, 8] |
You can also use decorated functions. (@rdeits: Decorations are flags that give information about what happened during a calculation.) julia> displaymode(decorations=true)
julia> y = @decorated(3..4)
[3, 4]_com
julia> sign(y)
[1, 1]_com
julia> f(y)
[6, 8]_com The julia> f(@decorated(3..4))
[6, 8]_com
julia> f(@decorated(-1..1))
[-2, 2]_com
julia> f(@decorated(-1..3))
[-6, 6]_def The |
So in principle it may be possible to write a macro to rewrite simple |
Note that |
(attempting to summarize our discussion from today).
The particular issue that I was referring to is this:
Suppose we have a function
f(x)
:We can evaluate this on an interval:
This gives a correct answer as long as the interval does not include the value 2. When the interval spans 2, we get the wrong answer:
That's because
1..3 > 2
just evaluates tofalse
, so the code path which is taken is the one in whichf(x) = -2x
across the entire interval.Given that I just learned about this entire field today, I don't really think I can suggest a "correct" behavior, but this seems like a case where it would be easy for a naive user like myself to get a wrong answer without realizing it.
The text was updated successfully, but these errors were encountered: