]> www.ginac.de Git - ginac.git/blobdiff - ginac/indexed.cpp
Bug in expand_dummy_sum is fixed.
[ginac.git] / ginac / indexed.cpp
index e9c4fee2e56e9bd09b9c36a8bf73cb4f79d2a820..bd41f44b0b38a271d243564c945d674bea9acaa3 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's indexed expressions. */
 
 /*
- *  GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2006 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
@@ -39,6 +39,7 @@
 #include "utils.h"
 #include "integral.h"
 #include "matrix.h"
+#include "inifcns.h"
 
 namespace GiNaC {
 
@@ -53,7 +54,7 @@ GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(indexed, exprseq,
 
 indexed::indexed() : symtree(not_symmetric())
 {
-       tinfo_key = TINFO_indexed;
+       tinfo_key = &indexed::tinfo_static;
 }
 
 //////////
@@ -62,79 +63,79 @@ indexed::indexed() : symtree(not_symmetric())
 
 indexed::indexed(const ex & b) : inherited(b), symtree(not_symmetric())
 {
-       tinfo_key = TINFO_indexed;
+       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symtree(not_symmetric())
 {
-       tinfo_key = TINFO_indexed;
+       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 = TINFO_indexed;
+       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 = TINFO_indexed;
+       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 = TINFO_indexed;
+       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 = TINFO_indexed;
+       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 = TINFO_indexed;
+       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 = TINFO_indexed;
+       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 = TINFO_indexed;
+       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 = TINFO_indexed;
+       tinfo_key = &indexed::tinfo_static;
        validate();
 }
 
 indexed::indexed(const symmetry & symm, const exprseq & es) : inherited(es), symtree(symm)
 {
-       tinfo_key = TINFO_indexed;
+       tinfo_key = &indexed::tinfo_static;
 }
 
 indexed::indexed(const symmetry & symm, const exvector & v, bool discardable) : inherited(v, discardable), symtree(symm)
 {
-       tinfo_key = TINFO_indexed;
+       tinfo_key = &indexed::tinfo_static;
 }
 
 indexed::indexed(const symmetry & symm, std::auto_ptr<exvector> vp) : inherited(vp), symtree(symm)
 {
-       tinfo_key = TINFO_indexed;
+       tinfo_key = &indexed::tinfo_static;
 }
 
 //////////
@@ -297,7 +298,7 @@ ex indexed::eval(int level) const
                return f * thiscontainer(v);
        }
 
-       if(this->tinfo()==TINFO_indexed && seq.size()==1)
+       if(this->tinfo()==&indexed::tinfo_static && seq.size()==1)
                return base;
 
        // Canonicalize indices according to the symmetry properties
@@ -317,6 +318,20 @@ ex indexed::eval(int level) const
        return ex_to<basic>(base).eval_indexed(*this);
 }
 
+ex indexed::real_part() const
+{
+       if(op(0).info(info_flags::real))
+               return *this;
+       return real_part_function(*this).hold();
+}
+
+ex indexed::imag_part() const
+{
+       if(op(0).info(info_flags::real))
+               return 0;
+       return imag_part_function(*this).hold();
+}
+
 ex indexed::thiscontainer(const exvector & v) const
 {
        return indexed(ex_to<symmetry>(symtree), v);
@@ -520,22 +535,6 @@ struct is_summation_idx : public std::unary_function<ex, bool> {
        }
 };
 
-exvector power::get_free_indices() const
-{
-       // Get free indices of basis
-       exvector basis_indices = basis.get_free_indices();
-
-       if (exponent.info(info_flags::even)) {
-               // If the exponent is an even number, then any "free" index that
-               // forms a dummy pair with itself is actually a summation index
-               exvector really_free;
-               std::remove_copy_if(basis_indices.begin(), basis_indices.end(),
-                                   std::back_inserter(really_free), is_summation_idx());
-               return really_free;
-       } else
-               return basis_indices;
-}
-
 exvector integral::get_free_indices() const
 {
        if (a.get_free_indices().size() || b.get_free_indices().size())
@@ -738,6 +737,9 @@ template<class T> ex idx_symmetrization(const ex& r,const exvector& local_dummy_
        return q;
 }
 
+// Forward declaration needed in absence of friend injection, C.f. [namespace.memdef]:
+ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp);
+
 /** Simplify product of indexed expressions (commutative, noncommutative and
  *  simple squares), return list of free indices. */
 ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
@@ -790,20 +792,12 @@ try_again:
 
                        // At least one dummy index, is it a defined scalar product?
                        bool contracted = false;
