diff --git a/src/diff.jl b/src/diff.jl index 53122df..115df0e 100644 --- a/src/diff.jl +++ b/src/diff.jl @@ -26,3 +26,32 @@ function MP.differentiate(p::Polynomial{C, T}, x::PolyVar{C}) where {C, T} Polynomial(a, MonomialVector(_vars(p), Z)) end end + +function MP.antidifferentiate(m::Monomial{C}, x::PolyVar{C}) where {C} + i = findfirst(isequal(x), _vars(m)) + if (i === nothing || i == 0) || m.z[i] == 0 + # Insert `x` in the monomial + return m * x + else + z = copy(m.z) + z[i] += 1 + (1 // (m.z[i] + 1)) * Monomial(_vars(m), z) + end +end + +function MP.antidifferentiate(p::Polynomial{C,T}, x::PolyVar{C}) where {C,T} + # Increase by one the order of `x` in all monomials of `p` + q = p * x + + # Find index of `x` in monovec of `q` + i = something(findfirst(isequal(x), _vars(q)), 0) + + # Divide the coef of each monomial by `1 + deg(p, x)` + # (i.e by `deg(q, x)`) + _x = MP.monomials(q) + for j in eachindex(_x) + q.a[j] //= _x.Z[j][i] + end + + return q +end \ No newline at end of file