# Repr formatting support#

sage.misc.repr.coeff_repr(c, is_latex=False)[source]#

String representing coefficients in a linear combination.

INPUT:

• c – a coefficient (i.e., an element of a ring)

OUTPUT:

A string

EXAMPLES:

sage: from sage.misc.repr import coeff_repr
sage: coeff_repr(QQ(1/2))
'1/2'
sage: coeff_repr(-x^2)                                                          # needs sage.symbolic
'(-x^2)'
sage: coeff_repr(QQ(1/2), is_latex=True)
'\\frac{1}{2}'
sage: coeff_repr(-x^2, is_latex=True)                                           # needs sage.symbolic
'\\left(-x^{2}\\right)'

>>> from sage.all import *
>>> from sage.misc.repr import coeff_repr
>>> coeff_repr(QQ(Integer(1)/Integer(2)))
'1/2'
>>> coeff_repr(-x**Integer(2))                                                          # needs sage.symbolic
'(-x^2)'
>>> coeff_repr(QQ(Integer(1)/Integer(2)), is_latex=True)
'\\frac{1}{2}'
>>> coeff_repr(-x**Integer(2), is_latex=True)                                           # needs sage.symbolic
'\\left(-x^{2}\\right)'

sage.misc.repr.repr_lincomb(terms, is_latex=False, scalar_mult='*', strip_one=False, repr_monomial=None, latex_scalar_mult=None)[source]#

Compute a string representation of a linear combination of some formal symbols.

INPUT:

• terms – list of terms, as pairs (support, coefficient)

• is_latex – whether to produce latex (default: False)

• scalar_mult – string representing the multiplication (default:'*')

• latex_scalar_mult – latex string representing the multiplication (default: a space if scalar_mult is '*'; otherwise scalar_mult)

• coeffs – for backward compatibility

OUTPUT:

• str – a string

EXAMPLES:

sage: repr_lincomb([('a',1), ('b',-2), ('c',3)])
'a - 2*b + 3*c'
sage: repr_lincomb([('a',0), ('b',-2), ('c',3)])
'-2*b + 3*c'
sage: repr_lincomb([('a',0), ('b',2), ('c',3)])
'2*b + 3*c'
sage: repr_lincomb([('a',1), ('b',0), ('c',3)])
'a + 3*c'
sage: repr_lincomb([('a',-1), ('b','2+3*x'), ('c',3)])
'-a + (2+3*x)*b + 3*c'
sage: repr_lincomb([('a', '1+x^2'), ('b', '2+3*x'), ('c', 3)])
'(1+x^2)*a + (2+3*x)*b + 3*c'
sage: repr_lincomb([('a', '1+x^2'), ('b', '-2+3*x'), ('c', 3)])
'(1+x^2)*a + (-2+3*x)*b + 3*c'
sage: repr_lincomb([('a', 1), ('b', -2), ('c', -3)])
'a - 2*b - 3*c'
sage: t = PolynomialRing(RationalField(),'t').gen()
sage: repr_lincomb([('a', -t), ('s', t - 2), ('', t^2 + 2)])
'-t*a + (t-2)*s + (t^2+2)'

>>> from sage.all import *
>>> repr_lincomb([('a',Integer(1)), ('b',-Integer(2)), ('c',Integer(3))])
'a - 2*b + 3*c'
>>> repr_lincomb([('a',Integer(0)), ('b',-Integer(2)), ('c',Integer(3))])
'-2*b + 3*c'
>>> repr_lincomb([('a',Integer(0)), ('b',Integer(2)), ('c',Integer(3))])
'2*b + 3*c'
>>> repr_lincomb([('a',Integer(1)), ('b',Integer(0)), ('c',Integer(3))])
'a + 3*c'
>>> repr_lincomb([('a',-Integer(1)), ('b','2+3*x'), ('c',Integer(3))])
'-a + (2+3*x)*b + 3*c'
>>> repr_lincomb([('a', '1+x^2'), ('b', '2+3*x'), ('c', Integer(3))])
'(1+x^2)*a + (2+3*x)*b + 3*c'
>>> repr_lincomb([('a', '1+x^2'), ('b', '-2+3*x'), ('c', Integer(3))])
'(1+x^2)*a + (-2+3*x)*b + 3*c'
>>> repr_lincomb([('a', Integer(1)), ('b', -Integer(2)), ('c', -Integer(3))])
'a - 2*b - 3*c'
>>> t = PolynomialRing(RationalField(),'t').gen()
>>> repr_lincomb([('a', -t), ('s', t - Integer(2)), ('', t**Integer(2) + Integer(2))])
'-t*a + (t-2)*s + (t^2+2)'


Examples for scalar_mult:

