JuliaAlgebra / DynamicPolynomials.jl

Multivariate polynomials implementation of commutative and non-commutative variables
Other
60 stars 20 forks source link

StackOverflowError using antiderivative #157

Closed chrhansk closed 1 month ago

chrhansk commented 3 months ago

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 commented 3 months ago

It's not implemented for DynamicPolynomials yet, see the discussion in https://github.com/JuliaAlgebra/MultivariatePolynomials.jl/pull/230 We would need to do something very similar to https://github.com/JuliaAlgebra/DynamicPolynomials.jl/blob/005621b454915428fa1a4c6ad72f90dc42b47f6f/src/diff.jl#L1-L10

chrhansk commented 3 months ago

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 commented 3 months ago

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 :)