]> www.ginac.de Git - ginac.git/blobdiff - ginac/idx.cpp
bug fixed in an incomplete assertion in power.cpp
[ginac.git] / ginac / idx.cpp
index 8de753c1a94a087718644f254a1d7e1f646504b4..e0b5ad05dc4d1b5e5eaf2d6e2a2aa23d9e65f3e3 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's indices. */
 
 /*
- *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2000 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
 #include "ex.h"
 #include "lst.h"
 #include "relational.h"
+#include "archive.h"
 #include "utils.h"
+#include "debugmsg.h"
+
+#ifndef NO_NAMESPACE_GINAC
+namespace GiNaC {
+#endif // ndef NO_NAMESPACE_GINAC
+
+GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
 
 //////////
 // default constructor, destructor, copy constructor assignment operator and helpers
@@ -34,7 +42,7 @@
 
 // public
 
-idx::idx() : basic(TINFO_idx), symbolic(true), covariant(false)
+idx::idx() : inherited(TINFO_idx), symbolic(true), covariant(false)
 {
     debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
     serial=next_serial++;
@@ -47,13 +55,13 @@ idx::~idx()
     destroy(0);
 }
 
-idx::idx(idx const & other)
+idx::idx(const idx & other)
 {
     debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT);
     copy(other);
 }
 
-idx const & idx::operator=(idx const & other)
+const idx & idx::operator=(const idx & other)
 {
     debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT);
     if (this != &other) {
@@ -65,9 +73,9 @@ idx const & idx::operator=(idx const & other)
 
 // protected
 
-void idx::copy(idx const & other)
+void idx::copy(const idx & other)
 {
-    basic::copy(other);
+    inherited::copy(other);
     serial=other.serial;
     symbolic=other.symbolic;
     name=other.name;
@@ -77,7 +85,7 @@ void idx::copy(idx const & other)
 
 void idx::destroy(bool call_parent)
 {
-    if (call_parent) basic::destroy(call_parent);
+    if (call_parent) inherited::destroy(call_parent);
 }
 
 //////////
@@ -86,34 +94,80 @@ void idx::destroy(bool call_parent)
 
 // public
 
-idx::idx(bool cov) : basic(TINFO_idx), symbolic(true), covariant(cov)
+idx::idx(bool cov) : inherited(TINFO_idx), symbolic(true), covariant(cov)
 {
     debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
     serial=next_serial++;
     name="index"+ToString(serial);
 }
 
-idx::idx(string const & n, bool cov) : basic(TINFO_idx),  
+idx::idx(const string & n, bool cov) : inherited(TINFO_idx),  
     symbolic(true), name(n), covariant(cov)
 {
     debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
     serial=next_serial++;
 }
 
-idx::idx(char const * n, bool cov) : basic(TINFO_idx),  
+idx::idx(const char * n, bool cov) : inherited(TINFO_idx),  
     symbolic(true), name(n), covariant(cov)
 {
     debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
     serial=next_serial++;
 }
 
-idx::idx(unsigned const v, bool cov) : basic(TINFO_idx),
+idx::idx(unsigned v, bool cov) : inherited(TINFO_idx),
     symbolic(false), value(v), covariant(cov)
 {
     debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
     serial=0;
 }
 
+//////////
+// archiving
+//////////
+
+/** Construct object from archive_node. */
+idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
+{
+    debugmsg("idx constructor from archive_node", LOGLEVEL_CONSTRUCT);
+    n.find_bool("symbolic", symbolic);
+    n.find_bool("covariant", covariant);
+    if (symbolic) {
+        serial = next_serial++;
+        if (!(n.find_string("name", name)))
+            name = "index" + ToString(serial);
+    } else {
+        serial = 0;
+        n.find_unsigned("value", value);
+    }
+}
+
+/** Unarchive the object. */
+ex idx::unarchive(const archive_node &n, const lst &sym_lst)
+{
+    ex s = (new idx(n, sym_lst))->setflag(status_flags::dynallocated);
+
+    if (ex_to_idx(s).symbolic) {
+        // If idx is in sym_lst, return the existing idx
+        for (unsigned i=0; i<sym_lst.nops(); i++) {
+            if (is_ex_of_type(sym_lst.op(i), idx) && (ex_to_idx(sym_lst.op(i)).name == ex_to_idx(s).name))
+                return sym_lst.op(i);
+        }
+    }
+    return s;
+}
+
+/** Archive the object. */
+void idx::archive(archive_node &n) const
+{
+    inherited::archive(n);
+    n.add_bool("symbolic", symbolic);
+    n.add_bool("covariant", covariant);
+    if (symbolic)
+        n.add_string("name", name);
+    else
+        n.add_unsigned("value", value);
+}
 
 //////////
 // functions overriding virtual functions from bases classes
