Skip to content
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

support for clamp and min, max, clamp with ForwardDiff #642

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

smkatz12
Copy link

@smkatz12 smkatz12 commented May 8, 2024

Based on the discussion in #640, we (@mossr, @ancorso, @mykelk) have implemented the following:

  • Support for min and max when one input is an interval and the other is a float
  • Support for clamp
  • Support for min, max, and clamp when computing first and second derivatives with ForwardDiff
  • Additional tests for the new functionality

function Base.$f(x::BareInterval{T}, y::Rational) where {T<:NumTypes}
isempty_interval(x) && return x
return _unsafe_bareinterval(T, $f(inf(x), y), $f(sup(x), y))
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We purposely do not allow functions mixing BareInterval and Number in their computations, because BareInterval cannot keep track of guarantees ("NG" flag)or decorations.
So my first impulse here would be not to define such function.

Base.$f(x::BareInterval, y::BareInterval) = $f(promote(x, y)...)
Base.$f(x::BareInterval, y::Rational) = $f(promote(x), y)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

d = decoration(x)
return _unsafe_interval(r, d, false)
end
Base.$f(x::Rational, y::Interval) = $f(y, x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the latest version of IntervalArithmetic.jl, min and max already do this ; so defining these functions is maybe superfluous here ? Or am I missing something ?

@OlivierHnt
Copy link
Member

Thx for opening the PR!

I have to think a bit more about the decoration requirement. This may be a little more subtle. @Kolaru don't you think we will run into the same issue as we did with the derivative of abs ?

@Kolaru
Copy link
Collaborator

Kolaru commented May 21, 2024

In term of decorations, for consistency we must have:

  • trv when differentiated over the non-differentiable region (x == y for min and max, similar for clamp), for consistency with abs.
  • We need to decide what to do with clamp(x, lo, hi) when lo > hi. Base.clamp doesn't complain, but we probably want to return an empty interval.
  • NG when numbers and interval are mixed, for consistency with all other mixing operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants