Skip to content

Commit 308c20c

Browse files
committed
fix tests
1 parent 3e2949e commit 308c20c

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

src/arithematics.jl

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct TruncatedPoly{K,T,TO} <: Number
6666
end
6767
const Max2Poly{T,TO} = TruncatedPoly{2,T,TO}
6868
Max2Poly(a, b, maxorder) = TruncatedPoly((a, b), maxorder)
69+
Max2Poly{T,TO}(a, b, maxorder) where {T,TO} = TruncatedPoly{2,T,TO}((a, b), maxorder)
6970

7071
function Base.:+(a::Max2Poly, b::Max2Poly)
7172
aa, ab = a.coeffs
@@ -83,40 +84,32 @@ function Base.:+(a::Max2Poly, b::Max2Poly)
8384
end
8485
end
8586

86-
function Base.:+(a::TruncatedPoly{K}, b::TruncatedPoly{K}) where K
87-
if a.maxorder == b.maxorder
88-
return TruncatedPoly(a.coeffs .+ b.coeffs, a.maxorder)
89-
elseif a.maxorder > b.maxorder
90-
offset = a.maxorder - b.maxorder
91-
return TruncatedPoly(ntuple(i->i+offset <= K ? a.coeffs[i] + b.coeffs[i+offset] : a.coeffs[i], K), a.maxorder)
92-
else
93-
offset = b.maxorder - a.maxorder
94-
return TruncatedPoly(ntuple(i->i+offset <= K ? b.coeffs[i] + a.coeffs[i+offset] : b.coeffs[i], K), b.maxorder)
87+
@generated function Base.:+(a::TruncatedPoly{K}, b::TruncatedPoly{K}) where K
88+
quote
89+
if a.maxorder == b.maxorder
90+
return TruncatedPoly(a.coeffs .+ b.coeffs, a.maxorder)
91+
elseif a.maxorder > b.maxorder
92+
offset = a.maxorder - b.maxorder
93+
return TruncatedPoly((@ntuple $K i->i+offset <= $K ? a.coeffs[i] + b.coeffs[i+offset] : a.coeffs[i]), a.maxorder)
94+
else
95+
offset = b.maxorder - a.maxorder
96+
return TruncatedPoly((@ntuple $K i->i+offset <= $K ? b.coeffs[i] + a.coeffs[i+offset] : b.coeffs[i]), b.maxorder)
97+
end
9598
end
9699
end
97100

98-
function Base.:*(a::Max2Poly, b::Max2Poly)
99-
maxorder = a.maxorder + b.maxorder
100-
aa, ab = a.coeffs
101-
ba, bb = b.coeffs
102-
Max2Poly(aa*bb + ab*ba, ab * bb, maxorder)
103-
end
104-
105-
function Base.:*(a::TruncatedPoly{K,T}, b::TruncatedPoly{K,T}) where {K,T}
106-
maxorder = a.maxorder + b.maxorder
107-
TruncatedPoly(ntuple(K) do k
108-
r = zero(T)
109-
for i=1:K-k+1
110-
r += a.coeffs[i+k-1]*b.coeffs[K-i+1]
111-
end
112-
return r
113-
end, maxorder)
101+
@generated function Base.:*(a::TruncatedPoly{K,T}, b::TruncatedPoly{K,T}) where {K,T}
102+
tupleexpr = Expr(:tuple, [K-k+1 > 1 ? Expr(:call, :+, [:(a.coeffs[$(i+k-1)]*b.coeffs[$(K-i+1)]) for i=1:K-k+1]...) : :(a.coeffs[$k]*b.coeffs[$K]) for k=1:K]...)
103+
quote
104+
maxorder = a.maxorder + b.maxorder
105+
TruncatedPoly($tupleexpr, maxorder)
106+
end
114107
end
115108

116109
Base.zero(::Type{TruncatedPoly{K,T,TO}}) where {K,T,TO} = TruncatedPoly(ntuple(i->zero(T), K), zero(Tropical{TO}).n)
117110
Base.one(::Type{TruncatedPoly{K,T,TO}}) where {K,T,TO} = TruncatedPoly(ntuple(i->i==K ? one(T) : zero(T), K), zero(TO))
118-
Base.zero(::TruncatedPoly{K,T,TO}) where {K,T,TO} = zero(TruncatedPoly{T,TO})
119-
Base.one(::TruncatedPoly{K,T,TO}) where {K,T,TO} = one(TruncatedPoly{T,TO})
111+
Base.zero(::TruncatedPoly{K,T,TO}) where {K,T,TO} = zero(TruncatedPoly{K,T,TO})
112+
Base.one(::TruncatedPoly{K,T,TO}) where {K,T,TO} = one(TruncatedPoly{K,T,TO})
120113

121114
Base.show(io::IO, x::TruncatedPoly) = show(io, MIME"text/plain"(), x)
122115
function Base.show(io::IO, ::MIME"text/plain", x::TruncatedPoly{K}) where K

test/configurations.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ end
4747
res6 = best2_solutions(code; all=true)[]
4848
res7 = all_solutions(code)[]
4949
idp = graph_polynomial(code, Val(:finitefield))[]
50-
@test all(x->x res7.coeffs[end-1].data, res6.a.data)
51-
@test all(x->x res7.coeffs[end].data, res6.b.data)
50+
@test all(x->x res7.coeffs[end-1].data, res6.coeffs[1].data)
51+
@test all(x->x res7.coeffs[end].data, res6.coeffs[2].data)
5252
for (i, (s, c)) in enumerate(zip(res7.coeffs, idp.coeffs))
5353
@test length(s) == c
5454
@test all(x->count_ones(x)==(i-1), s.data)

test/interfaces.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ using LightGraphs, Test
2020
@test res1.n == 4
2121
@test res2 == 76
2222
@test res3.n == 4 && res3.c == 5
23-
@test res4.maxorder == 4 && res4.a == 30 && res4.b==5
23+
@test res4.maxorder == 4 && res4.coeffs[1] == 30 && res4.coeffs[2]==5
2424
@test res5 == Polynomial([1.0, 10.0, 30, 30, 5])
2525
@test res6.c.data res7.c.data
2626
@test all(x->sum(x) == 4, res7.c.data)
27-
@test all(x->sum(x) == 3, res8.a.data) && all(x->sum(x) == 4, res8.b.data) && length(res8.a.data) == 30 && length(res8.b.data) == 5
27+
@test all(x->sum(x) == 3, res8.coeffs[1].data) && all(x->sum(x) == 4, res8.coeffs[2].data) && length(res8.coeffs[1].data) == 30 && length(res8.coeffs[2].data) == 5
2828
@test all(x->all(c->sum(c) == x[1]-1, x[2].data), enumerate(res9.coeffs))
2929
@test res10 res5
3030
@test res11 == res5

0 commit comments

Comments
 (0)