Skip to content

Commit c892c22

Browse files
feat: allow non-numeric arguments with AutoSymbolics (#945)
* feat: allow non-numeric arguments with `AutoSymbolics` * Replace dummy --------- Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com>
1 parent 0dd1abf commit c892c22

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/DifferentiationInterfaceSymbolicsExt.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dense_ad(backend::AutoSparse{<:AutoSymbolics}) = ADTypes.dense_ad(backend)
2626

2727
variablize(::Number, name::Symbol) = variable(name)
2828
variablize(x::AbstractArray, name::Symbol) = variables(name, axes(x)...)
29+
variablize(::T, name::Symbol) where {T} = variable(name; T)
2930

3031
function variablize(contexts::NTuple{C, DI.Context}) where {C}
3132
return ntuple(Val(C)) do k

DifferentiationInterface/test/Back/Symbolics/test.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,23 @@ test_differentiation(
4141
@test sparsity_pattern(jac!_prep) == Diagonal(trues(10))
4242
@test sparsity_pattern(hess_prep) == Diagonal(trues(10))
4343
end
44+
45+
@testset "Non-numeric arguments" begin
46+
function differentiate_me!(out, x, c)
47+
@assert c isa Any # Just to use it somewhere
48+
return copyto!(out, x)
49+
end
50+
function differentiate_me(x, c)
51+
tmp = similar(x)
52+
differentiate_me!(tmp, x, c)
53+
return tmp
54+
end
55+
x = rand(10)
56+
xbuffer = copy(x)
57+
c = "I am a string"
58+
backend = AutoSymbolics()
59+
jac_prep = prepare_jacobian(differentiate_me, backend, x, Constant(c))
60+
jac!_prep = prepare_jacobian(differentiate_me!, xbuffer, backend, x, Constant(c))
61+
@test jacobian(differentiate_me, jac_prep, backend, x, Constant(c)) I
62+
@test jacobian(differentiate_me!, xbuffer, jac!_prep, backend, x, Constant(c)) I
63+
end

0 commit comments

Comments
 (0)