Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive result of passing a Function to derivative #1256

Merged
merged 4 commits into from
Sep 4, 2024
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
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
Loading