Error differentiating through complex power due to isinteger
还没有人认领这个 Issue。
评估
调研方向
首先使用 ForwardDiff.Dual 值复现所提供的 MWE,并阅读 Julia 的 complex.jl 中 _cpow 的实现,尤其是 isinteger 检查以及随后转换为 Int 的过程。确定应如何处理 Dual 的偏导数,并验证复数幂的求导能够在不出现所报告的 InexactError 的情况下完成。
由索引模型根据 Issue 内容生成。
描述
I have a weird case where a complex power is giving me an inexact error because the _cpow helper function determines my object to be an integer.
Truncated error message:
julia> ForwardDiff.gradient(test_func, ptest)
ERROR: InexactError: Int(Int64, Dual{ForwardDiff.Tag{typeof(test_func), Float64}}(1.0,-0.0,-0.8333333333333333,-0.0,-0.0,-0.17599538919114094))
Stacktrace:
[1] Int64
@ ~/.julia/packages/ForwardDiff/QdStj/src/dual.jl:364 [inlined]
[2] convert(#unused#::Type{Int64}, x::ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_func), Float64}, Float64, 5})
@ Base ./number.jl:7
[3] _cpow(z::Complex{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_func), Float64}, Float64, 5}}, p::ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_func), Float64}, Float64, 5})
@ Base ./complex.jl:782
[4] ^
@ ./complex.jl:851 [inlined]
[5] ^
@ ./complex.jl:866 [inlined]
So given that
julia> typeof(test_func)
typeof(test_func) (singleton type of function test_func, subtype of Function)
It seems that we can get the following MWE:
julia> isinteger(Dual{ForwardDiff.Tag{Function, Float64}}(1.0,-0.0,-0.8333333333333333,-0.0,-0.0,-0.17599538919114094))
true
This is the associated culprit line in complex.jl that tries to do this conversion to Int (only the relevant part is shown):
# _cpow helper function to avoid method ambiguity with ^(::Complex,::Real)
function _cpow(z::Union{T,Complex{T}}, p::Union{T,Complex{T}}) where T
z = float(z)
p = float(p)
Tf = float(T)
if isreal(p)
pᵣ = real(p)
if isinteger(pᵣ) && abs(pᵣ) < typemax(Int32)
# |p| < typemax(Int32) serves two purposes: it prevents overflow
# when converting p to Int, and it also turns out to be roughly
# the crossover point for exp(p*log(z)) or similar to be faster.
if iszero(pᵣ) # fix signs of imaginary part for z^0
zer = flipsign(copysign(zero(Tf),pᵣ), imag(z))
return Complex(one(Tf), zer)
end
ip = convert(Int, pᵣ) # <--- HERE!
Admittedly, I am not sure if the partials should be considered for determining if it's an integer or not.
- 主要语言
- Julia
- 星标
- 1k
- 派生
- 162
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
JuliaDiff/ForwardDiff.jl 的其他 Issue
-
missing floatmin, floatmax, and issubnormal for Dual可能已有人在做 @devmotion 于 18 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
JuliaDiff/ForwardDiff.jl#852 · 6 条评论 ·
-
`fma` and `muladd` ignore NaN-safe mode可能已有人在做 @devmotion 于 4 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
JuliaDiff/ForwardDiff.jl#860 ·
-
Enable NaN-safe mode by default可能已有人在做 @devmotion 于 6 天前认领。 未关闭
难度 5/5 一周以上 新手友好度 25/100
JuliaDiff/ForwardDiff.jl#857 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 52/100
JuliaDiff/ForwardDiff.jl#854 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 75/100
JuliaDiff/ForwardDiff.jl#849 ·
查看 JuliaDiff/ForwardDiff.jl 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
WaterLily-jl/WaterLily.jl#367 ·
维护者通常 1 天内回复
-
R_to_gamma and R_to_v are wrong for |Z| != 1 (e.g. heavy ions)可能已有人在做 @DavidSagan 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
bmad-sim/BeamTracking.jl#366 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 74/100
SciML/PETScDiffEq.jl#277 ·
维护者通常 1 天内回复
-
enhancement good first issue help wanted
难度 2/5 半天 新手友好度 82/100
-
SavedSubsystem with an integer save_idxs maps the index the wrong way round可能已有人在做 @ChrisRackauckas 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
SciML/SciMLBase.jl#1643 ·
维护者通常 2 天内回复