Skip to content

Commit aaf2825

Browse files
authored
Use cmp instead of compare (#170)
* Use cmp instead of compare * Upate ci * Fix * Fixes * checkout MP * Fixes * Bump MP to v0.5.8 * Fixes * Fixes * Test with bl/revo * Fix * Fix * Fix * Bump [email protected]
1 parent e17b88d commit aaf2825

File tree

8 files changed

+66
-224
lines changed

8 files changed

+66
-224
lines changed

.github/workflows/ci.yml

+5-13
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,18 @@ jobs:
2727
os: ubuntu-latest
2828
arch: x64
2929
steps:
30-
- uses: actions/checkout@v3
31-
- uses: julia-actions/setup-julia@v1
30+
- uses: actions/checkout@v4
31+
- uses: julia-actions/setup-julia@v2
3232
with:
3333
version: ${{ matrix.version }}
3434
arch: ${{ matrix.arch }}
35-
- uses: actions/cache@v1
36-
env:
37-
cache-name: cache-artifacts
38-
with:
39-
path: ~/.julia/artifacts
40-
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
41-
restore-keys: |
42-
${{ runner.os }}-test-${{ env.cache-name }}-
43-
${{ runner.os }}-test-
44-
${{ runner.os }}-
35+
- uses: julia-actions/cache@v2
4536
- uses: julia-actions/julia-buildpkg@v1
4637
- uses: julia-actions/julia-runtest@v1
4738
with:
4839
depwarn: error
4940
- uses: julia-actions/julia-processcoverage@v1
50-
- uses: codecov/codecov-action@v3
41+
- uses: codecov/codecov-action@v4
5142
with:
5243
file: lcov.info
44+
token: ${{ secrets.CODECOV_TOKEN }}

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1212
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1313

1414
[compat]
15-
MultivariatePolynomials = "0.5.6"
15+
MultivariatePolynomials = "0.5.9"
1616
MutableArithmetics = "1"
1717
Reexport = "1"
1818
julia = "1"

src/comp.jl

+9-108
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Base.==
22

33
# TODO This should be in Base with T instead of Variable{V,M}.
44
# See https://github.com/blegat/MultivariatePolynomials.jl/issues/3
5-
function (==)(x::Vector{Variable{V,M}}, y::Vector{Variable{V,M}}) where {V,M}
5+
function Base.:(==)(x::Vector{Variable{V,M}}, y::Vector{Variable{V,M}}) where {V,M}
66
if length(x) != length(y)
77
false
88
else
@@ -20,119 +20,20 @@ end
2020

2121
const AnyCommutative{O} = Union{Commutative{O},NonCommutative{O}}
2222

23-
function (==)(
24-
x::Variable{<:AnyCommutative{CreationOrder}},
25-
y::Variable{<:AnyCommutative{CreationOrder}},
26-
)
27-
return x.variable_order.order.id == y.variable_order.order.id &&
28-
x.kind == y.kind
29-
end
30-
31-
function Base.isless(
23+
function Base.cmp(
3224
x::Variable{<:AnyCommutative{CreationOrder}},
3325
y::Variable{<:AnyCommutative{CreationOrder}},
3426
)
3527
if x.variable_order.order.id == y.variable_order.order.id
36-
return isless(y.kind, x.kind)
28+
return cmp(y.kind, x.kind)
3729
else
38-
return isless(y.variable_order.order.id, x.variable_order.order.id)
39-
end
40-
end
41-
42-
# Comparison of Monomial
43-
44-
function MP.compare(x::Monomial{V,M}, y::Monomial{V,M}) where {V,M}
45-
return MP.compare(x, y, M)
46-
end
47-
48-
function MP.compare(
49-
x::Monomial{V},
50-
y::Monomial{V},
51-
::Type{MP.InverseLexOrder},
52-
) where {V}
53-
i = MP.nvariables(x)
54-
j = MP.nvariables(y)
55-
@inbounds while i >= 1 && j >= 1
56-
if x.vars[i] < y.vars[j]
57-
if x.z[i] == 0
58-
i -= 1
59-
else
60-
return 1
61-
end
62-
elseif x.vars[i] > y.vars[j]
63-
if y.z[j] == 0
64-
j -= 1
65-
else
66-
return -1
67-
end
68-
elseif x.z[i] != y.z[j]
69-
return x.z[i] - y.z[j]
70-
else
71-
i -= 1
72-
j -= 1
73-
end
30+
return cmp(y.variable_order.order.id, x.variable_order.order.id)
7431
end
75-
return 0
7632
end
7733

78-
function MP.compare(
79-
x::Monomial{V},
80-
y::Monomial{V},
81-
::Type{MP.LexOrder},
82-
) where {V}
83-
i = j = 1
84-
@inbounds while i <= nvariables(x) && j <= nvariables(y)
85-
if x.vars[i] > y.vars[j]
86-
if x.z[i] == 0
87-
i += 1
88-
else
89-
return 1
90-
end
91-
elseif x.vars[i] < y.vars[j]
92-
if y.z[j] == 0
93-
j += 1
94-
else
95-
return -1
96-
end
97-
elseif x.z[i] != y.z[j]
98-
return x.z[i] - y.z[j]
99-
else
100-
i += 1
101-
j += 1
102-
end
103-
end
104-
@inbounds while i <= nvariables(x)
105-
if x.z[i] > 0
106-
return 1
107-
end
108-
i += 1
109-
end
110-
@inbounds while j <= nvariables(y)
111-
if y.z[j] > 0
112-
return -1
113-
end
114-
j += 1
115-
end
116-
return 0
117-
end
118-
119-
function (==)(x::Monomial{V,M}, y::Monomial{V,M}) where {V,M}
120-
return MP.compare(x, y) == 0
121-
end
122-
function (==)(x::Variable{V,M}, y::Monomial{V,M}) where {V,M}
123-
return convert(Monomial{V,M}, x) == y
124-
end
125-
126-
# graded lex ordering
127-
function Base.isless(x::Monomial{V,M}, y::Monomial{V,M}) where {V,M}
128-
return MP.compare(x, y) < 0
129-
end
130-
function Base.isless(x::Monomial{V,M}, y::Variable{V,M}) where {V,M}
131-
return isless(x, convert(Monomial{V,M}, y))
132-
end
133-
function Base.isless(x::Variable{V,M}, y::Monomial{V,M}) where {V,M}
134-
return isless(convert(Monomial{V,M}, x), y)
135-
end
34+
# TODO remove
35+
Base.:(==)(x::Variable, y::Variable) = iszero(cmp(x, y))
36+
Base.:(==)(x::Monomial, y::Monomial) = iszero(cmp(x, y))
13637

13738
# Comparison of MonomialVector
13839
function (==)(x::MonomialVector{V,M}, y::MonomialVector{V,M}) where {V,M}
@@ -143,8 +44,8 @@ function (==)(x::MonomialVector{V,M}, y::MonomialVector{V,M}) where {V,M}
14344
# Should be sorted in the same order since the non-common
14445
# polyvar should have exponent 0
14546
for (a, b) in zip(x.Z, y.Z)
146-
A = zeros(length(allvars))
147-
B = zeros(length(allvars))
47+
A = zeros(Int, length(allvars))
48+
B = zeros(Int, length(allvars))
14849
A[maps[1]] = a
14950
B[maps[2]] = b
15051
if A != B

src/monomial_vector.jl

+29-83
Original file line numberDiff line numberDiff line change
@@ -148,62 +148,6 @@ function _error_for_negative_degree(deg)
148148
end
149149
end
150150

151-
const _Lex = Union{MP.LexOrder,MP.InverseLexOrder}
152-
153-
_last_lex_index(n, ::Type{MP.LexOrder}) = n
154-
_prev_lex_index(i, ::Type{MP.LexOrder}) = i - 1
155-
_not_first_indices(n, ::Type{MP.LexOrder}) = n:-1:2
156-
_last_lex_index(_, ::Type{MP.InverseLexOrder}) = 1
157-
_prev_lex_index(i, ::Type{MP.InverseLexOrder}) = i + 1
158-
_not_first_indices(n, ::Type{MP.InverseLexOrder}) = 1:(n-1)
159-
160-
function _fill_exponents!(Z, n, degs, ::Type{Commutative}, M::Type{<:_Lex}, filter::Function)
161-
_error_for_negative_degree.(degs)
162-
maxdeg = maximum(degs, init = 0)
163-
I = _not_first_indices(n, M)
164-
z = zeros(Int, n)
165-
while true
166-
deg = sum(z)
167-
if deg in degs && filter(z)
168-
push!(Z, z)
169-
z = copy(z)
170-
end
171-
if deg == maxdeg
172-
i = findfirst(i -> !iszero(z[i]), I)
173-
if isnothing(i)
174-
break
175-
end
176-
j = I[i]
177-
z[j] = 0
178-
z[_prev_lex_index(j, M)] += 1
179-
else
180-
z[_last_lex_index(n, M)] += 1
181-
end
182-
end
183-
end
184-
185-
function _fill_exponents!(Z, n, deg, ::Type{Commutative}, M::Type{<:_Lex}, filter::Function, ::Int)
186-
_error_for_negative_degree(deg)
187-
I = _not_first_indices(n, M)
188-
z = zeros(Int, n)
189-
z[_last_lex_index(n, M)] = deg
190-
while true
191-
if filter(z)
192-
push!(Z, z)
193-
z = copy(z)
194-
end
195-
i = findfirst(i -> !iszero(z[i]), I)
196-
if isnothing(i)
197-
break
198-
end
199-
j = I[i]
200-
p = z[j]
201-
z[j] = 0
202-
z[_last_lex_index(n, M)] = p - 1
203-
z[_prev_lex_index(j, M)] += 1
204-
end
205-
end
206-
207151
function _fill_noncomm_exponents_rec!(Z, z, i, n, deg, ::Type{MP.LexOrder}, filter::Function)
208152
if deg == 0
209153
if filter(z)
@@ -235,13 +179,6 @@ function _fill_exponents!(
235179
return reverse!(view(Z, start:length(Z)))
236180
end
237181

238-
function _fill_exponents!(Z, n, deg, ::Type{V}, ::Type{MP.Reverse{M}}, args...) where {V,M}
239-
prev = lastindex(Z)
240-
_fill_exponents!(Z, n, deg, V, M, args...)
241-
reverse!(view(Z, (prev + 1):lastindex(Z)))
242-
return
243-
end
244-
245182
function _fill_exponents!(
246183
Z::Vector{Vector{Int}},
247184
n,
@@ -266,7 +203,7 @@ function _all_exponents(
266203
::Type{M},
267204
filter::Function,
268205
) where {V,M}
269-
Z = Vector{Vector{Int}}()
206+
Z = Vector{Int}[]
270207
_fill_exponents!(Z, n, degs, V, M, filter)
271208
_isless = let M = M
272209
(a, b) -> MP.compare(a, b, M) < 0
@@ -275,28 +212,11 @@ function _all_exponents(
275212
return Z
276213
end
277214

278-
function MonomialVector(
279-
vars::Vector{<:Variable{<:Commutative,M}},
280-
degs::AbstractVector{Int},
281-
filter::Function = x -> true,
282-
) where {M}
283-
vars = unique!(sort(vars, rev = true))
284-
return MonomialVector(
285-
vars,
286-
_all_exponents(
287-
length(vars),
288-
degs,
289-
Commutative,
290-
M,
291-
z -> filter(Monomial(vars, z)),
292-
),
293-
)
294-
end
295-
296215
function getvarsforlength(vars::Vector{<:Variable{<:NonCommutative}}, len::Int)
297216
n = length(vars)
298217
return map(i -> vars[((i-1)%n)+1], 1:len)
299218
end
219+
300220
function MonomialVector(
301221
vars::Vector{<:Variable{<:NonCommutative,M}},
302222
degs::AbstractVector{Int},
@@ -313,6 +233,31 @@ function MonomialVector(
313233
v = isempty(Z) ? vars : getvarsforlength(vars, length(first(Z)))
314234
return MonomialVector(v, Z)
315235
end
236+
237+
function MonomialVector(
238+
vars::Vector{<:Variable{<:Commutative,M}},
239+
degs::AbstractVector{Int},
240+
filter::Function = x -> true,
241+
) where {M}
242+
vars = unique!(sort(vars, rev = true))
243+
if isempty(degs)
244+
mindegree = 0
245+
maxdegree = -1
246+
else
247+
mindegree = minimum(degs)
248+
maxdegree = maximum(degs)
249+
end
250+
Z = Iterators.Filter(MP.ExponentsIterator{M}(
251+
zeros(Int, length(vars));
252+
mindegree,
253+
maxdegree,
254+
)) do z
255+
mono = Monomial(vars, z)
256+
MP.degree(mono) in degs && filter(mono)
257+
end
258+
return MonomialVector(vars, collect(Z))
259+
end
260+
316261
function MonomialVector(
317262
vars::Vector{<:Variable},
318263
degs::Int,
@@ -324,6 +269,7 @@ end
324269
function MP.monomials(vars::AbstractVector{<:Variable}, args...)
325270
return MonomialVector(vars, args...)
326271
end
272+
327273
function MP.monomials(vars::Tuple{Vararg{Variable}}, args...)
328274
return monomials([vars...], args...)
329275
end
@@ -390,7 +336,7 @@ end
390336
function MonomialVector{V,M}(X::DMonoVec{V,M}) where {V,M}
391337
allvars, Z = buildZvarsvec(Variable{V,M}, X)
392338
_isless = let M = M
393-
(a, b) -> MP.compare(a, b, M) < 0
339+
(a, b) -> cmp(M(), a, b) < 0
394340
end
395341
sort!(Z, lt = _isless)
396342
dups = findall(i -> Z[i] == Z[i-1], 2:length(Z))

src/operators.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function MA.operate!(
109109
end
110110

111111
function _exponents_compare(q::Polynomial{V,M}, j, e) where {V,M}
112-
return MP.compare(q.x.Z[j], e, M)
112+
return cmp(M(), q.x.Z[j], e)
113113
end
114114

115115
# TODO need to check that this also works for non-commutative

src/poly.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ function removedups_to!(
219219
::Type{M},
220220
) where {T,M}
221221
_isless = let M = M
222-
(a, b) -> MP.compare(a, b, M) < 0
222+
(a, b) -> cmp(M(), a, b) < 0
223223
end
224224
σ = sortperm(Zdup, lt = _isless)
225225
i = 0

src/promote.jl

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
function MP.promote_variables(m1::Monomial, m2::Monomial)
2+
if MP.variables(m1) == MP.variables(m2)
3+
return m1, m2
4+
end
5+
allvars, maps = mergevars([MP.variables(m1), MP.variables(m2)])
6+
z1 = zeros(Int, length(allvars))
7+
z1[maps[1]] = m1.z
8+
z2 = zeros(Int, length(allvars))
9+
z2[maps[2]] = m2.z
10+
return Monomial(allvars, z1), Monomial(allvars, z2)
11+
end
12+
113
function MP.promote_rule_constant(
214
::Type{T},
315
::Type{<:DMonomialLike{V,M}},

0 commit comments

Comments
 (0)