powers of indexed objects are now parenthesized correctly in LaTeX output
authorChristian Bauer <Christian.Bauer@uni-mainz.de>
Wed, 3 Jul 2002 18:19:57 +0000 (18:19 +0000)
committerChristian Bauer <Christian.Bauer@uni-mainz.de>
Wed, 3 Jul 2002 18:19:57 +0000 (18:19 +0000)
NEWS
ginac/indexed.cpp
ginac/indexed.h
ginac/power.cpp

diff --git a/NEWS b/NEWS
index 2d6b3c7..0e3d5bf 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,8 @@
 This file records noteworthy changes.
 
+1.0.10 (<date>)
+* Powers of indexed objects are now parenthesized correctly in LaTeX output.
+
 1.0.9 (11 June 2002)
 * simplify_indexed() now raises/lowers dummy indices to canonicalize the index
   variance. This allows some simplifications that weren't possible before,
index dca12fd..d9b2f47 100644 (file)
@@ -179,7 +179,7 @@ void indexed::print(const print_context & c, unsigned level) const
 {
        GINAC_ASSERT(seq.size() > 0);
 
-       if (is_of_type(c, print_tree)) {
+       if (is_a<print_tree>(c)) {
 
                c.s << std::string(level, ' ') << class_name()
                    << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
@@ -191,21 +191,19 @@ void indexed::print(const print_context & c, unsigned level) const
 
        } else {
 
-               bool is_tex = is_of_type(c, print_latex);
+               bool is_tex = is_a<print_latex>(c);
                const ex & base = seq[0];
-               bool need_parens = is_ex_exactly_of_type(base, add) || is_ex_exactly_of_type(base, mul)
-                               || is_ex_exactly_of_type(base, ncmul) || is_ex_exactly_of_type(base, power)
-                               || is_ex_of_type(base, indexed);
+
+               if (precedence() <= level)
+                       c.s << (is_tex ? "{(" : "(");
                if (is_tex)
                        c.s << "{";
-               if (need_parens)
-                       c.s << "(";
-               base.print(c);
-               if (need_parens)
-                       c.s << ")";
+               base.print(c, precedence());
                if (is_tex)
                        c.s << "}";
                printindices(c, level);
+               if (precedence() <= level)
+                       c.s << (is_tex ? ")}" : ")");
        }
 }
 
@@ -321,7 +319,7 @@ void indexed::printindices(const print_context & c, unsigned level) const
 
                exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
 
-               if (is_of_type(c, print_latex)) {
+               if (is_a<print_latex>(c)) {
 
                        // TeX output: group by variance
                        bool first = true;
index f539444..ad18ddb 100644 (file)
@@ -144,6 +144,7 @@ public:
        // functions overriding virtual functions from base classes
 public:
        void print(const print_context & c, unsigned level = 0) const;
+       unsigned precedence(void) const {return 55;}
        bool info(unsigned inf) const;
        ex eval(int level = 0) const;
        exvector get_free_indices(void) const;
index a6decf3..be7c2d3 100644 (file)
@@ -33,6 +33,7 @@
 #include "constant.h"
 #include "inifcns.h" // for log() in power::derivative()
 #include "matrix.h"
+#include "indexed.h"
 #include "symbol.h"
 #include "print.h"
 #include "archive.h"
@@ -169,39 +170,27 @@ void power::print(const print_context & c, unsigned level) const
 
        } else {
 
+               bool is_tex = is_a<print_latex>(c);
+
                if (exponent.is_equal(_ex1_2)) {
-                       if (is_a<print_latex>(c))
-                               c.s << "\\sqrt{";
-                       else
-                               c.s << "sqrt(";
+                       c.s << (is_tex ? "\\sqrt{" : "sqrt(");
                        basis.print(c);
-                       if (is_a<print_latex>(c))
-                               c.s << '}';
-                       else
-                               c.s << ')';
+                       c.s << (is_tex ? '}' : ')');
                } else {
-                       if (precedence() <= level) {
-                               if (is_a<print_latex>(c))
-                                       c.s << "{(";
-                               else
-                                       c.s << "(";
-                       }
+                       if (precedence() <= level)
+                               c.s << (is_tex ? "{(" : "(");
                        basis.print(c, precedence());
                        if (is_a<print_python>(c))
                                c.s << "**";
                        else
                                c.s << '^';
-                       if (is_a<print_latex>(c))
+                       if (is_tex)
                                c.s << '{';
                        exponent.print(c, precedence());
-                       if (is_a<print_latex>(c))
+                       if (is_tex)
                                c.s << '}';
-                       if (precedence() <= level) {
-                               if (is_a<print_latex>(c))
-                                       c.s << ")}";
-                               else
-                                       c.s << ')';
-                       }
+                       if (precedence() <= level)
+                               c.s << (is_tex ? ")}" : ")");
                }
        }
 }