Skip to content

Commit

Permalink
Add antidifferentiation for monomials
Browse files Browse the repository at this point in the history
  • Loading branch information
chrhansk committed Apr 2, 2024
1 parent 005621b commit e96c595
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/DynamicPolynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ include("promote.jl")
include("operators.jl")
include("comp.jl")

include("anti_diff.jl")
include("diff.jl")
include("subs.jl")

Expand Down
10 changes: 10 additions & 0 deletions src/anti_diff.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function MP.antidifferentiate(m::Monomial{V,M}, x::Variable{V,M}) where {V,M}
z = copy(m.z)
i = findfirst(isequal(x), MP.variables(m))
if (i === nothing || i == 0) || m.z[i] == 0
Monomial(MP.variables(m), z) * x
else
z[i] += 1
Monomial(MP.variables(m), z) / (m.z[i] + 1)
end
end
18 changes: 18 additions & 0 deletions test/mono.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ import MultivariatePolynomials as MP
@test m.z == [2, 1, 1, 0, 0]
end

@testset "Antidifferentiation" begin
@ncpolyvar x y z

@info typeof(x)

m = x
mi = DynamicPolynomials.MP.antidifferentiate(m, y)
@test mi == x * y

m = x^3
mi = DynamicPolynomials.MP.antidifferentiate(m, x)
@test mi == (x^4 / 4)

m = Monomial([x, y, z], [1, 2, 3])
mi = DynamicPolynomials.MP.antidifferentiate(m, z)
@test mi == (x*y^2*z^4) / 4
end

@testset "Evaluation" begin
@polyvar x y
@test (x^2 * y)(3, 2) == 18
Expand Down

0 comments on commit e96c595

Please sign in to comment.