Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

confused by this root finding example

Aperta
#595 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
32/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
julia
Ambito
tooling

Direzione di ricerca

Riprodurre i punti di ingresso MWE g1, g2 e ForwardDiff.derivative utilizzando le chiamate a Roots, ForwardDiff e FiniteDiff mostrate nell’issue. Confrontare il modo in cui i due limiti di find_zero influenzano la differenziazione e determinare se le derivate diverse sono previste; il lavoro è completo quando la causa è documentata o viene identificata una correzione mirata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I have some code which calls a root finding step, via the Roots package that I'd like to be able to differentiate. It appears that whether ForwardDiff "works" in this setting depends on some subtlety that I don't quite understand.

For an MWE, consider finding the (unique) root of the cubic function f(x, k) = (x-k)^3 which has a root at x = k. Let g1(k) = find_zero(x -> f(x, k), [k-1.0,k+1.0]) and similarly let g2(k) = find_zero(x -> f(x, k), [10.0,+10.0]). Note that for values of k between -10 to +10, g1(k) == g2(k). In this range, the only difference between g1 and g2 is that the root finding step in g1 is "aware" of the value of k to look for, via the bounds I have provided, whereas it is ignorant of them in g2. For values of k in this range, the derivatives of g1 and g2 are identical, and equal to 1.0. However, I am seeing that ForwardDiff thinks the derivative of g2 is zero, while it finds the derivative of g1 to be 1.0:

Using Roots, ForwardDiff, FiniteDiff
g0(x,k) = (x-k)^3
g1(k) = find_zero(x -> g0(x,k), [-10.0, +10.0])
g2(k) = find_zero(x -> g0(x,k), [-1.0 * k, +1.0 * k])

Julia reports that the functions are the same, and do vary with the input in the expected way:

ulia> g1(3)
3.0

julia> g2(3)
3.0

julia> g1(4)
4.0

julia> g2(4)
4.0

However, the ForwardDiff gradients are different:

julia> ForwardDiff.derivative(g1, 3.0)
0.0

julia> ForwardDiff.derivative(g2, 3.0)
1.0

Note that FiniteDiff works fine here (up to numerical approximation):

julia> FiniteDiff.finite_difference_derivative(g1, 3.0)
0.9999999999991082

julia> FiniteDiff.finite_difference_derivative(g2, 3.0)
0.9999999999991082

Thanks in advance for any suggestions the ForwardDiff team has in understanding this difference.

Lingua principale
Julia
Stelle
1k
Fork
162
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di JuliaDiff/ForwardDiff.jl

Tutte le issue di JuliaDiff/ForwardDiff.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.