Skip to content

Commit 0567d8c

Browse files
committed
Add tests
1 parent fa66e08 commit 0567d8c

File tree

7 files changed

+43
-10
lines changed

7 files changed

+43
-10
lines changed

src/diff.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ _vec_diff_promote_op(::Type{PT}, ::VT, xs...) where {PT, VT} = _diff_pr
2525
_vec_diff_promote_op(::Type{PT}, xs::Tuple) where PT = _vec_diff_promote_op(PT, xs...)
2626

2727
# even if I annotate with ::Array{_diff_promote_op(T, PolyVar{C}), N+1}, it cannot detect the type since it seems to be unable to determine the dimension N+1 :(
28-
function differentiate(ps::AbstractArray{PT, N}, xs) where {N, PT<:ARPL}
28+
function differentiate(ps::AbstractArray{PT, N}, xs::Union{AbstractArray, Tuple}) where {N, PT<:ARPL}
2929
qs = Array{_vec_diff_promote_op(PT, xs), N+1}(length(xs), size(ps)...)
3030
for (i, x) in enumerate(xs)
3131
for j in linearindices(ps)
@@ -35,7 +35,6 @@ function differentiate(ps::AbstractArray{PT, N}, xs) where {N, PT<:ARPL}
3535
end
3636
qs
3737
end
38-
differentiate(ps::AbstractArray{<:ARPL}, v::AbstractVariable) = differentiate.(ps, v)
3938

4039
differentiate(p::ARPL, xs) = [differentiate(p, x) for x in xs]
4140

src/operators.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ isapprox(α, p::APL; kwargs...) = isapproxconstant(promote(p, α)...; kwargs...)
2929
(-)(m::AbstractMonomialLike) = (-1) * m
3030
(-)(t::AbstractTermLike) = (-coefficient(t)) * monomial(t)
3131
(-)(p::APL) = polynomial((-).(terms(p)))
32+
(+)(p::Union{APL, RationalPoly}) = p
3233

3334
# Avoid adding a zero constant that might artificially increase the Newton polytope
3435
# Need to add polynomial conversion for type stability
@@ -56,6 +57,23 @@ multconstant(t::AbstractTermLike, α) = (coefficient(t)*α) * monomial(t)
5657
(*)(t::AbstractTermLike, p::APL) = polynomial(map(te -> t * te, terms(p)))
5758
(*)(p::APL, t::AbstractTermLike) = polynomial(map(te -> te * t, terms(p)))
5859

60+
for op in [:+, :-]
61+
@eval begin
62+
$op(t1::AbstractTermLike, t2::AbstractTermLike) = $op(term(t1), term(t2))
63+
$op(t1::AbstractTerm, t2::AbstractTerm) = $op(promote(t1, t2)...)
64+
function $op(t1::T, t2::T) where T <: AbstractTerm
65+
if monomial(t1) == monomial(t2)
66+
ts = [$op(coefficient(t1), coefficient(t2)) * monomial(t1)]
67+
elseif t1 > t2
68+
ts = [t1, $op(t2)]
69+
else
70+
ts = [$op(t2), t1]
71+
end
72+
polynomial(ts, SortedUniqState())
73+
end
74+
end
75+
end
76+
5977
Base.transpose(v::AbstractVariable) = v
6078
Base.transpose(m::AbstractMonomial) = m
6179
Base.transpose(t::T) where {T <: AbstractTerm} = transpose(coefficient(t)) * monomial(t)

src/poly.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ function uniqterms(ts::AbstractVector{T}) where T <: AbstractTerm
8888
if isempty(result) || monomial(t) != monomial(last(result))
8989
push!(result, t)
9090
else
91-
# t + ts would be a polynomial with DynamicPolynomials
92-
result[end] = (coefficient(last(result)) + coefficient(t)) * monomial(t)
91+
coef = coefficient(last(result)) + coefficient(t)
92+
if iszero(coef)
93+
pop!(result)
94+
else
95+
result[end] = coef * monomial(t)
96+
end
9397
end
9498
end
9599
end

test/comp.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
struct CustomTerms{T, P<:AbstractPolynomial{T}} <: AbstractPolynomialLike{T}
2+
p::P
3+
end
4+
CustomTerms(p::AbstractPolynomial{T}) where T = CustomTerms{T, typeof(p)}(p)
5+
MultivariatePolynomials.polynomial(p::CustomTerms) = p.p
6+
17
struct CustomPoly{T, P<:AbstractPolynomial{T}} <: AbstractPolynomialLike{T}
28
p::P
39
end
@@ -42,12 +48,12 @@ MultivariatePolynomials.terms(p::CustomPoly) = terms(p.p)
4248
end
4349
@testset "Polynomial equality" begin
4450
Mod.@polyvar x y
45-
@test polynomial(CustomPoly(x + 1 - x)) isa AbstractPolynomial
46-
@test MultivariatePolynomials.eqconstant(polynomial(CustomPoly(x + 1 - x)), 1)
47-
@test MultivariatePolynomials.eqconstant(CustomPoly(x + 1 - x), 1)
51+
@test polynomial(CustomTerms(x + 1 - x)) isa AbstractPolynomial
52+
@test MultivariatePolynomials.eqconstant(polynomial(CustomTerms(x + 1 - x)), 1)
53+
@test MultivariatePolynomials.eqconstant(CustomTerms(x + 1 - x), 1)
4854
@test CustomPoly(x + 1 - x) == 1
49-
@test 2 != CustomPoly(x + 1 - x)
50-
@test x^2 == CustomPoly(x - x + x^2)
55+
@test 2 != CustomTerms(x + 1 - x)
56+
@test x^2 == CustomTerms(x - x + x^2)
5157
@test CustomPoly(-x + x^2) != x^2
5258
@test 2*x*y + 3*y^2 == 3*y^2 + 2*y*x
5359
@test 3*x*y + 2*y^2 != 3*y^2 + 2*y*x

test/diff.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
@testset "Differentiation" begin
22
Mod.@polyvar x y
33
@test differentiate(3, y) == 0
4-
@test differentiate([x, y], y) == [0, 1]
4+
@test differentiate.([x, y], y) == [0, 1]
5+
@test differentiate([x, y], (x, y)) == eye(2)
56
@test differentiate(true*x+true*x^2, y) == 0
67
@inferred differentiate(true*x+true*x^2, y)
78
@test differentiate(x*y + 3y^2 , [x, y]) == [y, x+6y]

test/mono.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const MP = MultivariatePolynomials
12
@testset "PolyVar and Monomial tests" begin
23
@testset "polyvar macro index set" begin
34
Mod.@polyvar x y z

test/poly.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
end
3030
@testset "Polynomial" begin
3131
Mod.@polyvar x
32+
33+
@test terms(polynomial([1, x^2, x, 2x^2])) == [3x^2, x, 1]
34+
@test terms(polynomial([x^3, 2x^3, x^2, -2x^2, x^2, x, 2, -2], MP.SortedState())) == [3x^3, x]
35+
3236
@test polynomial(1 + x) == 1 + x
3337
@test leadingterm(1 + x) == x
3438
@test one(1 + x) == one(1.0 + x) == 1

0 commit comments

Comments
 (0)