Skip to content

Commit 85dfd80

Browse files
committed
Make exp/measure/rational implementation independent
1 parent 4e3e5a2 commit 85dfd80

File tree

7 files changed

+98
-86
lines changed

7 files changed

+98
-86
lines changed

src/MultivariatePolynomials.jl

+17-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,23 @@ import Base: *, +, -, /, ^, ==,
66
promote_rule, convert, show, isless, size, getindex,
77
one, zero, transpose, isapprox, @pure, dot, copy
88

9-
include("utils.jl")
10-
include("types.jl")
11-
include("operators.jl")
9+
abstract type AbstractPolynomialLike{T} end
10+
abstract type AbstractTermLike{T} <: AbstractPolynomialLike{T} end
11+
abstract type AbstractMonomialLike <: AbstractTermLike{Int} end
12+
13+
abstract type AbstractVariable <: AbstractMonomialLike end
14+
abstract type AbstractMonomial <: AbstractMonomialLike end
15+
abstract type AbstractTerm{T} <: AbstractTermLike{T} end
16+
abstract type AbstractPolynomial{T} <: AbstractPolynomialLike{T} end
17+
18+
const APL{T} = AbstractPolynomialLike{T}
19+
20+
include("measure.jl")
21+
include("exp.jl")
22+
23+
#include("utils.jl")
24+
#include("types.jl")
25+
#include("operators.jl")
1226
#include("show.jl")
1327
#include("substitution.jl")
1428

src/exp.jl

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export expectation
22

3-
function _dot{C}(m::Measure{C}, p::TermContainer{C}, f)
3+
function _dot(m::Measure, p::APL, f)
44
i = 1
55
s = 0
6-
for t in p
6+
for t in terms(p)
77
while i <= length(m.x) && t.x != m.x[i]
88
i += 1
99
end
@@ -15,11 +15,8 @@ function _dot{C}(m::Measure{C}, p::TermContainer{C}, f)
1515
end
1616
s
1717
end
18-
dot(m::Measure, p::TermContainer) = _dot(m, p, (*))
19-
dot(p::TermContainer, m::Measure) = _dot(m, p, (a, b) -> b * a)
18+
Base.dot(m::Measure, p::APL) = _dot(m, p, (*))
19+
Base.dot(p::APL, m::Measure) = _dot(m, p, (a, b) -> b * a)
2020

21-
dot(m::Measure, p::PolyType) = dot(m, TermContainer(p))
22-
dot(p::PolyType, m::Measure) = dot(TermContainer(p), m)
23-
24-
expectation(m::Measure, p::PolyType) = dot(m, p)
25-
expectation(p::PolyType, m::Measure) = dot(p, m)
21+
expectation(m::Measure, p::APL) = dot(m, p)
22+
expectation(p::APL, m::Measure) = dot(p, m)

src/measure.jl

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
11
export Measure, zeta, ζ
22

3-
type Moment{C, T}
3+
type Moment{T, MT <: AbstractMonomial}
44
α::T
5-
x::Monomial{C}
5+
x::MT
66
end
77

88
# If a monomial is not in x, it does not mean that the moment is zero, it means that it is unknown/undefined
9-
type Measure{C, T}
9+
type Measure{T, MT <: AbstractMonomial, MVT <: AbstractVector{MT}}
1010
a::Vector{T}
11-
x::MonomialVector{C}
11+
x::MVT
1212

13-
function Measure{C, T}(a::Vector{T}, x::MonomialVector{C}) where {C, T}
13+
function Measure{T, MT, MVT}(a::Vector{T}, x::MVT) where {T, MT, MVT}
1414
if length(a) != length(x)
1515
error("There should be as many coefficient than monomials")
1616
end
1717
new(a, x)
1818
end
1919
end
2020

21-
Measure{C, T}(a::Vector{T}, x::MonomialVector{C}) = Measure{C, T}(a, x)
22-
function (::Type{Measure{C}}){C}(a::Vector, x::Vector)
23-
if length(a) != length(x)
24-
error("There should be as many coefficient than monomials")
25-
end
26-
σ, X = sortmonovec(PolyVar{C}, x)
27-
Measure(a[σ], X)
28-
end
29-
Measure{T<:VectorOfPolyType{true}}(a::Vector, X::Vector{T}) = Measure{true}(a, X)
30-
Measure{T<:VectorOfPolyType{false}}(a::Vector, X::Vector{T}) = Measure{false}(a, X)
21+
Measure{T, MT <: AbstractMonomial}(a::Vector{T}, x::AbstractVector{MT}) = Measure{T, MT, monovectype(x)}(monovec(a, x)...)
3122

32-
function ζ{C, T}(v::Vector{T}, x::MonomialVector{C}, varorder::Vector{PolyVar{C}})
23+
function ζ{T, MT <: AbstractMonomial, PVT <: AbstractVariable}(v::Vector{T}, x::AbstractVector{MT}, varorder::AbstractVector{PVT})
3324
Measure(T[m(v, varorder) for m in x], x)
3425
end
3526

