Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

confused by this root finding example

未關閉
#595 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
32/100
Issue 類型
缺陷
描述清晰度
需要釐清
活躍度
停滯
技術堆疊
julia
領域
tooling

研究方向

使用 issue 中所示的 Roots、ForwardDiff 與 FiniteDiff 呼叫,重現 MWE 進入點 g1、g2 與 ForwardDiff.derivative。比較兩個 find_zero 界限如何影響微分,並判定不同的導數是否符合預期;完成標準是記錄原因或找出一項針對性的修正。

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
Julia
星號
1k
分支
162
PR 合併指標
30 天內沒有已合併 PR

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

JuliaDiff/ForwardDiff.jl 的其他 Issue

查看 JuliaDiff/ForwardDiff.jl 的全部 Issue

相似的 Issue

更多 Julia Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。