sage: repr_lincomb([('a',1), ('b',2), ('c',3)], scalar_mult='*')
'a + 2*b + 3*c'
sage: repr_lincomb([('a',2), ('b',0), ('c',-3)], scalar_mult='**')
'2**a - 3**c'
sage: repr_lincomb([('a',-1), ('b',2), ('c',3)], scalar_mult='**')
'-a + 2**b + 3**c'

>>> from sage.all import *
>>> repr_lincomb([('a',Integer(1)), ('b',Integer(2)), ('c',Integer(3))], scalar_mult='*')
'a + 2*b + 3*c'
>>> repr_lincomb([('a',Integer(2)), ('b',Integer(0)), ('c',-Integer(3))], scalar_mult='**')
'2**a - 3**c'
>>> repr_lincomb([('a',-Integer(1)), ('b',Integer(2)), ('c',Integer(3))], scalar_mult='**')
'-a + 2**b + 3**c'


Examples for scalar_mult and is_latex:

sage: repr_lincomb([('a',-1), ('b',2), ('c',3)], is_latex=True)
'-a + 2 b + 3 c'
sage: repr_lincomb([('a',-1), ('b',-1), ('c',3)], is_latex=True, scalar_mult='*')
'-a - b + 3 c'
sage: repr_lincomb([('a',-1), ('b',2), ('c',-3)], is_latex=True, scalar_mult='**')
'-a + 2**b - 3**c'
sage: repr_lincomb([('a',-2), ('b',-1), ('c',-3)], is_latex=True, latex_scalar_mult='*')
'-2*a - b - 3*c'
sage: repr_lincomb([('a',-2), ('b',-1), ('c',-3)], is_latex=True, latex_scalar_mult='')
'-2a - b - 3c'

>>> from sage.all import *
>>> repr_lincomb([('a',-Integer(1)), ('b',Integer(2)), ('c',Integer(3))], is_latex=True)
'-a + 2 b + 3 c'
>>> repr_lincomb([('a',-Integer(1)), ('b',-Integer(1)), ('c',Integer(3))], is_latex=True, scalar_mult='*')
'-a - b + 3 c'
>>> repr_lincomb([('a',-Integer(1)), ('b',Integer(2)), ('c',-Integer(3))], is_latex=True, scalar_mult='**')
'-a + 2**b - 3**c'
>>> repr_lincomb([('a',-Integer(2)), ('b',-Integer(1)), ('c',-Integer(3))], is_latex=True, latex_scalar_mult='*')
'-2*a - b - 3*c'
>>> repr_lincomb([('a',-Integer(2)), ('b',-Integer(1)), ('c',-Integer(3))], is_latex=True, latex_scalar_mult='')
'-2a - b - 3c'


Examples for strip_one:

sage: repr_lincomb([ ('a',1), (1,-2), ('3',3) ])
'a - 2*1 + 3*3'
sage: repr_lincomb([ ('a',-1), (1,1), ('3',3) ])
'-a + 1 + 3*3'
sage: repr_lincomb([ ('a',1), (1,-2), ('3',3) ], strip_one = True)
'a - 2 + 3*3'
sage: repr_lincomb([ ('a',-1), (1,1), ('3',3) ], strip_one = True)
'-a + 1 + 3*3'
sage: repr_lincomb([ ('a',1), (1,-1), ('3',3) ], strip_one = True)
'a - 1 + 3*3'

>>> from sage.all import *
>>> repr_lincomb([ ('a',Integer(1)), (Integer(1),-Integer(2)), ('3',Integer(3)) ])
'a - 2*1 + 3*3'
>>> repr_lincomb([ ('a',-Integer(1)), (Integer(1),Integer(1)), ('3',Integer(3)) ])
'-a + 1 + 3*3'
>>> repr_lincomb([ ('a',Integer(1)), (Integer(1),-Integer(2)), ('3',Integer(3)) ], strip_one = True)
'a - 2 + 3*3'
>>> repr_lincomb([ ('a',-Integer(1)), (Integer(1),Integer(1)), ('3',Integer(3)) ], strip_one = True)
'-a + 1 + 3*3'
>>> repr_lincomb([ ('a',Integer(1)), (Integer(1),-Integer(1)), ('3',Integer(3)) ], strip_one = True)
'a - 1 + 3*3'


Examples for repr_monomial:

sage: repr_lincomb([('a',1), ('b',2), ('c',3)], repr_monomial = lambda s: s+"1")
'a1 + 2*b1 + 3*c1'

>>> from sage.all import *
>>> repr_lincomb([('a',Integer(1)), ('b',Integer(2)), ('c',Integer(3))], repr_monomial = lambda s: s+"1")
'a1 + 2*b1 + 3*c1'