Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name = "CTBase"
uuid = "54762871-cc72-4466-b8e8-f6c8b58076cd"
authors = ["Olivier Cots <[email protected]>"]
version = "0.9.0"
version = "0.9.1"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
Expand All @@ -21,6 +22,7 @@ Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[compat]
DataStructures = "0.18"
DifferentiationInterface = "0.5"
DocStringExtensions = "0.9"
ForwardDiff = "0.10"
Interpolations = "0.15"
Expand Down
5 changes: 4 additions & 1 deletion src/CTBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module CTBase
# using
import Base
using DocStringExtensions
using ForwardDiff: jacobian, gradient, ForwardDiff # automatic differentiation
using DifferentiationInterface: AutoForwardDiff, derivative, gradient, jacobian, prepare_derivative, prepare_gradient, prepare_jacobian
import ForwardDiff
using Interpolations: linear_interpolation, Line, Interpolations # for default interpolation
using MLStyle # pattern matching
using Parameters # @with_kw: to have default values in struct
Expand Down Expand Up @@ -94,6 +95,8 @@ Type alias for a tangent vector to the costate space.
"""
const DCostate = ctVector

__auto() = AutoForwardDiff()

#
include("exception.jl")
include("description.jl")
Expand Down
22 changes: 17 additions & 5 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ $(TYPEDSIGNATURES)
Return the gradient of `f` at `x`.
"""
function ctgradient(f::Function, x::ctNumber)
return ForwardDiff.derivative(x -> f(x), x)
backend = __auto()
extras = prepare_derivative(f, backend, x)
return derivative(f, backend, x, extras)
end

"""
Expand All @@ -83,7 +85,9 @@ $(TYPEDSIGNATURES)
Return the gradient of `f` at `x`.
"""
function ctgradient(f::Function, x)
return ForwardDiff.gradient(f, x)
backend = __auto()
extras = prepare_gradient(f, backend, x)
return gradient(f, backend, x, extras)
end

"""
Expand All @@ -98,16 +102,24 @@ $(TYPEDSIGNATURES)

Return the Jacobian of `f` at `x`.
"""
function ctjacobian(f::Function, x::ctNumber)
return ForwardDiff.jacobian(x -> f(x[1]), [x])
function ctjacobian(f::Function, x::ctNumber)
f_number_to_number = only ∘ f ∘ only
backend = __auto()
extras = prepare_derivative(f_number_to_number, backend, x)
der = derivative(f_number_to_number, backend, x, extras)
return [der;;]
end

"""
$(TYPEDSIGNATURES)

Return the Jacobian of `f` at `x`.
"""
ctjacobian(f::Function, x) = ForwardDiff.jacobian(f, x)
function ctjacobian(f::Function, x)
backend = __auto()
extras = prepare_jacobian(f, backend, x)
return jacobian(f, backend, x, extras)
end

"""
$(TYPEDSIGNATURES)
Expand Down