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

StackOverflowError using antiderivative #157

Closed
chrhansk opened this issue Mar 28, 2024 · 3 comments
Closed

StackOverflowError using antiderivative #157

chrhansk opened this issue Mar 28, 2024 · 3 comments

Comments

@chrhansk
Copy link
Contributor

I am using the following version [7c1d4256] DynamicPolynomials v0.5.5 in my project. I wanted to compute the antiderivative of a polynomial. The Examples sections show the following:

Examples
––––––––

p = 3x^2*y + x + 2y + 1
antidifferentiate(p, x) # should return 3x^3* + 1/2*x + 2xy + x
antidifferentiate(p, x, Val{1}()) # equivalent to the above
antidifferentiate(p, (x, y)) # should return [3x^3* + 1/2*x + 2xy + x, 3/2x^2*y^2 + xy + y^2 + y]

However, when I launch julia and try to enter the exact example, I receive a StackOverflowError:

julia> using DynamicPolynomials

julia> @polyvar x y
(x, y)

julia> p = 3x^2*y + x + 2y + 1
1 + 2y + x + 3x²y

julia> antidifferentiate(p, x)
ERROR: StackOverflowError:
Stacktrace:
 [1] antidifferentiate(t::Monomial{DynamicPolynomials.Commutative{…}, Graded{…}}, v::Variable{DynamicPolynomials.Commutative{…}, Graded{…}}) (repeats 79984 times)
   @ MultivariatePolynomials ~/.julia/packages/MultivariatePolynomials/TpRhf/src/antidifferentiation.jl:33
Some type information was truncated. Use `show(err)` to see complete types.

The complete error is

julia> show(err)
1-element ExceptionStack:
StackOverflowError:
Stacktrace:
 [1] antidifferentiate(t::Monomial{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, Graded{LexOrder}}, v::Variable{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, Graded{LexOrder}}) (repeats 79984 times)
   @ MultivariatePolynomials ~/.julia/packages/MultivariatePolynomials/TpRhf/src/antidifferentiation.jl:33

This shows that the antidifferentiate function calls itself ~80k times before the stack memory runs out. Do you have any idea why this is happening?

@blegat
Copy link
Member

blegat commented Mar 30, 2024

It's not implemented for DynamicPolynomials yet, see the discussion in JuliaAlgebra/MultivariatePolynomials.jl#230
We would need to do something very similar to

function MP.differentiate(m::Monomial{V,M}, x::Variable{V,M}) where {V,M}
i = findfirst(isequal(x), MP.variables(m))
if (i === nothing || i == 0) || m.z[i] == 0
zero_term(m)
else
z = copy(m.z)
z[i] -= 1
m.z[i] * Monomial(MP.variables(m), z)
end
end

@chrhansk
Copy link
Contributor Author

chrhansk commented Apr 2, 2024

OK; thank you for the info. It is a bit weird that this causes a StackOverflowError as opposed to some error signaling that the function is not implemented, which was what confused me.

In principle, this should not be that hard to implement, right? At least the mathematics of it are not particularly hard :)

@blegat
Copy link
Member

blegat commented Apr 5, 2024

It is a bit weird that this causes a StackOverflowError as opposed to some error signaling that the function is not implemented, which was what confused me.

Yes, it's not ideal, thanks for the PR :)

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

2 participants