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 debugmsg("idx destructor",LOGLEVEL_DESTRUCT);
58 idx::idx(const idx & other)
60 debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT);
64 const idx & idx::operator=(const idx & other)
66 debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT);
76 void idx::copy(const idx & other)
78 inherited::copy(other);
80 symbolic=other.symbolic;
83 covariant=other.covariant;
86 void idx::destroy(bool call_parent)
88 if (call_parent) inherited::destroy(call_parent);
97 /** Construct symbolic index, using an automatically generated unique name.
99 * @param cov Index is covariant (contravariant otherwise)
100 * @return newly constructed index */
101 idx::idx(bool cov) : inherited(TINFO_idx), symbolic(true), covariant(cov)
103 debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
104 serial = next_serial++;
105 name = autoname_prefix()+ToString(serial);
108 /** Construct symbolic index with specified name.
110 * @param n Symbolic index name
111 * @param cov Index is covariant (contravariant otherwise)
112 * @return newly constructed index */
113 idx::idx(const std::string & n, bool cov) : inherited(TINFO_idx),
114 symbolic(true), name(n), covariant(cov)
116 debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
117 serial = next_serial++;
120 /** Construct symbolic index with specified name.
122 * @param n Symbolic index name
123 * @param cov Index is covariant (contravariant otherwise)
124 * @return newly constructed index */
125 idx::idx(const char * n, bool cov) : inherited(TINFO_idx), symbolic(true), name(n), covariant(cov)
127 debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
128 serial = next_serial++;
131 /** Construct numeric index with specified value.
133 * @param v Numeric index value
134 * @param cov Index is covariant (contravariant otherwise)
135 * @return newly constructed index */
136 idx::idx(unsigned v, bool cov) : inherited(TINFO_idx), symbolic(false), value(v), covariant(cov)
138 debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
146 /** Construct object from archive_node. */
147 idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
149 debugmsg("idx constructor from archive_node", LOGLEVEL_CONSTRUCT);
150 n.find_bool("symbolic", symbolic);
151 n.find_bool("covariant", covariant);
153 serial = next_serial++;
154 if (!(n.find_string("name", name)))
155 name = autoname_prefix() + ToString(serial);
158 n.find_unsigned("value", value);
162 /** Unarchive the object. */
163 ex idx::unarchive(const archive_node &n, const lst &sym_lst)
165 ex s = (new idx(n, sym_lst))->setflag(status_flags::dynallocated);
167 if (ex_to_idx(s).symbolic) {
168 // If idx is in sym_lst, return the existing idx
169 for (unsigned i=0; i<sym_lst.nops(); i++) {
170 if (is_ex_of_type(sym_lst.op(i), idx) && (ex_to_idx(sym_lst.op(i)).name == ex_to_idx(s).name))
171 return sym_lst.op(i);
177 /** Archive the object. */
178 void idx::archive(archive_node &n) const
180 inherited::archive(n);
181 n.add_bool("symbolic", symbolic);
182 n.add_bool("covariant", covariant);
184 n.add_string("name", name);
186 n.add_unsigned("value", value);
190 // functions overriding virtual functions from bases classes
195 basic * idx::duplicate() const
197 debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
198 return new idx(*this);
201 void idx::printraw(std::ostream & os) const
203 debugmsg("idx printraw",LOGLEVEL_PRINT);
208 os << "symbolic,name=" << name;
210 os << "non symbolic,value=" << value;
216 os << ",contravariant";
219 os << ",serial=" << serial;
220 os << ",hash=" << hashvalue << ",flags=" << flags;
224 void idx::printtree(std::ostream & os, unsigned indent) const
226 debugmsg("idx printtree",LOGLEVEL_PRINT);
228 os << std::string(indent,' ') << "idx: ";
231 os << "symbolic,name=" << name;
233 os << "non symbolic,value=" << value;
239 os << ",contravariant";
242 os << ", serial=" << serial
243 << ", hash=" << hashvalue
244 << " (0x" << std::hex << hashvalue << std::dec << ")"
245 << ", flags=" << flags << std::endl;
248 void idx::print(std::ostream & os, unsigned upper_precedence) const
250 debugmsg("idx print",LOGLEVEL_PRINT);
264 bool idx::info(unsigned inf) const
266 if (inf==info_flags::idx) return true;
267 return inherited::info(inf);
270 ex idx::subs(const lst & ls, const lst & lr) const
272 GINAC_ASSERT(ls.nops()==lr.nops());
273 #ifdef DO_GINAC_ASSERT
274 for (unsigned i=0; i<ls.nops(); i++) {
275 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
276 is_ex_of_type(ls.op(i),idx));
278 #endif // def DO_GINAC_ASSERT
280 for (unsigned i=0; i<ls.nops(); i++) {
281 if (is_equal(*(ls.op(i)).bp)) {
290 int idx::compare_same_type(const basic & other) const
292 GINAC_ASSERT(is_of_type(other,idx));
293 const idx &o = static_cast<const idx &>(&other);
295 if (covariant!=o.covariant) {
296 // different co/contravariant
297 return covariant ? -1 : 1;
300 if ((!symbolic) && (!o.symbolic)) {
301 // non-symbolic, of equal type: compare values
302 if (value==o.value) {
305 return value<o.value ? -1 : 1;
308 if (symbolic && o.symbolic) {
309 // both symbolic: compare serials
310 if (serial==o.serial) {
313 return serial<o.serial ? -1 : 1;
316 // one symbolic, one value: value is sorted first
317 return o.symbolic ? -1 : 1;
320 bool idx::is_equal_same_type(const basic & other) const
322 GINAC_ASSERT(is_of_type(other,idx));
323 const idx &o = static_cast<const idx &>(other);
325 if (covariant != o.covariant) return false;
326 if (symbolic != o.symbolic) return false;
327 if (symbolic && o.symbolic) return serial==o.serial;
328 return value==o.value;
331 unsigned idx::calchash(void) const
333 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
334 setflag(status_flags::hash_calculated);
339 // new virtual functions which can be overridden by derived classes
344 /** Check whether the index forms a co-/contravariant pair with another
345 * index (i.e. same name/value but opposite co-/contravariance). */
346 bool idx::is_co_contra_pair(const basic & other) const
348 // like is_equal_same_type(), but tests for different covariant status
349 GINAC_ASSERT(is_of_type(other,idx));
350 const idx & o=static_cast<const idx &>(const_cast<basic &>(other));
352 if (covariant==o.covariant) return false;
353 if (symbolic!=o.symbolic) return false;
354 if (symbolic && o.symbolic) return serial==o.serial;
355 return value==o.value;
358 /** Toggle co-/contravariance of index. */
359 ex idx::toggle_covariant(void) const
361 idx * i_copy=static_cast<idx *>(duplicate());
362 i_copy->covariant = !i_copy->covariant;
363 i_copy->clearflag(status_flags::hash_calculated);
364 return i_copy->setflag(status_flags::dynallocated);
368 // non-virtual functions in this class
373 std::string & idx::autoname_prefix(void)
375 static std::string * s = new std::string("index");
380 // static member variables
385 unsigned idx::next_serial=0;
391 /** Bring a vector of indices into a canonic order. This operation only makes
392 * sense if the object carrying these indices is either symmetric or totally
393 * antisymmetric with respect to the indices.
395 * @param iv Index vector
396 * @param antisymmetric Whether the object carrying the indices is antisymmetric (symmetric otherwise)
397 * @return the sign introduced by the reordering of the indices. For symmetric
398 * objects this is always +1. For antisymmetric objects this is either
399 * +1 or -1 or 0 (if two equal indices were encountered). If the index
400 * vector was unchanged this function returns INT_MAX. */
401 int canonicalize_indices(exvector & iv, bool antisymmetric)
404 // nothing do to for 0 or 1 indices
408 bool something_changed=false;
411 // simple bubble sort algorithm should be sufficient for the small number of indices needed
412 exvector::const_iterator last_idx=iv.end();
413 exvector::const_iterator next_to_last_idx=iv.end()-1;
414 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
415 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
416 int cmpval=(*it1).compare(*it2);
419 something_changed=true;
420 if (antisymmetric) sig=-sig;
421 } else if ((cmpval==0) && antisymmetric) {
422 something_changed=true;
428 return something_changed ? sig : INT_MAX;
431 /** Build a vector of indices as the set intersection of two other index
432 * vectors (i.e. the returned vector contains the indices which appear in
433 * both source vectors). */
434 exvector idx_intersect(const exvector & iv1, const exvector & iv2)
436 // Create union vector
438 iv_union.reserve(iv1.size() + iv2.size());
439 iv_union.insert(iv_union.end(), iv1.begin(), iv1.end());
440 iv_union.insert(iv_union.end(), iv2.begin(), iv2.end());
443 canonicalize_indices(iv_union);
445 // Look for duplicates
446 exvector iv_intersect;
447 exvector::const_iterator cit = iv_union.begin(), citend = iv_union.end();
451 while (cit != citend) {
452 if (e.is_equal(*cit)) {
453 iv_intersect.push_back(e);
456 } while (cit != citend && e.is_equal(*cit));
465 /** Given a vector iv3 of three indices and a vector iv2 of two indices
466 * where iv2 is a subset of iv3, return the (free) index that is in iv3
467 * but not in iv2 and the sign introduced by permuting that index to the
470 * @param iv3 Vector of 3 indices
471 * @param iv2 Vector of 2 indices, must be a subset of iv3
472 * @param sig Returns the sign introduced by permuting the free index to the
473 * front if the object carrying the indices was antisymmetric (if
474 * it's symmetric, you can just ignore the returned value).
475 * @return the free index (the one that is in iv3 but not in iv2) */
476 ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2, int * sig)
478 // match (return value,iv2) to iv3 by permuting indices
479 // iv3 is always cyclic
481 GINAC_ASSERT(iv3.size()==3);
482 GINAC_ASSERT(iv2.size()==2);
486 #define TEST_PERMUTATION(A,B,C,P) \
487 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
492 TEST_PERMUTATION(0,1,2, 1);
493 TEST_PERMUTATION(0,2,1, -1);
494 TEST_PERMUTATION(1,0,2, -1);
495 TEST_PERMUTATION(1,2,0, 1);
496 TEST_PERMUTATION(2,0,1, 1);
497 TEST_PERMUTATION(2,1,0, -1);
498 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
501 /** Substitute one index in a vector of expressions.
503 * @param v Vector to substitute in (will be modified)
504 * @param is Index being substituted
505 * @param ir Index to replace by
506 * @return number of performed substitutions */
507 unsigned subs_index_in_exvector(exvector & v, const ex & is, const ex & ir)
509 exvector::iterator it;
510 unsigned replacements=0;
511 unsigned current_replacements;
513 GINAC_ASSERT(is_ex_of_type(is,idx));
514 GINAC_ASSERT(is_ex_of_type(ir,idx));
516 for (it=v.begin(); it!=v.end(); ++it) {
517 current_replacements=count_index(*it,is);
518 if (current_replacements>0) {
519 (*it)=(*it).subs(is==ir);
521 replacements += current_replacements;
526 /** Count number of times a given index appears in the index vector of an
529 * @param e Indexed object
530 * @param i Index to look for
531 * @return number of times the index was found */
532 unsigned count_index(const ex & e, const ex & i)
534 exvector idxv=e.get_indices();
536 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
537 if ((*cit).is_equal(i)) count++;
542 /** Substitute multiple indices in an expression.
544 * @param e Expression to substitute in
545 * @param idxv_subs Vector of indices being substituted
546 * @param idxv_repl Vector of indices to replace by (1:1 correspondence to idxv_subs)
547 * @return expression with substituted indices */
548 ex subs_indices(const ex & e, const exvector & idxv_subs, const exvector & idxv_repl)
550 GINAC_ASSERT(idxv_subs.size()==idxv_repl.size());
552 for (unsigned i=0; i<idxv_subs.size(); ++i) {
553 res=res.subs(idxv_subs[i]==idxv_repl[i]);
558 #ifndef NO_NAMESPACE_GINAC
560 #endif // ndef NO_NAMESPACE_GINAC