From d6c58e7a7dceded81eccf840f7102ab1aaae8040 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Mon, 26 Feb 2024 16:08:15 -0600 Subject: [PATCH 1/6] fix and test derivative(x, 2x) --- src/diff.jl | 2 +- test/diff.jl | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/diff.jl b/src/diff.jl index 81ac4e9e2..be2aabad8 100644 --- a/src/diff.jl +++ b/src/diff.jl @@ -31,7 +31,7 @@ julia> D3 = Differential(x)^3 # 3rd order differential operator """ struct Differential <: Operator """The variable or expression to differentiate with respect to.""" - x + x::BasicSymbolic Differential(x) = new(value(x)) end function (D::Differential)(x) diff --git a/test/diff.jl b/test/diff.jl index 7486a78e7..be1ab261d 100644 --- a/test/diff.jl +++ b/test/diff.jl @@ -348,3 +348,12 @@ 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 MethodError Symbolics.derivative(x, 2x) +end From 8842aa63913316bf4c459bf8f6342e1acde522dc Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Tue, 27 Feb 2024 07:59:02 -0600 Subject: [PATCH 2/6] fixup: check exprtype --- src/diff.jl | 6 +++++- test/diff.jl | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/diff.jl b/src/diff.jl index be2aabad8..5671e11d1 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::BasicSymbolic - Differential(x) = new(value(x)) + function Differential(x) + 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) diff --git a/test/diff.jl b/test/diff.jl index be1ab261d..b4ef1a9ef 100644 --- a/test/diff.jl +++ b/test/diff.jl @@ -355,5 +355,6 @@ let using Symbolics @variables x @test Symbolics.derivative(2x, x) == 2 - @test_throws MethodError Symbolics.derivative(x, 2x) + @test_throws ArgumentError Symbolics.derivative(x, 2x) + @test_throws ArgumentError Symbolics.derivative(2x, 2x) # though arguably this should be 1 end From 81d67e432b5b0a3be19c666d46ade38378eee448 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Tue, 27 Feb 2024 14:19:14 +0000 Subject: [PATCH 3/6] Earlier error message --- src/diff.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diff.jl b/src/diff.jl index 5671e11d1..8e627b56c 100644 --- a/src/diff.jl +++ b/src/diff.jl @@ -32,7 +32,7 @@ julia> D3 = Differential(x)^3 # 3rd order differential operator struct Differential <: Operator """The variable or expression to differentiate with respect to.""" x::BasicSymbolic - function Differential(x) + function Differential(x::BasicSymbolic) vx = value(x) SymbolicUtils.exprtype(vx) == SymbolicUtils.SYM || throw(ArgumentError("Must differentiate with respect to a symbol, got $vx")) new(vx) From 34c84e641b9fae7ebbd3a183933782ba7cb30712 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Tue, 27 Feb 2024 14:23:07 +0000 Subject: [PATCH 4/6] fixup --- src/diff.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/diff.jl b/src/diff.jl index 8e627b56c..f05460fea 100644 --- a/src/diff.jl +++ b/src/diff.jl @@ -32,9 +32,10 @@ julia> D3 = Differential(x)^3 # 3rd order differential operator struct Differential <: Operator """The variable or expression to differentiate with respect to.""" x::BasicSymbolic - function Differential(x::BasicSymbolic) + function Differential(x) vx = value(x) - SymbolicUtils.exprtype(vx) == SymbolicUtils.SYM || throw(ArgumentError("Must differentiate with respect to a symbol, got $vx")) + vx isa BasicSymbolic && SymbolicUtils.exprtype(vx) == SymbolicUtils.SYM || + throw(ArgumentError("Must differentiate with respect to a symbol, got $vx")) new(vx) end end From ec67b9e7e3bcf7fdd088bc0ff397ed01b2495f73 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Tue, 27 Feb 2024 08:23:52 -0600 Subject: [PATCH 5/6] add more tests --- test/diff.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/diff.jl b/test/diff.jl index b4ef1a9ef..d78b1712a 100644 --- a/test/diff.jl +++ b/test/diff.jl @@ -357,4 +357,5 @@ let @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 From a7c9375139dce8b1cf924e32fe2c130e19f1fe14 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Tue, 12 Mar 2024 14:52:14 +0000 Subject: [PATCH 6/6] Implement @YingboMa's suggestions --- src/diff.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/diff.jl b/src/diff.jl index f05460fea..f9b82b463 100644 --- a/src/diff.jl +++ b/src/diff.jl @@ -31,11 +31,10 @@ julia> D3 = Differential(x)^3 # 3rd order differential operator """ struct Differential <: Operator """The variable or expression to differentiate with respect to.""" - x::BasicSymbolic + x function Differential(x) vx = value(x) - vx isa BasicSymbolic && SymbolicUtils.exprtype(vx) == SymbolicUtils.SYM || - throw(ArgumentError("Must differentiate with respect to a symbol, got $vx")) + istree(vx) && throw(ArgumentError("Cannot differentiate with respect to $vx")) new(vx) end end