* Implementation of GiNaC's indices. */
/*
- * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
+ * GiNaC Copyright (C) 1999-2001 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 <stdexcept>
#include "idx.h"
-#include "ex.h"
+#include "symbol.h"
#include "lst.h"
-#include "relational.h"
+#include "archive.h"
#include "utils.h"
#include "debugmsg.h"
-#ifndef NO_GINAC_NAMESPACE
+#include "exprseq.h" // !!
+
namespace GiNaC {
-#endif // ndef NO_GINAC_NAMESPACE
+
+GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
+GINAC_IMPLEMENT_REGISTERED_CLASS(varidx, idx)
//////////
// default constructor, destructor, copy constructor assignment operator and helpers
//////////
-// public
-
-idx::idx() : basic(TINFO_idx), symbolic(true), covariant(false)
+idx::idx() : inherited(TINFO_idx)
{
- debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
- serial=next_serial++;
- name="index"+ToString(serial);
+ debugmsg("idx default constructor", LOGLEVEL_CONSTRUCT);
}
-idx::~idx()
+varidx::varidx() : covariant(false)
{
- debugmsg("idx destructor",LOGLEVEL_DESTRUCT);
- destroy(0);
+ debugmsg("varidx default constructor", LOGLEVEL_CONSTRUCT);
+ tinfo_key = TINFO_varidx;
}
-idx::idx(const idx & other)
+void idx::copy(const idx & other)
{
- debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT);
- copy(other);
+ inherited::copy(other);
+ value = other.value;
+ dim = other.dim;
}
-const idx & idx::operator=(const idx & other)
+void varidx::copy(const varidx & other)
{
- debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT);
- if (this != &other) {
- destroy(1);
- copy(other);
- }
- return *this;
+ inherited::copy(other);
+ covariant = other.covariant;
}
-// protected
+DEFAULT_DESTROY(idx)
+DEFAULT_DESTROY(varidx)
-void idx::copy(const idx & other)
+//////////
+// other constructors
+//////////
+
+idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d)
{
- basic::copy(other);
- serial=other.serial;
- symbolic=other.symbolic;
- name=other.name;
- value=other.value;
- covariant=other.covariant;
+ debugmsg("idx constructor from ex,ex", LOGLEVEL_CONSTRUCT);
+ if (is_dim_numeric())
+ if (!dim.info(info_flags::posint))
+ throw(std::invalid_argument("dimension of space must be a positive integer"));
}
-void idx::destroy(bool call_parent)
+varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
{
- if (call_parent) basic::destroy(call_parent);
+ debugmsg("varidx constructor from ex,ex,bool", LOGLEVEL_CONSTRUCT);
+ tinfo_key = TINFO_varidx;
}
//////////
-// other constructors
+// archiving
//////////
-// public
-
-idx::idx(bool cov) : basic(TINFO_idx), symbolic(true), covariant(cov)
+idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
{
- debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
- serial=next_serial++;
- name="index"+ToString(serial);
+ debugmsg("idx constructor from archive_node", LOGLEVEL_CONSTRUCT);
+ n.find_ex("value", value, sym_lst);
+ n.find_ex("dim", dim, sym_lst);
}
-idx::idx(const string & n, bool cov) : basic(TINFO_idx),
- symbolic(true), name(n), covariant(cov)
+varidx::varidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
{
- debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
- serial=next_serial++;
+ debugmsg("varidx constructor from archive_node", LOGLEVEL_CONSTRUCT);
+ n.find_bool("covariant", covariant);
}
-idx::idx(const char * n, bool cov) : basic(TINFO_idx),
- symbolic(true), name(n), covariant(cov)
+void idx::archive(archive_node &n) const
{
- debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
- serial=next_serial++;
+ inherited::archive(n);
+ n.add_ex("value", value);
+ n.add_ex("dim", dim);
}
-idx::idx(unsigned v, bool cov) : basic(TINFO_idx),
- symbolic(false), value(v), covariant(cov)
+void varidx::archive(archive_node &n) const
{
- debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
- serial=0;
+ inherited::archive(n);
+ n.add_bool("covariant", covariant);
}
+DEFAULT_UNARCHIVE(idx)
+DEFAULT_UNARCHIVE(varidx)
//////////
// functions overriding virtual functions from bases classes
//////////
-// public
-
-basic * idx::duplicate() const
+void idx::printraw(std::ostream & os) const
{
- debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
- return new idx(*this);
+ debugmsg("idx printraw", LOGLEVEL_PRINT);
+
+ os << class_name() << "(";
+ value.printraw(os);
+ os << ",dim=";
+ dim.printraw(os);
+ os << ",hash=" << hashvalue << ",flags=" << flags;
+ os << ")";
}
-void idx::printraw(ostream & os) const
+void idx::printtree(std::ostream & os, unsigned indent) const
{
- debugmsg("idx printraw",LOGLEVEL_PRINT);
-
- os << "idx(";
-
- if (symbolic) {
- os << "symbolic,name=" << name;
- } else {
- os << "non symbolic,value=" << value;
- }
-
- if (covariant) {
- os << ",covariant";
- } else {
- os << ",contravariant";
- }
-
- os << ",serial=" << serial;
- os << ",hash=" << hashvalue << ",flags=" << flags;
- os << ")";
+ debugmsg("idx printtree",LOGLEVEL_PRINT);
+
+ os << std::string(indent, ' ') << "type=" << class_name();
+ value.printtree(os, indent + delta_indent);
+ os << std::string(indent, ' ');
+ os << ", hash=" << hashvalue
+ << " (0x" << std::hex << hashvalue << std::dec << ")"
+ << ", flags=" << flags << std::endl;
}
-void idx::printtree(ostream & os, unsigned indent) const
+void idx::print(std::ostream & os, unsigned upper_precedence) const
{
- debugmsg("idx printtree",LOGLEVEL_PRINT);
+ debugmsg("idx print", LOGLEVEL_PRINT);
- os << string(indent,' ') << "idx: ";
+ os << ".";
- if (symbolic) {
- os << "symbolic,name=" << name;
- } else {
- os << "non symbolic,value=" << value;
- }
-
- if (covariant) {
- os << ",covariant";
- } else {
- os << ",contravariant";
- }
-
- os << ", serial=" << serial
- << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
- << ", flags=" << flags << endl;
+ bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
+ if (need_parens)
+ os << "(";
+ os << value;
+ if (need_parens)
+ os << ")";
}
-void idx::print(ostream & os, unsigned upper_precedence) const
+void varidx::print(std::ostream & os, unsigned upper_precedence) const
{
- debugmsg("idx print",LOGLEVEL_PRINT);
-
- if (covariant) {
- os << "_";
- } else {
- os << "~";
- }
- if (symbolic) {
- os << name;
- } else {
- os << value;
- }
+ debugmsg("varidx print", LOGLEVEL_PRINT);
+
+ if (covariant)
+ os << ".";
+ else
+ os << "~";
+
+ bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
+ if (need_parens)
+ os << "(";
+ os << value;
+ if (need_parens)
+ os << ")";
}
bool idx::info(unsigned inf) const
{
- if (inf==info_flags::idx) return true;
- return basic::info(inf);
+ if (inf == info_flags::idx)
+ return true;
+ return inherited::info(inf);
}
-ex idx::subs(const lst & ls, const lst & lr) const
+unsigned idx::nops() const
{
- 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 DO_GINAC_ASSERT
-
- for (unsigned i=0; i<ls.nops(); i++) {
- if (is_equal(*(ls.op(i)).bp)) {
- return lr.op(i);
- }
- }
- return *this;
+ // don't count the dimension as that is not really a sub-expression
+ return 1;
}
-// protected
+ex & idx::let_op(int i)
+{
+ GINAC_ASSERT(i == 0);
+ return value;
+}
+/** Returns order relation between two indices of the same type. The order
+ * must be such that dummy indices lie next to each other. */
int idx::compare_same_type(const basic & other) const
{
- GINAC_ASSERT(is_of_type(other,idx));
- const idx & o=static_cast<const idx &>
- (const_cast<basic &>(other));
-
- if (covariant!=o.covariant) {
- // different co/contravariant
- return covariant ? -1 : 1;
- }
- if ((!symbolic) && (!o.symbolic)) {
- // non-symbolic, of equal type: compare values
- if (value==o.value) {
- return 0;
- }
- return value<o.value ? -1 : 1;
- }
- if (symbolic && o.symbolic) {
- // both symbolic: compare serials
- if (serial==o.serial) {
- return 0;
- }
- return serial<o.serial ? -1 : 1;
- }
- // one symbolic, one value: value is sorted first
- return o.symbolic ? -1 : 1;
+ GINAC_ASSERT(is_of_type(other, idx));
+ const idx &o = static_cast<const idx &>(other);
+
+ int cmpval = value.compare(o.value);
+ if (cmpval)
+ return cmpval;
+ return dim.compare(o.dim);
}
-bool idx::is_equal_same_type(const basic & other) const
+int varidx::compare_same_type(const basic & other) const
{
- GINAC_ASSERT(is_of_type(other,idx));
- const idx & o=static_cast<const idx &>
- (const_cast<basic &>(other));
+ GINAC_ASSERT(is_of_type(other, varidx));
+ const varidx &o = static_cast<const varidx &>(other);
- if (covariant!=o.covariant) return false;
- if (symbolic!=o.symbolic) return false;
- if (symbolic && o.symbolic) return serial==o.serial;
- return value==o.value;
-}
+ int cmpval = inherited::compare_same_type(other);
+ if (cmpval)
+ return cmpval;
-unsigned idx::calchash(void) const
+ // Check variance last so dummy indices will end up next to each other
+ if (covariant != o.covariant)
+ return covariant ? -1 : 1;
+ return 0;
+}
+
+ex idx::subs(const lst & ls, const lst & lr) const
{
- hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
- setflag(status_flags::hash_calculated);
- return hashvalue;
+ GINAC_ASSERT(ls.nops() == lr.nops());
+
+ // First look for index substitutions
+ for (unsigned i=0; i<ls.nops(); i++) {
+ if (is_equal(*(ls.op(i)).bp)) {
+
+ // Substitution index->index
+ if (is_ex_of_type(lr.op(i), idx))
+ return lr.op(i);
+
+ // Otherwise substitute value
+ idx *i_copy = static_cast<idx *>(duplicate());
+ i_copy->value = lr.op(i);
+ i_copy->clearflag(status_flags::hash_calculated);
+ return i_copy->setflag(status_flags::dynallocated);
+ }
+ }
+
+ // None, substitute objects in value (not in dimension)
+ const ex &subsed_value = value.subs(ls, lr);
+ if (are_ex_trivially_equal(value, subsed_value))
+ return *this;
+
+ idx *i_copy = static_cast<idx *>(duplicate());
+ i_copy->value = subsed_value;
+ i_copy->clearflag(status_flags::hash_calculated);
+ return i_copy->setflag(status_flags::dynallocated);
}
//////////
-// new virtual functions which can be overridden by derived classes
+// new virtual functions
//////////
-// public
-
-bool idx::is_co_contra_pair(const basic & other) const
-{
- // like is_equal_same_type(), but tests for different covariant status
- GINAC_ASSERT(is_of_type(other,idx));
- const idx & o=static_cast<const idx &>
- (const_cast<basic &>(other));
-
- if (covariant==o.covariant) return false;
- if (symbolic!=o.symbolic) return false;
- if (symbolic && o.symbolic) return serial==o.serial;
- return value==o.value;
-}
-
-bool idx::is_symbolic(void) const
+bool idx::is_dummy_pair_same_type(const basic & other) const
{
- return symbolic;
-}
+ const idx &o = static_cast<const idx &>(other);
-unsigned idx::get_value(void) const
-{
- return value;
-}
+ // Only pure symbols form dummy pairs, "2n+1" doesn't
+ if (!is_ex_of_type(value, symbol))
+ return false;
-bool idx::is_covariant(void) const
-{
- return covariant;
-}
+ // Value must be equal, of course
+ if (!value.is_equal(o.value))
+ return false;
-ex idx::toggle_covariant(void) const
-{
- idx * i_copy=static_cast<idx *>(duplicate());
- i_copy->covariant = !i_copy->covariant;
- i_copy->clearflag(status_flags::hash_calculated);
- return i_copy->setflag(status_flags::dynallocated);
+ // Also the dimension
+ return dim.is_equal(o.dim);
}
-//////////
-// non-virtual functions in this class
-//////////
-
-// none
-
-//////////
-// static member variables
-//////////
+bool varidx::is_dummy_pair_same_type(const basic & other) const
+{
+ const varidx &o = static_cast<const varidx &>(other);
-// protected
+ // Variance must be opposite
+ if (covariant == o.covariant)
+ return false;
-unsigned idx::next_serial=0;
+ return inherited::is_dummy_pair_same_type(other);
+}
//////////
-// global constants
+// non-virtual functions
//////////
-const idx some_idx;
-const type_info & typeid_idx=typeid(some_idx);
+ex varidx::toggle_variance(void) const
+{
+ varidx *i_copy = static_cast<varidx *>(duplicate());
+ i_copy->covariant = !i_copy->covariant;
+ i_copy->clearflag(status_flags::hash_calculated);
+ return i_copy->setflag(status_flags::dynallocated);
+}
//////////
-// other functions
+// global functions
//////////
-int canonicalize_indices(exvector & iv, bool antisymmetric)
+bool is_dummy_pair(const idx & i1, const idx & i2)
{
- if (iv.size()<2) {
- // nothing do to for 0 or 1 indices
- return INT_MAX;
- }
-
- bool something_changed=false;
- int sig=1;
- // simple bubble sort algorithm should be sufficient for the small number of indices needed
- exvector::const_iterator last_idx=iv.end();
- exvector::const_iterator next_to_last_idx=iv.end()-1;
- for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
- for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
- int cmpval=(*it1).compare(*it2);
- if (cmpval==1) {
- iter_swap(it1,it2);
- something_changed=true;
- if (antisymmetric) sig=-sig;
- } else if ((cmpval==0) && antisymmetric) {
- something_changed=true;
- sig=0;
- }
- }
- }
- return something_changed ? sig : INT_MAX;
-}
+ // The indices must be of exactly the same type
+ if (i1.tinfo() != i2.tinfo())
+ return false;
-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) {
- 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) {
- GINAC_ASSERT(is_ex_of_type(*cit2,idx));
- if ((*cit1).is_equal(*cit2)) {
- iv_intersect.push_back(*cit1);
- break;
- }
- }
- }
- }
- return iv_intersect;
+ // Same type, let the indices decide whether they are paired
+ return i1.is_dummy_pair_same_type(i2);
}
-#define TEST_PERMUTATION(A,B,C,P) \
- if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
- if (antisymmetric) *sig=P; \
- return iv3[A]; \
- }
-
-ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2,
- bool antisymmetric, int * sig)
+bool is_dummy_pair(const ex & e1, const ex & e2)
{
- // match (return value,iv2) to iv3 by permuting indices
- // iv3 is always cyclic
-
- GINAC_ASSERT(iv3.size()==3);
- GINAC_ASSERT(iv2.size()==2);
-
- *sig=1;
-
- TEST_PERMUTATION(0,1,2, 1);
- TEST_PERMUTATION(0,2,1, -1);
- TEST_PERMUTATION(1,0,2, -1);
- TEST_PERMUTATION(1,2,0, 1);
- TEST_PERMUTATION(2,0,1, 1);
- TEST_PERMUTATION(2,1,0, -1);
- throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
+ // The expressions must be indices
+ if (!is_ex_of_type(e1, idx) || !is_ex_of_type(e2, idx))
+ return false;
+
+ return is_dummy_pair(ex_to_idx(e1), ex_to_idx(e2));
}
-
-unsigned subs_index_in_exvector(exvector & v, const ex & is, const ex & ir)
+
+/** Bring a vector of indices into a canonic order. Dummy indices will lie
+ * next to each other after the sorting. */
+static void sort_index_vector(exvector &v)
{
- exvector::iterator it;
- unsigned replacements=0;
- unsigned current_replacements;
-
- 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);
- if (current_replacements>0) {
- (*it)=(*it).subs(is==ir);
- }
- replacements += current_replacements;
- }
- return replacements;
+ // Nothing to sort if less than 2 elements
+ if (v.size() < 2)
+ return;
+
+ // Simple bubble sort algorithm should be sufficient for the small
+ // number of indices expected
+ exvector::iterator it1 = v.begin(), itend = v.end(), next_to_last_idx = itend - 1;
+ while (it1 != next_to_last_idx) {
+ exvector::iterator it2 = it1 + 1;
+ while (it2 != itend) {
+ if (it1->compare(*it2) > 0)
+ it1->swap(*it2);
+ it2++;
+ }
+ it1++;
+ }
}
-unsigned count_index(const ex & e, const ex & i)
+
+void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
{
- exvector idxv=e.get_indices();
- unsigned count=0;
- for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
- if ((*cit).is_equal(i)) count++;
- }
- return count;
+ out_free.clear();
+ out_dummy.clear();
+
+ // No indices? Then do nothing
+ if (it == itend)
+ return;
+
+ // Only one index? Then it is a free one if it's not numeric
+ if (itend - it == 1) {
+ if (ex_to_idx(*it).is_symbolic())
+ out_free.push_back(*it);
+ return;
+ }
+
+ // Sort index vector. This will cause dummy indices come to lie next
+ // to each other (because the sort order is defined to guarantee this).
+ exvector v(it, itend);
+ sort_index_vector(v);
+
+ // Find dummy pairs and free indices
+ it = v.begin(); itend = v.end();
+ exvector::const_iterator last = it++;
+ while (it != itend) {
+ if (is_dummy_pair(*it, *last)) {
+ out_dummy.push_back(*last);
+ it++;
+ if (it == itend)
+ return;
+ } else {
+ if (!it->is_equal(*last) && ex_to_idx(*last).is_symbolic())
+ out_free.push_back(*last);
+ }
+ last = it++;
+ }
+ if (ex_to_idx(*last).is_symbolic())
+ out_free.push_back(*last);
}
-ex subs_indices(const ex & e, const exvector & idxv_subs,
- const exvector & idxv_repl)
+exvector index_set_difference(const exvector & set1, const exvector & set2)
{
- 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;
+ exvector ret;
+
+ exvector::const_iterator ait = set1.begin(), aitend = set1.end();
+ while (ait != aitend) {
+ exvector::const_iterator bit = set2.begin(), bitend = set2.end();
+ bool found = false;
+ while (bit != bitend) {
+ if (ait->is_equal(*bit)) {
+ found = true;
+ break;
+ }
+ bit++;
+ }
+ if (!found)
+ ret.push_back(*ait);
+ ait++;
+ }
+
+ return ret;
}
-#ifndef NO_GINAC_NAMESPACE
} // namespace GiNaC
-#endif // ndef NO_GINAC_NAMESPACE