jacobian(f, x) silently returns zeros if f is complex-valued
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
首先阅读 extract_jacobian!、extract_jacobian_chunk!、extract_derivative 和 valtype,重点关注 Complex{Dual} 值的处理方式。使用 issue 中的复现器测试 vector mode、chunk mode 和 jacobian!。完成的标准是:复值函数生成正确的 ComplexF64 Jacobian,同时实值路径保持不变。
由索引模型根据 Issue 内容生成。
描述
jacobian returns zeros, with Duals leaked into the eltype, instead of the Jacobian or an error:
julia> g(x) = [cis(x[1])*x[2], x[1] + 2im*x[2]];
julia> J = ForwardDiff.jacobian(g, [0.5, 1.0]);
julia> eltype(J)
Complex{ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Float64}, Float64, 2}}
julia> all(iszero, J)
true
extract_jacobian! extracts with partials(T, ydual, i), which for a Complex{Dual} hits the partials(x, i...) = zero(x) fallback. derivative(f, x) is fine since it goes through extract_derivative, which handles Complex{<:Dual}.
extract_derivative cannot be reused directly because it hardcodes the partial index, while the Jacobian needs index i. But generalising it to an index is enough, and keeps the knowledge of how a complex number carries derivative information in one place:
@inline extract_partial(::Type{T}, y::Real, i) where {T} = zero(y)
@inline extract_partial(::Type{T}, y::Complex, i) where {T} = zero(y)
@inline extract_partial(::Type{T}, y::Dual, i) where {T} = partials(T, y, i)
@inline extract_partial(::Type{T}, y::Complex{<:Dual}, i) where {T} =
complex(partials(T, real(y), i), partials(T, imag(y), i))
extract_jacobian!/extract_jacobian_chunk! then use extract_partial(T, ·, i) instead of partials(T, ·, i), and the scalar extract_derivative methods become the i = 1 case.
One more piece is needed for the result eltype, since jacobian allocates similar(ydual, valtype(T, eltype(ydual)), ...):
@inline valtype(::Type{T}, ::Type{Complex{D}}) where {T,D} = Complex{valtype(T, D)}
With those, vector mode, chunk mode and jacobian! all give the correct ComplexF64 Jacobian and the real paths are unchanged.
If we would rather not support complex-valued f here, it should at least throw rather than return zeros.
- 主要语言
- Julia
- 星标
- 1k
- 派生
- 165
- 平均合并
- 8 天 22 小时
- 30 天内合并 PR
- 3
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
JuliaDiff/ForwardDiff.jl 的其他 Issue
-
missing floatmin, floatmax, and issubnormal for Dual可能已有人在做 @devmotion 于 35 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
JuliaDiff/ForwardDiff.jl#852 · 6 条评论 ·
维护者通常 1 天内回复
-
`min` does not return the minimum with respect to `isless` when values tie可能已有人在做 @devmotion 于 9 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
JuliaDiff/ForwardDiff.jl#868 ·
维护者通常 1 天内回复
-
Enable NaN-safe mode by default可能已有人在做 @devmotion 于 23 天前认领。 未关闭
难度 5/5 一周以上 新手友好度 25/100
JuliaDiff/ForwardDiff.jl#857 · 1 个 reaction ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 52/100
JuliaDiff/ForwardDiff.jl#854 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 75/100
JuliaDiff/ForwardDiff.jl#849 ·
维护者通常 1 天内回复
查看 JuliaDiff/ForwardDiff.jl 的全部 Issue
相似的 Issue
-
release preparation spack
难度 2/5 1-3 小时 新手友好度 67/100
维护者通常 1 天内回复
-
30-minutes algorithm any-language codewars dart easy elixir hackerrank hacktoberfest haskell help wanted interview-prep julia leetcode lua open-source r scala students zig
难度 2/5 1-3 小时 新手友好度 68/100
Huerte/algo-solutions-python#163 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
QuantumSavory/QuantumClifford.jl#853 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
JuliaPackaging/Yggdrasil#15441 · 1 条评论 ·
维护者通常 1 天内回复
-
bug
难度 1/5 1 小时以内 新手友好度 78/100
JuliaSmoothOptimizers/Penelopt.jl#260 ·
维护者通常 1 天内回复