Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Derivative issue with DiffResults and nested arrays

未关闭
#725 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
julia
领域
devtools

调研方向

复现 issue 中的两个示例,并比较 DiffResults.jl:167、apiutils.jl:5 和 derivative.jl:49 中的 stack trace 条目。检查 value! 和 extract_value! 如何处理嵌套数组,然后确认 derivative! 对 g(x) 执行完成时不会出现报告的转换错误。

由索引模型根据 Issue 内容生成。

描述

The following code works:

using ForwardDiff

g(x) = [[x^2, sqrt(x)]]
println(ForwardDiff.derivative(g, 2))

But this code:

using DiffResults
using ForwardDiff

g(x) = [[x^2, sqrt(x)]]
result = DiffResults.DiffResult(similar(g(2)), similar(g(2)))
result = ForwardDiff.derivative!(result, g, 2)
println(result)

Leads to a runtime error:

ERROR: LoadError: MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Int64}, Float64, 1})

Closest candidates are:
  (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat
   @ Base rounding.jl:207
  (::Type{T})(::T) where T<:Number
   @ Core boot.jl:792
  Float64(::IrrationalConstants.Fourπ)
   @ IrrationalConstants C:\Users\bruno\.julia\packages\IrrationalConstants\vp5v4\src\macro.jl:112
  ...

Stacktrace:
  [1] convert(::Type{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Int64}, Float64, 1})
    @ Base .\number.jl:7
  [2] setindex!(A::Vector{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Int64}, Float64, 1}, i1::Int64)
    @ Base .\array.jl:1021
  [3] _unsafe_copyto!(dest::Vector{Float64}, doffs::Int64, src::Vector{ForwardDiff.Dual{…}}, soffs::Int64, n::Int64)
    @ Base .\array.jl:299
  [4] unsafe_copyto!
    @ .\array.jl:353 [inlined]
  [5] _copyto_impl!
    @ .\array.jl:376 [inlined]
  [6] copyto!
    @ .\array.jl:363 [inlined]
  [7] copyto!
    @ .\array.jl:385 [inlined]
  [8] copyto_axcheck!
    @ .\abstractarray.jl:1177 [inlined]
  [9] Vector{Float64}(x::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Int64}, Float64, 1}})
    @ Base .\array.jl:673
 [10] convert
    @ .\array.jl:665 [inlined]
 [11] setindex!
    @ .\array.jl:1021 [inlined]
 [12] map!
    @ .\abstractarray.jl:3279 [inlined]
 [13] value!
    @ C:\Users\bruno\.julia\packages\DiffResults\YLo25\src\DiffResults.jl:167 [inlined]
 [14] extract_value!
    @ C:\Users\bruno\.julia\packages\ForwardDiff\UBbGT\src\apiutils.jl:5 [inlined]
 [15] derivative!(result::DiffResults.MutableDiffResult{1, Vector{Vector{…}}, Tuple{Vector{…}}}, f::typeof(g), x::Int64)
    @ ForwardDiff C:\Users\bruno\.julia\packages\ForwardDiff\UBbGT\src\derivative.jl:49

Adding the following line fixes this problem:

ForwardDiff.value(::Type{T}, x::AbstractArray) where T = map(d -> ForwardDiff.value(T, d), x)
主要语言
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 摘要。