]> www.ginac.de Git - ginac.git/blobdiff - ginac/indexed.cpp
Fix some apparent typos and misindentations.
[ginac.git] / ginac / indexed.cpp
index aa8500399e270facb7fa72a59a6c9277c26214fa..873a9ad0b9661d721f14d09439ab8256661b7260 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's indexed expressions. */
 
 /*
- *  GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2015 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <iostream>
-#include <sstream>
-#include <stdexcept>
-#include <limits>
-
 #include "indexed.h"
 #include "idx.h"
 #include "add.h"
 #include "matrix.h"
 #include "inifcns.h"
 
+#include <iostream>
+#include <limits>
+#include <sstream>
+#include <stdexcept>
+
 namespace GiNaC {
 
 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(indexed, exprseq,
@@ -55,7 +55,6 @@ GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(indexed, exprseq,
 
 indexed::indexed() : symtree(not_symmetric())
 {
-       tinfo_key = &indexed::tinfo_static;
 }
 
 //////////
@@ -64,87 +63,75 @@ indexed::indexed() : symtree(not_symmetric())
 
 indexed::indexed(const ex & b) : inherited(b), symtree(not_symmetric())
 {
-       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symtree(not_symmetric())
 {
-       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(not_symmetric())
 {
-       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(not_symmetric())
 {
-       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symtree(not_symmetric())
 {
-       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(symm)
 {
-       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(symm)
 {
-       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symtree(symm)
 {
-       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symtree(not_symmetric())
 {
        seq.insert(seq.end(), v.begin(), v.end());
-       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const ex & b, const symmetry & symm, const exvector & v) : inherited(b), symtree(symm)
 {
        seq.insert(seq.end(), v.begin(), v.end());
-       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const symmetry & symm, const exprseq & es) : inherited(es), symtree(symm)
 {
-       tinfo_key = &indexed::tinfo_static;
 }
 
 indexed::indexed(const symmetry & symm, const exvector & v, bool discardable) : inherited(v, discardable), symtree(symm)
 {
-       tinfo_key = &indexed::tinfo_static;
 }
 
 indexed::indexed(const symmetry & symm, std::auto_ptr<exvector> vp) : inherited(vp), symtree(symm)
 {
-       tinfo_key = &indexed::tinfo_static;
 }
 
 //////////
 // archiving
 //////////
 
-indexed::indexed(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
+void indexed::read_archive(const archive_node &n, lst &sym_lst)
 {
+       inherited::read_archive(n, sym_lst);
        if (!n.find_ex("symmetry", symtree, sym_lst)) {
                // GiNaC versions <= 0.9.0 had an unsigned "symmetry" property
                unsigned symm = 0;
@@ -163,6 +150,7 @@ indexed::indexed(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
                const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
        }
 }
+GINAC_BIND_UNARCHIVER(indexed);
 
 void indexed::archive(archive_node &n) const
 {
@@ -170,8 +158,6 @@ void indexed::archive(archive_node &n) const
        n.add_ex("symmetry", symtree);
 }
 
-DEFAULT_UNARCHIVE(indexed)
-
 //////////
 // functions overriding virtual functions from base classes
 //////////
@@ -299,7 +285,7 @@ ex indexed::eval(int level) const
                return f * thiscontainer(v);
        }
 
-       if(this->tinfo()==&indexed::tinfo_static && seq.size()==1)
+       if((typeid(*this) == typeid(indexed)) && seq.size()==1)
                return base;
 
        // Canonicalize indices according to the symmetry properties
@@ -662,7 +648,7 @@ bool reposition_dummy_indices(ex & e, exvector & variant_dummy_indices, exvector
        }
 
        // In the case where a dummy symbol occurs twice in the same indexed object
-       // we try all posibilities of raising/lowering and keep the least one in
+       // we try all possibilities of raising/lowering and keep the least one in
        // the sense of ex_is_less.
        ex optimal_e = e;
        size_t numpossibs = 1 << local_var_dummies.size();
@@ -811,6 +797,7 @@ ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & du
 
        // Perform contractions
        bool something_changed = false;
+       bool has_nonsymmetric = false;
        GINAC_ASSERT(v.size() > 1);
        exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
        for (it1 = v.begin(); it1 != next_to_last; it1++) {
@@ -820,6 +807,7 @@ try_again:
                        continue;
 
                bool first_noncommutative = (it1->return_type() != return_types::commutative);
+               bool first_nonsymmetric = ex_to<symmetry>(ex_to<indexed>(*it1).get_symmetry()).has_nonsymmetric();
 
                // Indexed factor found, get free indices and look for contraction
                // candidates
@@ -886,8 +874,16 @@ contraction_done:
                                        // Non-commutative products are always re-expanded to give
                                        // eval_ncmul() the chance to re-order and canonicalize
                                        // the product
+                                       bool is_a_product = (is_exactly_a<mul>(*it1) || is_exactly_a<ncmul>(*it1)) &&
+                                                           (is_exactly_a<mul>(*it2) || is_exactly_a<ncmul>(*it2));
                                        ex r = (non_commutative ? ex(ncmul(v, true)) : ex(mul(v)));
-                                       return simplify_indexed(r, free_indices, dummy_indices, sp);
+
+                                       // If new expression is a product we can call this function again,
+                                       // otherwise we need to pass argument to simplify_indexed() to be expanded
+                                       if (is_a_product)
+                                               return simplify_indexed_product(r, free_indices, dummy_indices, sp);
+                                       else
+                                               return simplify_indexed(r, free_indices, dummy_indices, sp);
                                }
 
                                // Both objects may have new indices now or they might
@@ -896,6 +892,11 @@ contraction_done:
                                something_changed = true;
                                goto try_again;
                        }
+                       else if (!has_nonsymmetric &&
+                                       (first_nonsymmetric ||
+                                        ex_to<symmetry>(ex_to<indexed>(*it2).get_symmetry()).has_nonsymmetric())) {
+                               has_nonsymmetric = true;
+                       }
                }
        }
 
@@ -949,20 +950,22 @@ contraction_done:
        // The result should be symmetric with respect to exchange of dummy
        // indices, so if the symmetrization vanishes, the whole expression is
        // zero. This detects things like eps.i.j.k * p.j * p.k = 0.
-       ex q = idx_symmetrization<idx>(r, local_dummy_indices);
-       if (q.is_zero()) {
-               free_indices.clear();
-               return _ex0;
-       }
-       q = idx_symmetrization<varidx>(q, local_dummy_indices);
-       if (q.is_zero()) {
-               free_indices.clear();
-               return _ex0;
-       }
-       q = idx_symmetrization<spinidx>(q, local_dummy_indices);
-       if (q.is_zero()) {
-               free_indices.clear();
-               return _ex0;
+       if (has_nonsymmetric) {
+               ex q = idx_symmetrization<idx>(r, local_dummy_indices);
+               if (q.is_zero()) {
+                       free_indices.clear();
+                       return _ex0;
+               }
+               q = idx_symmetrization<varidx>(q, local_dummy_indices);
+               if (q.is_zero()) {
+                       free_indices.clear();
+                       return _ex0;
+               }
+               q = idx_symmetrization<spinidx>(q, local_dummy_indices);
+               if (q.is_zero()) {
+                       free_indices.clear();
+                       return _ex0;
+               }
        }
 
        // Dummy index renaming
@@ -985,7 +988,7 @@ public:
        terminfo(const ex & orig_, const ex & symm_) : orig(orig_), symm(symm_) {}
 
        ex orig; /**< original term */
-       ex symm; /**< symmtrized term */
+       ex symm; /**< symmetrized term */
 };
 
 class terminfo_is_less {