IntervalArithmetic.jl icon indicating copy to clipboard operation
IntervalArithmetic.jl copied to clipboard

Powers of rational intervals

Open schillic opened this issue 2 years ago • 1 comments

Is the following expected?

julia> using IntervalArithmetic

julia> a = interval(-1, 1)
[-1, 1]

julia> a^2  # looks good
[0, 1]

julia> a = interval(-1//1, 1//1)
[-1//1, 1//1]

julia> a^2  # expected: [0//1, 1//1]
[-1//1, 1//1]
``

schillic avatar Apr 05 '22 09:04 schillic

The reason for this seems to be that if picks some fallback case from base (which uses power by squaring)

julia> a = interval(-1//1, 1//1)
[-1//1, 1//1]

julia> a ^ 2
[-1//1, 1//1]

julia> @which a ^ 2
^(x::Number, p::Integer) in Base at intfuncs.jl:291

julia> b = interval(-1, 1)
[-1, 1]

julia> b ^ 2
[0, 1]

julia> @which b ^ 2
^(a::Interval{Float64}, x::Integer) in IntervalArithmetic at /home/lferrant/.julia/packages/IntervalArithmetic/997vH/src/intervals/functions.jl:10

I guess this could be fixed by updating the type signature

lucaferranti avatar Apr 05 '22 09:04 lucaferranti

Has there been any update on this? I am hitting this issue as well.

agerlach avatar Dec 19 '22 16:12 agerlach

@schillic @agerlach I do not think anyone got around to fix this. In fact, there are some discussion about restricting the bounds of Interval to AbstractFloat (see e.g. #495). It is still unclear whether having Interval{Rational{Int}} is useful or not (since it also introduces some issues). So out of curiosity, what are you using Interval{Rational{Int}} for? Why not using Interval{Float64} for instance? 🙂

OlivierHnt avatar Feb 08 '23 07:02 OlivierHnt

I guess rational intervals can be useful for symbolical computations. Sure they are restricted to only rational operations, but that is probably enough for several applications (think e.g. matrix power).

Side note, we could always define

const FloatOrRat = Union{AbstractFloat, Rational}

and have

struct Interval{T <: FloatOrRat} <: Real # to reflect how it is now, does not imply I agree it should be suptype of Real :P
  low::T
  high::T
end

this would allow to keep rational and floats and avoid stack overflow issues we had in the past

lucaferranti avatar Feb 08 '23 08:02 lucaferranti

I am not really using it. We just test our functionality also with Rationals and so I stumbled upon this issue. In any case, the behavior seems wrong to me, so it should either be fixed or warned about in the documentation.

schillic avatar Feb 08 '23 09:02 schillic

I was using it to validate a manually derived interval remainder for a Taylor model. I was getting much wider intervals numerically and wanted to test with rationals to quantify the effects due to round-off vs truncation.

agerlach avatar Feb 08 '23 13:02 agerlach

So out of curiosity, what are you using Interval{Rational{Int}} for? Why not using Interval{Float64} for instance?

Interval{Rational{Int}} are indeed very limited. Yet, within those limitations, they essentially avoid issues with rounding (for rational polynomials with rational coefficients). At least for the time being, I'm in favor of having them around.

lbenet avatar Feb 08 '23 14:02 lbenet

That definitely sounds useful! Thanks for chiming in.

Side note, we could always define

const FloatOrRat = Union{AbstractFloat, Rational}

Seems like a good idea.

I am not really using it. We just test our functionality also with Rationals and so I stumbled upon this issue. In any case, the behavior seems wrong to me, so it should either be fixed or warned about in the documentation.

You are absolutely right. Side note: ^(a::Interval{Rational{Int64}}, x::Int64) and ^(a::Interval{Rational{Int64}}, x::Float64) are currently broken on the 1.0-dev branch.

OlivierHnt avatar Feb 08 '23 18:02 OlivierHnt