X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fidx.cpp;h=a3767fe8c5c65e7dc357e712cf79b34756737347;hp=26e91ff87f7444bfb40d6d3300389d6fc8214034;hb=759fd0ecbd27c082d89d69a5f77a71eef046f96a;hpb=9eab44408b9213d8909b7a9e525f404ad06064dd diff --git a/ginac/idx.cpp b/ginac/idx.cpp index 26e91ff8..a3767fe8 100644 --- a/ginac/idx.cpp +++ b/ginac/idx.cpp @@ -3,7 +3,7 @@ * Implementation of GiNaC's indices. */ /* - * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2002 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 @@ -20,430 +20,505 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include #include #include "idx.h" -#include "ex.h" +#include "symbol.h" #include "lst.h" -#include "relational.h" +#include "print.h" +#include "archive.h" #include "utils.h" -#include "debugmsg.h" namespace GiNaC { +GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic) +GINAC_IMPLEMENT_REGISTERED_CLASS(varidx, idx) +GINAC_IMPLEMENT_REGISTERED_CLASS(spinidx, varidx) + ////////// -// default constructor, destructor, copy constructor assignment operator and helpers +// default ctor, dtor, copy ctor, assignment operator and helpers ////////// -// public +idx::idx() : inherited(TINFO_idx) {} -idx::idx() : basic(TINFO_idx), symbolic(true), covariant(false) +varidx::varidx() : covariant(false) { - debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT); - serial=next_serial++; - name="index"+ToString(serial); + tinfo_key = TINFO_varidx; } -idx::~idx() +spinidx::spinidx() : dotted(false) { - debugmsg("idx destructor",LOGLEVEL_DESTRUCT); - destroy(0); + tinfo_key = TINFO_spinidx; } -idx::idx(idx const & other) +void idx::copy(const idx & other) { - debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT); - copy(other); + inherited::copy(other); + value = other.value; + dim = other.dim; } -idx const & idx::operator=(idx const & 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 +void spinidx::copy(const spinidx & other) +{ + inherited::copy(other); + dotted = other.dotted; +} -void idx::copy(idx const & other) +DEFAULT_DESTROY(idx) +DEFAULT_DESTROY(varidx) +DEFAULT_DESTROY(spinidx) + +////////// +// 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; + 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); + tinfo_key = TINFO_varidx; +} + +spinidx::spinidx(const ex & v, const ex & d, bool cov, bool dot) : inherited(v, d, cov), dotted(dot) +{ + tinfo_key = TINFO_spinidx; } ////////// -// other constructors +// archiving ////////// -// public +idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) +{ + n.find_ex("value", value, sym_lst); + n.find_ex("dim", dim, sym_lst); +} + +varidx::varidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) +{ + n.find_bool("covariant", covariant); +} -idx::idx(bool cov) : basic(TINFO_idx), symbolic(true), covariant(cov) +spinidx::spinidx(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); + n.find_bool("dotted", dotted); } -idx::idx(string const & n, bool cov) : basic(TINFO_idx), - symbolic(true), name(n), covariant(cov) +void idx::archive(archive_node &n) const { - debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT); - serial=next_serial++; + inherited::archive(n); + n.add_ex("value", value); + n.add_ex("dim", dim); } -idx::idx(char const * n, bool cov) : basic(TINFO_idx), - symbolic(true), name(n), covariant(cov) +void varidx::archive(archive_node &n) const { - debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT); - serial=next_serial++; + inherited::archive(n); + n.add_bool("covariant", covariant); } -idx::idx(unsigned const v, bool cov) : basic(TINFO_idx), - symbolic(false), value(v), covariant(cov) +void spinidx::archive(archive_node &n) const { - debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT); - serial=0; + inherited::archive(n); + n.add_bool("dotted", dotted); } +DEFAULT_UNARCHIVE(idx) +DEFAULT_UNARCHIVE(varidx) +DEFAULT_UNARCHIVE(spinidx) ////////// -// functions overriding virtual functions from bases classes +// functions overriding virtual functions from base classes ////////// -// public - -basic * idx::duplicate() const +void idx::print(const print_context & c, unsigned level) const { - debugmsg("idx duplicate",LOGLEVEL_DUPLICATE); - return new idx(*this); + if (is_of_type(c, print_tree)) { + + c.s << std::string(level, ' ') << class_name() + << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << std::endl; + unsigned delta_indent = static_cast(c).delta_indent; + value.print(c, level + delta_indent); + dim.print(c, level + delta_indent); + + } else { + + if (is_a(c)) + c.s << "{"; + else + c.s << "."; + bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol)); + if (need_parens) + c.s << "("; + value.print(c); + if (need_parens) + c.s << ")"; + if (is_a(c)) + c.s << "}"; + } } -void idx::printraw(ostream & os) const +void varidx::print(const print_context & c, unsigned level) const { - debugmsg("idx printraw",LOGLEVEL_PRINT); - - os << "idx("; + if (is_of_type(c, print_tree)) { + + c.s << std::string(level, ' ') << class_name() + << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << (covariant ? ", covariant" : ", contravariant") + << std::endl; + unsigned delta_indent = static_cast(c).delta_indent; + value.print(c, level + delta_indent); + dim.print(c, level + delta_indent); + + } else { + if (is_a(c)) + c.s << "{"; + else { + if (covariant) + c.s << "."; + else + c.s << "~"; + } + bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol)); + if (need_parens) + c.s << "("; + value.print(c); + if (need_parens) + c.s << ")"; + if (is_a(c)) + c.s << "}"; + } +} - if (symbolic) { - os << "symbolic,name=" << name; - } else { - os << "non symbolic,value=" << value; - } +void spinidx::print(const print_context & c, unsigned level) const +{ + if (is_of_type(c, print_tree)) { + + c.s << std::string(level, ' ') << class_name() + << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << (covariant ? ", covariant" : ", contravariant") + << (dotted ? ", dotted" : ", undotted") + << std::endl; + unsigned delta_indent = static_cast(c).delta_indent; + value.print(c, level + delta_indent); + dim.print(c, level + delta_indent); + + } else { + + bool is_tex = is_of_type(c, print_latex); + if (is_tex) { + if (covariant) + c.s << "_{"; + else + c.s << "^{"; + } else { + if (covariant) + c.s << "."; + else + c.s << "~"; + } + if (dotted) { + if (is_tex) + c.s << "\\dot{"; + else + c.s << "*"; + } + bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol)); + if (need_parens) + c.s << "("; + value.print(c); + if (need_parens) + c.s << ")"; + if (is_tex && dotted) + c.s << "}"; + if (is_tex) + c.s << "}"; + } +} - if (covariant) { - os << ",covariant"; - } else { - os << ",contravariant"; - } +bool idx::info(unsigned inf) const +{ + if (inf == info_flags::idx) + return true; + return inherited::info(inf); +} - os << ",serial=" << serial; - os << ",hash=" << hashvalue << ",flags=" << flags; - os << ")"; +unsigned idx::nops() const +{ + // don't count the dimension as that is not really a sub-expression + return 1; } -void idx::printtree(ostream & os, unsigned indent) const +ex & idx::let_op(int i) { - debugmsg("idx printtree",LOGLEVEL_PRINT); + GINAC_ASSERT(i == 0); + return value; +} - os << string(indent,' ') << "idx: "; +/** 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_a(other)); + const idx &o = static_cast(other); - if (symbolic) { - os << "symbolic,name=" << name; - } else { - os << "non symbolic,value=" << value; - } + int cmpval = value.compare(o.value); + if (cmpval) + return cmpval; + return dim.compare(o.dim); +} - if (covariant) { - os << ",covariant"; - } else { - os << ",contravariant"; - } +bool idx::match_same_type(const basic & other) const +{ + GINAC_ASSERT(is_a(other)); + const idx &o = static_cast(other); - os << ", serial=" << serial - << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")" - << ", flags=" << flags << endl; + return dim.is_equal(o.dim); } -void idx::print(ostream & os, unsigned upper_precedence) const +int varidx::compare_same_type(const basic & other) const { - debugmsg("idx print",LOGLEVEL_PRINT); + GINAC_ASSERT(is_a(other)); + const varidx &o = static_cast(other); - if (covariant) { - os << "_"; - } else { - os << "~"; - } - if (symbolic) { - os << name; - } else { - os << value; - } + int cmpval = inherited::compare_same_type(other); + if (cmpval) + return cmpval; + + // Check variance last so dummy indices will end up next to each other + if (covariant != o.covariant) + return covariant ? -1 : 1; + return 0; } -bool idx::info(unsigned inf) const +bool varidx::match_same_type(const basic & other) const { - if (inf==info_flags::idx) return true; - return basic::info(inf); + GINAC_ASSERT(is_a(other)); + const varidx &o = static_cast(other); + + if (covariant != o.covariant) + return false; + return inherited::match_same_type(other); } -ex idx::subs(lst const & ls, lst const & lr) const +int spinidx::compare_same_type(const basic & other) const { - ASSERT(ls.nops()==lr.nops()); -#ifdef DOASSERT - for (int i=0; i(other)); + const spinidx &o = static_cast(other); - for (int i=0; i - (const_cast(other)); + GINAC_ASSERT(is_a(other)); + const spinidx &o = static_cast(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 - (const_cast(other)); + return *this; +} - if (covariant!=o.covariant) return false; - if (symbolic!=o.symbolic) return false; - if (symbolic && o.symbolic) return serial==o.serial; - return value==o.value; -} +ex idx::subs(const lst & ls, const lst & lr, bool no_pattern) const +{ + GINAC_ASSERT(ls.nops() == lr.nops()); + + // First look for index substitutions + for (unsigned i=0; i(ls.op(i)))) { + + // Substitution index->index + if (is_ex_of_type(lr.op(i), idx)) + return lr.op(i); + + // Otherwise substitute value + idx *i_copy = static_cast(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, no_pattern); + if (are_ex_trivially_equal(value, subsed_value)) + return *this; + + idx *i_copy = static_cast(duplicate()); + i_copy->value = subsed_value; + i_copy->clearflag(status_flags::hash_calculated); + return i_copy->setflag(status_flags::dynallocated); +} -unsigned idx::calchash(void) const +/** Implementation of ex::diff() for an index always returns 0. + * + * @see ex::diff */ +ex idx::derivative(const symbol & s) const { - hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial)); - setflag(status_flags::hash_calculated); - return hashvalue; + return _ex0; } ////////// -// new virtual functions which can be overridden by derived classes +// new virtual functions ////////// -// public - -bool idx::is_co_contra_pair(basic const & other) const +bool idx::is_dummy_pair_same_type(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 - (const_cast(other)); + const idx &o = static_cast(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; -} + // Only pure symbols form dummy pairs, "2n+1" doesn't + if (!is_ex_of_type(value, symbol)) + return false; -bool idx::is_symbolic(void) const -{ - return symbolic; -} + // Value must be equal, of course + if (!value.is_equal(o.value)) + return false; -unsigned idx::get_value(void) const -{ - return value; + // Also the dimension + return dim.is_equal(o.dim); } -bool idx::is_covariant(void) const +bool varidx::is_dummy_pair_same_type(const basic & other) const { - return covariant; + const varidx &o = static_cast(other); + + // Variance must be opposite + if (covariant == o.covariant) + return false; + + return inherited::is_dummy_pair_same_type(other); } -ex idx::toggle_covariant(void) const +bool spinidx::is_dummy_pair_same_type(const basic & other) const { - idx * i_copy=static_cast(duplicate()); - i_copy->covariant = !i_copy->covariant; - i_copy->clearflag(status_flags::hash_calculated); - return i_copy->setflag(status_flags::dynallocated); -} + const spinidx &o = static_cast(other); -////////// -// non-virtual functions in this class -////////// + // Dottedness must be the same + if (dotted != o.dotted) + return false; + + return inherited::is_dummy_pair_same_type(other); +} -// none ////////// -// static member variables +// non-virtual functions ////////// -// protected +ex varidx::toggle_variance(void) const +{ + varidx *i_copy = static_cast(duplicate()); + i_copy->covariant = !i_copy->covariant; + i_copy->clearflag(status_flags::hash_calculated); + return i_copy->setflag(status_flags::dynallocated); +} + +ex spinidx::toggle_dot(void) const +{ + spinidx *i_copy = static_cast(duplicate()); + i_copy->dotted = !i_copy->dotted; + i_copy->clearflag(status_flags::hash_calculated); + return i_copy->setflag(status_flags::dynallocated); +} -unsigned idx::next_serial=0; +ex spinidx::toggle_variance_dot(void) const +{ + spinidx *i_copy = static_cast(duplicate()); + i_copy->covariant = !i_copy->covariant; + i_copy->dotted = !i_copy->dotted; + i_copy->clearflag(status_flags::hash_calculated); + return i_copy->setflag(status_flags::dynallocated); +} ////////// -// global constants +// global functions ////////// -const idx some_idx; -type_info const & typeid_idx=typeid(some_idx); +bool is_dummy_pair(const idx & i1, const idx & i2) +{ + // The indices must be of exactly the same type + if (i1.tinfo() != i2.tinfo()) + return false; + + // Same type, let the indices decide whether they are paired + return i1.is_dummy_pair_same_type(i2); +} -////////// -// other functions -////////// +bool is_dummy_pair(const ex & e1, const ex & e2) +{ + // 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(e1), ex_to(e2)); +} -int canonicalize_indices(exvector & iv, bool antisymmetric) -{ - 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; -} - -exvector idx_intersect(exvector const & iv1, exvector const & 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)); - 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)); - if ((*cit1).is_equal(*cit2)) { - iv_intersect.push_back(*cit1); - break; - } - } - } - } - return iv_intersect; -} - -#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(exvector const & iv3, exvector const & 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); - - *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")); -} - -unsigned subs_index_in_exvector(exvector & v, ex const & is, ex const & ir) -{ - exvector::iterator it; - unsigned replacements=0; - unsigned current_replacements; - - ASSERT(is_ex_of_type(is,idx)); - 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; -} - -unsigned count_index(ex const & e, ex const & i) -{ - 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; -} - -ex subs_indices(ex const & e, exvector const & idxv_subs, - exvector const & idxv_repl) -{ - ASSERT(idxv_subs.size()==idxv_repl.size()); - ex res=e; - for (unsigned i=0; i(*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); + shaker_sort(v.begin(), v.end(), ex_is_less(), ex_swap()); + + // 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(*last).is_symbolic()) + out_free.push_back(*last); + } + last = it++; + } + if (ex_to(*last).is_symbolic()) + out_free.push_back(*last); } } // namespace GiNaC