Skip to content

Commit

Permalink
Trac #32153: Refactor _repr_ of some Terms
Browse files Browse the repository at this point in the history
Refactor ExactTerm._repr_ and TermWithCoefficient._repr_

URL: https://trac.sagemath.org/32153
Reported by: gh-thhagelmayer
Ticket author(s): Thomas Hagelmayer
Reviewer(s): Benjamin Hackl, Daniel Krenn
  • Loading branch information
Release Manager committed Jul 23, 2021
2 parents a22c586 + c11b2f4 commit 7bd598d
Showing 1 changed file with 54 additions and 28 deletions.
82 changes: 54 additions & 28 deletions src/sage/rings/asymptotic/term_monoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2906,6 +2906,59 @@ def _repr_(self):
return 'Term with coefficient %s and growth %s' % \
(self.coefficient, self.growth)

def _repr_product_(self, latex=False):
r"""
A representation string for this term with coefficient as a product.
INPUT:
- ``latex`` -- (default: ``False``) a boolean. If set, then
LaTeX-output is returned.
OUTPUT:
A string
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import TermWithCoefficientMonoid
sage: from sage.rings.asymptotic.term_monoid import TermMonoidFactory
sage: TermMonoid = TermMonoidFactory('__main__.TermMonoid')
sage: G = GrowthGroup('x^ZZ'); x = G.gen()
sage: T = TermWithCoefficientMonoid(TermMonoid, G, ZZ)
sage: T(x^2, 5)._repr_product_()
'5*x^2'
"""
if latex:
from sage.misc.latex import latex as latex_repr
f = latex_repr
else:
f = repr

g = f(self.growth)
c = f(self.coefficient)

if g == '1':
return c
elif c == '1':
return '{g}'.format(g=g)
elif c == '-1':
return '-{g}'.format(g=g)
elif self.coefficient._is_atomic() or (-self.coefficient)._is_atomic():
# note that -pi/2 is not atomic, but -5 is. As subtractions are handled
# in the asymptotic ring, we ignore such non-atomicity.
s = '{c} {g}' if latex else '{c}*{g}'
else:
s = r'\left({c}\right) {g}' if latex else '({c})*{g}'
s = s.format(c=c, g=g)

if latex:
import re
s = re.sub(r'([0-9])\s+([0-9])', r'\1 \\cdot \2', s)
return s

def _mul_(self, other):
r"""
Multiplication method for asymptotic terms with coefficients.
Expand Down Expand Up @@ -3337,34 +3390,7 @@ def _repr_(self, latex=False):
sage: (1+a)/n
(a + 1)*n^(-1)
"""
if latex:
from sage.misc.latex import latex as latex_repr
f = latex_repr
else:
f = repr

g = f(self.growth)
c = f(self.coefficient)

if g == '1':
return c
elif c == '1':
return '{g}'.format(g=g)
elif c == '-1':
return '-{g}'.format(g=g)
elif self.coefficient._is_atomic() or (-self.coefficient)._is_atomic():
# note that -pi/2 is not atomic, but -5 is. As subtractions are handled
# in the asymptotic ring, we ignore such non-atomicity.
s = '{c} {g}' if latex else '{c}*{g}'
else:
s = r'\left({c}\right) {g}' if latex else '({c})*{g}'
s = s.format(c=c, g=g)

if latex:
import re
s = re.sub(r'([0-9])\s+([0-9])', r'\1 \\cdot \2', s)

return s
return self._repr_product_(latex=latex)

def _latex_(self):
r"""
Expand Down

0 comments on commit 7bd598d

Please sign in to comment.