Skip to content

Add flexibility in dispatch for iszero_tuple - #667

Open
timholy wants to merge 2 commits into
JuliaDiff:masterfrom
timholy:teh/iszero_tuple
Open

Add flexibility in dispatch for iszero_tuple#667
timholy wants to merge 2 commits into
JuliaDiff:masterfrom
timholy:teh/iszero_tuple

Conversation

@timholy

@timholy timholy commented Sep 28, 2023

Copy link
Copy Markdown
Contributor

IntervalArithmetic.jl may abandon support for == among intervals (JuliaIntervals/IntervalArithmetic.jl#571). To support specialization for specific Number subtypes, this makes iszero_tuple into a "trait"-dispatched function, first unwrapping all the way down to the elementary numeric type and then jointly dispatching on that type and the actual tuple. This makes it possible to create an extension in IntervalArithmetic that specializes the implementation to use the new comparison operator . The use of recursive unwrapping enables support for higher-order derivatives.

This doesn't pass tests locally, but neither does master, and the failures look identical.

@codecov

codecov Bot commented Sep 28, 2023

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.71%. Comparing base (b742809) to head (63a008b).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #667      +/-   ##
==========================================
+ Coverage   90.68%   90.71%   +0.02%     
==========================================
  Files          11       11              
  Lines        1052     1055       +3     
==========================================
+ Hits          954      957       +3     
  Misses         98       98              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@OlivierHnt

OlivierHnt commented Jul 23, 2026

Copy link
Copy Markdown

Is there someone specific we can ping to revive this PR?
Context: the bottom line is that we want a small hook/helper function that specifically checks unambiguously whether a quantity is exactly zero (similarly to how it is in SparseArrays). I did not look at this PR specifically, but the changes should be very minimal.

EDIT: after looking at it, this PR should be updated to include other zero checks, such as

ForwardDiff.jl/src/dual.jl

Lines 566 to 575 in 090ddbb

begin
v = value(x)
expv = ($f)(v, y)
if y == zero(y) || iszero(partials(x))
new_partials = zero(partials(x))
else
new_partials = partials(x) * y * ($f)(v, y - 1)
end
return Dual{Tx}(expv, new_partials)
end,
.

@OlivierHnt

Copy link
Copy Markdown

Not sure who to ping about the internal of ForwardDiff..

@devmotion , @KristofferC , would you consider merging a PR creating a helper function _iszero(x) = iszero(x) that can be overloaded by other numerical types (in our case, iszero can return an error)?
Basically mimicking what was done in SparseArrays:
https://github.com/JuliaSparse/SparseArrays.jl/blob/ce9fe7818cdfa3e4391cc1cb2587a058dbdba1d8/src/SparseArrays.jl#L42-L50

Side note: maybe in this repo a _check_equality helper function will also be needed.

IntervalArithmetic.jl may abandon support for `==` among intervals
(JuliaIntervals/IntervalArithmetic.jl#571).
To support specialization for specific Number subtypes, this makes
`iszero_tuple` into a "trait"-dispatched function, first unwrapping
all the way down to the elementary numeric type and then jointly
dispatching on that type and the actual tuple. This makes it possible
to create an extension in IntervalArithmetic that specializes the
implementation to use the new comparison operator `≛`.
The use of recursive unwrapping enables support for higher-order
derivatives.
The test defines a scalar type whose `==` throws, so the tests pass
only if the `_iszero_tuple` specialization is reached, both directly
and through nested `Dual`s.

Assisted-by: Claude Opus 5 <noreply@anthropic.com>
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.

2 participants