36-
type MatMeasure{C, T}
27+
type MatMeasure{T, MT <: AbstractMonomial, MVT <: AbstractVector{MT}}
3728
Q::Vector{T}
38-
x::MonomialVector{C}
29+
x::MVT
3930
end

src/rational.jl

+26-36
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,49 @@
11
export RationalPoly
22
import Base.+, Base.-, Base.*, Base./
33

4-
immutable RationalPoly{C, S, T} <: PolyType{C}
5-
num::TermContainer{C, S}
6-
den::TermContainer{C, T}
4+
immutable RationalPoly{NT <: APL, DT <: APL}
5+
num::NT
6+
den::DT
77
end
8-
iscomm{C, S, T}(r::Type{RationalPoly{C, S, T}}) = C
98

10-
Base.convert{C, S, T}(::Type{RationalPoly{C, S, T}}, q::RationalPoly{C, S, T}) = q
11-
Base.convert{C, S, T, U, V}(::Type{RationalPoly{C, S, T}}, q::RationalPoly{C, U, V}) = TermContainer{C, S}(q.num) / TermContainer{C, T}(q.den)
12-
function Base.convert{C, S, T}(::Type{RationalPoly{C, S, T}}, p::TermContainer{C, S})
13-
p / one(TermContainer{C, T})
9+
Base.convert{NT, DT}(::Type{RationalPoly{NT, DT}}, q::RationalPoly{NT, DT}) = q
10+
Base.convert{NTout, DTout, NTin, DTin}(::Type{RationalPoly{NTout, DTout}}, q::RationalPoly{NTin, DTin}) = convert(NTout, q.num) / convert(DTout, q.den)
11+
function Base.convert{NT, DT}(::Type{RationalPoly{NT, DT}}, p::NT)
12+
p / one(DT)
1413
end
15-
function Base.convert{C, S, T}(::Type{RationalPoly{C, S, T}}, p::TermContainer)
16-
convert(RationalPoly{C, S, T}, TermContainer{C, S}(p))
17-
end
18-
function Base.convert{C, S, T}(::Type{RationalPoly{C, S, T}}, p)
19-
Base.convert(RationalPoly{C, S, T}, TermContainer{C, S}(p))
14+
function Base.convert{NT, DT}(::Type{RationalPoly{NT, DT}}, p)
15+
convert(RationalPoly{NT, DT}, convert(NT, p))
2016
end
2117

22-
(/)(r::RationalPoly, p::TermContainer) = r.num / (r.den * p)
23-
function (/){C, S, T}(num::TermContainer{C, S}, den::TermContainer{C, T})
24-
RationalPoly{C, S, T}(num, den)
18+
(/)(r::RationalPoly, p) = r.num / (r.den * p)
19+
function (/){NT <: APL, DT <: APL}(num::NT, den::DT)
20+
RationalPoly{NT, DT}(num, den)
2521
end
26-
function (/){C}(num, den::PolyType{C})
27-
TermContainer{C}(num) / den
22+
function (/)(num, den::APL)
23+
term(num) / den
2824
end
29-
(/){C}(num::PolyType{C}, den::PolyType{C}) = TermContainer{C}(num) / TermContainer{C}(den)
30-
3125
# Polynomial divided by coefficient is a polynomial not a rational polynomial
32-
(/){C}(num::PolyType{C}, den) = num * (1 / den)
26+
# (1/den) * num would not be correct in case of noncommutative coefficients
27+
(/)(num::APL, den) = num * (1 / den)
3328

3429
function (+)(r::RationalPoly, s::RationalPoly)
3530
(r.num*s.den + r.den*s.num) / (r.den * s.den)
3631
end
37-
function (+)(p::TermContainer, r::RationalPoly)
32+
function (+)(p, r::RationalPoly)
3833
(p*r.den + r.num) / r.den
3934
end
40-
(+)(r::RationalPoly, p::Polynomial) = p + r
41-
(+)(r::RationalPoly, t::Term) = t + r
35+
(+)(r::RationalPoly, p) = p + r
4236
function (-)(r::RationalPoly, s::RationalPoly)
4337
(r.num*s.den - r.den*s.num) / (r.den * s.den)
4438
end
45-
(-)(p::PolyType, s::RationalPoly) = (p * s.den - s.num) / s.den
46-
(-)(s::RationalPoly, p::PolyType) = (s.num - p * s.den) / s.den
39+
(-)(p, s::RationalPoly) = (p * s.den - s.num) / s.den
40+
(-)(s::RationalPoly, p) = (s.num - p * s.den) / s.den
4741

