]> www.ginac.de Git - ginac.git/blobdiff - ginac/symmetry.cpp
Added calchash and compare_same_type for symmetries. Necessary for archiving.
[ginac.git] / ginac / symmetry.cpp
index 84f6e6cf058016734a4ce4c5c30301f97fc7725a..9cee0ffb37b24ec718cabf91c3897307ff3c9fab 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's symmetry definitions. */
 
 /*
- *  GiNaC Copyright (C) 1999-2004 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
@@ -17,7 +17,7 @@
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
 #include <iostream>
@@ -55,25 +55,25 @@ GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symmetry, basic,
 // default constructor
 //////////
 
-symmetry::symmetry() : type(none)
+symmetry::symmetry() : inherited(&symmetry::tinfo_static), type(none)
 {
-       tinfo_key = TINFO_symmetry;
+       setflag(status_flags::evaluated | status_flags::expanded);
 }
 
 //////////
 // other constructors
 //////////
 
-symmetry::symmetry(unsigned i) : type(none)
+symmetry::symmetry(unsigned i) : inherited(&symmetry::tinfo_static), type(none)
 {
        indices.insert(i);
-       tinfo_key = TINFO_symmetry;
+       setflag(status_flags::evaluated | status_flags::expanded);
 }
 
-symmetry::symmetry(symmetry_type t, const symmetry &c1, const symmetry &c2) : type(t)
+symmetry::symmetry(symmetry_type t, const symmetry &c1, const symmetry &c2) : inherited(&symmetry::tinfo_static), type(t)
 {
        add(c1); add(c2);
-       tinfo_key = TINFO_symmetry;
+       setflag(status_flags::evaluated | status_flags::expanded);
 }
 
 //////////
@@ -142,11 +142,69 @@ int symmetry::compare_same_type(const basic & other) const
 {
        GINAC_ASSERT(is_a<symmetry>(other));
 
-       // All symmetry trees are equal. They are not supposed to appear in
-       // ordinary expressions anyway...
+       // For archiving purposes we need to have an ordering of symmetries.
+       const symmetry &othersymm = ex_to<symmetry>(other);
+
+       // Compare type.
+       if (type > othersymm.type)
+               return 1;
+       if (type < othersymm.type)
+               return -1;
+
+       // Compare the index set.
+       size_t this_size = indices.size();
+       size_t that_size = othersymm.indices.size();
+       if (this_size > that_size)
+               return 1;
+       if (this_size < that_size)
+               return -1;
+       typedef std::set<unsigned>::iterator set_it;
+       set_it end = indices.end();
+       for (set_it i=indices.begin(),j=othersymm.indices.begin(); i!=end; ++i,++j) {
+               if(*i < *j)
+                       return 1;
+               if(*i > *j)
+                       return -1;
+       }
+
+       // Compare the children.
+       if (children.size() > othersymm.children.size())
+               return 1;
+       if (children.size() < othersymm.children.size())
+               return -1;
+       for (size_t i=0; i<children.size(); ++i) {
+               int cmpval = ex_to<symmetry>(children[i])
+                       .compare_same_type(ex_to<symmetry>(othersymm.children[i]));
+               if (cmpval)
+                       return cmpval;
+       }
+
        return 0;
 }
 
+unsigned symmetry::calchash() const
+{
+       unsigned v = golden_ratio_hash((p_int)tinfo());
+
+       if (type == none) {
+               v = rotate_left(v);
+               v ^= *(indices.begin());
+       } else {
+               for (exvector::const_iterator i=children.begin(); i!=children.end(); ++i)
+               {
+                       v = rotate_left(v);
+                       v ^= i->gethash();
+               }
+       }
+
+       if (flags & status_flags::evaluated) {
+               setflag(status_flags::hash_calculated);
+               hashvalue = v;
+       }
+
+       return v;
+}
+
 void symmetry::do_print(const print_context & c, unsigned level) const
 {
        if (children.empty()) {
@@ -427,7 +485,7 @@ static ex symm(const ex & e, exvector::const_iterator first, exvector::const_ite
                lst new_lst;
                for (unsigned i=0; i<num; i++)
                        new_lst.append(orig_lst.op(iv[i]));
-               ex term = e.subs(orig_lst, new_lst, subs_options::no_pattern);
+               ex term = e.subs(orig_lst, new_lst, subs_options::no_pattern|subs_options::no_index_renaming);
                if (asymmetric) {
                        memcpy(iv2, iv, num * sizeof(unsigned));
                        term *= permutation_sign(iv2, iv2 + num);
@@ -468,7 +526,7 @@ ex symmetrize_cyclic(const ex & e, exvector::const_iterator first, exvector::con
        for (unsigned i=0; i<num-1; i++) {
                ex perm = new_lst.op(0);
                new_lst.remove_first().append(perm);
-               sum += e.subs(orig_lst, new_lst, subs_options::no_pattern);
+               sum += e.subs(orig_lst, new_lst, subs_options::no_pattern|subs_options::no_index_renaming);
        }
        return sum / num;
 }