JuliaDiff / JuliaDiff/FiniteDifferences.jl

NaNs inputs to function when using one-sided differences

Open
#239 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
318
Forks
32
PR merge metrics
No merged PRs in 30d

Description

Adapted one-sided differences gives a NaN input while receiving finite values from the function.

(jl_yIAkgW) pkg> st
Status `/tmp/jl_yIAkgW/Project.toml`
  [26cc04aa] FiniteDifferences v0.12.32 `https://github.com/JuliaDiff/FiniteDifferences.jl.git#main`

julia> using FiniteDifferences

julia> function f(x::Real)
       A = L = 1.0
       x2 = abs2(x)
       @show x, x2
       @assert x2 ≤ 1
       if x2 == 1
       y = oftype(A + L + x, Inf)
       x < 0 ? -y : y
       else
       A + L * x / √(1 - x2)
       end
       end
f (generic function with 1 method)

julia> forward_fdm(5, 1)(f, -1.0)
(x, x2) = (-1.0, 1.0)
(x, x2) = (-0.9959053413840389, 0.9918274489972589)
(x, x2) = (-0.9918106827680776, 0.9836884304528802)
(x, x2) = (-0.9877160241521165, 0.9755829443668643)
(x, x2) = (-0.9836213655361552, 0.9675109907392107)
(x, x2) = (-0.9795267069201941, 0.9594725695699198)
(x, x2) = (-0.9754320483042329, 0.9514676808589914)
(x, x2) = (NaN, NaN)
ERROR: AssertionError: x2 ≤ 1
Stacktrace:
  [1] f(x::Float64)
    @ Main ./REPL[10]:5
  [2] newf
    @ ~/.julia/packages/StaticArrays/85pEu/src/broadcast.jl:186 [inlined]
  [3] macro expansion
    @ ~/.julia/packages/StaticArrays/85pEu/src/broadcast.jl:135 [inlined]
  [4] __broadcast
    @ ~/.julia/packages/StaticArrays/85pEu/src/broadcast.jl:123 [inlined]
  [5] _broadcast
    @ ~/.julia/packages/StaticArrays/85pEu/src/broadcast.jl:119 [inlined]
  [6] copy
    @ ~/.julia/packages/StaticArrays/85pEu/src/broadcast.jl:60 [inlined]
  [7] materialize
    @ ./broadcast.jl:903 [inlined]
  [8] _eval_function(m::FiniteDifferences.AdaptedFiniteDifferenceMethod{…}, f::typeof(f), x::Float64, step::Float64)
    @ FiniteDifferences ~/.julia/packages/FiniteDifferences/IPGFN/src/methods.jl:249
  [9] AdaptedFiniteDifferenceMethod
    @ ~/.julia/packages/FiniteDifferences/IPGFN/src/methods.jl:240 [inlined]
 [10] (::FiniteDifferences.AdaptedFiniteDifferenceMethod{…})(f::typeof(f), x::Float64)
    @ FiniteDifferences ~/.julia/packages/FiniteDifferences/IPGFN/src/methods.jl:194
 [11] top-level scope
    @ REPL[11]:1
Some type information was truncated. Use `show(err)` to see complete types.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the forward_fdm(5, 1)(f, -1.0) reproduction and inspect _eval_function and AdaptedFiniteDifferenceMethod in src/methods.jl around lines 194, 240, and 249. Trace how one-sided difference inputs are generated and determine the expected behavior for finite function values, then add a regression test covering the reported NaN input.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.