-
Notifications
You must be signed in to change notification settings - Fork 42
Description
Hi!
I've noticed that when computing the the n
-th derivative in x
of a function f(x)
the number of allocations grows with n
. This is expected for some general f
but let's suppose that f^{m}(x) = 0
for some m < n
. If this is true the derivative of f(x)
is guaranteed to be zero for all subsequent evaluations and thus the algorithm should simply stop computing and return Basic(0)
. This would save time and the required memory remains constant after the m
-th derivative.
That said, I would like to suggest changing
function diff(b1::SymbolicType, b2::SymbolicType, n::Integer=1)
n < 0 && throw(DomainError("n must be non-negative integer"))
n==0 && return b1
n==1 && return diff(b1, BasicType(b2))
n > 1 && return diff(diff(b1, BasicType(b2)), BasicType(b2), n-1)
end
in lines 19-24 of calculus.jl to something like
function diff(b1::SymbolicType, b2::SymbolicType, n::Integer=1)
n < 0 && throw(DomainError("n must be non-negative integer"))
#=NEW INSTRUCTION =# b1==Basic(0) && return Basic(0)
n==0 && return b1
n==1 && return diff(b1, BasicType(b2))
n > 1 && return diff(diff(b1, BasicType(b2)), BasicType(b2), n-1)
end
This improvement could come in handy when computing high order derivatives of polynomials, for instance.
In fact it would be even better to instead of checking whether f(x)
is zero to check if it does not depend on the symbol x
but I am unaware of any way to do this using SymEngine.
All the best