Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrease allocations #60

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/grad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,40 @@ Approximate the gradient of `f` at `xs...` using `fdm`. Assumes that `f(xs...)`
"""
function grad end

function grad(fdm, f, x::AbstractArray{T}) where T <: Number
function _grad(fdm, f, x::AbstractArray{T}) where T <: Number
# x must be mutable, we will mutate it and then mutate it back.
dx = similar(x)
tmp = similar(x)
for k in eachindex(x)
dx[k] = fdm(zero(T)) do ϵ
tmp .= x
tmp[k] += ϵ
return f(tmp)
xk = x[k]
x[k] = xk + ϵ
ret = f(x)
x[k] = xk # Can't do `x[k] -= ϵ` as floating-point math is not associative
return ret::T
end
end
return (dx, )
end

grad(fdm, f, x::Array{<:Number}) = _grad(fdm, f, x)
# Fallback for when we don't know `x` will be mutable:
grad(fdm, f, x::AbstractArray{<:Number}) = _grad(fdm, f, similar(x).=x)

grad(fdm, f, x::Real) = (fdm(f, x), )
grad(fdm, f, x::Tuple) = (grad(fdm, (xs...)->f(xs), x...), )

function grad(fdm, f, d::Dict{K, V}) where {K, V}
dd = Dict{K, V}()
∇d = Dict{K, V}()
for (k, v) in d
dk = d[k]
function f′(x)
tmp = copy(d)
tmp[k] = x
return f(tmp)
d[k] = x
return f(d)
end
dd[k] = grad(fdm, f′, v)[1]
∇d[k] = grad(fdm, f′, v)[1]
d[k] = dk
end
return (dd, )
return (∇d, )
end

function grad(fdm, f, x)
Expand Down
35 changes: 20 additions & 15 deletions src/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ The recognized keywords are:
* `bound`: Bound on the value of the function and its derivatives at `x`.
* `condition`: The condition number. See [`DEFAULT_CONDITION`](@ref).
* `eps`: The assumed roundoff error. Defaults to `eps()` plus [`TINY`](@ref).
* `track_history`: wether to update the history of the method `m` with e.g. accuracy stats.
oxinabox marked this conversation as resolved.
Show resolved Hide resolved

!!! warning
Bounds can't be adaptively computed over nonstandard grids; passing a value for
Expand Down Expand Up @@ -211,14 +212,15 @@ julia> fdm(central_fdm(2, 1), exp, 0, Val(true))
function fdm(
m::M,
f,
x,
x::T,
::Val{true};
condition=DEFAULT_CONDITION,
bound=_estimate_bound(f(x), condition),
eps=(Base.eps(float(bound)) + TINY),
bound::T=_estimate_bound(f(x)::T, condition)::T,
eps::T=(Base.eps(float(bound)) + TINY)::T,
adapt=m.history.adapt,
max_step=0.1,
) where M<:FiniteDifferenceMethod
track_history=true # will be set to false if `Val{false}()` used
)::Tuple{M, T} where {M<:FiniteDifferenceMethod, T}
if M <: Nonstandard && adapt > 0
throw(ArgumentError("can't adaptively compute bounds over Nonstandard grids"))
end
Expand Down Expand Up @@ -256,22 +258,25 @@ function fdm(
C₂ = bound * sum(n->abs(coefs[n] * grid[n]^p), eachindex(coefs)) / factorial(p)
ĥ = min((q / (p - q) * C₁ / C₂)^(1 / p), max_step)

# Estimate the accuracy of the method.
accuracy = ĥ^(-q) * C₁ + ĥ^(p - q) * C₂

# Estimate the value of the derivative.
dfdx = sum(i->coefs[i] * f(x + ĥ * grid[i]), eachindex(grid)) / ĥ^q

m.history.eps = eps
m.history.bound = bound
m.history.step = ĥ
m.history.accuracy = accuracy

dfdx_s = sum(eachindex(grid)) do i
(@inbounds coefs[i] * f(x + ĥ * grid[i]))::T
end
dfdx = dfdx_s / ĥ^q

if track_history
# Estimate the accuracy of the method.
accuracy = ĥ^(-q) * C₁ + ĥ^(p - q) * C₂
m.history.eps = eps
m.history.bound = bound
m.history.step = ĥ
m.history.accuracy = accuracy
end
return m, dfdx
end

function fdm(m::FiniteDifferenceMethod, f, x, ::Val{false}=Val(false); kwargs...)
_, dfdx = fdm(m, f, x, Val(true); kwargs...)
_, dfdx = fdm(m, f, x, Val{true}(); track_history=false, kwargs...)
return dfdx
end

Expand Down