pygae / galgebra

Symbolic Geometric Algebra/Calculus package for SymPy :crystal_ball:
https://galgebra.rtfd.io/
BSD 3-Clause "New" or "Revised" License
227 stars 62 forks source link

Work out how to integrate GaLatexPrinter._print_Pow upstream #351

Open eric-wieser opened 4 years ago

eric-wieser commented 4 years ago

Our copy of this function seems to have originated from sympy/sympy@f14bb4c5f44b3af78488f0a9cf02a95a2fbfb99b

The diff below shows the changes made in galgebra (with trivial changes omitted)

--- galgebra\printer.py Sun May 17 12:31:00 2020
+++ galgebra\printer.py Sun May 17 12:31:11 2020
@@ -571,9 +571,15 @@
             Matrix.__ga_print_str__ = GaLatexPrinter.Matrix__ga_print_str__

     def _print_Pow(self, expr):
+        base = self._print(expr.base)
+        if ('_' in base or '^' in base) and 'cdot' not in base:
+            mode = True
+        else:
+            mode = False
+
         # Treat x**Rational(1, n) as special case
         if expr.exp.is_Rational and abs(expr.exp.p) == 1 and expr.exp.q != 1:
-            base = self._print(expr.base)
+            #base = self._print(expr.base)
             expq = expr.exp.q

             if expq == 2:
@@ -591,14 +597,18 @@
             and expr.exp.is_Rational \
                 and expr.exp.q != 1:
             base, p, q = self._print(expr.base), expr.exp.p, expr.exp.q
-            return r"%s^{%s/%s}" % (base, p, q)
+            if mode:
+                return r"{\left ( %s \right )}^{%s/%s}" % (base, p, q)
+            else:
+                return r"%s^{%s/%s}" % (base, p, q)
+
         elif expr.exp.is_Rational and expr.exp.is_negative and expr.base.is_Function:
             # Things like 1/x
             return r"\frac{%s}{%s}" % \
                 (1, self._print(Pow(expr.base, -expr.exp)))
         else:
             if expr.base.is_Function:
-                return self._print(expr.base, self._print(expr.exp))
+                return r"{%s}^{%s}" % (self._print(expr.base), self._print(expr.exp))
             else:
                 if expr.is_commutative and expr.exp == -1:
                     #solves issue 1030
@@ -613,7 +623,10 @@
                 if self._needs_brackets(expr.base):
                     tex = r"\left(%s\right)^{%s}"
                 else:
-                    tex = r"%s^{%s}"
+                    if mode:
+                        tex = r"{\left ( %s \right )}^{%s}"
+                    else:
+                        tex = r"%s^{%s}"

                 return tex % (self._print(expr.base),
                               self._print(expr.exp))

The key change here seems to be making Symbol('a_2')^3 print as \left( a_2 \right) ^ 3

utensil commented 4 years ago

x-ref #276