Skip to content

Commit

Permalink
Merge pull request #1256 from kapple19/#1085
Browse files Browse the repository at this point in the history
Fix false positive result of passing a `Function` to `derivative`
  • Loading branch information
ChrisRackauckas authored Sep 4, 2024
2 parents b75c55c + 6fcede0 commit 69cf971
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ derivative(::typeof(+), args::NTuple{N,Any}, ::Val) where {N} = 1
derivative(::typeof(*), args::NTuple{N,Any}, ::Val{i}) where {N,i} = *(deleteat!(collect(args), i)...)
derivative(::typeof(one), args::Tuple{<:Any}, ::Val) = 0

derivative(f::Function, x::Num) = derivative(f(x), x)
derivative(::Function, x::Any) = TypeError(:derivative, "2nd argument", Num, typeof(x)) |> throw

function count_order(x)
@assert !(x isa Symbol) "The variable $x must have an order of differentiation that is greater or equal to 1!"
n = 1
Expand Down
21 changes: 20 additions & 1 deletion test/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,23 @@ let
Dt = Differential(t)^0
@test isequal(Dt, identity)
test_equal(Dt(t + 2t^2), t + 2t^2)
end
end

# Check `Function` inputs for derivative (#1085)
let
@variables x
@testset for f in [sqrt, sin, acos, exp, cis]
@test isequal(
Symbolics.derivative(f, x),
Symbolics.derivative(f(x), x)
)
end
end

# Check `Function` inputs throw for non-Num second input (#1085)
let
@testset for f in [sqrt, sin, acos, exp, cis]
@test_throws TypeError Symbolics.derivative(f, rand())
@test_throws TypeError Symbolics.derivative(f, Val(rand(Int)))
end
end

0 comments on commit 69cf971

Please sign in to comment.