3 * Implementation of GiNaC's indices. */
6 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "relational.h"
33 #ifndef NO_NAMESPACE_GINAC
35 #endif // ndef NO_NAMESPACE_GINAC
37 GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
40 // default constructor, destructor, copy constructor assignment operator and helpers
45 idx::idx() : inherited(TINFO_idx), symbolic(true), covariant(false)
47 debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
49 name=autoname_prefix()+ToString(serial);
54 void idx::copy(const idx & other)
56 inherited::copy(other);
58 symbolic=other.symbolic;
61 covariant=other.covariant;
64 void idx::destroy(bool call_parent)
66 if (call_parent) inherited::destroy(call_parent);
75 /** Construct symbolic index, using an automatically generated unique name.
77 * @param cov Index is covariant (contravariant otherwise)
78 * @return newly constructed index */
79 idx::idx(bool cov) : inherited(TINFO_idx), symbolic(true), covariant(cov)
81 debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
82 serial = next_serial++;
83 name = autoname_prefix()+ToString(serial);
86 /** Construct symbolic index with specified name.
88 * @param n Symbolic index name
89 * @param cov Index is covariant (contravariant otherwise)
90 * @return newly constructed index */
91 idx::idx(const std::string & n, bool cov) : inherited(TINFO_idx),
92 symbolic(true), name(n), covariant(cov)
94 debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
95 serial = next_serial++;
98 /** Construct symbolic index with specified name.
100 * @param n Symbolic index name
101 * @param cov Index is covariant (contravariant otherwise)
102 * @return newly constructed index */
103 idx::idx(const char * n, bool cov) : inherited(TINFO_idx), symbolic(true), name(n), covariant(cov)
105 debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
106 serial = next_serial++;
109 /** Construct numeric index with specified value.
111 * @param v Numeric index value
112 * @param cov Index is covariant (contravariant otherwise)
113 * @return newly constructed index */
114 idx::idx(unsigned v, bool cov) : inherited(TINFO_idx), symbolic(false), value(v), covariant(cov)
116 debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
124 /** Construct object from archive_node. */
125 idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
127 debugmsg("idx constructor from archive_node", LOGLEVEL_CONSTRUCT);
128 n.find_bool("symbolic", symbolic);
129 n.find_bool("covariant", covariant);
131 serial = next_serial++;
132 if (!(n.find_string("name", name)))
133 name = autoname_prefix() + ToString(serial);
136 n.find_unsigned("value", value);
140 /** Unarchive the object. */
141 ex idx::unarchive(const archive_node &n, const lst &sym_lst)
143 ex s = (new idx(n, sym_lst))->setflag(status_flags::dynallocated);
145 if (ex_to_idx(s).symbolic) {
146 // If idx is in sym_lst, return the existing idx
147 for (unsigned i=0; i<sym_lst.nops(); i++) {
148 if (is_ex_of_type(sym_lst.op(i), idx) && (ex_to_idx(sym_lst.op(i)).name == ex_to_idx(s).name))
149 return sym_lst.op(i);
155 /** Archive the object. */
156 void idx::archive(archive_node &n) const
158 inherited::archive(n);
159 n.add_bool("symbolic", symbolic);
160 n.add_bool("covariant", covariant);
162 n.add_string("name", name);
164 n.add_unsigned("value", value);
168 // functions overriding virtual functions from bases classes
173 void idx::printraw(std::ostream & os) const
175 debugmsg("idx printraw",LOGLEVEL_PRINT);
180 os << "symbolic,name=" << name;
182 os << "non symbolic,value=" << value;
188 os << ",contravariant";
191 os << ",serial=" << serial;
192 os << ",hash=" << hashvalue << ",flags=" << flags;
196 void idx::printtree(std::ostream & os, unsigned indent) const
198 debugmsg("idx printtree",LOGLEVEL_PRINT);
200 os << std::string(indent,' ') << "idx: ";
203 os << "symbolic,name=" << name;
205 os << "non symbolic,value=" << value;
211 os << ",contravariant";
214 os << ", serial=" << serial
215 << ", hash=" << hashvalue
216 << " (0x" << std::hex << hashvalue << std::dec << ")"
217 << ", flags=" << flags << std::endl;
220 void idx::print(std::ostream & os, unsigned upper_precedence) const
222 debugmsg("idx print",LOGLEVEL_PRINT);
236 bool idx::info(unsigned inf) const
238 if (inf==info_flags::idx) return true;
239 return inherited::info(inf);
242 ex idx::subs(const lst & ls, const lst & lr) const
244 GINAC_ASSERT(ls.nops()==lr.nops());
245 #ifdef DO_GINAC_ASSERT
246 for (unsigned i=0; i<ls.nops(); i++) {
247 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
248 is_ex_of_type(ls.op(i),idx));
250 #endif // def DO_GINAC_ASSERT
252 for (unsigned i=0; i<ls.nops(); i++) {
253 if (is_equal(*(ls.op(i)).bp)) {
262 int idx::compare_same_type(const basic & other) const
264 GINAC_ASSERT(is_of_type(other,idx));
265 const idx &o = static_cast<const idx &>(other);
267 if (covariant!=o.covariant) {
268 // different co/contravariant
269 return covariant ? -1 : 1;
272 if ((!symbolic) && (!o.symbolic)) {
273 // non-symbolic, of equal type: compare values
274 if (value==o.value) {
277 return value<o.value ? -1 : 1;
280 if (symbolic && o.symbolic) {
281 // both symbolic: compare serials
282 if (serial==o.serial) {
285 return serial<o.serial ? -1 : 1;
288 // one symbolic, one value: value is sorted first
289 return o.symbolic ? -1 : 1;
292 bool idx::is_equal_same_type(const basic & other) const
294 GINAC_ASSERT(is_of_type(other,idx));
295 const idx &o = static_cast<const idx &>(other);
297 if (covariant != o.covariant) return false;
298 if (symbolic != o.symbolic) return false;
299 if (symbolic && o.symbolic) return serial==o.serial;
300 return value==o.value;
303 unsigned idx::calchash(void) const
305 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
306 setflag(status_flags::hash_calculated);
311 // new virtual functions which can be overridden by derived classes
316 /** Check whether the index forms a co-/contravariant pair with another
317 * index (i.e. same name/value but opposite co-/contravariance). */
318 bool idx::is_co_contra_pair(const basic & other) const
320 // like is_equal_same_type(), but tests for different covariant status
321 GINAC_ASSERT(is_of_type(other,idx));
322 const idx & o=static_cast<const idx &>(const_cast<basic &>(other));
324 if (covariant==o.covariant) return false;
325 if (symbolic!=o.symbolic) return false;
326 if (symbolic && o.symbolic) return serial==o.serial;
327 return value==o.value;
330 /** Toggle co-/contravariance of index. */
331 ex idx::toggle_covariant(void) const
333 idx * i_copy=static_cast<idx *>(duplicate());
334 i_copy->covariant = !i_copy->covariant;
335 i_copy->clearflag(status_flags::hash_calculated);
336 return i_copy->setflag(status_flags::dynallocated);
340 // non-virtual functions in this class
345 std::string & idx::autoname_prefix(void)
347 static std::string * s = new std::string("index");
352 // static member variables
357 unsigned idx::next_serial=0;
363 /** Bring a vector of indices into a canonic order. This operation only makes
364 * sense if the object carrying these indices is either symmetric or totally
365 * antisymmetric with respect to the indices.
367 * @param iv Index vector
368 * @param antisymmetric Whether the object carrying the indices is antisymmetric (symmetric otherwise)
369 * @return the sign introduced by the reordering of the indices. For symmetric
370 * objects this is always +1. For antisymmetric objects this is either
371 * +1 or -1 or 0 (if two equal indices were encountered). If the index
372 * vector was unchanged this function returns INT_MAX. */
373 int canonicalize_indices(exvector & iv, bool antisymmetric)
376 // nothing do to for 0 or 1 indices
380 bool something_changed=false;
383 // simple bubble sort algorithm should be sufficient for the small number of indices needed
384 exvector::const_iterator last_idx=iv.end();
385 exvector::const_iterator next_to_last_idx=iv.end()-1;
386 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
387 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
388 int cmpval=(*it1).compare(*it2);
391 something_changed=true;
392 if (antisymmetric) sig=-sig;
393 } else if ((cmpval==0) && antisymmetric) {
394 something_changed=true;
400 return something_changed ? sig : INT_MAX;
403 /** Build a vector of indices as the set intersection of two other index
404 * vectors (i.e. the returned vector contains the indices which appear in
405 * both source vectors). */
406 exvector idx_intersect(const exvector & iv1, const exvector & iv2)
408 // Create union vector
410 iv_union.reserve(iv1.size() + iv2.size());
411 iv_union.insert(iv_union.end(), iv1.begin(), iv1.end());
412 iv_union.insert(iv_union.end(), iv2.begin(), iv2.end());
415 canonicalize_indices(iv_union);
417 // Look for duplicates
418 exvector iv_intersect;
419 exvector::const_iterator cit = iv_union.begin(), citend = iv_union.end();
423 while (cit != citend) {
424 if (e.is_equal(*cit)) {
425 iv_intersect.push_back(e);
428 } while (cit != citend && e.is_equal(*cit));
437 /** Given a vector iv3 of three indices and a vector iv2 of two indices
438 * where iv2 is a subset of iv3, return the (free) index that is in iv3
439 * but not in iv2 and the sign introduced by permuting that index to the
442 * @param iv3 Vector of 3 indices
443 * @param iv2 Vector of 2 indices, must be a subset of iv3
444 * @param sig Returns the sign introduced by permuting the free index to the
445 * front if the object carrying the indices was antisymmetric (if
446 * it's symmetric, you can just ignore the returned value).
447 * @return the free index (the one that is in iv3 but not in iv2) */
448 ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2, int * sig)
450 // match (return value,iv2) to iv3 by permuting indices
451 // iv3 is always cyclic
453 GINAC_ASSERT(iv3.size()==3);
454 GINAC_ASSERT(iv2.size()==2);
458 #define TEST_PERMUTATION(A,B,C,P) \
459 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
464 TEST_PERMUTATION(0,1,2, 1);
465 TEST_PERMUTATION(0,2,1, -1);
466 TEST_PERMUTATION(1,0,2, -1);
467 TEST_PERMUTATION(1,2,0, 1);
468 TEST_PERMUTATION(2,0,1, 1);
469 TEST_PERMUTATION(2,1,0, -1);
470 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
473 /** Substitute one index in a vector of expressions.
475 * @param v Vector to substitute in (will be modified)
476 * @param is Index being substituted
477 * @param ir Index to replace by
478 * @return number of performed substitutions */
479 unsigned subs_index_in_exvector(exvector & v, const ex & is, const ex & ir)
481 exvector::iterator it;
482 unsigned replacements=0;
483 unsigned current_replacements;
485 GINAC_ASSERT(is_ex_of_type(is,idx));
486 GINAC_ASSERT(is_ex_of_type(ir,idx));
488 for (it=v.begin(); it!=v.end(); ++it) {
489 current_replacements=count_index(*it,is);
490 if (current_replacements>0) {
491 (*it)=(*it).subs(is==ir);
493 replacements += current_replacements;
498 /** Count number of times a given index appears in the index vector of an
501 * @param e Indexed object
502 * @param i Index to look for
503 * @return number of times the index was found */
504 unsigned count_index(const ex & e, const ex & i)
506 exvector idxv=e.get_indices();
508 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
509 if ((*cit).is_equal(i)) count++;
514 /** Substitute multiple indices in an expression.
516 * @param e Expression to substitute in
517 * @param idxv_subs Vector of indices being substituted
518 * @param idxv_repl Vector of indices to replace by (1:1 correspondence to idxv_subs)
519 * @return expression with substituted indices */
520 ex subs_indices(const ex & e, const exvector & idxv_subs, const exvector & idxv_repl)
522 GINAC_ASSERT(idxv_subs.size()==idxv_repl.size());
524 for (unsigned i=0; i<idxv_subs.size(); ++i) {
525 res=res.subs(idxv_subs[i]==idxv_repl[i]);
530 #ifndef NO_NAMESPACE_GINAC
532 #endif // ndef NO_NAMESPACE_GINAC