Skip to content

Commit e2296f0

Browse files
committed
use parent in weyl_algebra.py
1 parent 439065e commit e2296f0

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

src/sage/algebras/weyl_algebra.py

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
from sage.misc.lazy_attribute import lazy_attribute
2323
from sage.misc.misc_c import prod
2424
from sage.structure.richcmp import richcmp
25-
from sage.structure.element import AlgebraElement
25+
from sage.structure.element import Element
26+
from sage.structure.parent import Parent
2627
from sage.structure.unique_representation import UniqueRepresentation
2728
from sage.categories.action import Action
2829
from sage.categories.rings import Rings
@@ -36,7 +37,7 @@
3637
from sage.structure.global_options import GlobalOptions
3738

3839

39-
def repr_from_monomials(monomials, term_repr, use_latex=False):
40+
def repr_from_monomials(monomials, term_repr, use_latex=False) -> str:
4041
r"""
4142
Return a string representation of an element of a free module
4243
from the dictionary ``monomials``.
@@ -161,7 +162,7 @@ def repr_from_monomials(monomials, term_repr, use_latex=False):
161162
return ret
162163

163164

164-
def repr_factored(w, latex_output=False):
165+
def repr_factored(w, latex_output=False) -> str:
165166
r"""
166167
Return a string representation of ``w`` with the `dx_i` generators
167168
factored on the right.
@@ -231,11 +232,11 @@ def repr_dx(k):
231232
return ret
232233

233234

234-
class DifferentialWeylAlgebraElement(AlgebraElement):
235+
class DifferentialWeylAlgebraElement(Element):
235236
"""
236237
An element in a differential Weyl algebra.
237238
"""
238-
def __init__(self, parent, monomials):
239+
def __init__(self, parent, monomials) -> None:
239240
"""
240241
Initialize ``self``.
241242
@@ -246,10 +247,10 @@ def __init__(self, parent, monomials):
246247
sage: elt = ((x^3-z)*dx + dy)^2
247248
sage: TestSuite(elt).run()
248249
"""
249-
AlgebraElement.__init__(self, parent)
250+
Element.__init__(self, parent)
250251
self.__monomials = monomials
251252

252-
def _repr_(self):
253+
def _repr_(self) -> str:
253254
r"""
254255
Return a string representation of ``self``.
255256
@@ -279,7 +280,7 @@ def term(m):
279280
return ret
280281
return repr_from_monomials(self.list(), term)
281282

282-
def _latex_(self):
283+
def _latex_(self) -> str:
283284
r"""
284285
Return a `\LaTeX` representation of ``self``.
285286
@@ -319,15 +320,15 @@ def half_term(mon, polynomial):
319320
return ret
320321
p = half_term(m[0], True)
321322
d = half_term(m[1], False)
322-
if p == '1': # No polynomial part
323+
if p == '1': # No polynomial part
323324
return d
324-
elif d == '1': # No differential part
325+
elif d == '1': # No differential part
325326
return p
326327
else:
327328
return p + ' ' + d
328329
return repr_from_monomials(self.list(), term, True)
329330

330-
def _richcmp_(self, other, op):
331+
def _richcmp_(self, other, op) -> bool:
331332
"""
332333
Rich comparison for equal parents.
333334
@@ -369,7 +370,7 @@ def __neg__(self):
369370
dy + z*dx - 3*x*dx
370371
"""
371372
return self.__class__(self.parent(),
372-
{m:-c for m, c in self.__monomials.items()})
373+
{m: -c for m, c in self.__monomials.items()})
373374

374375
def _add_(self, other):
375376
"""
@@ -399,28 +400,30 @@ def _mul_(self, other):
399400
dx*dy*dz^2 + x^3*dx^2*dz^2 - z*dx^2*dz^2 - 10*x*dy - 10*x^4*dx
400401
+ 10*x*z*dx - 10*x^3 + 10*z
401402
"""
402-
add_tuples = lambda x,y: tuple(a + y[i] for i,a in enumerate(x))
403+
def add_tuples(x, y):
404+
return tuple(a + y[i] for i, a in enumerate(x))
405+
403406
d = {}
404407
n = self.parent()._n
405-
t = tuple([0]*n)
408+
t = tuple([0] * n)
406409
zero = self.parent().base_ring().zero()
407410
for ml in self.__monomials:
408411
cl = self.__monomials[ml]
409412
for mr in other.__monomials:
410413
cr = other.__monomials[mr]
411-
cur = [ ((mr[0], t), cl * cr) ]
414+
cur = [((mr[0], t), cl * cr)]
412415
for i, p in enumerate(ml[1]):
413416
for _ in range(p):
414417
next = []
415418
for m, c in cur: # Distribute and apply the derivative
416419
diff = list(m[1])
417420
diff[i] += 1
418-
next.append( ((m[0], tuple(diff)), c) )
421+
next.append(((m[0], tuple(diff)), c))
419422
if m[0][i] != 0:
420423
poly = list(m[0])
421424
c *= poly[i]
422425
poly[i] -= 1
423-
next.append( ((tuple(poly), m[1]), c) )
426+
next.append(((tuple(poly), m[1]), c))
424427
cur = next
425428

