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 and test derivative(x, 2x) #1078

Closed
Show file tree
Hide file tree
Changes from 2 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
8 changes: 6 additions & 2 deletions src/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ julia> D3 = Differential(x)^3 # 3rd order differential operator
"""
struct Differential <: Operator
"""The variable or expression to differentiate with respect to."""
x
Differential(x) = new(value(x))
x::BasicSymbolic
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't limit it to BasicSymbolic

function Differential(x)
LilithHafner marked this conversation as resolved.
Show resolved Hide resolved
vx = value(x)
SymbolicUtils.exprtype(vx) == SymbolicUtils.SYM || throw(ArgumentError("Must differentiate with respect to a symbol, got $vx"))
new(vx)
end
end
function (D::Differential)(x)
x = unwrap(x)
Expand Down
10 changes: 10 additions & 0 deletions test/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,13 @@ let
@test isequal(expand_derivatives(Differential(t)(im*t)), im)
@test isequal(expand_derivatives(Differential(t)(t^2 + im*t)), 2t + im)
end

#1077
#
let
using Symbolics
@variables x
@test Symbolics.derivative(2x, x) == 2
@test_throws ArgumentError Symbolics.derivative(x, 2x)
@test_throws ArgumentError Symbolics.derivative(2x, 2x) # though arguably this should be 1
end
Loading