From 15b7835e90912ad75e621003077e307c6d289a05 Mon Sep 17 00:00:00 2001 From: mmatera Date: Wed, 13 Nov 2024 22:29:47 -0300 Subject: [PATCH] restoring parens as it was before. Adding parenthesis with the new behavior --- sympy/physics/quantum/anticommutator.py | 2 +- sympy/physics/quantum/cg.py | 6 +- sympy/physics/quantum/commutator.py | 2 +- sympy/physics/quantum/hilbert.py | 4 +- sympy/physics/quantum/operator.py | 4 +- sympy/physics/quantum/qexpr.py | 2 +- sympy/physics/quantum/spin.py | 2 +- sympy/physics/quantum/tensorproduct.py | 4 +- sympy/physics/vector/dyadic.py | 2 +- sympy/physics/vector/vector.py | 2 +- sympy/printing/pretty/pretty.py | 150 ++++++++++++------------ sympy/printing/pretty/stringpict.py | 78 +++++++----- 12 files changed, 139 insertions(+), 119 deletions(-) diff --git a/sympy/physics/quantum/anticommutator.py b/sympy/physics/quantum/anticommutator.py index 8a3217d3ae4f..786042053565 100644 --- a/sympy/physics/quantum/anticommutator.py +++ b/sympy/physics/quantum/anticommutator.py @@ -141,7 +141,7 @@ def _pretty(self, printer, *args): pform = printer._print(self.args[0], *args) pform = pform.right(prettyForm(',')) pform = pform.right(printer._print(self.args[1], *args)) - pform = pform.parens(left='{', right='}') + pform = pform.parenthesis(left='{', right='}') return pform def _latex(self, printer, *args): diff --git a/sympy/physics/quantum/cg.py b/sympy/physics/quantum/cg.py index 612826a97bd7..866fd1182c92 100644 --- a/sympy/physics/quantum/cg.py +++ b/sympy/physics/quantum/cg.py @@ -143,7 +143,7 @@ def _pretty(self, printer, *args): for _ in range(vsep): D = D.below(' ') D = D.below(D_row) - D = D.parens() + D = D.parenthesis() return D def _latex(self, printer, *args): @@ -318,7 +318,7 @@ def _pretty(self, printer, *args): for _ in range(vsep): D = D.below(' ') D = D.below(D_row) - D = D.parens(left='{', right='}') + D = D.parenthesis(left='{', right='}') return D def _latex(self, printer, *args): @@ -422,7 +422,7 @@ def _pretty(self, printer, *args): for _ in range(vsep): D = D.below(' ') D = D.below(D_row) - D = D.parens(left='{', right='}') + D = D.parenthesis(left='{', right='}') return D def _latex(self, printer, *args): diff --git a/sympy/physics/quantum/commutator.py b/sympy/physics/quantum/commutator.py index 702dcfbd5434..980ea8ab2adc 100644 --- a/sympy/physics/quantum/commutator.py +++ b/sympy/physics/quantum/commutator.py @@ -231,7 +231,7 @@ def _pretty(self, printer, *args): pform = printer._print(self.args[0], *args) pform = pform.right(prettyForm(',')) pform = pform.right(printer._print(self.args[1], *args)) - pform = pform.parens(left='[', right=']') + pform = pform.parenthesis(left='[', right=']') return pform def _latex(self, printer, *args): diff --git a/sympy/physics/quantum/hilbert.py b/sympy/physics/quantum/hilbert.py index 69daffaa4043..50405ba35cfe 100644 --- a/sympy/physics/quantum/hilbert.py +++ b/sympy/physics/quantum/hilbert.py @@ -414,7 +414,7 @@ def _pretty(self, printer, *args): next_pform = printer._print(self.args[i], *args) if isinstance(self.args[i], (DirectSumHilbertSpace, TensorProductHilbertSpace)): - next_pform = next_pform.parens(left='(', right=')') + next_pform = next_pform.parenthesis(left='(', right=')') pform = pform.right(next_pform) if i != length - 1: if printer._use_unicode: @@ -522,7 +522,7 @@ def _pretty(self, printer, *args): next_pform = printer._print(self.args[i], *args) if isinstance(self.args[i], (DirectSumHilbertSpace, TensorProductHilbertSpace)): - next_pform = next_pform.parens(left='(', right=')') + next_pform = next_pform.parenthesis(left='(', right=')') pform = pform.right(next_pform) if i != length - 1: if printer._use_unicode: diff --git a/sympy/physics/quantum/operator.py b/sympy/physics/quantum/operator.py index e774d9ff11bf..00ef1b689df2 100644 --- a/sympy/physics/quantum/operator.py +++ b/sympy/physics/quantum/operator.py @@ -136,7 +136,7 @@ def _print_contents_pretty(self, printer, *args): else: pform = self._print_operator_name_pretty(printer, *args) label_pform = self._print_label_pretty(printer, *args) - label_pform = label_pform.parens(left='(', right=')') + label_pform = label_pform.parenthesis(left='(', right=')') pform = pform.right(label_pform) return pform @@ -649,7 +649,7 @@ def _print_pretty(self, printer, *args): pform = self._print_operator_name_pretty(printer, *args) label_pform = self._print_label_pretty(printer, *args) label_pform = prettyForm( - *label_pform.parens(left='(', right=')') + *label_pform.parenthesis(left='(', right=')') ) pform = pform.right(label_pform) return pform diff --git a/sympy/physics/quantum/qexpr.py b/sympy/physics/quantum/qexpr.py index e9f5dd4b76fc..3927dea6a343 100644 --- a/sympy/physics/quantum/qexpr.py +++ b/sympy/physics/quantum/qexpr.py @@ -243,7 +243,7 @@ def _print_superscript_pretty(self, a, b): return a**b def _print_parens_pretty(self, pform, left='(', right=')'): - return pform.parens(left=left, right=right) + return pform.parenthesis(left=left, right=right) # Printing of labels (i.e. args) diff --git a/sympy/physics/quantum/spin.py b/sympy/physics/quantum/spin.py index 454df7bf3cc7..e09498223a10 100644 --- a/sympy/physics/quantum/spin.py +++ b/sympy/physics/quantum/spin.py @@ -851,7 +851,7 @@ def _pretty(self, printer, *args): s = prettyForm('D' + ' '*pad) - args = args.parens() + args = args.parenthesis() s = s.above(top) s = s.below(bot) s = s.right(args) diff --git a/sympy/physics/quantum/tensorproduct.py b/sympy/physics/quantum/tensorproduct.py index 18d60448af4d..5cf57c8ce55b 100644 --- a/sympy/physics/quantum/tensorproduct.py +++ b/sympy/physics/quantum/tensorproduct.py @@ -181,7 +181,7 @@ def _pretty(self, printer, *args): next_pform = next_pform.right(', ') if len(self.args[i].args) > 1: - next_pform = next_pform.parens(left='{', right='}') + next_pform = next_pform.parenthesis(left='{', right='}') pform = pform.right(next_pform) if i != length - 1: pform = pform.right(',' + ' ') @@ -195,7 +195,7 @@ def _pretty(self, printer, *args): for i in range(length): next_pform = printer._print(self.args[i], *args) if isinstance(self.args[i], (Add, Mul)): - next_pform = next_pform.parens(left='(', right=')') + next_pform = next_pform.parenthesis(left='(', right=')') pform = pform.right(next_pform) if i != length - 1: if printer._use_unicode: diff --git a/sympy/physics/vector/dyadic.py b/sympy/physics/vector/dyadic.py index 88a4f12f6998..47ee44494c5f 100644 --- a/sympy/physics/vector/dyadic.py +++ b/sympy/physics/vector/dyadic.py @@ -235,7 +235,7 @@ def render(self, *args, **kwargs): elif v[0] != 0: if isinstance(v[0], Add): arg_str = mpp._print( - v[0]).parens()[0] + v[0]).parenthesis()[0] else: arg_str = mpp.doprint(v[0]) if arg_str.startswith("-"): diff --git a/sympy/physics/vector/vector.py b/sympy/physics/vector/vector.py index 480dad40a6c1..89b78766f364 100644 --- a/sympy/physics/vector/vector.py +++ b/sympy/physics/vector/vector.py @@ -265,7 +265,7 @@ def juxtapose(a, b): pa = printer._print(a) pb = printer._print(b) if a.is_Add: - pa = prettyForm(*pa.parens()) + pa = prettyForm(*pa.parenthesis()) return printer._print_seq([pa, pb], delimiter=' ') for M, N in self.args: diff --git a/sympy/printing/pretty/pretty.py b/sympy/printing/pretty/pretty.py index 88b702dc17b9..10f61b3b7022 100644 --- a/sympy/printing/pretty/pretty.py +++ b/sympy/printing/pretty/pretty.py @@ -77,7 +77,7 @@ def _print_basestring(self, e): return prettyForm(e) def _print_atan2(self, e): - pform = self._print_seq(e.args).parens() + pform = self._print_seq(e.args).parenthesis() pform = pform.left('atan2') return pform @@ -185,7 +185,7 @@ def _print_subfactorial(self, e): pform = self._print(x) # Add parentheses if needed if not ((x.is_Integer and x.is_nonnegative) or x.is_Symbol): - pform = pform.parens() + pform = pform.parenthesis() pform = pform.left('!') return pform @@ -194,7 +194,7 @@ def _print_factorial(self, e): pform = self._print(x) # Add parentheses if needed if not ((x.is_Integer and x.is_nonnegative) or x.is_Symbol): - pform = pform.parens() + pform = pform.parenthesis() pform = pform.right('!') return pform @@ -203,7 +203,7 @@ def _print_factorial2(self, e): pform = self._print(x) # Add parentheses if needed if not ((x.is_Integer and x.is_nonnegative) or x.is_Symbol): - pform = pform.parens() + pform = pform.parenthesis() pform = pform.right('!!') return pform @@ -217,7 +217,7 @@ def _print_binomial(self, e): pform = k_pform.above(bar) pform = pform.above(n_pform) - pform = pform.parens('(', ')') + pform = pform.parenthesis('(', ')') pform.baseline = (pform.baseline + 1)//2 @@ -242,7 +242,7 @@ def _print_Not(self, e): return self._print_Implies(arg, altchar=pretty_atom('NotArrow')) if arg.is_Boolean and not arg.is_Not: - pform = pform.parens() + pform = pform.parenthesis() return pform.left(pretty_atom('Not')) else: @@ -256,13 +256,13 @@ def __print_Boolean(self, e, char, sort=True): pform = self._print(arg) if arg.is_Boolean and not arg.is_Not: - pform = pform.parens() + pform = pform.parenthesis() for arg in args[1:]: pform_arg = self._print(arg) if arg.is_Boolean and not arg.is_Not: - pform_arg = pform_arg.parens() + pform_arg = pform_arg.parenthesis() pform = pform.right(' %s ' % char) pform = pform.right(pform_arg) @@ -317,13 +317,13 @@ def _print_conjugate(self, e): def _print_Abs(self, e): pform = self._print(e.args[0]) - pform = pform.parens('|', '|') + pform = pform.parenthesis('|', '|') return pform def _print_floor(self, e): if self._use_unicode: pform = self._print(e.args[0]) - pform = pform.parens('lfloor', 'rfloor') + pform = pform.parenthesis('lfloor', 'rfloor') return pform else: return self._print_Function(e) @@ -331,7 +331,7 @@ def _print_floor(self, e): def _print_ceiling(self, e): if self._use_unicode: pform = self._print(e.args[0]) - pform = pform.parens('lceil', 'rceil') + pform = pform.parenthesis('lceil', 'rceil') return pform else: return self._print_Function(e) @@ -358,7 +358,7 @@ def _print_Derivative(self, deriv): x = x.right(' ') x = x.right(ds) - f = self._print(deriv.expr).parens() + f = self._print(deriv.expr).parenthesis() f.binding=prettyForm.FUNC pform = prettyForm(deriv_symbol) @@ -378,13 +378,13 @@ def _print_Cycle(self, dc): # for Empty Cycle if dc == Cycle(): cyc = stringPict('') - return cyc.parens() + return cyc.parenthesis() dc_list = Permutation(dc.list()).cyclic_form # for Identity Cycle if dc_list == []: cyc = self._print(dc.size - 1) - return cyc.parens() + return cyc.parenthesis() cyc = stringPict('') for i in dc_list: @@ -426,7 +426,7 @@ def _print_Permutation(self, expr): else: col = col.left(" ") result = result.right(col) - return result.parens() + return result.parenthesis() def _print_Integral(self, integral): @@ -437,7 +437,7 @@ def _print_Integral(self, integral): prettyF = self._print(f) # XXX generalize parens if f.is_Add: - prettyF = prettyF.parens() + prettyF = prettyF.parenthesis() # dx dy dz ... arg = prettyF @@ -445,7 +445,7 @@ def _print_Integral(self, integral): prettyArg = self._print(x[0]) # XXX qparens (parens if needs-parens) if prettyArg.width() > 1: - prettyArg = prettyArg.parens() + prettyArg = prettyArg.parenthesis() arg = arg.right(' d', prettyArg) @@ -620,7 +620,7 @@ def adjust(s, wid=None, how='<^>'): prettyF = self._print(f) if f.is_Add: # add parens - prettyF = prettyF.parens() + prettyF = prettyF.parenthesis() H = prettyF.height() + 2 @@ -672,7 +672,7 @@ def _print_Limit(self, l): E = self._print(e) if precedence(e) <= PRECEDENCE["Mul"]: - E = E.parens('(', ')') + E = E.parenthesis('(', ')') Lim = prettyForm('lim') LimArg = self._print(z) @@ -766,7 +766,7 @@ def _print_matrix_contents(self, e): def _print_MatrixBase(self, e, lparens='[', rparens=']'): D = self._print_matrix_contents(e) D.baseline = D.height()//2 - D = D.parens(lparens, rparens) + D = D.parenthesis(lparens, rparens) return D def _print_Determinant(self, e): @@ -777,7 +777,7 @@ def _print_Determinant(self, e): return self._print_MatrixBase(mat.blocks, lparens='|', rparens='|') D = self._print(mat) D.baseline = D.height()//2 - return D.parens('|', '|') + return D.parenthesis('|', '|') else: return self._print_MatrixBase(mat, lparens='|', rparens='|') @@ -801,7 +801,7 @@ def _print_WedgeProduct(self, expr): def _print_Trace(self, e): D = self._print(e.arg) - D = D.parens('(',')') + D = D.parenthesis('(',')') D.baseline = D.height()//2 D = D.left('\n'*(0) + 'tr') return D @@ -815,9 +815,9 @@ def _print_MatrixElement(self, expr): Symbol(expr.parent.name + '_%d%d' % (expr.i, expr.j))) else: prettyFunc = self._print(expr.parent) - prettyFunc = prettyFunc.parens() + prettyFunc = prettyFunc.parenthesis() prettyIndices = self._print_seq((expr.i, expr.j), delimiter=', ' - ).parens(left='[', right=']') + ).parenthesis(left='[', right=']') pform = prettyForm(binding=prettyForm.FUNC, *stringPict.next(prettyFunc, prettyIndices)) @@ -833,7 +833,7 @@ def _print_MatrixSlice(self, m): from sympy.matrices import MatrixSymbol prettyFunc = self._print(m.parent) if not isinstance(m.parent, MatrixSymbol): - prettyFunc = prettyFunc.parens() + prettyFunc = prettyFunc.parenthesis() def ppslice(x, dim): x = list(x) if x[2] == 1: @@ -844,7 +844,7 @@ def ppslice(x, dim): x[1] = '' return self._print_seq(x, delimiter=':') prettyArgs = str(self._print_seq((ppslice(m.rowslice, m.parent.rows), - ppslice(m.colslice, m.parent.cols)), delimiter=', ').parens(left='[', right=']')) + ppslice(m.colslice, m.parent.cols)), delimiter=', ').parenthesis(left='[', right=']')) pform = prettyForm( binding=prettyForm.FUNC, *stringPict.next(prettyFunc, prettyArgs)) @@ -861,7 +861,7 @@ def _print_Transpose(self, expr): from sympy.matrices import MatrixSymbol, BlockMatrix if (not isinstance(mat, MatrixSymbol) and not isinstance(mat, BlockMatrix) and mat.is_MatrixExpr): - pform = pform.parens() + pform = pform.parenthesis() pform = pform**(prettyForm('T')) return pform @@ -875,7 +875,7 @@ def _print_Adjoint(self, expr): from sympy.matrices import MatrixSymbol, BlockMatrix if (not isinstance(mat, MatrixSymbol) and not isinstance(mat, BlockMatrix) and mat.is_MatrixExpr): - pform = pform.parens() + pform = pform.parenthesis() pform = pform**dag return pform @@ -909,7 +909,7 @@ def _print_MatMul(self, expr): for i, a in enumerate(args): if (isinstance(a, (Add, MatAdd, HadamardProduct, KroneckerProduct)) and len(expr.args) > 1): - args[i] = self._print(a).parens() + args[i] = self._print(a).parenthesis() else: args[i] = self._print(a) @@ -944,7 +944,7 @@ def _print_MatPow(self, expr): pform = self._print(expr.base) from sympy.matrices import MatrixSymbol if not isinstance(expr.base, MatrixSymbol) and expr.base.is_MatrixExpr: - pform = pform.parens() + pform = pform.parenthesis() pform = pform**(self._print(expr.exp)) return pform @@ -968,7 +968,7 @@ def _print_HadamardPower(self, expr): pretty_base = self._print(expr.base) pretty_exp = self._print(expr.exp) if precedence(expr.exp) < PRECEDENCE["Mul"]: - pretty_exp = pretty_exp.parens() + pretty_exp = pretty_exp.parenthesis() pretty_circ_exp = prettyForm( binding=prettyForm.LINE, *stringPict.next(circ, pretty_exp) @@ -987,7 +987,7 @@ def _print_KroneckerProduct(self, expr): def _print_FunctionMatrix(self, X): D = self._print(X.lamda.expr) - D = D.parens('[', ']') + D = D.parenthesis('[', ']') return D def _print_TransferFunction(self, expr): @@ -1001,7 +1001,7 @@ def _print_TransferFunction(self, expr): def _print_Series(self, expr): args = list(expr.args) for i, a in enumerate(expr.args): - args[i] = self._print(a).parens() + args[i] = self._print(a).parenthesis() return prettyForm.__mul__(*args) def _print_MIMOSeries(self, expr): @@ -1012,7 +1012,7 @@ def _print_MIMOSeries(self, expr): if (isinstance(a, MIMOParallel) and len(expr.args) > 1): expression = self._print(a) expression.baseline = expression.height()//2 - pretty_args.append(expression.parens()) + pretty_args.append(expression.parenthesis()) else: expression = self._print(a) expression.baseline = expression.height()//2 @@ -1093,7 +1093,7 @@ def _print_MIMOFeedback(self, expr): _feedback = prettyForm(*stringPict.next(inv_mat)) _feedback = stringPict.right(prettyForm("I + "), _feedback) if expr.sign == -1 \ else stringPict.right(prettyForm("I - "), _feedback) - _feedback = stringPict.parens(_feedback) + _feedback = stringPict.parenthesis(_feedback) _feedback.baseline = 0 _feedback = stringPict.right(_feedback, stringPict('-1 ')) _feedback.baseline = _feedback.height()//2 @@ -1151,7 +1151,7 @@ def _print_BasisDependent(self, expr): #We always wrap the measure numbers in #parentheses arg_str = str(self._print( - v).parens()) + v).parenthesis()) o1.append(arg_str + ' ' + k._pretty_form) vectstrs.append(k._pretty_form) @@ -1303,7 +1303,7 @@ def _print_TensorElement(self, expr): def _print_TensMul(self, expr): sign, args = expr._get_args_for_traditional_printer() args = [ - self._print(i).parens() if + self._print(i).parenthesis() if precedence_traditional(i) < PRECEDENCE["Mul"] else self._print(i) for i in args ] @@ -1315,7 +1315,7 @@ def _print_TensMul(self, expr): def _print_TensAdd(self, expr): args = [ - self._print(i).parens() if + self._print(i).parenthesis() if precedence_traditional(i) < PRECEDENCE["Mul"] else self._print(i) for i in expr.args ] @@ -1344,7 +1344,7 @@ def _print_PartialDerivative(self, deriv): x = x.right(' ') x = x.right(ds) - f = self._print(deriv.expr).parens() + f = self._print(deriv.expr).parenthesis() f.binding = prettyForm.FUNC pform = prettyForm(deriv_symbol) @@ -1409,7 +1409,7 @@ def _print_Piecewise(self, pexpr): D = D.below(D_row) - D = D.parens('{', '') + D = D.parenthesis('{', '') D.baseline = D.height()//2 D.binding = prettyForm.OPEN return D @@ -1471,7 +1471,7 @@ def _print_hyper(self, e): D = self._hprint_vseparator(D, P) # add parens - D = D.parens('(', ')') + D = D.parenthesis('(', ')') # create the F symbol above = D.height()//2 - 1 @@ -1532,7 +1532,7 @@ def _print_meijerg(self, e): D = self._hprint_vseparator(D, P) # add parens - D = D.parens('(', ')') + D = D.parenthesis('(', ')') # create the G symbol above = D.height()//2 - 1 @@ -1613,7 +1613,7 @@ def _helper_print_function(self, func, args, sort=False, func_name=None, if func_name: prettyFunc = self._print(Symbol(func_name)) else: - prettyFunc = self._print(func).parens() + prettyFunc = self._print(func).parenthesis() if elementwise: if self._use_unicode: @@ -1626,7 +1626,7 @@ def _helper_print_function(self, func, args, sort=False, func_name=None, *stringPict.next(prettyFunc, circ) ) - prettyArgs = self._print_seq(args, delimiter=delimiter).parens( + prettyArgs = self._print_seq(args, delimiter=delimiter).parenthesis( left=left, right=right) pform = prettyForm( @@ -1691,7 +1691,7 @@ def _print_dirichlet_eta(self, e): def _print_Heaviside(self, e): func_name = greek_unicode['theta'] if self._use_unicode else 'Heaviside' if e.args[1] is S.Half: - pform = self._print(e.args[0]).parens() + pform = self._print(e.args[0]).parenthesis() pform = pform.left(func_name) return pform else: @@ -1760,7 +1760,7 @@ def _print_Order(self, expr): pform = pform.right(self._print(expr.point)) else: pform = pform.right(self._print(expr.point[0])) - pform = pform.parens() + pform = pform.parenthesis() pform = pform.left("O") return pform @@ -1808,15 +1808,15 @@ def _print_DiracDelta(self, e): if len(e.args) == 2: a = prettyForm(greek_unicode['delta']) b = self._print(e.args[1]) - b = b.parens() + b = b.parenthesis() c = self._print(e.args[0]) - c = c.parens() + c = c.parenthesis() pform = a**b pform = pform.right(' ') pform = pform.right(c) return pform pform = self._print(e.args[0]) - pform = pform.parens() + pform = pform.parenthesis() pform = pform.left(greek_unicode['delta']) return pform else: @@ -1832,7 +1832,7 @@ def _print_Chi(self, e): # This needs a special case since otherwise it comes out as greek # letter chi... prettyFunc = prettyForm("Chi") - prettyArgs = self._print_seq(e.args).parens() + prettyArgs = self._print_seq(e.args).parenthesis() pform = prettyForm( binding=prettyForm.FUNC, *stringPict.next(prettyFunc, prettyArgs)) @@ -1850,13 +1850,13 @@ def _print_elliptic_e(self, e): else: pforma1 = self._print(e.args[1]) pform = self._hprint_vseparator(pforma0, pforma1) - pform = pform.parens() + pform = pform.parenthesis() pform = pform.left('E') return pform def _print_elliptic_k(self, e): pform = self._print(e.args[0]) - pform = pform.parens() + pform = pform.parenthesis() pform = pform.left('K') return pform @@ -1864,7 +1864,7 @@ def _print_elliptic_f(self, e): pforma0 = self._print(e.args[0]) pforma1 = self._print(e.args[1]) pform = self._hprint_vseparator(pforma0, pforma1) - pform = pform.parens() + pform = pform.parenthesis() pform = pform.left('F') return pform @@ -1879,7 +1879,7 @@ def _print_elliptic_pi(self, e): pforma = self._hprint_vseparator(pforma1, pforma2, ifascii_nougly=False) pforma = pforma.left('; ') pform = pforma.left(pforma0) - pform = pform.parens() + pform = pform.parenthesis() pform = pform.left(name) return pform @@ -1899,7 +1899,7 @@ def _print_Catalan(self, expr): def _print_Mod(self, expr): pform = self._print(expr.args[0]) if pform.binding > prettyForm.MUL: - pform = pform.parens() + pform = pform.parenthesis() pform = pform.right(' mod ') pform = pform.right(self._print(expr.args[1])) pform.binding = prettyForm.OPEN @@ -1922,7 +1922,7 @@ def pretty_negative(pform, index): if (pform.binding > prettyForm.NEG or pform.binding == prettyForm.ADD): - p = pform.parens() + p = pform.parenthesis() else: p = pform p = stringPict.next(pform_neg, p) @@ -1946,7 +1946,7 @@ def pretty_negative(pform, index): pform = self._print(-term) pforms.append(pretty_negative(pform, i)) elif term.is_Relational: - pforms.append(self._print(term).parens()) + pforms.append(self._print(term).parenthesis()) else: pforms.append(self._print(term)) @@ -2070,7 +2070,7 @@ def _print_Pow(self, power): return prettyForm("1")/self._print(Pow(b, -e, evaluate=False)) if b.is_Relational: - return self._print(b).parens().__pow__(self._print(e)) + return self._print(b).parenthesis().__pow__(self._print(e)) return self._print(b)**self._print(e) @@ -2251,7 +2251,7 @@ def _print_ConditionSet(self, ts): cond = self._print(ts.condition) if self._use_unicode: cond = self._print(cond) - cond = cond.parens() + cond = cond.parenthesis() if ts.base_set is S.UniversalSet: return self._hprint_vseparator(variables, cond, left="{", @@ -2300,7 +2300,7 @@ def _print_FormalPowerSeries(self, s): return self._print_Add(s.infinite) def _print_SetExpr(self, se): - pretty_set = self._print(se.set).parens() + pretty_set = self._print(se.set).parenthesis() pretty_name = self._print(Symbol("SetExpr")) return pretty_name.right(pretty_set) @@ -2336,7 +2336,7 @@ def _print_seq(self, seq, left=None, right=None, delimiter=', ', for item in seq: pform = self._print(item) if parenthesize(item): - pform = pform.parens() + pform = pform.parenthesis() if pforms: pforms.append(delimiter) pforms.append(pform) @@ -2346,7 +2346,7 @@ def _print_seq(self, seq, left=None, right=None, delimiter=', ', else: s = prettyForm(*stringPict.next(*pforms)) - s = s.parens(left, right, ifascii_nougly=ifascii_nougly) + s = s.parenthesis(left, right, ifascii_nougly=ifascii_nougly) return s def join(self, delimiter, args): @@ -2370,7 +2370,7 @@ def _print_list(self, l): def _print_tuple(self, t): if len(t) == 1: ptuple = prettyForm(*stringPict.next(self._print(t[0]), ',')) - return ptuple.parens('(', ')', ifascii_nougly=True) + return ptuple.parenthesis('(', ')', ifascii_nougly=True) else: return self._print_seq(t, '(', ')') @@ -2398,7 +2398,7 @@ def _print_set(self, s): return prettyForm('set()') items = sorted(s, key=default_sort_key) pretty = self._print_seq(items) - pretty = pretty.parens('{', '}', ifascii_nougly=True) + pretty = pretty.parenthesis('{', '}', ifascii_nougly=True) return pretty def _print_frozenset(self, s): @@ -2406,8 +2406,8 @@ def _print_frozenset(self, s): return prettyForm('frozenset()') items = sorted(s, key=default_sort_key) pretty = self._print_seq(items) - pretty = pretty.parens('{', '}', ifascii_nougly=True) - pretty = pretty.parens('(', ')', ifascii_nougly=True) + pretty = pretty.parenthesis('{', '}', ifascii_nougly=True) + pretty = pretty.parenthesis('(', ')', ifascii_nougly=True) pretty = prettyForm(*stringPict.next(type(s).__name__, pretty)) return pretty @@ -2440,7 +2440,7 @@ def _print_AlgebraicNumber(self, expr): def _print_ComplexRootOf(self, expr): args = [self._print_Add(expr.expr, order='lex'), expr.index] - pform = self._print_seq(args).parens() + pform = self._print_seq(args).parenthesis() pform = pform.left('CRootOf') return pform @@ -2450,7 +2450,7 @@ def _print_RootSum(self, expr): if expr.fun is not S.IdentityFunction: args.append(self._print(expr.fun)) - pform = self._print_seq(args).parens() + pform = self._print_seq(args).parenthesis() pform = pform.left('RootSum') return pform @@ -2533,7 +2533,7 @@ def _print_PolynomialRingBase(self, expr): def _print_GroebnerBasis(self, basis): exprs = [ self._print_Add(arg, order=basis.order) for arg in basis.exprs ] - exprs = self.join(", ", exprs).parens(left="[", right="]") + exprs = self.join(", ", exprs).parenthesis(left="[", right="]") gens = [ self._print(gen) for gen in basis.gens ] @@ -2542,14 +2542,14 @@ def _print_GroebnerBasis(self, basis): pform = self.join(", ", [exprs] + gens + [domain, order]) - pform = pform.parens() + pform = pform.parenthesis() pform = pform.left(basis.__class__.__name__) return pform def _print_Subs(self, e): pform = self._print(e.expr) - pform = pform.parens() + pform = pform.parenthesis() h = pform.height() if pform.height() > 1 else 2 rvert = stringPict(vobj('|', h), baseline=pform.baseline) @@ -2577,7 +2577,7 @@ def _print_number_function(self, e, name): m, x = e.args # TODO: copy-pasted from _print_Function: can we do better? prettyFunc = pform - prettyArgs = self._print_seq([x]).parens() + prettyArgs = self._print_seq([x]).parenthesis() pform = prettyForm( binding=prettyForm.FUNC, *stringPict.next(prettyFunc, prettyArgs)) pform.prettyFunc = prettyFunc @@ -2778,7 +2778,7 @@ def _print_Differential(self, diff): return self._print(d + ' ' + pretty_symbol(string)) else: pform = self._print(field) - pform = pform.parens() + pform = pform.parenthesis() return pform.left(d) def _print_Tr(self, p): @@ -2790,7 +2790,7 @@ def _print_Tr(self, p): def _print_primenu(self, e): pform = self._print(e.args[0]) - pform = pform.parens() + pform = pform.parenthesis() if self._use_unicode: pform = pform.left(greek_unicode['nu']) else: @@ -2799,7 +2799,7 @@ def _print_primenu(self, e): def _print_primeomega(self, e): pform = self._print(e.args[0]) - pform = pform.parens() + pform = pform.parenthesis() if self._use_unicode: pform = pform.left(greek_unicode['Omega']) else: diff --git a/sympy/printing/pretty/stringpict.py b/sympy/printing/pretty/stringpict.py index 2cfe27d6edbc..15dc4295fd2e 100644 --- a/sympy/printing/pretty/stringpict.py +++ b/sympy/printing/pretty/stringpict.py @@ -1,19 +1,7 @@ -"""Prettyprinter by Jurjen Bos. -(I hate spammers: mail me at pietjepuk314 at the reverse of ku.oc.oohay). -All objects have a method that create a "stringPict", -that can be used in the str method for pretty printing. - -Updates by Jason Gedge (email at cs mun ca) - - terminal_string() method - - minor fixes and changes (mostly to prettyForm) - -Updates by Juan Mauricio Matera (email at ar_ude_plnu_acisif): - - support for align attribute in `left`, `right`, `above` and `below` - methods - - adding the v_align() method - - Methods `parens`, `left`, `right`, `above` and `below` now returns - objects of the caller object class. - - The method `root` now works. +""" +Prettyprinter by Jurjen Bos. + +Classes used to render mathematical expressions as text-based pictures. """ import shutil @@ -173,7 +161,7 @@ def left(self, *args, align=""): >>> from sympy.printing.pretty.stringpict import stringPict >>> stringPict('a\\n-\\nb', 1).left( ... "numerator-> ",align="t").right(" <-denominator", - ... align="b").parens("","}").right(" fraction") + ... align="b").parenthesis("","}").right(" fraction") numerator-> a \\ - > fraction b <-denominator/ @@ -292,6 +280,7 @@ def above(self, *args, align="c"): baseline = len(string.splitlines()) - self.height() + self.baseline return self.__class__(string, baseline) + def parens(self, left="(", right=")", ifascii_nougly=False): """Put parentheses around self. Returns string, baseline arguments for stringPict. @@ -299,6 +288,37 @@ def parens(self, left="(", right=")", ifascii_nougly=False): left or right can be None or empty string which means 'no paren from that side' """ + + # TODO: Add a deprecation warning in favor of + # `self.parenthesis(...)`? + + h = self.height() + b = self.baseline + + # XXX this is a hack -- ascii parens are ugly! + if ifascii_nougly and not pretty_use_unicode(): + h = 1 + b = 0 + + res = self + + if left: + lparen = stringPict(vobj(left, h), baseline=b) + res = lparen.right(self) + if right: + rparen = stringPict(vobj(right, h), baseline=b) + res = res.right(rparen) + + return "\n".join(res.picture), res.baseline + + def parenthesis(self, left="(", right=")", ifascii_nougly=False): + """Returns a new object of the same type than self, + sorrounded by parenthesis of the type specified by + the arguments `left` and `right`. + + `left` or `right` can be None or empty string which means 'no paren from + that side' + """ h = self.height() b = self.baseline @@ -386,7 +406,7 @@ def root(self, n=None): elif n.width()<2: n = stringPict(str(n).ljust(2)) if n.height() > 1: - exponent = n.parens().left(stringPict("1 / "), align="c") + exponent = n.parenthesis().left(stringPict("1 / "), align="c") return self ** exponent # put line over expression @@ -501,7 +521,7 @@ def subindex(self, sub_index): >>> from sympy.printing.pretty.stringpict import stringPict >>> print( (stringPict("a").below("-").below(stringPict("b")) - ... ).parens().subindex(stringPict("a=4"))) + ... ).parenthesis().subindex(stringPict("a=4"))) /a\\ |-| \\b/ @@ -640,12 +660,12 @@ def __add__(self, *others): """ arg = self if arg.binding > prettyForm.NEG: - arg = arg.parens() + arg = arg.parenthesis() result = [arg] for arg in others: # add parentheses for weak binders if arg.binding > prettyForm.NEG: - arg = arg.parens() + arg = arg.parenthesis() #use existing minus sign if available if arg.binding != prettyForm.NEG: result.append(' + ') @@ -658,9 +678,9 @@ def __truediv__(self, den, slashed=False): raise NotImplementedError("Can't do slashed fraction yet") num = self if num.binding == prettyForm.DIV: - num = num.parens() + num = num.parenthesis() if den.binding == prettyForm.DIV: - den = den.parens() + den = den.parenthesis() if num.binding == prettyForm.NEG: num = num.right(' ') @@ -681,14 +701,14 @@ def __mul__(self, *others): #add parens on args that need them arg = self if arg.binding > prettyForm.MUL and arg.binding != prettyForm.NEG: - arg = arg.parens() + arg = arg.parenthesis() result = [arg] for arg in others: if arg.picture[0] not in quantity.values(): result.append(xsym('*')) #add parentheses for weak binders if arg.binding > prettyForm.MUL and arg.binding != prettyForm.NEG: - arg = arg.parens() + arg = arg.parenthesis() result.append(arg) len_res = len(result) @@ -722,13 +742,13 @@ def __pow__(self, b): a = self use_inline_func_form = False if b.binding == prettyForm.POW: - b = b.parens() + b = b.parenthesis() if a.binding > prettyForm.FUNC: - a = a.parens() + a = a.parenthesis() elif a.binding == prettyForm.FUNC: # heuristic for when to use inline power if b.height() > 1: - a = a.parens() + a = a.parenthesis() else: use_inline_func_form = True @@ -774,7 +794,7 @@ def apply(function, *args): argument_pict = first_arg.right(*rest) else: argument_pict = stringPict("") - argument_pict = argument_pict.parens() + argument_pict = argument_pict.parenthesis() func_pict = argument_pict.left(function) func_pict.binding = prettyForm.ATOM return func_pict