Skip to content

Commit 5ea1125

Browse files
committed
Fixes
1 parent 08ca2b7 commit 5ea1125

File tree

3 files changed

+31
-125
lines changed

3 files changed

+31
-125
lines changed

src/monomial_vector.jl

Lines changed: 19 additions & 113 deletions
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,68 +179,12 @@ 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-
245-
function _fill_exponents!(
246-
Z::Vector{Vector{Int}},
247-
n,
248-
degs::AbstractVector{Int},
249-
::Type{V},
250-
::Type{MP.Graded{M}},
251-
filter::Function,
252-
) where {V,M}
253-
# For non-commutative, lower degree need to create a vector of exponent as large as for the highest degree
254-
maxdeg = maximum(degs, init = 0)
255-
for deg in sort(degs)
256-
_fill_exponents!(Z, n, deg, V, M, filter, maxdeg)
257-
end
258-
return
259-
end
260-
261182
# List exponents in decreasing Graded Lexicographic Order
262-
function _all_exponents(
263-
n,
264-
degs::AbstractVector{Int},
265-
::Type{V},
266-
::Type{M},
267-
filter::Function,
268-
) where {V,M}
269-
Z = Vector{Int}[]
270-
_fill_exponents!(Z, n, degs, V, M, filter)
271-
_isless = let M = M
272-
(a, b) -> MP.compare(a, b, M) < 0
273-
end
274-
@assert issorted(Z, lt = _isless)
275-
return Z
276-
end
277-
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-
296183
function getvarsforlength(vars::Vector{<:Variable{<:NonCommutative}}, len::Int)
297184
n = length(vars)
298185
return map(i -> vars[((i-1)%n)+1], 1:len)
299186
end
187+
300188
function MonomialVector(
301189
vars::Vector{<:Variable{<:NonCommutative,M}},
302190
degs::AbstractVector{Int},
@@ -313,6 +201,23 @@ function MonomialVector(
313201
v = isempty(Z) ? vars : getvarsforlength(vars, length(first(Z)))
314202
return MonomialVector(v, Z)
315203
end
204+
205+
function MonomialVector(
206+
vars::Vector{<:Variable{<:Commutative,M}},
207+
degs::AbstractVector{Int},
208+
filter::Function = x -> true,
209+
) where {M}
210+
vars = unique!(sort(vars, rev = true))
211+
Z = Iterators.Filter(MP.ExponentsIterator{M}(
212+
zeros(Int, length(vars));
213+
mindegree = minimum(degs),
214+
maxdegree = maximum(degs),
215+
)) do z
216+
filter(Monomial(vars, z))
217+
end
218+
return MonomialVector(v, Z)
219+
end
220+
316221
function MonomialVector(
317222
vars::Vector{<:Variable},
318223
degs::Int,
@@ -324,6 +229,7 @@ end
324229
function MP.monomials(vars::AbstractVector{<:Variable}, args...)
325230
return MonomialVector(vars, args...)
326231
end
232+
327233
function MP.monomials(vars::Tuple{Vararg{Variable}}, args...)
328234
return monomials([vars...], args...)
329235
end

src/promote.jl

Lines changed: 12 additions & 0 deletions
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}},

src/var.jl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,3 @@ function mergevars_of(::Type{Variable{V,M}}, polys::AbstractVector) where {V,M}
291291
# TODO avoid computing `maps`
292292
return mergevars(varsvec)
293293
end
294-
295-
function MP.promote_variables(m1::Monomial, m2::Monomial)
296-
if MP.variables(m1) == MP.variables(m2)
297-
return m1, m2
298-
end
299-
allvars, maps = mergevars([MP.variables(m1), MP.variables(m2)])
300-
z1 = zeros(Int, length(allvars))
301-
z1[maps[1]] = m1.z
302-
z2 = zeros(Int, length(allvars))
303-
z2[maps[2]] = m2.z
304-
return Monomial(allvars, z1), Monomial(allvars, z2)
305-
end

0 commit comments

Comments
 (0)