426429
for m, c in cur:
@@ -446,7 +449,7 @@ def _rmul_(self, other):
446449
if other == 0:
447450
return self.parent().zero()
448451
M = self.__monomials
449-
return self.__class__(self.parent(), {t: other*M[t] for t in M})
452+
return self.__class__(self.parent(), {t: other * M[t] for t in M})
450453

451454
def _lmul_(self, other):
452455
"""
@@ -463,9 +466,9 @@ def _lmul_(self, other):
463466
if other == 0:
464467
return self.parent().zero()
465468
M = self.__monomials
466-
return self.__class__(self.parent(), {t: M[t]*other for t in M})
469+
return self.__class__(self.parent(), {t: M[t] * other for t in M})
467470

468-
def monomial_coefficients(self, copy=True):
471+
def monomial_coefficients(self, copy=True) -> dict:
469472
"""
470473
Return a dictionary which has the basis keys in the support
471474
of ``self`` as keys and their corresponding coefficients
@@ -509,7 +512,7 @@ def __iter__(self):
509512
"""
510513
return iter(self.list())
511514

512-
def list(self):
515+
def list(self) -> list:
513516
"""
514517
Return ``self`` as a list.
515518
@@ -529,9 +532,9 @@ def list(self):
529532
(((1, 0, 0), (1, 0, 0)), -3)]
530533
"""
531534
return sorted(self.__monomials.items(),
532-
key=lambda x: (-sum(x[0][1]), x[0][1], -sum(x[0][0]), x[0][0]) )
535+
key=lambda x: (-sum(x[0][1]), x[0][1], -sum(x[0][0]), x[0][0]))
533536

534-
def support(self):
537+
def support(self) -> list:
535538
"""
536539
Return the support of ``self``.
537540
@@ -567,15 +570,15 @@ def __truediv__(self, x):
567570
F = self.parent()
568571
D = self.__monomials
569572
if F.base_ring().is_field():
570-
x = F.base_ring()( x )
573+
x = F.base_ring()(x)
571574
x_inv = x**-1
572-
D = blas.linear_combination( [ ( D, x_inv ) ] )
575+
D = blas.linear_combination([(D, x_inv)])
573576

574577
return self.__class__(F, D)
575578

576579
return self.__class__(F, {t: D[t]._divide_if_possible(x) for t in D})
577580

578-
def factor_differentials(self):
581+
def factor_differentials(self) -> dict:
579582
"""
580583
Return a dict representing ``self`` with the differentials
581584
factored out.
@@ -617,7 +620,7 @@ def factor_differentials(self):
617620
DW = self.parent()
618621
P = DW.polynomial_ring()
619622
gens = P.gens()
620-
for m,c in self:
623+
for m, c in self:
621624
x, dx = m
622625
if dx not in ret:
623626
ret[dx] = P.zero()
@@ -743,7 +746,7 @@ def __classcall__(cls, R, names=None):
743746
raise TypeError("argument R must be a commutative ring")
744747
return super().__classcall__(cls, R, names)
745748

746-
def __init__(self, R, names=None):
749+
def __init__(self, R, names=None) -> None:
747750
r"""
748751
Initialize ``self``.
749752
@@ -765,9 +768,9 @@ def __init__(self, R, names=None):
765768
cat = AlgebrasWithBasis(R).NoZeroDivisors().Super()
766769
else:
767770
cat = AlgebrasWithBasis(R).Super()
768-
Algebra.__init__(self, R, names, category=cat)
771+
Parent.__init__(self, base=R, names=names, category=cat)
769772