-                       if (free.empty()) {
-
-                               // Find minimal dimension of all indices of both factors
-                               exvector::const_iterator dit = ex_to<indexed>(*it1).seq.begin() + 1, ditend = ex_to<indexed>(*it1).seq.end();
-                               ex dim = ex_to<idx>(*dit).get_dim();
-                               ++dit;
-                               for (; dit != ditend; ++dit) {
-                                       dim = minimal_dim(dim, ex_to<idx>(*dit).get_dim());
-                               }
-                               dit = ex_to<indexed>(*it2).seq.begin() + 1;
-                               ditend = ex_to<indexed>(*it2).seq.end();
-                               for (; dit != ditend; ++dit) {
-                                       dim = minimal_dim(dim, ex_to<idx>(*dit).get_dim());
-                               }
+                       if (free.empty() && it1->nops()==2 && it2->nops()==2) {
+
+                               ex dim = minimal_dim(
+                                       ex_to<idx>(it1->op(1)).get_dim(),
+                                       ex_to<idx>(it2->op(1)).get_dim()
+                               );
 
                                // User-defined scalar product?
                                if (sp.is_defined(*it1, *it2, dim)) {
@@ -1348,6 +1342,46 @@ void scalar_products::debugprint() const
        }
 }
 
+exvector get_all_dummy_indices_safely(const ex & e)
+{
+       if (is_a<indexed>(e))
+               return ex_to<indexed>(e).get_dummy_indices();
+       else if (is_a<power>(e) && e.op(1)==2) {
+               return e.op(0).get_free_indices();
+       }       
+       else if (is_a<mul>(e) || is_a<ncmul>(e)) {
+               exvector dummies;
+               exvector free_indices;
+               for (int i=0; i<e.nops(); ++i) {
+                       exvector dummies_of_factor = get_all_dummy_indices_safely(e.op(i));
+                       dummies.insert(dummies.end(), dummies_of_factor.begin(),
+                               dummies_of_factor.end());
+                       exvector free_of_factor = e.op(i).get_free_indices();
+                       free_indices.insert(free_indices.begin(), free_of_factor.begin(),
+                               free_of_factor.end());
+               }
+               exvector free_out, dummy_out;
+               find_free_and_dummy(free_indices.begin(), free_indices.end(), free_out,
+                       dummy_out);
+               dummies.insert(dummies.end(), dummy_out.begin(), dummy_out.end());
+               return dummies;
+       }
+       else if(is_a<add>(e)) {
+               exvector result;
+               for(int i=0; i<e.nops(); ++i) {
+                       exvector dummies_of_term = get_all_dummy_indices_safely(e.op(i));
+                       sort(dummies_of_term.begin(), dummies_of_term.end());
+                       exvector new_vec;
+                       set_union(result.begin(), result.end(), dummies_of_term.begin(),
+                               dummies_of_term.end(), std::back_inserter<exvector>(new_vec),
+                               ex_is_less());
+                       result.swap(new_vec);
+               }
+               return result;
+       }
+       return exvector();
+}
+
 /** Returns all dummy indices from the exvector */
 exvector get_all_dummy_indices(const ex & e)
 {
@@ -1415,20 +1449,20 @@ lst rename_dummy_indices_uniquely(const exvector & va, const exvector & vb)
 ex rename_dummy_indices_uniquely(const exvector & va, const exvector & vb, const ex & b)
 {
        lst indices_subs = rename_dummy_indices_uniquely(va, vb);
-       return (indices_subs.op(0).nops()>0 ? b.subs((lst)indices_subs.op(0), (lst)indices_subs.op(1), subs_options::no_pattern) : b);
+       return (indices_subs.op(0).nops()>0 ? b.subs(ex_to<lst>(indices_subs.op(0)), ex_to<lst>(indices_subs.op(1)), subs_options::no_pattern|subs_options::no_index_renaming) : b);
 }
 
 ex rename_dummy_indices_uniquely(const ex & a, const ex & b)
 {
-       exvector va = get_all_dummy_indices(a);
+       exvector va = get_all_dummy_indices_safely(a);
        if (va.size() > 0) {
-               exvector vb = get_all_dummy_indices(b);
+               exvector vb = get_all_dummy_indices_safely(b);
                if (vb.size() > 0) {
                        sort(va.begin(), va.end(), ex_is_less());
                        sort(vb.begin(), vb.end(), ex_is_less());
                        lst indices_subs = rename_dummy_indices_uniquely(va, vb);
                        if (indices_subs.op(0).nops() > 0)
-                               return b.subs((lst)indices_subs.op(0), (lst)indices_subs.op(1), subs_options::no_pattern);
+                               return b.subs(ex_to<lst>(indices_subs.op(0)), ex_to<lst>(indices_subs.op(1)), subs_options::no_pattern|subs_options::no_index_renaming);
                }
        }
        return b;
@@ -1437,7 +1471,7 @@ ex rename_dummy_indices_uniquely(const ex & a, const ex & b)
 ex rename_dummy_indices_uniquely(exvector & va, const ex & b, bool modify_va)
 {
        if (va.size() > 0) {
-               exvector vb = get_all_dummy_indices(b);
+               exvector vb = get_all_dummy_indices_safely(b);
                if (vb.size() > 0) {
                        sort(vb.begin(), vb.end(), ex_is_less());
                        lst indices_subs = rename_dummy_indices_uniquely(va, vb);
@@ -1454,7 +1488,7 @@ ex rename_dummy_indices_uniquely(exvector & va, const ex & b, bool modify_va)
                                        }
                                        sort(va.begin(), va.end(), ex_is_less());
                                }
-                               return b.subs((lst)indices_subs.op(0), (lst)indices_subs.op(1), subs_options::no_pattern);
+                               return b.subs(ex_to<lst>(indices_subs.op(0)), ex_to<lst>(indices_subs.op(1)), subs_options::no_pattern|subs_options::no_index_renaming);
                        }
                }
        }
@@ -1467,44 +1501,33 @@ ex expand_dummy_sum(const ex & e, bool subs_idx)
        pointer_to_map_function_1arg<bool> fcn(expand_dummy_sum, subs_idx);
        if (is_a<add>(e_expanded) || is_a<lst>(e_expanded) || is_a<matrix>(e_expanded)) {
                return e_expanded.map(fcn);
-       } else if (is_a<ncmul>(e_expanded) || is_a<mul>(e_expanded) || is_a<power>(e_expanded)) {
-               exvector v = get_all_dummy_indices(e_expanded);
-               exvector::const_iterator it = v.begin(), itend = v.end();
-               while (it != itend) {
-                       varidx nu = ex_to<varidx>(*it);
-                       if (nu.is_dim_numeric()) {
-                               ex en = 0;
-                               for (int i=0; i < ex_to<numeric>(nu.get_dim()).to_int(); i++) {
-                                       if (is_a<varidx>(nu) && !subs_idx) {
-                                               en += e_expanded.subs(lst(nu == varidx(i, nu.get_dim(), true), nu.toggle_variance() == varidx(i, nu.get_dim())));
-                                       } else {
-                                               en += e_expanded.subs(lst(nu == idx(i, nu.get_dim()), nu.toggle_variance() == idx(i, nu.get_dim())));
-                                       }
-                               }
-                               return expand_dummy_sum(en, subs_idx);
-                       } 
-                       ++it;
-               }
-               return e;
-       } else if (is_a<indexed>(e_expanded)) {
-               exvector v = ex_to<indexed>(e_expanded).get_dummy_indices();
-               exvector::const_iterator it = v.begin(), itend = v.end();
-               while (it != itend) {
-                       varidx nu = ex_to<varidx>(*it);
-                       if (nu.is_dim_numeric()) {
+       } else if (is_a<ncmul>(e_expanded) || is_a<mul>(e_expanded) || is_a<power>(e_expanded) || is_a<indexed>(e_expanded)) {
+               exvector v;
+               if (is_a<indexed>(e_expanded))
+                       v = ex_to<indexed>(e_expanded).get_dummy_indices();
+               else
+                       v = get_all_dummy_indices(e_expanded);
+               ex result = e_expanded;
+               for(exvector::const_iterator it=v.begin(); it!=v.end(); ++it) {
+                       ex nu = *it;
+                       if (ex_to<idx>(nu).get_dim().info(info_flags::nonnegint)) {
+                               int idim = ex_to<numeric>(ex_to<idx>(nu).get_dim()).to_int();
                                ex en = 0;
-                               for (int i=0; i < ex_to<numeric>(nu.get_dim()).to_int(); i++) {
-                                       if (is_a<varidx>(nu) && !subs_idx) {
-                                               en += e_expanded.subs(lst(nu == varidx(i, nu.get_dim(), true), nu.toggle_variance() == varidx(i, nu.get_dim())));
+                               for (int i=0; i < idim; i++) {
+                                       if (subs_idx && is_a<varidx>(nu)) {
+                                               ex other = ex_to<varidx>(nu).toggle_variance();
+                                               en += result.subs(lst(
+                                                       nu == idx(i, idim),
+                                                       other == idx(i, idim)
+                                               ));
                                        } else {
-                                               en += e_expanded.subs(lst(nu == idx(i, nu.get_dim()), nu.toggle_variance() == idx(i, nu.get_dim())));
+                                               en += result.subs( nu.op(0) == i );
                                        }
                                }
-                               return expand_dummy_sum(en, subs_idx);
-                       } 
-                       ++it;
+                               result = en;
+                       }
                }
-               return e;
+               return result;
        } else {
                return e;
        }