# Ticket #13735: trac_13735_fix_latex_repr_lincomb.patch

File trac_13735_fix_latex_repr_lincomb.patch, 5.4 KB (added by vittucek, 9 years ago)

fix the representation of linear combinations and include more testcases

• ## sage/misc/misc.py

```# HG changeset patch
# User vit.tucek <vit.tucek@gmail.com>
# Date 1365427510 -7200
# Node ID f6f0d82d6ac9cb872fe8c9600c2f2116fd44c561
# Parent  bfdc420249e7d251de5c3b0343a5c9d56a2c6df4
Trac 13735: avoid extra parentheses in latex expression for linear combinations with negative coefficients

diff --git a/sage/misc/misc.py b/sage/misc/misc.py```
 a def coeff_repr(c, is_latex=False): if c == 1: return "" if not is_latex: try: return c._coeff_repr() EXAMPLES:: sage: repr_lincomb([('a',1), ('b',2), ('c',3)]) 'a + 2*b + 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), ('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)]) sage: repr_lincomb([('a',1), ('b',2), ('c',3)], scalar_mult='*') 'a + 2*b + 3*c' sage: repr_lincomb([('a',1), ('b',2), ('c',3)], scalar_mult='**') 'a + 2**b + 3**c' 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' Examples for ``scalar_mult`` and ``is_latex``:: sage: repr_lincomb([('a',1), ('b',2), ('c',3)], is_latex=True) 'a + 2b + 3c' sage: repr_lincomb([('a',1), ('b',2), ('c',3)], is_latex=True, scalar_mult='*') 'a + 2b + 3c' sage: repr_lincomb([('a',1), ('b',2), ('c',3)], is_latex=True, scalar_mult='**') 'a + 2**b + 3**c' sage: repr_lincomb([('a',1), ('b',2), ('c',3)], is_latex=True, latex_scalar_mult='*') 'a + 2*b + 3*c' sage: repr_lincomb([('a',-1), ('b',2), ('c',3)], is_latex=True) '-a + 2b + 3c' sage: repr_lincomb([('a',-1), ('b',-1), ('c',3)], is_latex=True, scalar_mult='*') '-a - b + 3c' 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' 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,-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' Examples for ``repr_monomial``:: all_atomic = True for (monomial,c) in terms: b = repr_monomial(monomial) if c != 0: coeff = coeff_repr(c, is_latex) b = repr_monomial(monomial) if c < 0: coeff = coeff_repr(-c, is_latex) else: coeff = coeff_repr(c, is_latex) if coeff != "0": if coeff == "1": coeff = "" elif coeff == "-1": coeff = "-" elif len(b) > 0: if len(coeff) > 0 and b == "1" and strip_one: if c < 0: if first: coeff = "-%s"%coeff # leading space else: coeff = " - %s"%coeff else: if first: coeff = "%s"%coeff else: coeff = " + %s"%coeff if len(b) > 0 and c != 1 and c != -1: if b =="1" and strip_one: b = "" else: b = scalar_mult + b if not first: if len(coeff) > 0 and coeff[0] == "-": coeff = " - %s"%coeff[1:] else: coeff = " + %s"%coeff else: coeff = "%s"%coeff s += "%s%s"%(coeff, b) first = False s += "%s%s"%(coeff, b) first = False if first: return "0" return "0" # this can happen only if are only terms with coeff_repr(c) == "0" elif s == "": return "1" else: