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

Create ZeroOperator and make D(const) a hard zero #1279

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions src/diff.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
abstract type Operator end
propagate_shape(::Operator, x) = axes(x)

struct ZeroOperator <: Operator
end
(::ZeroOperator)(x) = wrap(0)
Base.show(io::IO, D::Differential) = print(io, "ZeroOperator")
Base.nameof(D::ZeroOperator) = :ZeroOperator
Base.:*(D1, D2::ZeroOperator) = ZeroOperator()
Base.:*(D1::ZeroOperator, D2) = ZeroOperator()
Base.:*(D1::ZeroOperator, D2::ZeroOperator) = ZeroOperator()
Base.:^(D::ZeroOperator, n::Integer) = ZeroOperator()

"""
$(TYPEDEF)

Expand Down Expand Up @@ -33,6 +43,7 @@ struct Differential <: Operator
"""The variable or expression to differentiate with respect to."""
x
Differential(x) = new(value(x))
Differential(x::Union{AbstractFloat, Integer}) = ZeroOperator()
end
function (D::Differential)(x)
x = unwrap(x)
Expand All @@ -42,6 +53,8 @@ function (D::Differential)(x)
term(D, x)
end
end

(D::Differential)(x::Union{AbstractFloat, Integers}) = wrap(0)
(D::Differential)(x::Union{Num, Arr}) = wrap(D(unwrap(x)))
(D::Differential)(x::Complex{Num}) = wrap(ComplexTerm{Real}(D(unwrap(real(x))), D(unwrap(imag(x)))))
SymbolicUtils.promote_symtype(::Differential, T) = T
Expand Down
7 changes: 7 additions & 0 deletions test/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ Dx = Differential(x)

test_equal(a, b) = @test isequal(simplify(a), simplify(b))

@testset "ZeroOperator handling" begin
@test isequal(Differential(0.1)(x), 0)
@test isequal(Differential(0.1)(0.1x), 0)
@test isequal(Differential(1)(x), 0)
@test isequal(Differential(2)(2x), 0)
end

#@test @macroexpand(@derivatives D'~t D2''~t) == @macroexpand(@derivatives (D'~t), (D2''~t))

@test isequal(expand_derivatives(D(t)), 1)
Expand Down
Loading