Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Aug 12, 2017
1 parent fa66e08 commit 0567d8c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ _vec_diff_promote_op(::Type{PT}, ::VT, xs...) where {PT, VT} = _diff_pr
_vec_diff_promote_op(::Type{PT}, xs::Tuple) where PT = _vec_diff_promote_op(PT, xs...)

# 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 :(
function differentiate(ps::AbstractArray{PT, N}, xs) where {N, PT<:ARPL}
function differentiate(ps::AbstractArray{PT, N}, xs::Union{AbstractArray, Tuple}) where {N, PT<:ARPL}
qs = Array{_vec_diff_promote_op(PT, xs), N+1}(length(xs), size(ps)...)
for (i, x) in enumerate(xs)
for j in linearindices(ps)
Expand All @@ -35,7 +35,6 @@ function differentiate(ps::AbstractArray{PT, N}, xs) where {N, PT<:ARPL}
end
qs
end
differentiate(ps::AbstractArray{<:ARPL}, v::AbstractVariable) = differentiate.(ps, v)

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

Expand Down
18 changes: 18 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ isapprox(α, p::APL; kwargs...) = isapproxconstant(promote(p, α)...; kwargs...)
(-)(m::AbstractMonomialLike) = (-1) * m
(-)(t::AbstractTermLike) = (-coefficient(t)) * monomial(t)
(-)(p::APL) = polynomial((-).(terms(p)))
(+)(p::Union{APL, RationalPoly}) = p

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

for op in [:+, :-]
@eval begin
$op(t1::AbstractTermLike, t2::AbstractTermLike) = $op(term(t1), term(t2))
$op(t1::AbstractTerm, t2::AbstractTerm) = $op(promote(t1, t2)...)
function $op(t1::T, t2::T) where T <: AbstractTerm
if monomial(t1) == monomial(t2)
ts = [$op(coefficient(t1), coefficient(t2)) * monomial(t1)]
elseif t1 > t2
ts = [t1, $op(t2)]
else
ts = [$op(t2), t1]
end
polynomial(ts, SortedUniqState())
end
end
end

Base.transpose(v::AbstractVariable) = v
Base.transpose(m::AbstractMonomial) = m
Base.transpose(t::T) where {T <: AbstractTerm} = transpose(coefficient(t)) * monomial(t)
Expand Down
8 changes: 6 additions & 2 deletions src/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ function uniqterms(ts::AbstractVector{T}) where T <: AbstractTerm
if isempty(result) || monomial(t) != monomial(last(result))
push!(result, t)
else
# t + ts would be a polynomial with DynamicPolynomials
result[end] = (coefficient(last(result)) + coefficient(t)) * monomial(t)
coef = coefficient(last(result)) + coefficient(t)
if iszero(coef)
pop!(result)
else
result[end] = coef * monomial(t)
end
end
end
end
Expand Down
16 changes: 11 additions & 5 deletions test/comp.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
struct CustomTerms{T, P<:AbstractPolynomial{T}} <: AbstractPolynomialLike{T}
p::P
end
CustomTerms(p::AbstractPolynomial{T}) where T = CustomTerms{T, typeof(p)}(p)
MultivariatePolynomials.polynomial(p::CustomTerms) = p.p

struct CustomPoly{T, P<:AbstractPolynomial{T}} <: AbstractPolynomialLike{T}
p::P
end
Expand Down Expand Up @@ -42,12 +48,12 @@ MultivariatePolynomials.terms(p::CustomPoly) = terms(p.p)
end
@testset "Polynomial equality" begin
Mod.@polyvar x y
@test polynomial(CustomPoly(x + 1 - x)) isa AbstractPolynomial
@test MultivariatePolynomials.eqconstant(polynomial(CustomPoly(x + 1 - x)), 1)
@test MultivariatePolynomials.eqconstant(CustomPoly(x + 1 - x), 1)
@test polynomial(CustomTerms(x + 1 - x)) isa AbstractPolynomial
@test MultivariatePolynomials.eqconstant(polynomial(CustomTerms(x + 1 - x)), 1)
@test MultivariatePolynomials.eqconstant(CustomTerms(x + 1 - x), 1)
@test CustomPoly(x + 1 - x) == 1
@test 2 != CustomPoly(x + 1 - x)
@test x^2 == CustomPoly(x - x + x^2)
@test 2 != CustomTerms(x + 1 - x)
@test x^2 == CustomTerms(x - x + x^2)
@test CustomPoly(-x + x^2) != x^2
@test 2*x*y + 3*y^2 == 3*y^2 + 2*y*x
@test 3*x*y + 2*y^2 != 3*y^2 + 2*y*x
Expand Down
3 changes: 2 additions & 1 deletion test/diff.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
@testset "Differentiation" begin
Mod.@polyvar x y
@test differentiate(3, y) == 0
@test differentiate([x, y], y) == [0, 1]
@test differentiate.([x, y], y) == [0, 1]
@test differentiate([x, y], (x, y)) == eye(2)
@test differentiate(true*x+true*x^2, y) == 0
@inferred differentiate(true*x+true*x^2, y)
@test differentiate(x*y + 3y^2 , [x, y]) == [y, x+6y]
Expand Down
1 change: 1 addition & 0 deletions test/mono.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const MP = MultivariatePolynomials
@testset "PolyVar and Monomial tests" begin
@testset "polyvar macro index set" begin
Mod.@polyvar x y z
Expand Down
4 changes: 4 additions & 0 deletions test/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
end
@testset "Polynomial" begin
Mod.@polyvar x

@test terms(polynomial([1, x^2, x, 2x^2])) == [3x^2, x, 1]
@test terms(polynomial([x^3, 2x^3, x^2, -2x^2, x^2, x, 2, -2], MP.SortedState())) == [3x^3, x]

@test polynomial(1 + x) == 1 + x
@test leadingterm(1 + x) == x
@test one(1 + x) == one(1.0 + x) == 1
Expand Down

0 comments on commit 0567d8c

Please sign in to comment.