@@ -192,20 +246,20 @@ void idx::print(ostream & os, unsigned upper_precedence) const
 bool idx::info(unsigned inf) const
 {
     if (inf==info_flags::idx) return true;
-    return basic::info(inf);
+    return inherited::info(inf);
 }
 
-ex idx::subs(lst const & ls, lst const & lr) const
+ex idx::subs(const lst & ls, const lst & lr) const
 {
-    ASSERT(ls.nops()==lr.nops());
-#ifdef DOASSERT
-    for (int i=0; i<ls.nops(); i++) {
-        ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
+    GINAC_ASSERT(ls.nops()==lr.nops());
+#ifdef DO_GINAC_ASSERT
+    for (unsigned i=0; i<ls.nops(); i++) {
+        GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
                is_ex_of_type(ls.op(i),idx));
     }
-#endif // def DOASSERT
+#endif // def DO_GINAC_ASSERT
 
-    for (int i=0; i<ls.nops(); i++) {
+    for (unsigned i=0; i<ls.nops(); i++) {
         if (is_equal(*(ls.op(i)).bp)) {
             return lr.op(i);
         }
@@ -215,10 +269,10 @@ ex idx::subs(lst const & ls, lst const & lr) const
 
 // protected
 
-int idx::compare_same_type(basic const & other) const
+int idx::compare_same_type(const basic & other) const
 {
-    ASSERT(is_of_type(other,idx));
-    idx const & o=static_cast<idx const &>
+    GINAC_ASSERT(is_of_type(other,idx));
+    const idx & o=static_cast<const idx &>
                              (const_cast<basic &>(other));
 
     if (covariant!=o.covariant) {
@@ -243,10 +297,10 @@ int idx::compare_same_type(basic const & other) const
     return o.symbolic ? -1 : 1;
 }
 
-bool idx::is_equal_same_type(basic const & other) const
+bool idx::is_equal_same_type(const basic & other) const
 {
-    ASSERT(is_of_type(other,idx));
-    idx const & o=static_cast<idx const &>
+    GINAC_ASSERT(is_of_type(other,idx));
+    const idx & o=static_cast<const idx &>
                              (const_cast<basic &>(other));
 
     if (covariant!=o.covariant) return false;
@@ -268,11 +322,11 @@ unsigned idx::calchash(void) const
 
 // public
 
-bool idx::is_co_contra_pair(basic const & other) const
+bool idx::is_co_contra_pair(const basic & other) const
 {
     // like is_equal_same_type(), but tests for different covariant status
-    ASSERT(is_of_type(other,idx));
-    idx const & o=static_cast<idx const &>
+    GINAC_ASSERT(is_of_type(other,idx));
+    const idx & o=static_cast<const idx &>
                              (const_cast<basic &>(other));
 
     if (covariant==o.covariant) return false;
@@ -323,7 +377,7 @@ unsigned idx::next_serial=0;
 //////////
 
 const idx some_idx;
-type_info const & typeid_idx=typeid(some_idx);
+const type_info & typeid_idx=typeid(some_idx);
 
 //////////
 // other functions
@@ -357,16 +411,16 @@ int canonicalize_indices(exvector & iv, bool antisymmetric)
     return something_changed ? sig : INT_MAX;
 }
 
-exvector idx_intersect(exvector const & iv1, exvector const & iv2)
+exvector idx_intersect(const exvector & iv1, const exvector & iv2)
 {
     // build a vector of symbolic indices contained in iv1 and iv2 simultaneously
     // assumes (but does not test) that each index occurs at most twice
     exvector iv_intersect;
     for (exvector::const_iterator cit1=iv1.begin(); cit1!=iv1.end(); ++cit1) {
-        ASSERT(is_ex_of_type(*cit1,idx));
+        GINAC_ASSERT(is_ex_of_type(*cit1,idx));
         if (ex_to_idx(*cit1).is_symbolic()) {
             for (exvector::const_iterator cit2=iv2.begin(); cit2!=iv2.end(); ++cit2) {
-                ASSERT(is_ex_of_type(*cit2,idx));
+                GINAC_ASSERT(is_ex_of_type(*cit2,idx));
                 if ((*cit1).is_equal(*cit2)) {
                     iv_intersect.push_back(*cit1);
                     break;
@@ -383,14 +437,14 @@ exvector idx_intersect(exvector const & iv1, exvector const & iv2)
         return iv3[A]; \
     }
 
-ex permute_free_index_to_front(exvector const & iv3, exvector const & iv2,
+ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2,
                                bool antisymmetric, int * sig)
 {
     // match (return value,iv2) to iv3 by permuting indices
     // iv3 is always cyclic
 
-    ASSERT(iv3.size()==3);
-    ASSERT(iv2.size()==2);
+    GINAC_ASSERT(iv3.size()==3);
+    GINAC_ASSERT(iv2.size()==2);
 
     *sig=1;
     
@@ -403,14 +457,14 @@ ex permute_free_index_to_front(exvector const & iv3, exvector const & iv2,
     throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
 }
     
-unsigned subs_index_in_exvector(exvector & v, ex const & is, ex const & ir)
+unsigned subs_index_in_exvector(exvector & v, const ex & is, const ex & ir)
 {
     exvector::iterator it;
     unsigned replacements=0;
     unsigned current_replacements;
 
-    ASSERT(is_ex_of_type(is,idx));
-    ASSERT(is_ex_of_type(ir,idx));
+    GINAC_ASSERT(is_ex_of_type(is,idx));
+    GINAC_ASSERT(is_ex_of_type(ir,idx));
    
     for (it=v.begin(); it!=v.end(); ++it) {
         current_replacements=count_index(*it,is);
@@ -422,7 +476,7 @@ unsigned subs_index_in_exvector(exvector & v, ex const & is, ex const & ir)
     return replacements;
 }
 
-unsigned count_index(ex const & e, ex const & i)
+unsigned count_index(const ex & e, const ex & i)
 {
     exvector idxv=e.get_indices();
     unsigned count=0;
@@ -432,10 +486,10 @@ unsigned count_index(ex const & e, ex const & i)
     return count;
 }
 
-ex subs_indices(ex const & e, exvector const & idxv_subs,
-                exvector const & idxv_repl)
+ex subs_indices(const ex & e, const exvector & idxv_subs,
+                const exvector & idxv_repl)
 {
-    ASSERT(idxv_subs.size()==idxv_repl.size());
+    GINAC_ASSERT(idxv_subs.size()==idxv_repl.size());
     ex res=e;
     for (unsigned i=0; i<idxv_subs.size(); ++i) {
         res=res.subs(idxv_subs[i]==idxv_repl[i]);
@@ -443,6 +497,6 @@ ex subs_indices(ex const & e, exvector const & idxv_subs,
     return res;
 }
 
-
-
-
+#ifndef NO_NAMESPACE_GINAC
+} // namespace GiNaC
+#endif // ndef NO_NAMESPACE_GINAC