Skip to content

Commit 4443d7f

Browse files
committed
refresh MacMahon Omega file
1 parent c9dd1e8 commit 4443d7f

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

src/sage/rings/polynomial/omega.py

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@
4949
# https://www.gnu.org/licenses/
5050
# ****************************************************************************
5151
import operator
52+
5253
from sage.misc.cachefunc import cached_function
5354

5455

5556
def MacMahonOmega(var, expression, denominator=None, op=operator.ge,
56-
Factorization_sort=False, Factorization_simplify=True):
57+
Factorization_sort=False, Factorization_simplify=True):
5758
r"""
5859
Return `\Omega_{\mathrm{op}}` of ``expression`` with respect to ``var``.
5960
@@ -221,33 +222,33 @@ def MacMahonOmega(var, expression, denominator=None, op=operator.ge,
221222
sage: MacMahonOmega(mu, 1, [1 - x*mu], op=operator.lt)
222223
Traceback (most recent call last):
223224
...
224-
NotImplementedError: At the moment, only Omega_ge is implemented.
225+
NotImplementedError: only Omega_ge is implemented
225226
226227
sage: MacMahonOmega(mu, 1, Factorization([(1 - x*mu, -1)]))
227228
Traceback (most recent call last):
228229
...
229-
ValueError: Factorization (-mu*x + 1)^-1 of the denominator
230-
contains negative exponents.
230+
ValueError: factorization (-mu*x + 1)^-1 of the denominator
231+
contains negative exponents
231232
232233
sage: MacMahonOmega(2*mu, 1, [1 - x*mu])
233234
Traceback (most recent call last):
234235
...
235-
ValueError: 2*mu is not a variable.
236+
ValueError: 2*mu is not a variable
236237
237238
sage: MacMahonOmega(mu, 1, Factorization([(0, 2)]))
238239
Traceback (most recent call last):
239240
...
240-
ZeroDivisionError: Denominator contains a factor 0.
241+
ZeroDivisionError: denominator contains a factor 0
241242
242243
sage: MacMahonOmega(mu, 1, [2 - x*mu])
243244
Traceback (most recent call last):
244245
...
245-
NotImplementedError: Factor 2 - x*mu is not normalized.
246+
NotImplementedError: factor 2 - x*mu is not normalized
246247
247248
sage: MacMahonOmega(mu, 1, [1 - x*mu - mu^2])
248249
Traceback (most recent call last):
249250
...
250-
NotImplementedError: Cannot handle factor 1 - x*mu - mu^2.
251+
NotImplementedError: cannot handle factor 1 - x*mu - mu^2
251252
252253
::
253254
@@ -259,12 +260,14 @@ def MacMahonOmega(var, expression, denominator=None, op=operator.ge,
259260
from sage.arith.misc import factor
260261
from sage.misc.misc_c import prod
261262
from sage.rings.integer_ring import ZZ
262-
from sage.rings.polynomial.laurent_polynomial_ring \
263-
import LaurentPolynomialRing, LaurentPolynomialRing_univariate
263+
from sage.rings.polynomial.laurent_polynomial_ring import (
264+
LaurentPolynomialRing,
265+
LaurentPolynomialRing_univariate,
266+
)
264267
from sage.structure.factorization import Factorization
265268

266269
if op != operator.ge:
267-
raise NotImplementedError('At the moment, only Omega_ge is implemented.')
270+
raise NotImplementedError('only Omega_ge is implemented')
268271

269272
if denominator is None:
270273
if isinstance(expression, Factorization):
@@ -285,9 +288,9 @@ def MacMahonOmega(var, expression, denominator=None, op=operator.ge,
285288
if not isinstance(denominator, Factorization):
286289
denominator = factor(denominator)
287290
if not denominator.is_integral():
288-
raise ValueError('Factorization {} of the denominator '
289-
'contains negative exponents.'.format(denominator))
290-
numerator *= ZZ(1) / denominator.unit()
291+
raise ValueError(f'factorization {denominator} of '
292+
'the denominator contains negative exponents')
293+
numerator *= ZZ.one() / denominator.unit()
291294
factors_denominator = tuple(factor
292295
for factor, exponent in denominator
293296
for _ in range(exponent))
@@ -304,30 +307,30 @@ def MacMahonOmega(var, expression, denominator=None, op=operator.ge,
304307
L = LaurentPolynomialRing(L0, var)
305308
var = L.gen()
306309
else:
307-
raise ValueError('{} is not a variable.'.format(var))
310+
raise ValueError(f'{var} is not a variable')
308311

309312
other_factors = []
310313
to_numerator = []
311314
decoded_factors = []
312-
for factor in factors_denominator:
313-
factor = L(factor)
314-
D = factor.monomial_coefficients()
315+
for fact in factors_denominator:
316+
fac = L(fact)
317+
D = fac.monomial_coefficients()
315318
if not D:
316-
raise ZeroDivisionError('Denominator contains a factor 0.')
319+
raise ZeroDivisionError('denominator contains a factor 0')
317320
elif len(D) == 1:
318321
exponent, coefficient = next(iter(D.items()))
319322
if exponent == 0:
320-
other_factors.append(L0(factor))
323+
other_factors.append(L0(fac))
321324
else:
322-
to_numerator.append(factor)
325+
to_numerator.append(fac)
323326
elif len(D) == 2:
324327
if D.get(0, 0) != 1:
325-
raise NotImplementedError('Factor {} is not normalized.'.format(factor))
328+
raise NotImplementedError(f'factor {fac} is not normalized')
326329
D.pop(0)
327330
exponent, coefficient = next(iter(D.items()))
328331
decoded_factors.append((-coefficient, exponent))
329332
else:
330-
raise NotImplementedError('Cannot handle factor {}.'.format(factor))
333+
raise NotImplementedError(f'cannot handle factor {fac}')
331334
numerator = L(numerator) / prod(to_numerator)
332335

333336
result_numerator, result_factors_denominator = \
@@ -336,13 +339,13 @@ def MacMahonOmega(var, expression, denominator=None, op=operator.ge,
336339
return Factorization([], unit=result_numerator)
337340

338341
return Factorization([(result_numerator, 1)] +
339-
list((f, -1) for f in other_factors) +
340-
list((1-f, -1) for f in result_factors_denominator),
342+
[(f, -1) for f in other_factors] +
343+
[(1 - f, -1) for f in result_factors_denominator],
341344
sort=Factorization_sort,
342345
simplify=Factorization_simplify)
343346

344347

345-
def _simplify_(numerator, terms):
348+
def _simplify_(numerator, terms) -> tuple:
346349
r"""
347350
Cancels common factors of numerator and denominator.
348351
@@ -438,7 +441,7 @@ def _Omega_(A, decoded_factors):
438441
(x + 1) * (-x*y + 1)^-1
439442
"""
440443
if not decoded_factors:
441-
return sum(c for a, c in A.items() if a >= 0), tuple()
444+
return sum(c for a, c in A.items() if a >= 0), ()
442445

443446
# Below we sort to make the caching more efficient. Doing this here
444447
# (in contrast to directly in Omega_ge) results in much cleaner
@@ -459,7 +462,7 @@ def _Omega_(A, decoded_factors):
459462
numerator += c * n.subs(rules)
460463

461464
if numerator == 0:
462-
factors_denominator = tuple()
465+
factors_denominator = ()
463466
return _simplify_(numerator,
464467
tuple(f.subs(rules) for f in factors_denominator))
465468

@@ -557,27 +560,26 @@ def Omega_ge(a, exponents):
557560
logger.info('Omega_ge: a=%s, exponents=%s', a, exponents)
558561

559562
from sage.arith.functions import lcm
560-
from sage.arith.srange import srange
561563
from sage.rings.integer_ring import ZZ
562-
from sage.rings.polynomial.laurent_polynomial_ring import LaurentPolynomialRing
563564
from sage.rings.number_field.number_field import CyclotomicField
565+
from sage.rings.polynomial.laurent_polynomial_ring import LaurentPolynomialRing
564566

565567
if not exponents or any(e == 0 for e in exponents):
566568
raise NotImplementedError
567569

568-
rou = sorted(set(abs(e) for e in exponents) - set([1]))
570+
rou = sorted({abse for e in exponents if (abse := abs(e)) != 1})
569571
ellcm = lcm(rou)
570572
B = CyclotomicField(ellcm, 'zeta')
571573
zeta = B.gen()
572-
z_names = tuple('z{}'.format(i) for i in range(len(exponents)))
574+
z_names = tuple(f'z{i}' for i in range(len(exponents)))
573575
L = LaurentPolynomialRing(B, ('t',) + z_names, len(z_names) + 1)
574576
t = L.gens()[0]
575577
Z = LaurentPolynomialRing(ZZ, z_names, len(z_names))
576578
powers = {i: L(zeta**(ellcm//i)) for i in rou}
577579
powers[2] = L(-1)
578580
powers[1] = L(1)
579581
exponents_and_values = tuple(
580-
(e, tuple(powers[abs(e)]**j * z for j in srange(abs(e))))
582+
(e, tuple(powers[abs(e)]**j * z for j in range(abs(e))))
581583
for z, e in zip(L.gens()[1:], exponents))
582584
x = tuple(v for e, v in exponents_and_values if e > 0)
583585
y = tuple(v for e, v in exponents_and_values if e < 0)
@@ -597,9 +599,8 @@ def subs_e(e):
597599
e[p] = e[p] // exponent
598600
return tuple(e)
599601
parent = expression.parent()
600-
result = parent({subs_e(e): c
601-
for e, c in expression.monomial_coefficients().items()})
602-
return result
602+
return parent({subs_e(e): c
603+
for e, c in expression.monomial_coefficients().items()})
603604

604605
def de_power(expression):
605606
expression = Z(expression)
@@ -719,8 +720,8 @@ def _Omega_numerator_(a, x, y, t):
719720
from sage.arith.srange import srange
720721
from sage.misc.misc_c import prod
721722

722-
x_flat = sum(x, tuple())
723-
y_flat = sum(y, tuple())
723+
x_flat = sum(x, ())
724+
y_flat = sum(y, ())
724725
n = len(x_flat)
725726
m = len(y_flat)
726727
xy = x_flat + y_flat
@@ -807,12 +808,13 @@ def _Omega_numerator_P_(a, x, y, t):
807808
p2 = Pprev.subs({t: x2})
808809
logger.debug('Omega_numerator: P(%s): preparing...', n)
809810
dividend = x1 * (1-x2) * prod(1 - x2*yy for yy in y) * p1 - \
810-
x2 * (1-x1) * prod(1 - x1*yy for yy in y) * p2
811+
x2 * (1-x1) * prod(1 - x1*yy for yy in y) * p2
811812
logger.debug('Omega_numerator: P(%s): dividing...', n)
812813
q, r = dividend.quo_rem(x1 - x2)
813814
assert r == 0
814815
result = q
815-
logger.debug('Omega_numerator: P(%s) has %s terms', n, result.number_of_terms())
816+
logger.debug('Omega_numerator: P(%s) has %s terms', n,
817+
result.number_of_terms())
816818
return result
817819

818820

@@ -900,11 +902,11 @@ def _Omega_factors_denominator_(x, y):
900902
from sage.misc.misc_c import prod
901903

902904
result = tuple(prod(1 - xx for xx in gx) for gx in x) + \
903-
sum(((prod(1 - xx*yy for xx in gx for yy in gy),)
904-
if len(gx) != len(gy)
905-
else tuple(prod(1 - xx*yy for xx in gx) for yy in gy)
906-
for gx in x for gy in y),
907-
tuple())
905+
sum(((prod(1 - xx*yy for xx in gx for yy in gy),)
906+
if len(gx) != len(gy)
907+
else tuple(prod(1 - xx*yy for xx in gx) for yy in gy)
908+
for gx in x for gy in y),
909+
())
908910

909911
logger.info('Omega_denominator: %s factors', len(result))
910912
return result

0 commit comments

Comments
 (0)