X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fidx.cpp;h=a3f0b843f153fefc8da3be335e5510e9f2d23c1b;hp=caaa79e73702a8ebd963c02c91060d4a08b1f207;hb=073bf40a73e419a3dbcb6dfa190947ce2cc3bdce;hpb=15d4b353c85f8815a95d97ab977c9ca48e155574 diff --git a/ginac/idx.cpp b/ginac/idx.cpp index caaa79e7..a3f0b843 100644 --- a/ginac/idx.cpp +++ b/ginac/idx.cpp @@ -3,7 +3,7 @@ * Implementation of GiNaC's indices. */ /* - * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2011 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,437 +17,559 @@ * * 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 - #include "idx.h" -#include "ex.h" +#include "symbol.h" #include "lst.h" #include "relational.h" +#include "operators.h" +#include "archive.h" #include "utils.h" -#include "debugmsg.h" +#include "hash_seed.h" + +#include +#include +#include -#ifndef NO_GINAC_NAMESPACE namespace GiNaC { -#endif // ndef NO_GINAC_NAMESPACE + +GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(idx, basic, + print_func(&idx::do_print). + print_func(&idx::do_print_latex). + print_func(&idx::do_print_csrc). + print_func(&idx::do_print_tree)) + +GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(varidx, idx, + print_func(&varidx::do_print). + print_func(&varidx::do_print_latex). + print_func(&varidx::do_print_tree)) + +GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(spinidx, varidx, + print_func(&spinidx::do_print). + print_func(&spinidx::do_print_latex). + print_func(&spinidx::do_print_tree)) ////////// -// default constructor, destructor, copy constructor assignment operator and helpers +// default constructor ////////// -// public +idx::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); } -idx::~idx() +spinidx::spinidx() : dotted(false) { - debugmsg("idx destructor",LOGLEVEL_DESTRUCT); - destroy(0); } -idx::idx(idx const & other) -{ - debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT); - copy(other); -} +////////// +// other constructors +////////// -idx const & idx::operator=(idx const & other) +idx::idx(const ex & v, const ex & d) : value(v), dim(d) { - debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT); - if (this != &other) { - destroy(1); - copy(other); - } - return *this; + if (is_dim_numeric()) + if (!dim.info(info_flags::posint)) + throw(std::invalid_argument("dimension of space must be a positive integer")); } -// protected - -void idx::copy(idx const & other) +varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov) { - basic::copy(other); - serial=other.serial; - symbolic=other.symbolic; - name=other.name; - value=other.value; - covariant=other.covariant; } -void idx::destroy(bool call_parent) +spinidx::spinidx(const ex & v, const ex & d, bool cov, bool dot) : inherited(v, d, cov), dotted(dot) { - if (call_parent) basic::destroy(call_parent); } ////////// -// other constructors +// archiving ////////// -// public +void idx::read_archive(const archive_node& n, lst& sym_lst) +{ + inherited::read_archive(n, sym_lst); + n.find_ex("value", value, sym_lst); + n.find_ex("dim", dim, sym_lst); +} +GINAC_BIND_UNARCHIVER(idx); -idx::idx(bool cov) : basic(TINFO_idx), symbolic(true), covariant(cov) +void varidx::read_archive(const archive_node& n, lst& sym_lst) { - debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT); - serial=next_serial++; - name="index"+ToString(serial); + inherited::read_archive(n, sym_lst); + n.find_bool("covariant", covariant); } +GINAC_BIND_UNARCHIVER(varidx); -idx::idx(string const & n, bool cov) : basic(TINFO_idx), - symbolic(true), name(n), covariant(cov) +void spinidx::read_archive(const archive_node& n, lst& sym_lst) { - debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT); - serial=next_serial++; + inherited::read_archive(n, sym_lst); + n.find_bool("dotted", dotted); } +GINAC_BIND_UNARCHIVER(spinidx); -idx::idx(char const * 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 const 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); } +void spinidx::archive(archive_node &n) const +{ + inherited::archive(n); + n.add_bool("dotted", dotted); +} ////////// -// functions overriding virtual functions from bases classes +// functions overriding virtual functions from base classes ////////// -// public - -basic * idx::duplicate() const +void idx::print_index(const print_context & c, unsigned level) const { - debugmsg("idx duplicate",LOGLEVEL_DUPLICATE); - return new idx(*this); + bool need_parens = !(is_exactly_a(value) || is_a(value)); + if (need_parens) + c.s << "("; + value.print(c); + if (need_parens) + c.s << ")"; + if (c.options & print_options::print_index_dimensions) { + c.s << "["; + dim.print(c); + c.s << "]"; + } } -void idx::printraw(ostream & os) const +void idx::do_print(const print_context & c, unsigned level) const { - debugmsg("idx printraw",LOGLEVEL_PRINT); + c.s << "."; + print_index(c, level); +} - os << "idx("; +void idx::do_print_latex(const print_latex & c, unsigned level) const +{ + c.s << "{"; + print_index(c, level); + c.s << "}"; +} - if (symbolic) { - os << "symbolic,name=" << name; - } else { - os << "non symbolic,value=" << value; - } +void idx::do_print_csrc(const print_csrc & c, unsigned level) const +{ + c.s << "["; + if (value.info(info_flags::integer)) + c.s << ex_to(value).to_int(); + else + value.print(c); + c.s << "]"; +} - if (covariant) { - os << ",covariant"; - } else { - os << ",contravariant"; - } +void idx::do_print_tree(const print_tree & c, unsigned level) const +{ + c.s << std::string(level, ' ') << class_name() << " @" << this + << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << std::endl; + value.print(c, level + c.delta_indent); + dim.print(c, level + c.delta_indent); +} - os << ",serial=" << serial; - os << ",hash=" << hashvalue << ",flags=" << flags; - os << ")"; +void varidx::do_print(const print_context & c, unsigned level) const +{ + if (covariant) + c.s << "."; + else + c.s << "~"; + print_index(c, level); } -void idx::printtree(ostream & os, unsigned indent) const +void varidx::do_print_tree(const print_tree & c, unsigned level) const { - debugmsg("idx printtree",LOGLEVEL_PRINT); + c.s << std::string(level, ' ') << class_name() << " @" << this + << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << (covariant ? ", covariant" : ", contravariant") + << std::endl; + value.print(c, level + c.delta_indent); + dim.print(c, level + c.delta_indent); +} - os << string(indent,' ') << "idx: "; +void spinidx::do_print(const print_context & c, unsigned level) const +{ + if (covariant) + c.s << "."; + else + c.s << "~"; + if (dotted) + c.s << "*"; + print_index(c, level); +} - if (symbolic) { - os << "symbolic,name=" << name; - } else { - os << "non symbolic,value=" << value; - } +void spinidx::do_print_latex(const print_latex & c, unsigned level) const +{ + if (dotted) + c.s << "\\dot{"; + else + c.s << "{"; + print_index(c, level); + c.s << "}"; +} - if (covariant) { - os << ",covariant"; - } else { - os << ",contravariant"; - } +void spinidx::do_print_tree(const print_tree & c, unsigned level) const +{ + c.s << std::string(level, ' ') << class_name() << " @" << this + << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << (covariant ? ", covariant" : ", contravariant") + << (dotted ? ", dotted" : ", undotted") + << std::endl; + value.print(c, level + c.delta_indent); + dim.print(c, level + c.delta_indent); +} - os << ", serial=" << serial - << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")" - << ", flags=" << flags << endl; +bool idx::info(unsigned inf) const +{ + switch(inf) { + case info_flags::idx: + case info_flags::has_indices: + return true; + } + return inherited::info(inf); } -void idx::print(ostream & os, unsigned upper_precedence) const +size_t idx::nops() const { - debugmsg("idx print",LOGLEVEL_PRINT); + // don't count the dimension as that is not really a sub-expression + return 1; +} - if (covariant) { - os << "_"; - } else { - os << "~"; - } - if (symbolic) { - os << name; - } else { - os << value; - } +ex idx::op(size_t i) const +{ + GINAC_ASSERT(i == 0); + return value; } -bool idx::info(unsigned inf) const +ex idx::map(map_function & f) const { - if (inf==info_flags::idx) return true; - return basic::info(inf); + const ex &mapped_value = f(value); + if (are_ex_trivially_equal(value, mapped_value)) + return *this; + else { + idx *copy = duplicate(); + copy->setflag(status_flags::dynallocated); + copy->clearflag(status_flags::hash_calculated); + copy->value = mapped_value; + return *copy; + } } -ex idx::subs(lst const & ls, lst const & lr) const +/** 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(ls.nops()==lr.nops()); -#ifdef DO_GINAC_ASSERT - for (unsigned i=0; i(other)); + const idx &o = static_cast(other); - for (unsigned i=0; i - (const_cast(other)); + GINAC_ASSERT(is_a(other)); + const idx &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)); + GINAC_ASSERT(is_a(other)); + const varidx &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; -} + 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; +} -unsigned idx::calchash(void) const +bool varidx::match_same_type(const basic & other) const { - hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial)); - setflag(status_flags::hash_calculated); - return hashvalue; + GINAC_ASSERT(is_a(other)); + const varidx &o = static_cast(other); + + if (covariant != o.covariant) + return false; + + return inherited::match_same_type(other); } -////////// -// new virtual functions which can be overridden by derived classes -////////// +int spinidx::compare_same_type(const basic & other) const +{ + GINAC_ASSERT(is_a(other)); + const spinidx &o = static_cast(other); + + // Check dottedness first so dummy indices will end up next to each other + if (dotted != o.dotted) + return dotted ? -1 : 1; -// public + int cmpval = inherited::compare_same_type(other); + if (cmpval) + return cmpval; -bool idx::is_co_contra_pair(basic const & other) const + return 0; +} + +bool spinidx::match_same_type(const basic & other) const { - // like is_equal_same_type(), but tests for different covariant status - GINAC_ASSERT(is_of_type(other,idx)); - idx const & o=static_cast - (const_cast(other)); + GINAC_ASSERT(is_a(other)); + const spinidx &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; -} + if (dotted != o.dotted) + return false; + return inherited::match_same_type(other); +} -bool idx::is_symbolic(void) const +unsigned idx::calchash() const { - return symbolic; + // NOTE: The code in simplify_indexed() assumes that canonically + // ordered sequences of indices have the two members of dummy index + // pairs lying next to each other. The hash values for indices must + // be devised accordingly. The easiest (only?) way to guarantee the + // desired ordering is to make indices with the same value have equal + // hash keys. That is, the hash values must not depend on the index + // dimensions or other attributes (variance etc.). + // The compare_same_type() methods will take care of the rest. + unsigned v = make_hash_seed(typeid(*this)); + v = rotate_left(v); + v ^= value.gethash(); + + // Store calculated hash value only if object is already evaluated + if (flags & status_flags::evaluated) { + setflag(status_flags::hash_calculated); + hashvalue = v; + } + + return v; } -unsigned idx::get_value(void) const +/** By default, basic::evalf would evaluate the index value but we don't want + * a.1 to become a.(1.0). */ +ex idx::evalf(int level) const { - return value; + return *this; } -bool idx::is_covariant(void) const +ex idx::subs(const exmap & m, unsigned options) const { - return covariant; + // First look for index substitutions + exmap::const_iterator it = m.find(*this); + if (it != m.end()) { + + // Substitution index->index + if (is_a(it->second) || (options & subs_options::really_subs_idx)) + return it->second; + + // Otherwise substitute value + idx *i_copy = duplicate(); + i_copy->value = it->second; + 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(m, options); + if (are_ex_trivially_equal(value, subsed_value)) + return *this; + + idx *i_copy = duplicate(); + i_copy->value = subsed_value; + i_copy->clearflag(status_flags::hash_calculated); + return i_copy->setflag(status_flags::dynallocated); } -ex idx::toggle_covariant(void) const +/** Implementation of ex::diff() for an index always returns 0. + * + * @see ex::diff */ +ex idx::derivative(const symbol & s) 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); + return _ex0; } ////////// -// non-virtual functions in this class +// new virtual functions ////////// -// none +bool idx::is_dummy_pair_same_type(const basic & other) const +{ + const idx &o = static_cast(other); -////////// -// static member variables -////////// + // Only pure symbols form dummy pairs, "2n+1" doesn't + if (!is_a(value)) + return false; + + // Value must be equal, of course + if (!value.is_equal(o.value)) + return false; + + // Dimensions need not be equal but must be comparable (so we can + // determine the minimum dimension of contractions) + if (dim.is_equal(o.dim)) + return true; + + return is_exactly_a(dim) || is_exactly_a(o.dim); +} + +bool varidx::is_dummy_pair_same_type(const basic & other) const +{ + const varidx &o = static_cast(other); + + // Variance must be opposite + if (covariant == o.covariant) + return false; + + return inherited::is_dummy_pair_same_type(other); +} + +bool spinidx::is_dummy_pair_same_type(const basic & other) const +{ + const spinidx &o = static_cast(other); -// protected + // Dottedness must be the same + if (dotted != o.dotted) + return false; + + return inherited::is_dummy_pair_same_type(other); +} -unsigned idx::next_serial=0; ////////// -// global constants +// non-virtual functions ////////// -const idx some_idx; -type_info const & typeid_idx=typeid(some_idx); +ex idx::replace_dim(const ex & new_dim) const +{ + idx *i_copy = duplicate(); + i_copy->dim = new_dim; + i_copy->clearflag(status_flags::hash_calculated); + return i_copy->setflag(status_flags::dynallocated); +} + +ex idx::minimal_dim(const idx & other) const +{ + return GiNaC::minimal_dim(dim, other.dim); +} + +ex varidx::toggle_variance() const +{ + varidx *i_copy = 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() const +{ + spinidx *i_copy = duplicate(); + i_copy->dotted = !i_copy->dotted; + i_copy->clearflag(status_flags::hash_calculated); + return i_copy->setflag(status_flags::dynallocated); +} + +ex spinidx::toggle_variance_dot() const +{ + spinidx *i_copy = 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); +} ////////// -// other functions +// global functions ////////// -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) { - 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; -} - -#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 - - 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")); -} - -unsigned subs_index_in_exvector(exvector & v, ex const & is, ex const & ir) -{ - 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; -} - -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) -{ - GINAC_ASSERT(idxv_subs.size()==idxv_repl.size()); - ex res=e; - for (unsigned i=0; i(e1) || !is_a(e2)) + return false; + + return is_dummy_pair(ex_to(e1), ex_to(e2)); +} + +void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy) +{ + 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(*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); +} + +ex minimal_dim(const ex & dim1, const ex & dim2) +{ + if (dim1.is_equal(dim2) || dim1 < dim2 || (is_exactly_a(dim1) && !is_a(dim2))) + return dim1; + else if (dim1 > dim2 || (!is_a(dim1) && is_exactly_a(dim2))) + return dim2; + else { + std::ostringstream s; + s << "minimal_dim(): index dimensions " << dim1 << " and " << dim2 << " cannot be ordered"; + throw (std::runtime_error(s.str())); + } +} + } // namespace GiNaC -#endif // ndef NO_GINAC_NAMESPACE