]> www.ginac.de Git - ginac.git/commitdiff
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 2d6b3c758f2ae59e78d3c081218c7d041523e8b6..0e3d5bf303fa6a196a9d4039d39d3e152b311561 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,8 @@
 This file records noteworthy changes.
 
 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,
 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 dca12fd8945344c1c826e469aaa739dfa94631de..d9b2f474fc8266e7e96a87ee483c1f5eace86352 100644 (file)
@@ -179,7 +179,7 @@ void indexed::print(const print_context & c, unsigned level) const
 {
        GINAC_ASSERT(seq.size() > 0);
 
 {
        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
 
                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 {
 
 
        } else {
 
-               bool is_tex = is_of_type(c, print_latex);
+               bool is_tex = is_a<print_latex>(c);
                const ex & base = seq[0];
                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 (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 (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();
 
 
                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;
 
                        // TeX output: group by variance
                        bool first = true;
index f539444e7b29f04b39edf42bbf38d0059ecd1924..ad18ddb55f91c568e1b09f727a4f8b814ff59329 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;
        // 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;
        bool info(unsigned inf) const;
        ex eval(int level = 0) const;
        exvector get_free_indices(void) const;
index a6decf3cb39804eb1c255dd64d2ecebb317861ad..be7c2d371f61a12f1ec72697bd21f7d0ff41fa3b 100644 (file)
@@ -33,6 +33,7 @@
 #include "constant.h"
 #include "inifcns.h" // for log() in power::derivative()
 #include "matrix.h"
 #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"
 #include "symbol.h"
 #include "print.h"
 #include "archive.h"
@@ -169,39 +170,27 @@ void power::print(const print_context & c, unsigned level) const
 
        } else {
 
 
        } else {
 
+               bool is_tex = is_a<print_latex>(c);
+
                if (exponent.is_equal(_ex1_2)) {
                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);
                        basis.print(c);
-                       if (is_a<print_latex>(c))
-                               c.s << '}';
-                       else
-                               c.s << ')';
+                       c.s << (is_tex ? '}' : ')');
                } else {
                } 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 << '^';
                        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());
                                c.s << '{';
                        exponent.print(c, precedence());
-                       if (is_a<print_latex>(c))
+                       if (is_tex)
                                c.s << '}';
                                c.s << '}';
-                       if (precedence() <= level) {
-                               if (is_a<print_latex>(c))
-                                       c.s << ")}";
-                               else
-                                       c.s << ')';
-                       }
+                       if (precedence() <= level)
+                               c.s << (is_tex ? ")}" : ")");
                }
        }
 }
                }
        }
 }