Skip to content

Commit

Permalink
Update stringpict.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera authored Nov 11, 2024
1 parent 2fcbc77 commit b25a898
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions sympy/printing/pretty/stringpict.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def parens(self, left='(', right=')', ifascii_nougly=False):
rparen = stringPict(vobj(right, h), baseline=b)
res = stringPict(*res.right(rparen))

return ('\n'.join(res.picture), res.baseline)
return self.__class__('\n'.join(res.picture), res.baseline)

def leftslash(self):
"""Precede object by a slash of the proper size.
Expand Down Expand Up @@ -431,12 +431,12 @@ def __add__(self, *others):
"""
arg = self
if arg.binding > prettyForm.NEG:
arg = stringPict(*arg.parens())
arg = arg.parens()
result = [arg]
for arg in others:
#add parentheses for weak binders
if arg.binding > prettyForm.NEG:
arg = stringPict(*arg.parens())
arg = arg.parens()
#use existing minus sign if available
if arg.binding != prettyForm.NEG:
result.append(' + ')
Expand All @@ -450,9 +450,9 @@ def __truediv__(self, den, slashed=False):
raise NotImplementedError("Can't do slashed fraction yet")
num = self
if num.binding == prettyForm.DIV:
num = stringPict(*num.parens())
num = num.parens()
if den.binding == prettyForm.DIV:
den = stringPict(*den.parens())
den = den.parens()

if num.binding==prettyForm.NEG:
num = num.right(" ")[0]
Expand All @@ -476,14 +476,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 = stringPict(*arg.parens())
arg = arg.parens()
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 = stringPict(*arg.parens())
arg = arg.parens()
result.append(arg)

len_res = len(result)
Expand Down Expand Up @@ -517,29 +517,31 @@ def __pow__(self, b):
a = self
use_inline_func_form = False
if b.binding == prettyForm.POW:
b = stringPict(*b.parens())
b = b.parens()
if a.binding > prettyForm.FUNC:
a = stringPict(*a.parens())
a = a.parens()
elif a.binding == prettyForm.FUNC:
# heuristic for when to use inline power
if b.height() > 1:
a = stringPict(*a.parens())
a = a.parens()
else:
use_inline_func_form = True

if use_inline_func_form:
# 2
# sin + + (x)
b.baseline = a.prettyFunc.baseline + b.height()
func = stringPict(*a.prettyFunc.right(b))
return prettyForm(*func.right(a.prettyArgs))
func = prettyForm(*a.prettyFunc.right(b))
return func.right(a.prettyArgs)
else:
# 2 <-- top
# (x+y) <-- bot
top = stringPict(*b.left(' '*a.width()))
bot = stringPict(*a.right(' '*b.width()))
top = b.left(' '*a.width())
bot = a.right(' '*b.width())

return prettyForm(binding=prettyForm.POW, *bot.above(top))
result = bot.above(top)
result.binding = prettyForm.POW
return result

simpleFunctions = ["sin", "cos", "tan"]

Expand All @@ -554,11 +556,19 @@ def apply(function, *args):
arg = args[0].__pretty__()
if arg.binding <= prettyForm.DIV:
#optimization: no parentheses necessary
return prettyForm(binding=prettyForm.FUNC, *arg.left(function + ' '))
result = arg.left(function + ' ')
result.binding = prettyForm.FUNC
return result
argumentList = []
for arg in args:
argumentList.append(',')
argumentList.append(arg.__pretty__())
argumentList = stringPict(*stringPict.next(*argumentList[1:]))
argumentList = stringPict(*argumentList.parens())
return prettyForm(binding=prettyForm.ATOM, *argumentList.left(function))
if argumentList:
first_arg, *rest = argumentList
argument_pict = first_arg.right(*rest)
else:
argument_pict = stringPict("")
argument_pict = argumentList.parens()
func_pict = argumentList.left(function)
func_pict.binding = prettyForm.ATOM
return func_pict

0 comments on commit b25a898

Please sign in to comment.