Skip to content

Output depends on values in cache #127

@bjack205

Description

@bjack205

I'm not sure if this is intended behavior, but the output of finite_difference_hessian! is dependent on the values stored in the cache, as illustrated below:

using FiniteDiff
using Random
Random.seed!(1)

# Simple function
f(x) = x[1]^2 + cos(x[2] + 2x[6]) + x[5]*x[4] + x[3]^2
H = zeros(6,6)
x = randn(6)
cache = FiniteDiff.HessianCache(copy(x))
FiniteDiff.finite_difference_hessian!(H,f,x,cache)
H[1,1]  # about 2.0 (correct)

# Change the input value drastically
x = randn(6) * 1e6
FiniteDiff.finite_difference_hessian!(H,f,x,cache)
H[1,1]  # usually 1.35e7 (far off)

# Set the cache to be equal to the current value
cache.xmm .= x
cache.xmp .= x
cache.xpm .= x
cache.xpp .= x
FiniteDiff.finite_difference_hessian!(H,f,x,cache)
H[1,1] # about 2.0 (correct)

If this is intended behavior, it's probably worth mentioning in the documentation somewhere, and let the user know they should reset the cache variables before calling finite_difference_hessian! for best performance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions