Skip to content

Commit

Permalink
Fix comp
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jul 13, 2017
1 parent 122c35e commit 2f95508
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 40 deletions.
33 changes: 20 additions & 13 deletions src/comp.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export isapproxzero

Base.iszero(v::AbstractVariable) = false
Base.iszero(m::AbstractMonomial) = false
Base.iszero(t::AbstractTerm) = iszero(coefficient(t))
Expand Down Expand Up @@ -90,11 +92,20 @@ end
(==)(p::APL, q::MatPolynomial) = p == polynomial(q)
(==)(p::MatPolynomial, q::MatPolynomial) = iszero(p - q)

function isapproxzero(α; ztol::Real=1e-6)
-ztol < α < ztol
end

isapproxzero(m::AbstractMonomialLike; kwargs...) = false
isapproxzero(t::AbstractTermLike; kwargs...) = isapproxzero(coefficient(t); kwargs...)
isapproxzero(p::APL; kwargs...) = isapprox(p, zero(p); kwargs...)
isapproxzero(p::RationalPoly; kwargs...) = isapproxzero(p.num; kwargs...)

isapprox(t1::AbstractTerm, t2::AbstractTerm; kwargs...) = isapprox(coefficient(t1), coefficient(t2); kwargs...) && monomial(t1) == monomial(t2)
isapprox(p1::AbstractPolynomial, p2::AbstractPolynomial; kwargs...) = compare_terms(p1, p2, (x, y) -> isapprox(x, y; kwargs...))

function isapprox(p::MatPolynomial, q::MatPolynomial)
p.x == q.x && isapprox(p.Q, q.Q)
function isapprox(p::MatPolynomial, q::MatPolynomial; kwargs...)
p.x == q.x && isapprox(p.Q, q.Q; kwargs...)
end

function permcomp(f, m)
Expand All @@ -115,21 +126,17 @@ function permcomp(f, m)
true
end

function isapprox{C, S, T}(p::SOSDecomposition{C, S}, q::SOSDecomposition{C, T}; rtol::Real=Base.rtoldefault(S, T), atol::Real=0, ztol::Real=1e-6)
function isapprox(p::SOSDecomposition, q::SOSDecomposition; kwargs...)
m = length(p.ps)
if length(q.ps) != m
false
else
permcomp((i, j) -> isapprox(p.ps[i], q.ps[j], rtol=rtol, atol=atol, ztol=ztol), m)
permcomp((i, j) -> isapprox(p.ps[i], q.ps[j]; kwargs...), m)
end
end

function isapproxzero(p::RationalPoly; ztol::Real=1e-6)
isapproxzero(p.num, ztol=ztol)
end

isapprox(p::RationalPoly, q::RationalPoly; kwargs...) = isapprox(p.num*q.den, q.num*p.den, kwargs...)
isapprox(p::RationalPoly, q::APL; kwargs...) = isapprox(p.num, q*p.den, kwargs...)
isapprox(p::APL, q::RationalPoly; kwargs...) = isapprox(p*q.den, q.num, rtol=rtol, atol=atol, ztol=ztol)
isapprox{C}(p::RationalPoly{C}, q; kwargs...) = isapprox(p, polynomial(q), kwargs...)
isapprox{C}(p, q::RationalPoly{C}; kwargs...) = isapprox(polynomial(p), q, kwargs...)
isapprox(p::RationalPoly, q::RationalPoly; kwargs...) = isapprox(p.num*q.den, q.num*p.den; kwargs...)
isapprox(p::RationalPoly, q::APL; kwargs...) = isapprox(p.num, q*p.den; kwargs...)
isapprox(p::APL, q::RationalPoly; kwargs...) = isapprox(p*q.den, q.num; kwargs...)
isapprox{C}(q::RationalPoly{C}, α; kwargs...) = isapprox(q, term(α, q.den); kwargs...)
isapprox{C}(α, q::RationalPoly{C}; kwargs...) = isapprox(term(α, q.den), q; kwargs...)
8 changes: 4 additions & 4 deletions src/matpoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ function MatPolynomial{T}(Q::AbstractMatrix{T}, x)
MatPolynomial(Q, X, σ)
end

function Base.convert{T, PT <: AbstractPolynomial{T}}(::Type{PT}, p::MatPolynomial)
# eltype(p) may be different than T and polynomial(p) may be different than PT (different module)
convert(PT, polynomial(p))
end
#function Base.convert{T, PT <: AbstractPolynomial{T}}(::Type{PT}, p::MatPolynomial)
# # coefficienttype(p) may be different than T and polynomial(p) may be different than PT (different module)
# convert(PT, polynomial(p))
#end
function polynomial(p::MatPolynomial)
polynomial(getmat(p), p.x)
end
Expand Down
3 changes: 3 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ end
for op in [:+, :-, :*, :(==)]
@eval $op(p1::APL, p2::APL) = $op(promote(p1, p2)...)
end
isapprox(p1::APL, p2::APL; kwargs...) = isapprox(promote(p1, p2)...; kwargs...)

# @eval $op(p::APL, α) = $op(promote(p, α)...) would be less efficient
for (op, fun) in [(:+, :plusconstant), (:-, :minusconstant), (:*, :multconstant), (:(==), :eqconstant)]
@eval $op(p::APL, α) = $fun(p, α)
@eval $op(α, p::APL) = $fun(α, p)
end
isapprox(p::APL, α; kwargs...) = isapproxconstant(promote(p, α)...; kwargs...)
isapprox(α, p::APL; kwargs...) = isapproxconstant(promote(p, α)...; kwargs...)

(-)(m::AbstractMonomialLike) = (-1) * m
(-)(t::AbstractTermLike) = (-coefficient(t)) * monomial(t)
Expand Down
2 changes: 1 addition & 1 deletion test/alltests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include("rational.jl")
include("measure.jl")
include("exp.jl")
#include("promote.jl")
#include("comp.jl")
include("comp.jl")
#include("nccomp.jl")
#include("alg.jl")
#include("ncalg.jl")
Expand Down
26 changes: 13 additions & 13 deletions test/comp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ end
@testset "Equality" begin
@testset "Monomial equality" begin
@polyvar x y
@test 1 == Monomial([x, y], [0, 0])
@test 2 != Monomial([x, y], [0, 0])
#@test 1 == constantmonomial([x, y])
#@test 2 != constantmonomial([x, y])
@test 2 != x
@test 2 != x*y
@test 2 != MonomialVector([x, y], 1)
@test x != MonomialVector([x, y], 1)
#@test 2 != MonomialVector([x, y], 1)
#@test x != MonomialVector([x, y], 1)
@test x*y != x
@test 1x*y == x*y
@test 2x*y != x*y
@test x == Monomial(x)
@test Monomial([x, y], [1, 0]) == x
@test x != Monomial([x, y], [0, 1])
@test MonomialVector([x, y], [[1, 0], [0, 0]]) == MonomialVector([x], [[1], [0]])
@test MonomialVector([x, y], 2) != MonomialVector([x, y], 1)
@test x == monomial(x)
#@test Monomial([x, y], [1, 0]) == x
#@test x != Monomial([x, y], [0, 1])
#@test MonomialVector([x, y], [[1, 0], [0, 0]]) == MonomialVector([x], [[1], [0]])
#@test MonomialVector([x, y], 2) != MonomialVector([x, y], 1)
z = x
@polyvar x
@test z != x
Expand All @@ -57,10 +57,10 @@ end
end
@testset "SOSDecomposition equality" begin
@polyvar x y
@test !isapprox(SOSDecomposition{true}([x+y, x-y]), SOSDecomposition{true}([x+y]))
@test !isapprox(SOSDecomposition{true}([x+y, x-y]), SOSDecomposition{true}([x+y, x+y]))
@test isapprox(SOSDecomposition{true}([x+y, x-y]), SOSDecomposition{true}([x+y, x-y]))
@test isapprox(SOSDecomposition{true}([x+y, x-y]), SOSDecomposition{true}([x-y, x+y+1e-8]), ztol=1e-7)
@test !isapprox(SOSDecomposition([x+y, x-y]), SOSDecomposition([x+y]))
@test !isapprox(SOSDecomposition([x+y, x-y]), SOSDecomposition([x+y, x+y]))
@test isapprox(SOSDecomposition([x+y, x-y]), SOSDecomposition([x+y, x-y]))
@test isapprox(SOSDecomposition([x+y, x-y]), SOSDecomposition([x-y, x+y+1e-8]), ztol=1e-7)
end
@testset "RationalPoly equality" begin
@polyvar x y
Expand Down
18 changes: 9 additions & 9 deletions test/promote.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
@testset "Promotion" begin
@polyvar x y
@inferred x*y+x
@test typeof([x, x*y+x, x]) == Vector{Polynomial{true, Int}}
@test typeof([1, x/y, x]) == Vector{RationalPoly{true, Int, Int}}
@test typeof([(x^2-2x+2) x; x x^2]) == Matrix{Polynomial{true, Int}}
@test typeof([2.0x, 3x]) == Vector{Term{true, Float64}}
@test [x, x*y+x, x] isa Vector{<:AbstractPolynomial{Int}}
@test [1, x/y, x] isa Vector{RationalPoly{<:AbstractTerm{Int}, <:AbstractVariable}}
@test [(x^2-2x+2) x; x x^2] isa Matrix{<:AbstractPolynomial{Int}}
@test [2.0x, 3x] == Vector{<:AbstractTerm{Float64}}
@inferred Any[x*y, x+y]
@test typeof(Any[x*y, x+y]) == Vector{Any}
@test typeof([x*y, x+y]) == Vector{Polynomial{true, Int}}
@test typeof([2x*y, x+y]) == Vector{Polynomial{true, Int}}
@test typeof([2.0x, x/y, 1y]) == Vector{RationalPoly{true, Float64, Int}}
@test typeof([2x+y, x/2.0y, x+1y]) == Vector{RationalPoly{true, Int, Float64}}
@test Any[x*y, x+y] isa Vector{Any}
@test [x*y, x+y] == Vector{AbstractPolynomial{Int}}
@test [2x*y, x+y] == Vector{AbstractPolynomial{Int}}
@test [2.0x, x/y, 1y] == Vector{RationalPoly{<:AbstractTerm{Float64}, <:AbstractVariable}}
@test [2x+y, x/2.0y, x+1y] == Vector{RationalPoly{<:AbstractPolynomial{Int}, <:AbstractTerm{Float64}}}

X = [x, y]
Y = [1 2; 3 4] * X
Expand Down

0 comments on commit 2f95508

Please sign in to comment.