X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fidx.cpp;h=503a74ace174f6ab32c03a07bea7c3b9236dbb0d;hp=da410a21f089c410bae9ba184e4c2b2127731daa;hb=dbd9c306a74f1cb258c0d15a346b973b39deaad2;hpb=9e2d60e206395fc9908e1f9025e50c76b3d7c182 diff --git a/ginac/idx.cpp b/ginac/idx.cpp index da410a21..503a74ac 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-2003 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,479 +20,529 @@ * 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" -#ifndef NO_NAMESPACE_GINAC namespace GiNaC { -#endif // ndef NO_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() : inherited(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(false); + tinfo_key = TINFO_spinidx; } -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(true); - copy(other); - } - return *this; + inherited::copy(other); + covariant = other.covariant; } -// protected - -void idx::copy(const idx & other) +void spinidx::copy(const spinidx & other) { inherited::copy(other); - serial=other.serial; - symbolic=other.symbolic; - name=other.name; - value=other.value; - covariant=other.covariant; + dotted = other.dotted; } -void idx::destroy(bool call_parent) -{ - if (call_parent) inherited::destroy(call_parent); -} +DEFAULT_DESTROY(idx) +DEFAULT_DESTROY(varidx) +DEFAULT_DESTROY(spinidx) ////////// // other constructors ////////// -// public - -idx::idx(bool cov) : inherited(TINFO_idx), symbolic(true), covariant(cov) +idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d) { - debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT); - serial = next_serial++; - name = "index"+ToString(serial); + if (is_dim_numeric()) + if (!dim.info(info_flags::posint)) + throw(std::invalid_argument("dimension of space must be a positive integer")); } -idx::idx(const std::string & n, bool cov) : inherited(TINFO_idx), - symbolic(true), name(n), covariant(cov) +varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov) { - debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT); - serial = next_serial++; + tinfo_key = TINFO_varidx; } -idx::idx(const char * n, bool cov) : inherited(TINFO_idx), symbolic(true), name(n), covariant(cov) +spinidx::spinidx(const ex & v, const ex & d, bool cov, bool dot) : inherited(v, d, cov), dotted(dot) { - debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT); - serial = next_serial++; -} - -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; + tinfo_key = TINFO_spinidx; } ////////// // 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); - } + n.find_ex("value", value, sym_lst); + n.find_ex("dim", dim, sym_lst); } -/** Unarchive the object. */ -ex idx::unarchive(const archive_node &n, const lst &sym_lst) +varidx::varidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) { - ex s = (new idx(n, sym_lst))->setflag(status_flags::dynallocated); + n.find_bool("covariant", covariant); +} - if (ex_to_idx(s).symbolic) { - // If idx is in sym_lst, return the existing idx - for (unsigned i=0; i(c).delta_indent; + value.print(c, level + delta_indent); + dim.print(c, level + delta_indent); - if (symbolic) { - os << "symbolic,name=" << name; } else { - os << "non symbolic,value=" << value; - } - if (covariant) { - os << ",covariant"; - } else { - os << ",contravariant"; + if (is_a(c)) + c.s << "{"; + else + c.s << "."; + bool need_parens = !(is_exactly_a(value) || is_a(value)); + if (need_parens) + c.s << "("; + value.print(c); + if (need_parens) + c.s << ")"; + if (is_a(c)) + c.s << "}"; } - - os << ",serial=" << serial; - os << ",hash=" << hashvalue << ",flags=" << flags; - os << ")"; } -void idx::printtree(std::ostream & os, unsigned indent) const +void varidx::print(const print_context & c, unsigned level) const { - debugmsg("idx printtree",LOGLEVEL_PRINT); + if (is_of_type(c, print_tree)) { - os << std::string(indent,' ') << "idx: "; + 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); - if (symbolic) { - os << "symbolic,name=" << name; } else { - os << "non symbolic,value=" << value; - } - - if (covariant) { - os << ",covariant"; - } else { - os << ",contravariant"; + if (is_a(c)) + c.s << "{"; + else { + if (covariant) + c.s << "."; + else + c.s << "~"; + } + bool need_parens = !(is_exactly_a(value) || is_a(value)); + if (need_parens) + c.s << "("; + value.print(c); + if (need_parens) + c.s << ")"; + if (is_a(c)) + c.s << "}"; } - - os << ", serial=" << serial - << ", hash=" << hashvalue - << " (0x" << std::hex << hashvalue << std::dec << ")" - << ", flags=" << flags << std::endl; } -void idx::print(std::ostream & os, unsigned upper_precedence) const +void spinidx::print(const print_context & c, unsigned level) const { - debugmsg("idx print",LOGLEVEL_PRINT); + 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); - if (covariant) { - os << "_"; - } else { - os << "~"; - } - if (symbolic) { - os << name; } else { - os << value; + + 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_exactly_a(value) || is_a(value)); + if (need_parens) + c.s << "("; + value.print(c); + if (need_parens) + c.s << ")"; + if (is_tex && dotted) + c.s << "}"; + if (is_tex) + c.s << "}"; } } bool idx::info(unsigned inf) const { - if (inf==info_flags::idx) return true; + 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(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 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; -} + return dim.is_equal(o.dim); +} -unsigned idx::calchash(void) const +int varidx::compare_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); -////////// -// new virtual functions which can be overridden by derived classes -////////// + int cmpval = inherited::compare_same_type(other); + if (cmpval) + return cmpval; -// public + // 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::is_co_contra_pair(const basic & other) const +bool varidx::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)); - const idx & o=static_cast(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; -} + if (covariant != o.covariant) + return false; + return inherited::match_same_type(other); +} -bool idx::is_symbolic(void) const +int spinidx::compare_same_type(const basic & other) const { - return symbolic; + 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; + + int cmpval = inherited::compare_same_type(other); + if (cmpval) + return cmpval; + + return 0; } -unsigned idx::get_value(void) const +bool spinidx::match_same_type(const basic & other) const { - return value; + GINAC_ASSERT(is_a(other)); + const spinidx &o = static_cast(other); + + if (dotted != o.dotted) + return false; + return inherited::match_same_type(other); } -bool idx::is_covariant(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 covariant; + return *this; } -ex idx::toggle_covariant(void) const +ex idx::subs(const lst & ls, const lst & lr, bool no_pattern) const { - idx * i_copy=static_cast(duplicate()); - i_copy->covariant = !i_copy->covariant; + GINAC_ASSERT(ls.nops() == lr.nops()); + + // First look for index substitutions + for (unsigned i=0; i(ls.op(i)))) { + + // Substitution index->index + if (is_a(lr.op(i))) + 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); } +/** Implementation of ex::diff() for an index always returns 0. + * + * @see ex::diff */ +ex idx::derivative(const symbol & s) const +{ + 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, numeric indices and expressions + // like "2n+1" don'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 (dim < o.dim || dim > o.dim || (is_exactly_a(dim) && is_a(o.dim)) || (is_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; -// protected + 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); + + // 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; -const std::type_info & typeid_idx = typeid(some_idx); +ex idx::replace_dim(const ex & new_dim) const +{ + idx *i_copy = static_cast(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 +{ + if (dim.is_equal(other.dim) || dim < other.dim || (is_exactly_a(dim) && is_a(other.dim))) + return dim; + else if (dim > other.dim || (is_a(dim) && is_exactly_a(other.dim))) + return other.dim; + else + throw (std::runtime_error("idx::minimal_dim: index dimensions cannot be ordered")); +} + +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); +} + +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); +} ////////// -// 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; - } + // The indices must be of exactly the same type + if (i1.tinfo() != i2.tinfo()) + return false; - 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(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]; \ - } +bool is_dummy_pair(const ex & e1, const ex & e2) +{ + // The expressions must be indices + if (!is_a(e1) || !is_a(e2)) + return false; -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 - - 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, const ex & is, const ex & 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; + return is_dummy_pair(ex_to(e1), ex_to(e2)); } -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++; + 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; } - return count; -} -ex subs_indices(const ex & e, const exvector & idxv_subs, - const exvector & idxv_repl) -{ - GINAC_ASSERT(idxv_subs.size()==idxv_repl.size()); - ex res=e; - for (unsigned i=0; iis_equal(*last) && ex_to(*last).is_symbolic()) + out_free.push_back(*last); + } + last = it++; } - return res; + if (ex_to(*last).is_symbolic()) + out_free.push_back(*last); } -#ifndef NO_NAMESPACE_GINAC } // namespace GiNaC -#endif // ndef NO_NAMESPACE_GINAC