Ticket #13735: trac_13735_fix_latex_repr_lincomb.patch
File trac_13735_fix_latex_repr_lincomb.patch, 5.4 KB (added by , 9 years ago) 


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 b 694 694 695 695 696 696 def coeff_repr(c, is_latex=False): 697 if c == 1: 698 return "" 697 699 if not is_latex: 698 700 try: 699 701 return c._coeff_repr() … … 732 734 733 735 EXAMPLES:: 734 736 735 sage: repr_lincomb([('a',1), ('b',2), ('c',3)]) 736 'a + 2*b + 3*c' 737 sage: repr_lincomb([('a',1), ('b','2+3*x'), ('c',3)]) 738 'a + (2+3*x)*b + 3*c' 737 sage: repr_lincomb([('a',1), ('b',2), ('c',3)]) 738 'a  2*b + 3*c' 739 sage: repr_lincomb([('a',0), ('b',2), ('c',3)]) 740 '2*b + 3*c' 741 sage: repr_lincomb([('a',0), ('b',2), ('c',3)]) 742 '2*b + 3*c' 743 sage: repr_lincomb([('a',1), ('b',0), ('c',3)]) 744 'a + 3*c' 745 sage: repr_lincomb([('a',1), ('b','2+3*x'), ('c',3)]) 746 'a + (2+3*x)*b + 3*c' 739 747 sage: repr_lincomb([('a', '1+x^2'), ('b', '2+3*x'), ('c', 3)]) 740 748 '(1+x^2)*a + (2+3*x)*b + 3*c' 741 749 sage: repr_lincomb([('a', '1+x^2'), ('b', '2+3*x'), ('c', 3)]) … … 750 758 751 759 sage: repr_lincomb([('a',1), ('b',2), ('c',3)], scalar_mult='*') 752 760 'a + 2*b + 3*c' 753 sage: repr_lincomb([('a', 1), ('b',2), ('c',3)], scalar_mult='**')754 ' a + 2**b +3**c'755 sage: repr_lincomb([('a', 1), ('b',2), ('c',3)], scalar_mult='**')756 ' a + 2**b + 3**c'761 sage: repr_lincomb([('a',2), ('b',0), ('c',3)], scalar_mult='**') 762 '2**a  3**c' 763 sage: repr_lincomb([('a',1), ('b',2), ('c',3)], scalar_mult='**') 764 'a + 2**b + 3**c' 757 765 758 766 Examples for ``scalar_mult`` and ``is_latex``:: 759 767 760 sage: repr_lincomb([('a', 1), ('b',2), ('c',3)], is_latex=True)761 ' a + 2b + 3c'762 sage: repr_lincomb([('a', 1), ('b',2), ('c',3)], is_latex=True, scalar_mult='*')763 ' a + 2b + 3c'764 sage: repr_lincomb([('a', 1), ('b',2), ('c',3)], is_latex=True, scalar_mult='**')765 ' a + 2**b +3**c'766 sage: repr_lincomb([('a', 1), ('b',2), ('c',3)], is_latex=True, latex_scalar_mult='*')767 ' a + 2*b +3*c'768 sage: repr_lincomb([('a',1), ('b',2), ('c',3)], is_latex=True) 769 'a + 2b + 3c' 770 sage: repr_lincomb([('a',1), ('b',1), ('c',3)], is_latex=True, scalar_mult='*') 771 'a  b + 3c' 772 sage: repr_lincomb([('a',1), ('b',2), ('c',3)], is_latex=True, scalar_mult='**') 773 'a + 2**b  3**c' 774 sage: repr_lincomb([('a',2), ('b',1), ('c',3)], is_latex=True, latex_scalar_mult='*') 775 '2*a  b  3*c' 768 776 769 777 Examples for ``strip_one``:: 770 778 771 sage: repr_lincomb([ ('a',1), (1,2), ('3',3) ]) 772 'a + 2*1 + 3*3' 773 sage: repr_lincomb([ ('a',1), (1,1), ('3',3) ]) 774 'a + 1 + 3*3' 775 sage: repr_lincomb([ ('a',1), (1,2), ('3',3) ], strip_one = True) 776 'a + 2 + 3*3' 777 sage: repr_lincomb([ ('a',1), (1,1), ('3',3) ], strip_one = True) 778 'a + 1 + 3*3' 779 sage: repr_lincomb([ ('a',1), (1,2), ('3',3) ]) 780 'a  2*1 + 3*3' 781 sage: repr_lincomb([ ('a',1), (1,1), ('3',3) ]) 782 'a + 1 + 3*3' 783 sage: repr_lincomb([ ('a',1), (1,2), ('3',3) ], strip_one = True) 784 'a  2 + 3*3' 785 sage: repr_lincomb([ ('a',1), (1,1), ('3',3) ], strip_one = True) 786 'a + 1 + 3*3' 787 sage: repr_lincomb([ ('a',1), (1,1), ('3',3) ], strip_one = True) 788 'a  1 + 3*3' 779 789 780 790 Examples for ``repr_monomial``:: 781 791 … … 819 829 820 830 all_atomic = True 821 831 for (monomial,c) in terms: 822 b = repr_monomial(monomial)823 832 if c != 0: 824 coeff = coeff_repr(c, is_latex) 833 b = repr_monomial(monomial) 834 if c < 0: 835 coeff = coeff_repr(c, is_latex) 836 else: 837 coeff = coeff_repr(c, is_latex) 825 838 if coeff != "0": 826 if coeff == "1": 827 coeff = "" 828 elif coeff == "1": 829 coeff = "" 830 elif len(b) > 0: 831 if len(coeff) > 0 and b == "1" and strip_one: 839 if c < 0: 840 if first: 841 coeff = "%s"%coeff # leading space 842 else: 843 coeff = "  %s"%coeff 844 else: 845 if first: 846 coeff = "%s"%coeff 847 else: 848 coeff = " + %s"%coeff 849 if len(b) > 0 and c != 1 and c != 1: 850 if b =="1" and strip_one: 832 851 b = "" 833 852 else: 834 853 b = scalar_mult + b 835 if not first: 836 if len(coeff) > 0 and coeff[0] == "": 837 coeff = "  %s"%coeff[1:] 838 else: 839 coeff = " + %s"%coeff 840 else: 841 coeff = "%s"%coeff 842 s += "%s%s"%(coeff, b) 843 first = False 854 s += "%s%s"%(coeff, b) 855 first = False 844 856 if first: 845 return "0" 857 return "0" # this can happen only if are only terms with coeff_repr(c) == "0" 846 858 elif s == "": 847 859 return "1" 848 860 else: