From 652c1c20e6fc469133029d2f233e0ba41f713d4f Mon Sep 17 00:00:00 2001 From: mmatera Date: Tue, 12 Nov 2024 21:58:29 -0300 Subject: [PATCH] use stringPict.root to draw n-roots adjust comments on root --- sympy/printing/pretty/pretty.py | 34 +++-------------------- sympy/printing/pretty/stringpict.py | 42 ++++++++++++++--------------- 2 files changed, 25 insertions(+), 51 deletions(-) diff --git a/sympy/printing/pretty/pretty.py b/sympy/printing/pretty/pretty.py index 7d3c19c11969..4f3dd8485a91 100644 --- a/sympy/printing/pretty/pretty.py +++ b/sympy/printing/pretty/pretty.py @@ -2044,39 +2044,13 @@ def _print_nth_root(self, base, root): or (base.is_Integer and base.is_nonnegative))): return bpretty.left(nth_root[2]) - # Construct root sign, start with the \/ shape - _zZ = xobj('/', 1) - rootsign = xobj('\\', 1) + _zZ - # Constructing the number to put on root rpretty = self._print(root) - # roots look bad if they are not a single line if rpretty.height() != 1: return self._print(base)**self._print(1/root) - # If power is half, no number should appear on top of root sign - exp = '' if root == 2 else str(rpretty).ljust(2) - if len(exp) > 2: - rootsign = ' '*(len(exp) - 2) + rootsign - # Stack the exponent - rootsign = stringPict(exp + '\n' + rootsign) - rootsign.baseline = 0 - # Diagonal: length is one less than height of base - linelength = bpretty.height() - 1 - diagonal = stringPict('\n'.join( - ' '*(linelength - i - 1) + _zZ + ' '*i - for i in range(linelength) - )) - # Put baseline just below lowest line: next to exp - diagonal.baseline = linelength - 1 - # Make the root symbol - rootsign = rootsign.right(diagonal) - # Det the baseline to match contents to fix the height - # but if the height of bpretty is one, the rootsign must be one higher - rootsign.baseline = max(1, bpretty.baseline) - #build result - s = prettyForm(hobj('_', 2 + bpretty.width())) - s = bpretty.above(s) - s = s.left(rootsign) - return s + + if root == 2: + return bpretty.root() + return bpretty.root(rpretty) def _print_Pow(self, power): from sympy.simplify.simplify import fraction diff --git a/sympy/printing/pretty/stringpict.py b/sympy/printing/pretty/stringpict.py index ee11d241f2da..e1ccf60b2aef 100644 --- a/sympy/printing/pretty/stringpict.py +++ b/sympy/printing/pretty/stringpict.py @@ -337,34 +337,34 @@ def root(self, n=None): >>> from sympy.printing.pretty.stringpict import stringPict, prettyForm >>> print(stringPict("x+3").root().right(" + a")) - ___ - \\/x+3 + a + _____ + \\/ x+3 + a >>> print(stringPict("x+3").root(stringPict("3")).right(" + a")) - 3 ___ - \\/x+3 + a + 3 _____ + \\/ x+3 + a >>> print((prettyForm("x")**stringPict("a")).root().right(" + a")) - __ - / a - \\/ x + a + ____ + / a + \\/ x + a >>> print((prettyForm("x")**stringPict("a")).root(stringPict("3")).right(" + a")) - __ - 3 / a - \\/ x + a + ____ + 3 / a + \\/ x + a >>> print((prettyForm("x+3")/prettyForm("y")).root().right(" + a")) - ___ - /x+3 - / --- + a + _____ + / x+3 + / --- + a \\/ y >>> print((prettyForm("x+3")/prettyForm("y")).root(stringPict("3")).right(" + a")) - ___ - /x+3 - 3 / --- + a - \\/ y + _____ + / x+3 + 3 / --- + a + \\/ y For indices with more than one line, use the Pow form: @@ -377,9 +377,8 @@ def root(self, n=None): |---| + a \\ y / """ - # TODO: use it in root drawing in PrettyPrinter. - # - # put line over expression + # Decide if using a square root symbol or + # an base - exponent form: if n is not None: if isinstance(n, str): n = stringPict(n) @@ -387,7 +386,8 @@ def root(self, n=None): exponent = n.parens().left(stringPict("1 / ")) return self ** exponent - result = self.above('_' * self.width()) + # put line over expression + result = self.above(hobj('_', 2 + self.width())) #construct right half of root symbol height = self.height()