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
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.
Hi!
I've noticed that when computing the the
n
-th derivative inx
of a functionf(x)
the number of allocations grows withn
. This is expected for some generalf
but let's suppose thatf^{m}(x) = 0
for somem < n
. If this is true the derivative off(x)
is guaranteed to be zero for all subsequent evaluations and thus the algorithm should simply stop computing and returnBasic(0)
. This would save time and the required memory remains constant after them
-th derivative.That said, I would like to suggest changing
in lines 19-24 of calculus.jl to something like
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 symbolx
but I am unaware of any way to do this using SymEngine.All the best