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

Antidifferentiate #81

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/TypedPolynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export @polyvar,
variables,
terms,
differentiate,
antidifferentiate,
subs

include("types.jl")
Expand Down
35 changes: 35 additions & 0 deletions src/calculus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,38 @@ function _diff(m::Monomial{Vars},
end
end, Val{N}()))
end

MP.antidifferentiate(v1::V, v2::V) where {V <: Variable} = v1*v2/2
MP.antidifferentiate(v1::Variable, v2::Variable) = v1 * v2

function MP.antidifferentiate(m::Monomial{Vars}, v::V) where {Vars, V <: Variable}
if inmonomial(v, Vars...)
return _antidiff(m, v)
else
# Insert `v` in the monomial
return m * v
end
end

_antidiff(m::Monomial, v::Variable) = _antidiff(m, exponents(m), v)

function _antidiff(::Monomial{Vars},
exponents::NTuple{N, Integer},
v::Variable) where {Vars, N}
vi = find_variable_index(v, Vars)
new_m = Monomial{Vars,N}(
ntuple(i -> begin
if i == vi
(exponents[i] == 0) ? 1 : exponents[i] + 1
else
exponents[i]
end
end,
Val{N}()
)
)
# Remark : as `exponents` are imposed to be `Integer` according to
# the method signature, we can use the Rational{Int} type here for the
# coefficient
return (1 // (exponents[vi] + 1)) * new_m
end
28 changes: 28 additions & 0 deletions test/calculus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,31 @@

@test @inferred(differentiate(1, x)) == 0
end

@testset "antiderivatives" begin
@polyvar x y z

@test @inferred(antidifferentiate(x, x)) == 1//2*x^2
@test @inferred(antidifferentiate(x, y)) == x*y
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can be moved to MultivariatePolynomials/test/differentiation.jl

@test @inferred(antidifferentiate(y, x)) == x*y
@test @inferred(antidifferentiate(x^2, x)) == 1//3*x^3
@test @inferred(antidifferentiate(x^2, y)) == x^2*y
@test @inferred(antidifferentiate(x^2 * y^3, y)) == x^2 * 1//4*y^4
@test @inferred(antidifferentiate(x^2 * y^3, x)) == x^3 * 1//3*y^3
@test @inferred(antidifferentiate(x^2 * y^3, z)) == x^2 * y^3 * z
@test @inferred(antidifferentiate(3x^2, x)) == x^3
@test @inferred(antidifferentiate(3x^2 * y^0, y)) == 3x^2 * y
@test @inferred(antidifferentiate(3 * x^2 + 2 * x + 1, x)) == x^3 + x^2 + x

@test @inferred(exponents(antidifferentiate(x^0,x))==(1,))
@test @inferred(exponents(antidifferentiate(x^0*y*z^2,x))==(1,1,2))

m = x^2
@test @wrappedallocs(antidifferentiate(m, x)) == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can be moved to MultivariatePolynomials/test/allocations.jl

@test @wrappedallocs(antidifferentiate(m, y)) == 0
m = x^2 * y^3
@test @wrappedallocs(antidifferentiate(m, x)) == 0
@test @wrappedallocs(antidifferentiate(m, y)) == 0

@test @inferred(antidifferentiate(1, x)) == x
end