Skip to content

Make isless, <, >, <=, and >= consistent with == and isequal - #695

Merged
devmotion merged 2 commits into
masterfrom
dw/isless
Jul 28, 2024
Merged

devmotion merged 2 commits into
masterfrom
dw/isless

Conversation

@devmotion

Copy link
Copy Markdown
Member

Currently, the inequality operators for Duals are inconsistent with the definitions of == and isequal. In particular, isless violates the first requirement listed in its docstring:

If `isless(x, y)` is defined, then so is `isless(y, x)` and `isequal(x, y)`, and exactly one of those three yields true.

The PR fixes these inconsistencies.

@mcabbott

mcabbott commented Jun 15, 2024 •

Copy link
Copy Markdown
Member

To summarise what this does, tagged version is like so:

julia> using ForwardDiff: Dual

julia> pr(x, op, y) = println("x ", op, " y = ", op(x,y));

julia> pr.(Dual(1,1), (>,>=,==,<=,<), 1);  # like pr.(1.0, (>,>=,==,<=,<), 1.0);
x > y = false
x >= y = true
x == y = true
x <= y = true
x < y = false

(@v1.12) pkg> st ForwardDiff
Status `~/.julia/environments/v1.12/Project.toml`
  [f6369f11] ForwardDiff v0.10.36

master has instead this:

julia> pr.(Dual(1,1), (>,>=,==,<=,<), 1);
x > y = false
x >= y = true
x == y = false
x <= y = true
x < y = false

(jl_I2PrM4) pkg> st ForwardDiff
Status `/private/var/folders/yq/4p2zwd614y59gszh7y9ypyhh0000gn/T/jl_I2PrM4/Project.toml`
  [f6369f11] ForwardDiff v0.11.0-DEV `~/.julia/dev/ForwardDiff`

With this PR,

julia> pr.(Dual(1,1), (>,>=,==,<=,<), 1);  # like pr.(1.01, (>,>=,==,<=,<), 1.0);
x > y = true
x >= y = true
x == y = false
x <= y = false
x < y = false

julia> pr.(Dual(1,-1), (>,>=,==,<=,<), 1);  # like pr.(1-0.01, (>,>=,==,<=,<), 1.0);
x > y = false
x >= y = false
x == y = false
x <= y = true
x < y = true

(jl_FcBNXv) pkg> st ForwardDiff
Status `/private/var/folders/yq/4p2zwd614y59gszh7y9ypyhh0000gn/T/jl_FcBNXv/Project.toml`
  [f6369f11] ForwardDiff v0.11.0-DEV `https://github.com/JuliaDiff/ForwardDiff.jl#dw/isless`

A similar change was proposed in #377, and also discussed in #609. Probably this PR closes both of those.

@devmotion
devmotion merged commit a286920 into master Jul 28, 2024
@KristofferC
KristofferC deleted the dw/isless branch July 29, 2024 10:11
andreasnoack pushed a commit that referenced this pull request Nov 13, 2026
`min`/`max` were generated from DiffRules, which choose the partials by
value only. On ties in value the result depended on the argument order
rather than `isless`, which breaks ties on the partials since #695.

Fixes #868.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
andreasnoack pushed a commit that referenced this pull request Nov 13, 2026
`min`/`max` were generated from DiffRules, which choose the partials by
value only. On ties in value the result depended on the argument order
rather than `isless`, which breaks ties on the partials since #695.

Fixes #868.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
devmotion added a commit that referenced this pull request Nov 13, 2026
* Make `min`/`max` on `Dual` consistent with `isless`

`min`/`max` were generated from DiffRules, which choose the partials by
value only. On ties in value the result depended on the argument order
rather than `isless`, which breaks ties on the partials since #695.

Fixes #868.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Test nested derivatives of `min`/`max` at ties

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Bump version from 1.4.7 to 1.4.8

* Remove `min`/`max` rules

* Revert "Remove `min`/`max` rules"

This reverts commit 6c93fd3.

Without these methods, `min`/`max` on `Dual` fall back to Base's
`min(x::T, y::T) where {T<:Real}`, which uses `<` instead of `isless`
and returns the first argument whenever a NaN is involved.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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