* Implementation of GiNaC's indices. */
/*
- * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
+ * GiNaC Copyright (C) 1999-2008 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
*
* 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 <iostream>
+#include <sstream>
#include <stdexcept>
#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"
-#ifndef NO_NAMESPACE_GINAC
namespace GiNaC {
-#endif // ndef NO_NAMESPACE_GINAC
-GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
+GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(idx, basic,
+ print_func<print_context>(&idx::do_print).
+ print_func<print_latex>(&idx::do_print_latex).
+ print_func<print_csrc>(&idx::do_print_csrc).
+ print_func<print_tree>(&idx::do_print_tree))
+
+GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(varidx, idx,
+ print_func<print_context>(&varidx::do_print).
+ print_func<print_latex>(&varidx::do_print_latex).
+ print_func<print_tree>(&varidx::do_print_tree))
+
+GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(spinidx, varidx,
+ print_func<print_context>(&spinidx::do_print).
+ print_func<print_latex>(&spinidx::do_print_latex).
+ print_func<print_tree>(&spinidx::do_print_tree))
//////////
-// default constructor, destructor, copy constructor assignment operator and helpers
+// default constructor
//////////
-// public
+idx::idx() : inherited(&idx::tinfo_static) {}
+
+varidx::varidx() : covariant(false)
+{
+ tinfo_key = &varidx::tinfo_static;
+}
-idx::idx() : inherited(TINFO_idx), symbolic(true), covariant(false)
+spinidx::spinidx() : dotted(false)
{
- debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
- serial=next_serial++;
- name=autoname_prefix()+ToString(serial);
+ tinfo_key = &spinidx::tinfo_static;
}
-// protected
+//////////
+// other constructors
+//////////
+
+idx::idx(const ex & v, const ex & d) : inherited(&idx::tinfo_static), value(v), dim(d)
+{
+ if (is_dim_numeric())
+ if (!dim.info(info_flags::posint))
+ throw(std::invalid_argument("dimension of space must be a positive integer"));
+}
-void idx::copy(const idx & other)
+varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
{
- inherited::copy(other);
- serial=other.serial;
- symbolic=other.symbolic;
- name=other.name;
- value=other.value;
- covariant=other.covariant;
+ tinfo_key = &varidx::tinfo_static;
}
-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) inherited::destroy(call_parent);
+ tinfo_key = &spinidx::tinfo_static;
}
//////////
-// other constructors
+// archiving
//////////
-// public
+idx::idx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
+{
+ n.find_ex("value", value, sym_lst);
+ n.find_ex("dim", dim, sym_lst);
+}
-/** Construct symbolic index, using an automatically generated unique name.
- *
- * @param cov Index is covariant (contravariant otherwise)
- * @return newly constructed index */
-idx::idx(bool cov) : inherited(TINFO_idx), symbolic(true), covariant(cov)
+varidx::varidx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
{
- debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
- serial = next_serial++;
- name = autoname_prefix()+ToString(serial);
+ n.find_bool("covariant", covariant);
}
-/** Construct symbolic index with specified name.
- *
- * @param n Symbolic index name
- * @param cov Index is covariant (contravariant otherwise)
- * @return newly constructed index */
-idx::idx(const std::string & n, bool cov) : inherited(TINFO_idx),
- symbolic(true), name(n), covariant(cov)
+spinidx::spinidx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
{
- debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
- serial = next_serial++;
+ n.find_bool("dotted", dotted);
}
-/** Construct symbolic index with specified name.
- *
- * @param n Symbolic index name
- * @param cov Index is covariant (contravariant otherwise)
- * @return newly constructed index */
-idx::idx(const char * n, bool cov) : inherited(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);
}
-/** Construct numeric index with specified value.
- *
- * @param v Numeric index value
- * @param cov Index is covariant (contravariant otherwise)
- * @return newly constructed index */
-idx::idx(unsigned v, bool cov) : inherited(TINFO_idx), symbolic(false), value(v), covariant(cov)
+void varidx::archive(archive_node &n) const
+{
+ inherited::archive(n);
+ n.add_bool("covariant", covariant);
+}
+
+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)
+
//////////
-// archiving
+// functions overriding virtual functions from base classes
//////////
-/** Construct object from archive_node. */
-idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
+void idx::print_index(const print_context & c, unsigned level) const
{
- 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 = autoname_prefix() + ToString(serial);
- } else {
- serial = 0;
- n.find_unsigned("value", value);
+ bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(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 << "]";
}
}
-/** Unarchive the object. */
-ex idx::unarchive(const archive_node &n, const lst &sym_lst)
+void idx::do_print(const print_context & c, unsigned level) const
{
- ex s = (new idx(n, sym_lst))->setflag(status_flags::dynallocated);
+ c.s << ".";
+ print_index(c, level);
+}
- 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;
+void idx::do_print_latex(const print_latex & c, unsigned level) const
+{
+ c.s << "{";
+ print_index(c, level);
+ c.s << "}";
}
-/** Archive the object. */
-void idx::archive(archive_node &n) const
+void idx::do_print_csrc(const print_csrc & c, unsigned level) const
{
- inherited::archive(n);
- n.add_bool("symbolic", symbolic);
- n.add_bool("covariant", covariant);
- if (symbolic)
- n.add_string("name", name);
+ c.s << "[";
+ if (value.info(info_flags::integer))
+ c.s << ex_to<numeric>(value).to_int();
else
- n.add_unsigned("value", value);
+ value.print(c);
+ c.s << "]";
}
-//////////
-// functions overriding virtual functions from bases classes
-//////////
+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);
+}
-// public
+void varidx::do_print(const print_context & c, unsigned level) const
+{
+ if (covariant)
+ c.s << ".";
+ else
+ c.s << "~";
+ print_index(c, level);
+}
-basic * idx::duplicate() const
+void varidx::do_print_tree(const print_tree & c, unsigned level) const
{
- debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
- return new idx(*this);
+ 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);
}
-void idx::printraw(std::ostream & os) const
+void spinidx::do_print(const print_context & c, unsigned level) const
{
- debugmsg("idx printraw",LOGLEVEL_PRINT);
+ if (covariant)
+ c.s << ".";
+ else
+ c.s << "~";
+ if (dotted)
+ c.s << "*";
+ print_index(c, level);
+}
- os << "idx(";
+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 (symbolic) {
- os << "symbolic,name=" << name;
- } else {
- os << "non symbolic,value=" << value;
- }
+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);
+}
- if (covariant) {
- os << ",covariant";
- } else {
- os << ",contravariant";
+bool idx::info(unsigned inf) const
+{
+ switch(inf) {
+ case info_flags::idx:
+ case info_flags::has_indices:
+ return true;
}
-
- os << ",serial=" << serial;
- os << ",hash=" << hashvalue << ",flags=" << flags;
- os << ")";
+ return inherited::info(inf);
}
-void idx::printtree(std::ostream & os, unsigned indent) const
+size_t idx::nops() const
{
- debugmsg("idx printtree",LOGLEVEL_PRINT);
+ // don't count the dimension as that is not really a sub-expression
+ return 1;
+}
- os << std::string(indent,' ') << "idx: ";
+ex idx::op(size_t i) const
+{
+ GINAC_ASSERT(i == 0);
+ return value;
+}
- if (symbolic) {
- os << "symbolic,name=" << name;
- } else {
- os << "non symbolic,value=" << value;
+ex idx::map(map_function & f) const
+{
+ 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;
}
+}
- if (covariant) {
- os << ",covariant";
- } else {
- os << ",contravariant";
- }
+/** 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<idx>(other));
+ const idx &o = static_cast<const idx &>(other);
- os << ", serial=" << serial
- << ", hash=" << hashvalue
- << " (0x" << std::hex << hashvalue << std::dec << ")"
- << ", flags=" << flags << std::endl;
+ int cmpval = value.compare(o.value);
+ if (cmpval)
+ return cmpval;
+ return dim.compare(o.dim);
}
-void idx::print(std::ostream & os, unsigned upper_precedence) const
+bool idx::match_same_type(const basic & other) const
{
- debugmsg("idx print",LOGLEVEL_PRINT);
+ GINAC_ASSERT(is_a<idx>(other));
+ const idx &o = static_cast<const idx &>(other);
- if (covariant) {
- os << "_";
- } else {
- os << "~";
- }
- if (symbolic) {
- os << name;
- } else {
- os << value;
- }
+ return dim.is_equal(o.dim);
}
-bool idx::info(unsigned inf) const
+int varidx::compare_same_type(const basic & other) const
{
- if (inf==info_flags::idx) return true;
- return inherited::info(inf);
+ GINAC_ASSERT(is_a<varidx>(other));
+ const varidx &o = static_cast<const varidx &>(other);
+
+ 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;
}
-ex idx::subs(const lst & ls, const lst & lr) const
+bool varidx::match_same_type(const basic & other) 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
+ GINAC_ASSERT(is_a<varidx>(other));
+ const varidx &o = static_cast<const varidx &>(other);
- for (unsigned i=0; i<ls.nops(); i++) {
- if (is_equal(*(ls.op(i)).bp)) {
- return lr.op(i);
- }
- }
- return *this;
+ if (covariant != o.covariant)
+ return false;
+
+ return inherited::match_same_type(other);
}
-// protected
+int spinidx::compare_same_type(const basic & other) const
+{
+ GINAC_ASSERT(is_a<spinidx>(other));
+ const spinidx &o = static_cast<const spinidx &>(other);
-int idx::compare_same_type(const basic & other) const
+ // 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;
+}
+
+bool spinidx::match_same_type(const basic & other) const
{
- GINAC_ASSERT(is_of_type(other,idx));
- const idx &o = static_cast<const idx &>(&other);
+ GINAC_ASSERT(is_a<spinidx>(other));
+ const spinidx &o = static_cast<const spinidx &>(other);
- if (covariant!=o.covariant) {
- // different co/contravariant
- return covariant ? -1 : 1;
- }
+ if (dotted != o.dotted)
+ return false;
+ return inherited::match_same_type(other);
+}
- if ((!symbolic) && (!o.symbolic)) {
- // non-symbolic, of equal type: compare values
- if (value==o.value) {
- return 0;
- }
- return value<o.value ? -1 : 1;
+unsigned idx::calchash() const
+{
+ // 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.
+ const void* this_tinfo = (const void*)(typeid(*this).name());
+ unsigned v = golden_ratio_hash((p_int)this_tinfo);
+ 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;
}
- if (symbolic && o.symbolic) {
- // both symbolic: compare serials
- if (serial==o.serial) {
- return 0;
- }
- return serial<o.serial ? -1 : 1;
- }
+ return v;
+}
- // one symbolic, one value: value is sorted first
- return o.symbolic ? -1 : 1;
+/** 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 *this;
}
-bool idx::is_equal_same_type(const basic & other) const
+ex idx::subs(const exmap & m, unsigned options) const
{
- GINAC_ASSERT(is_of_type(other,idx));
- const idx &o = static_cast<const idx &>(other);
+ // First look for index substitutions
+ exmap::const_iterator it = m.find(*this);
+ if (it != m.end()) {
+
+ // Substitution index->index
+ if (is_a<idx>(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;
- if (covariant != o.covariant) return false;
- if (symbolic != o.symbolic) return false;
- if (symbolic && o.symbolic) return serial==o.serial;
- return value==o.value;
-}
+ idx *i_copy = 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_dummy_pair_same_type(const basic & other) const
+{
+ const idx &o = static_cast<const idx &>(other);
+
+ // Only pure symbols form dummy pairs, "2n+1" doesn't
+ if (!is_a<symbol>(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<numeric>(dim) || is_exactly_a<numeric>(o.dim);
+}
-/** Check whether the index forms a co-/contravariant pair with another
- * index (i.e. same name/value but opposite co-/contravariance). */
-bool idx::is_co_contra_pair(const basic & other) const
+bool varidx::is_dummy_pair_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 idx &>(const_cast<basic &>(other));
+ const varidx &o = static_cast<const varidx &>(other);
+
+ // Variance must be opposite
+ if (covariant == o.covariant)
+ return false;
- 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 inherited::is_dummy_pair_same_type(other);
+}
-/** Toggle co-/contravariance of index. */
-ex idx::toggle_covariant(void) const
+bool spinidx::is_dummy_pair_same_type(const basic & other) 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);
+ const spinidx &o = static_cast<const spinidx &>(other);
+
+ // Dottedness must be the same
+ if (dotted != o.dotted)
+ return false;
+
+ return inherited::is_dummy_pair_same_type(other);
}
+
//////////
-// non-virtual functions in this class
+// non-virtual functions
//////////
-// private
+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);
+}
-std::string & idx::autoname_prefix(void)
+ex idx::minimal_dim(const idx & other) const
{
- static std::string * s = new std::string("index");
- return *s;
+ return GiNaC::minimal_dim(dim, other.dim);
}
-//////////
-// static member variables
-//////////
+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);
+}
-// protected
+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);
+}
-unsigned idx::next_serial=0;
+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
//////////
-/** Bring a vector of indices into a canonic order. This operation only makes
- * sense if the object carrying these indices is either symmetric or totally
- * antisymmetric with respect to the indices.
- *
- * @param iv Index vector
- * @param antisymmetric Whether the object carrying the indices is antisymmetric (symmetric otherwise)
- * @return the sign introduced by the reordering of the indices. For symmetric
- * objects this is always +1. For antisymmetric objects this is either
- * +1 or -1 or 0 (if two equal indices were encountered). If the index
- * vector was unchanged this function returns INT_MAX. */
-int canonicalize_indices(exvector & iv, bool antisymmetric)
-{
- if (iv.size()<2) {
- // nothing do to for 0 or 1 indices
- return INT_MAX;
- }
+bool is_dummy_pair(const idx & i1, const idx & i2)
+{
+ // The indices must be of exactly the same type
+ if (typeid(i1) != typeid(i2))
+ 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;
- }
- }
- }
+ // Same type, let the indices decide whether they are paired
+ return i1.is_dummy_pair_same_type(i2);
+}
- return something_changed ? sig : INT_MAX;
-}
-
-/** Build a vector of indices as the set intersection of two other index
- * vectors (i.e. the returned vector contains the indices which appear in
- * both source vectors). */
-exvector idx_intersect(const exvector & iv1, const exvector & iv2)
-{
- // Create union vector
- exvector iv_union;
- iv_union.reserve(iv1.size() + iv2.size());
- iv_union.insert(iv_union.end(), iv1.begin(), iv1.end());
- iv_union.insert(iv_union.end(), iv2.begin(), iv2.end());
-
- // Sort it
- canonicalize_indices(iv_union);
-
- // Look for duplicates
- exvector iv_intersect;
- exvector::const_iterator cit = iv_union.begin(), citend = iv_union.end();
- ex e;
- if (cit != citend)
- e = *cit++;
- while (cit != citend) {
- if (e.is_equal(*cit)) {
- iv_intersect.push_back(e);
- do {
- cit++;
- } while (cit != citend && e.is_equal(*cit));
- if (cit == citend)
- break;
- }
- e = *cit++;
- }
- return iv_intersect;
+bool is_dummy_pair(const ex & e1, const ex & e2)
+{
+ // The expressions must be indices
+ if (!is_a<idx>(e1) || !is_a<idx>(e2))
+ return false;
+
+ return is_dummy_pair(ex_to<idx>(e1), ex_to<idx>(e2));
}
-/** Given a vector iv3 of three indices and a vector iv2 of two indices
- * where iv2 is a subset of iv3, return the (free) index that is in iv3
- * but not in iv2 and the sign introduced by permuting that index to the
- * front.
- *
- * @param iv3 Vector of 3 indices
- * @param iv2 Vector of 2 indices, must be a subset of iv3
- * @param sig Returns the sign introduced by permuting the free index to the
- * front if the object carrying the indices was antisymmetric (if
- * it's symmetric, you can just ignore the returned value).
- * @return the free index (the one that is in iv3 but not in iv2) */
-ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2, 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;
-
-#define TEST_PERMUTATION(A,B,C,P) \
- if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
- *sig=P; \
- return iv3[A]; \
- }
-
- 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"));
-}
-
-/** Substitute one index in a vector of expressions.
- *
- * @param v Vector to substitute in (will be modified)
- * @param is Index being substituted
- * @param ir Index to replace by
- * @return number of performed substitutions */
-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;
+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<idx>(*it).is_symbolic())
+ out_free.push_back(*it);
+ return;
}
- return replacements;
-}
-/** Count number of times a given index appears in the index vector of an
- * indexed object.
- *
- * @param e Indexed object
- * @param i Index to look for
- * @return number of times the index was found */
-unsigned count_index(const ex & e, const ex & 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++;
+ // 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<idx>(*last).is_symbolic())
+ out_free.push_back(*last);
+ }
+ last = it++;
}
- return count;
+ if (ex_to<idx>(*last).is_symbolic())
+ out_free.push_back(*last);
}
-/** Substitute multiple indices in an expression.
- *
- * @param e Expression to substitute in
- * @param idxv_subs Vector of indices being substituted
- * @param idxv_repl Vector of indices to replace by (1:1 correspondence to idxv_subs)
- * @return expression with substituted indices */
-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; i<idxv_subs.size(); ++i) {
- res=res.subs(idxv_subs[i]==idxv_repl[i]);
+ex minimal_dim(const ex & dim1, const ex & dim2)
+{
+ if (dim1.is_equal(dim2) || dim1 < dim2 || (is_exactly_a<numeric>(dim1) && !is_a<numeric>(dim2)))
+ return dim1;
+ else if (dim1 > dim2 || (!is_a<numeric>(dim1) && is_exactly_a<numeric>(dim2)))
+ return dim2;
+ else {
+ std::ostringstream s;
+ s << "minimal_dim(): index dimensions " << dim1 << " and " << dim2 << " cannot be ordered";
+ throw (std::runtime_error(s.str()));
}
- return res;
}
-#ifndef NO_NAMESPACE_GINAC
} // namespace GiNaC
-#endif // ndef NO_NAMESPACE_GINAC