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

Improvement: Early quitting when computing derivatives #225

Open
lucass-carneiro opened this issue Dec 23, 2020 · 0 comments
Open

Improvement: Early quitting when computing derivatives #225

lucass-carneiro opened this issue Dec 23, 2020 · 0 comments

Comments

@lucass-carneiro
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant