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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ SafeTestsets = "0.1"
SparseConnectivityTracer = "1"
StableRNGs = "1"
StaticArrays = "1.9"
Symbolics = "6.46"
Symbolics = "6.46, 7"
Test = "1.10"
Unitful = "1.21.1"
Zygote = "0.6.77, 0.7"
Expand Down
41 changes: 32 additions & 9 deletions ext/DataInterpolationsSymbolicsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,40 @@ using Symbolics: Num, unwrap, SymbolicUtils
@register_symbolic (interp::AbstractInterpolation)(t)
Base.nameof(interp::AbstractInterpolation) = :Interpolation

function derivative(interp::AbstractInterpolation, t::Num, order = 1)
Symbolics.wrap(SymbolicUtils.term(derivative, interp, unwrap(t), order))
end
SymbolicUtils.promote_symtype(::typeof(derivative), _...) = Real
@static if pkgversion(Symbolics) >= v"7"
@register_symbolic derivative(interp::AbstractInterpolation, t, order::Integer) false
function SymbolicUtils.promote_symtype(::typeof(derivative), Ti::SymbolicUtils.TypeT,
Tt::SymbolicUtils.TypeT,
To::SymbolicUtils.TypeT)
@assert Ti <: AbstractInterpolation
@assert Tt <: Real
@assert To <: Integer
Real
end
function SymbolicUtils.promote_shape(::typeof(derivative),
@nospecialize(shi::SymbolicUtils.ShapeT),
@nospecialize(sht::SymbolicUtils.ShapeT),
@nospecialize(sho::SymbolicUtils.ShapeT))
@assert !SymbolicUtils.is_array_shape(shi)
@assert !SymbolicUtils.is_array_shape(sht)
@assert !SymbolicUtils.is_array_shape(sho)
return SymbolicUtils.ShapeVecT()
end

function Symbolics.derivative(::typeof(derivative), args::NTuple{3, Any}, ::Val{2})
Symbolics.unwrap(derivative(args[1], Symbolics.wrap(args[2]), args[3] + 1))
end
@register_derivative derivative(interp, t, ord) 2 derivative(interp, t, ord + 1)
@register_derivative (interp::AbstractInterpolation)(t) 1 derivative(interp, t, 1)
else
function derivative(interp::AbstractInterpolation, t::Num, order = 1)
Symbolics.wrap(SymbolicUtils.term(derivative, interp, unwrap(t), order))
end
SymbolicUtils.promote_symtype(::typeof(derivative), _...) = Real
function Symbolics.derivative(::typeof(derivative), args::NTuple{3, Any}, ::Val{2})
Symbolics.unwrap(derivative(args[1], Symbolics.wrap(args[2]), args[3] + 1))
end

function Symbolics.derivative(interp::AbstractInterpolation, args::NTuple{1, Any}, ::Val{1})
Symbolics.unwrap(derivative(interp, Symbolics.wrap(args[1])))
function Symbolics.derivative(interp::AbstractInterpolation, args::NTuple{1, Any}, ::Val{1})
Symbolics.unwrap(derivative(interp, Symbolics.wrap(args[1])))
end
end

end # module
4 changes: 2 additions & 2 deletions test/derivative_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ end
expr = A(ω)
@test isequal(Symbolics.derivative(expr, τ), D(ω) * DataInterpolations.derivative(A, ω))

derivexpr1 = expand_derivatives(substitute(D(A(ω)), Dict(ω => 0.5τ)))
derivexpr2 = expand_derivatives(substitute(D2(A(ω)), Dict(ω => 0.5τ)))
derivexpr1 = expand_derivatives(substitute(D(A(ω)), Dict(ω => 0.5τ); filterer = Returns(true)))
derivexpr2 = expand_derivatives(substitute(D2(A(ω)), Dict(ω => 0.5τ); filterer = Returns(true)))
symfunc1 = Symbolics.build_function(derivexpr1, τ; expression = Val{false})
symfunc2 = Symbolics.build_function(derivexpr2, τ; expression = Val{false})
@test symfunc1(0.5) == 1.5
Expand Down
6 changes: 3 additions & 3 deletions test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ end
@variables t x(t)
substitute(A(t), Dict(t => x))
t_val = 2.7
@test substitute(A(t), Dict(t => t_val)) == A(t_val)
@test substitute(B(A(t)), Dict(t => t_val)) == B(A(t_val))
@test substitute(A(B(A(t))), Dict(t => t_val)) == A(B(A(t_val)))
@test substitute(A(t), Dict(t => t_val); fold = Val(true)) == A(t_val)
@test substitute(B(A(t)), Dict(t => t_val); fold = Val(true)) == B(A(t_val))
@test substitute(A(B(A(t))), Dict(t => t_val); fold = Val(true)) == A(B(A(t_val)))
end

@testset "Type Inference" begin
Expand Down
Loading