]> www.ginac.de Git - ginac.git/blobdiff - ginac/indexed.cpp
documentation update
[ginac.git] / ginac / indexed.cpp
index dcd6d9510d6d0ac688802373f30f7d93e0f07362..d9b2f474fc8266e7e96a87ee483c1f5eace86352 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;
@@ -870,6 +868,27 @@ ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indi
                        }
                }
 
+               if (sum.is_zero()) {
+                       free_indices.clear();
+                       return sum;
+               }
+
+               // Symmetrizing over the dummy indices may cancel terms
+               int num_terms_orig = (is_a<add>(sum) ? sum.nops() : 1);
+               if (num_terms_orig > 1 && dummy_indices.size() >= 2) {
+                       lst dummy_syms;
+                       for (int i=0; i<dummy_indices.size(); i++)
+                               dummy_syms.append(dummy_indices[i].op(0));
+                       ex sum_symm = sum.symmetrize(dummy_syms);
+                       if (sum_symm.is_zero()) {
+                               free_indices.clear();
+                               return _ex0;
+                       }
+                       int num_terms = (is_a<add>(sum_symm) ? sum_symm.nops() : 1);
+                       if (num_terms < num_terms_orig)
+                               return sum_symm;
+               }
+
                return sum;
        }