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

Add antidifferentiation #158

Merged
merged 7 commits into from
Apr 11, 2024
Merged
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/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
34 changes: 34 additions & 0 deletions src/anti_diff.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function _div_by_power(x::T, y::Int) where {T}
x / y
end

function _div_by_power(x::T, y::Int)::Rational{T} where {T<:Int}
chrhansk marked this conversation as resolved.
Show resolved Hide resolved
x // y
end

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

function MP.antidifferentiate(p::Polynomial{V,M,T}, x::Variable{V,M}) where {V,M,T}
i = something(findfirst(isequal(x), MP.variables(p)), 0)
S = typeof(_div_by_power(zero(T), Int(1)))
if iszero(i)
x * p
else
Z = copy.(p.x.Z)
a = Vector{S}(undef, length(p.a))
for j in 1:length(Z)
a[j] = _div_by_power(p.a[j], (Z[j][i] + 1))
Z[j][i] += 1
end
Polynomial(a, MonomialVector(MP.variables(p), Z))
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
32 changes: 32 additions & 0 deletions test/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,38 @@
@inferred polynomial(2.0u, Int)
end

@testset "Antiderivative" begin
@polyvar x y

p = (x^2 + 4 * y^3)
(_, _, T) = typeof(p).parameters
Copy link
Member

Choose a reason for hiding this comment

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

Can you use coefficient_type instead ?

@test T == Int

pi = DynamicPolynomials.antidifferentiate(p, y)
@test pi == (x^2 * y + y^4)

pi = DynamicPolynomials.antidifferentiate(p, x)
(_, _, T) = typeof(pi).parameters
@test T == Rational{Int}

p = (1.0 * x^2 + 2.0 * y^2)
(_, _, T) = typeof(p).parameters
@test T == Float64

pi = DynamicPolynomials.antidifferentiate(p, x)
(_, _, T) = typeof(pi).parameters
@test T == Float64

p = 2 * y
pi = DynamicPolynomials.antidifferentiate(p, y)
@test pi == y^2

p = x^2
pi = DynamicPolynomials.antidifferentiate(p, y)
@test pi == x^2 * y

end

@testset "Evaluation" begin
@polyvar x y
@test (x^2 + y^3)(2, 3) == 31
Expand Down
Loading