770-
def _repr_(self):
773+
def _repr_(self) -> str:
771774
r"""
772775
Return a string representation of ``self``.
773776
@@ -779,7 +782,7 @@ def _repr_(self):
779782
"""
780783
poly_gens = ', '.join(repr(x) for x in self.gens()[:self._n])
781784
return "Differential Weyl algebra of polynomials in {} over {}".format(
782-
poly_gens, self.base_ring())
785+
poly_gens, self.base_ring())
783786

784787
# add options to class
785788
class options(GlobalOptions):
@@ -829,7 +832,7 @@ def _element_constructor_(self, x):
829832
sage: W(x^2 - y*z)
830833
-y*z + x^2
831834
"""
832-
t = tuple([0]*(self._n))
835+
t = tuple([0] * (self._n))
833836
if x in self.base_ring():
834837
if x == self.base_ring().zero():
835838
return self.zero()
@@ -839,7 +842,7 @@ def _element_constructor_(self, x):
839842
if x.parent().base_ring() is R:
840843
return self.element_class(self, dict(x))
841844
zero = R.zero()
842-
return self.element_class(self, {i: R(c) for i,c in x if R(c) != zero})
845+
return self.element_class(self, {i: R(c) for i, c in x if R(c) != zero})
843846
x = self._poly_ring(x)
844847
return self.element_class(self, {(tuple(m), t): c
845848
for m, c in x.dict().items()})
@@ -888,8 +891,8 @@ def _coerce_map_from_(self, R):
888891
if self._poly_ring.has_coerce_map_from(R):
889892
return True
890893
if isinstance(R, DifferentialWeylAlgebra):
891-
return ( R.variable_names() == self.variable_names()
892-
and self.base_ring().has_coerce_map_from(R.base_ring()) )
894+
return (R.variable_names() == self.variable_names()
895+
and self.base_ring().has_coerce_map_from(R.base_ring()))
893896
return super()._coerce_map_from_(R)
894897

895898
def degree_on_basis(self, i):
@@ -952,10 +955,16 @@ def basis(self):
952955
"""
953956
n = self._n
954957
from sage.combinat.integer_lists.nn import IntegerListsNN
955-
elt_map = lambda u : (tuple(u[:n]), tuple(u[n:]))
956-
I = IntegerListsNN(length=2*n, element_constructor=elt_map)
958+
959+
def elt_map(u):
960+
return (tuple(u[:n]), tuple(u[n:]))
961+
962+
I = IntegerListsNN(length=2 * n, element_constructor=elt_map)
957963
one = self.base_ring().one()
958-
f = lambda x: self.element_class(self, {(x[0], x[1]): one})
964+
965+
def f(x):
966+
return self.element_class(self, {(x[0], x[1]): one})
967+
959968
return Family(I, f, name="basis map")
960969

961970
@cached_method
@@ -1012,7 +1021,7 @@ def differentials(self):
10121021
Finite family {'dx': dx, 'dy': dy, 'dz': dz}
10131022
"""
10141023
N = self.variable_names()[self._n:]
1015-
d = {x: self.gen(self._n+i) for i, x in enumerate(N)}
1024+
d = {x: self.gen(self._n + i) for i, x in enumerate(N)}
10161025
return Family(N, lambda x: d[x])
10171026

10181027
def gen(self, i):
@@ -1035,8 +1044,8 @@ def gen(self, i):
10351044
if i < self._n:
10361045
P[i] = 1
10371046
else:
1038-
D[i-self._n] = 1
1039-
return self.element_class(self, {(tuple(P), tuple(D)): self.base_ring().one()} )
1047+
D[i - self._n] = 1
1048+
return self.element_class(self, {(tuple(P), tuple(D)): self.base_ring().one()})
10401049

10411050
def ngens(self):
10421051
"""
@@ -1063,8 +1072,8 @@ def one(self):
10631072
sage: W.one()
10641073
1
10651074
"""
1066-
t = tuple([0]*self._n)
1067-
return self.element_class( self, {(t, t): self.base_ring().one()} )
1075+
t = tuple([0] * self._n)
1076+
return self.element_class(self, {(t, t): self.base_ring().one()})
10681077

10691078
@cached_method
10701079
def zero(self):
@@ -1140,7 +1149,7 @@ class DifferentialWeylAlgebraAction(Action):
11401149
True
11411150
"""
11421151

1143-
def __init__(self, G):
1152+
def __init__(self, G) -> None:
11441153
"""
11451154
INPUT:
11461155

0 commit comments

Comments
 (0)