forked from JuliaDiff/DifferentiationInterface.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDifferentiationInterfaceSymbolicsExt.jl
More file actions
53 lines (45 loc) · 1.38 KB
/
DifferentiationInterfaceSymbolicsExt.jl
File metadata and controls
53 lines (45 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module DifferentiationInterfaceSymbolicsExt
using ADTypes: ADTypes, AutoSymbolics, AutoSparse
import DifferentiationInterface as DI
using LinearAlgebra: dot
using Symbolics:
build_function,
derivative,
gradient,
hessian,
hessian_sparsity,
jacobian,
jacobian_sparsity,
sparsehessian,
sparsejacobian,
substitute,
variable,
variables
using Symbolics.RuntimeGeneratedFunctions: RuntimeGeneratedFunction
DI.check_available(::AutoSymbolics) = true
DI.pullback_performance(::AutoSymbolics) = DI.PullbackSlow()
dense_ad(backend::AutoSymbolics) = backend
dense_ad(backend::AutoSparse{<:AutoSymbolics}) = ADTypes.dense_ad(backend)
variablize(::Number, name::Symbol) = variable(name)
variablize(x::AbstractArray, name::Symbol) = variables(name, axes(x)...)
variablize(::T, name::Symbol) where {T} = variable(name; T)
function variablize(contexts::NTuple{C, DI.Context}) where {C}
return ntuple(Val(C)) do k
c = contexts[k]
variablize(DI.unwrap(c), Symbol("context$k"))
end
end
function erase_cache_vars!(
context_vars::NTuple{C}, contexts::NTuple{C, DI.Context}
) where {C}
# erase the active data from caches before building function
for (v, c) in zip(context_vars, contexts)
if c isa DI.Cache
fill!(v, zero(eltype(v)))
end
end
return
end
include("onearg.jl")
include("twoarg.jl")
end