From 34704348ad3e512010cbf85b6a9dee9fff22cd66 Mon Sep 17 00:00:00 2001 From: Christian Bauer Date: Wed, 3 Jul 2002 18:19:57 +0000 Subject: [PATCH] powers of indexed objects are now parenthesized correctly in LaTeX output --- NEWS | 3 +++ ginac/indexed.cpp | 20 +++++++++----------- ginac/indexed.h | 1 + ginac/power.cpp | 33 +++++++++++---------------------- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/NEWS b/NEWS index 2d6b3c75..0e3d5bf3 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,8 @@ This file records noteworthy changes. +1.0.10 () +* 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, diff --git a/ginac/indexed.cpp b/ginac/indexed.cpp index dca12fd8..d9b2f474 100644 --- a/ginac/indexed.cpp +++ b/ginac/indexed.cpp @@ -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(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(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(c)) { // TeX output: group by variance bool first = true; diff --git a/ginac/indexed.h b/ginac/indexed.h index f539444e..ad18ddb5 100644 --- a/ginac/indexed.h +++ b/ginac/indexed.h @@ -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; diff --git a/ginac/power.cpp b/ginac/power.cpp index a6decf3c..be7c2d37 100644 --- a/ginac/power.cpp +++ b/ginac/power.cpp @@ -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(c); + if (exponent.is_equal(_ex1_2)) { - if (is_a(c)) - c.s << "\\sqrt{"; - else - c.s << "sqrt("; + c.s << (is_tex ? "\\sqrt{" : "sqrt("); basis.print(c); - if (is_a(c)) - c.s << '}'; - else - c.s << ')'; + c.s << (is_tex ? '}' : ')'); } else { - if (precedence() <= level) { - if (is_a(c)) - c.s << "{("; - else - c.s << "("; - } + if (precedence() <= level) + c.s << (is_tex ? "{(" : "("); basis.print(c, precedence()); if (is_a(c)) c.s << "**"; else c.s << '^'; - if (is_a(c)) + if (is_tex) c.s << '{'; exponent.print(c, precedence()); - if (is_a(c)) + if (is_tex) c.s << '}'; - if (precedence() <= level) { - if (is_a(c)) - c.s << ")}"; - else - c.s << ')'; - } + if (precedence() <= level) + c.s << (is_tex ? ")}" : ")"); } } } -- 2.44.0