Skip to content

Commit b1d02c5

Browse files
committed
implement __getitem__ and alias
1 parent e017444 commit b1d02c5

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/sage/modular/quasimodform/element.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from sage.structure.element import ModuleElement
2525
from sage.structure.richcmp import richcmp, op_NE, op_EQ
2626

27+
from sage.rings.integer import Integer
2728
from sage.rings.polynomial.polynomial_element import Polynomial
2829

2930
class QuasiModularFormsElement(ModuleElement):
@@ -543,6 +544,49 @@ def homogeneous_components(self):
543544
pol_hom_comp = poly_self.homogeneous_components()
544545
return {k: QM.from_polynomial(pol) for k, pol in pol_hom_comp.items()}
545546

547+
def __getitem__(self, weight):
548+
r"""
549+
Return the homogeneous component of the given quasimodular form ring
550+
element.
551+
552+
An alias of this method is ``homogeneous_component``.
553+
554+
EXAMPLES::
555+
556+
sage: QM = QuasiModularForms(1)
557+
sage: E2, E4, E6 = QM.gens()
558+
sage: F = E2 + E4*E6 + E2^3*E6
559+
sage: F[2]
560+
1 - 24*q - 72*q^2 - 96*q^3 - 168*q^4 - 144*q^5 + O(q^6)
561+
sage: F[10]
562+
1 - 264*q - 135432*q^2 - 5196576*q^3 - 69341448*q^4 - 515625264*q^5 + O(q^6)
563+
sage: F[12]
564+
1 - 576*q + 21168*q^2 + 308736*q^3 - 15034608*q^4 - 39208320*q^5 + O(q^6)
565+
sage: F[4]
566+
0
567+
sage: F.homogeneous_component(2)
568+
1 - 24*q - 72*q^2 - 96*q^3 - 168*q^4 - 144*q^5 + O(q^6)
569+
570+
TESTS::
571+
572+
sage: F[x]
573+
Traceback (most recent call last):
574+
...
575+
KeyError: 'the weight must be an integer'
576+
sage: F[-1]
577+
Traceback (most recent call last):
578+
...
579+
ValueError: the weight must be nonnegative
580+
"""
581+
if not isinstance(weight, (int, Integer)):
582+
raise KeyError("the weight must be an integer")
583+
if weight < 0:
584+
raise ValueError("the weight must be nonnegative")
585+
return self.homogeneous_components().get(weight, self.parent().zero())
586+
587+
homogeneous_component = __getitem__ # alias
588+
589+
546590
def serre_derivative(self):
547591
r"""
548592
Return the Serre derivative of the given quasimodular form.

0 commit comments

Comments
 (0)