4842
(*)(r::RationalPoly, s::RationalPoly) = (r.num*s.num) / (r.den*s.den)
49-
(*)(p::TermContainer, r::RationalPoly) = p == r.den ? r.num : (p * r.num) / r.den
50-
(*)(r::RationalPoly, p::Polynomial) = p == r.den ? r.num : (r.num * p) / r.den
51-
(*)(r::RationalPoly, t::Term) = t == r.den ? r.num : (r.num * t) / r.den
52-
(*)(p::PolyType, r::RationalPoly) = TermContainer(p) * r
53-
(*)(r::RationalPoly, p::Monomial) = r * TermContainer(p)
54-
(*)(r::RationalPoly, p::PolyVar) = r * TermContainer(p)
55-
(*){C}(α, r::RationalPoly{C}) = TermContainer{C}(α) * r
56-
(*){C}(r::RationalPoly{C}, α) = r * TermContainer{C}(α)
43+
# Not type stable, currently it is a hack for SumOfSquares/test/SOSdemo2.jl:line 22
44+
# We should take gcd between numerator and denominator instead and in sosdemo2, we should cast to polynomial manually
45+
(*)(p, r::RationalPoly) = p == r.den ? r.num : (p * r.num) / r.den
46+
(*)(r::RationalPoly, p) = p == r.den ? r.num : (r.num * p) / r.den
5747

58-
zero(r::RationalPoly) = zero(r.num)
59-
zero{C, S, T}(::Type{RationalPoly{C, S, T}}) = zero(Polynomial{C, S})
48+
zero{NT}(::RationalPoly{NT}) = zero(NT)
49+
zero{NT, DT}(::Type{RationalPoly{NT, DT}}) = zero(NT)

test/alltests.jl

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include("mono.jl")
2+
#include("ncmono.jl")
3+
#include("poly.jl")
4+
#include("rational.jl")
5+
include("measure.jl")
6+
include("exp.jl")
7+
#include("promote.jl")
8+
#include("comp.jl")
9+
#include("nccomp.jl")
10+
#include("alg.jl")
11+
#include("ncalg.jl")
12+
#include("diff.jl")
13+
#include("subs.jl")
14+
#include("algebraicset.jl")
15+
#include("hash.jl")
16+
17+
include("show.jl")
18+
19+
#include("example1.jl")
20+
#include("example2.jl")

test/measure.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@testset "Measure" begin
22
@polyvar x y
33
@test_throws ErrorException Measure([1, 2], [x, x*y, y])
4-
@test_throws ErrorException Measure([1, 2, 3, 4], MonomialVector([x, x*y, y]))
4+
@test_throws ErrorException Measure([1, 2, 3, 4], [x, x*y, y])
55
m = Measure([1, 0, 2, 3], [x^2*y^2, y*x^2, x*y*x^2, x*y^2])
66
@test m.a == [2, 1, 0, 3]
77
end

test/mono.jl

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
@testset "PolyVar and Monomial tests" begin
2-
# @testset "polyvar macro index set" begin
3-
# n = 3
4-
# @polyvar x[1:n] y z[1:n-1]
5-
# @test length(x) == 3
6-
# @test length(z) == 2
7-
# @test x[1] > x[2] > x[3] > y > z[1] > z[2]
8-
# end
2+
@testset "polyvar macro index set" begin
3+
n = 3
4+
@polyvar x[1:n] y z[1:n-1]
5+
@test length(x) == 3
6+
@test length(z) == 2
7+
@test x[1] > x[2] > x[3] > y > z[1] > z[2]
8+
end
99
@testset "PolyVar" begin
1010
@polyvar x
1111
@test copy(x) == x
@@ -21,19 +21,19 @@
2121
@inferred zero(x^2)
2222
@test one(x^2) == 1
2323
@inferred one(x^2)
24-
# @polyvar y[1:7]
25-
# m = y[1] * y[3] * y[5] * y[7]
26-
# @test issorted(vars(y[2] * m), rev=true)
27-
# @test issorted(vars(m * y[4]), rev=true)
28-
# @test issorted(vars(y[6] * m), rev=true)
24+
@polyvar y[1:7]
25+
m = y[1] * y[3] * y[5] * y[7]
26+
@test issorted(vars(y[2] * m), rev=true)
27+
@test issorted(vars(m * y[4]), rev=true)
28+
@test issorted(vars(y[6] * m), rev=true)
29+
end
30+
@testset "MonomialVector" begin
31+
@polyvar x y
32+
X = [x^2,x*y,y^2]
33+
for (i, m) in enumerate(monomials([x, y], 2))
34+
@test m == X[i]
35+
end
2936
end
30-
# @testset "MonomialVector" begin
31-
# @polyvar x y
32-
# X = [x^2,x*y,y^2]
33-
# for (i, m) in enumerate(monomials([x, y], 2))
34-
# @test m == X[i]
35-
# end
36-
# end
3737
end
3838
module newmodule
3939
using Base.Test

0 commit comments

Comments
 (0)