/** @file idx.cpp
*
- * Implementation of GiNaC's indices.
- *
- * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
+ * Implementation of GiNaC's indices. */
+
+/*
+ * 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
// 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++;
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) {
// 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;
void idx::destroy(bool call_parent)
{
- if (call_parent) basic::destroy(call_parent);
+ if (call_parent) inherited::destroy(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
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);
}
// 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) {
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;
// 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;
//////////
const idx some_idx;
-type_info const & typeid_idx=typeid(some_idx);
+const type_info & typeid_idx=typeid(some_idx);
//////////
// other functions
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;
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;
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);
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;
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]);
return res;
}
-
-
-
+#ifndef NO_NAMESPACE_GINAC
+} // namespace GiNaC
+#endif // ndef NO_NAMESPACE_GINAC