diff --git a/src/diff.jl b/src/diff.jl index 81ac4e9e2..f9b82b463 100644 --- a/src/diff.jl +++ b/src/diff.jl @@ -32,7 +32,11 @@ 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)) + function Differential(x) + vx = value(x) + istree(vx) && throw(ArgumentError("Cannot differentiate with respect to $vx")) + new(vx) + end end function (D::Differential)(x) x = unwrap(x) diff --git a/test/diff.jl b/test/diff.jl index 7486a78e7..d78b1712a 100644 --- a/test/diff.jl +++ b/test/diff.jl @@ -348,3 +348,14 @@ 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 + @test_throws ArgumentError Symbolics.derivative(x, 0) +end