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="index"+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 = "index"+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 = "index" + 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 &>(const_cast<basic &>(other));
295 if (covariant!=o.covariant) {
296 // different co/contravariant
297 return covariant ? -1 : 1;
299 if ((!symbolic) && (!o.symbolic)) {
300 // non-symbolic, of equal type: compare values
301 if (value==o.value) {
304 return value<o.value ? -1 : 1;
306 if (symbolic && o.symbolic) {
307 // both symbolic: compare serials
308 if (serial==o.serial) {
311 return serial<o.serial ? -1 : 1;
313 // one symbolic, one value: value is sorted first
314 return o.symbolic ? -1 : 1;
317 bool idx::is_equal_same_type(const basic & other) const
319 GINAC_ASSERT(is_of_type(other,idx));
320 const idx & o=static_cast<const idx &>(const_cast<basic &>(other));
322 if (covariant!=o.covariant) return false;
323 if (symbolic!=o.symbolic) return false;
324 if (symbolic && o.symbolic) return serial==o.serial;
325 return value==o.value;
328 unsigned idx::calchash(void) const
330 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
331 setflag(status_flags::hash_calculated);
336 // new virtual functions which can be overridden by derived classes
341 /** Check whether the index forms a co-/contravariant pair with another
342 * index (i.e. same name/value but opposite co-/contravariance). */
343 bool idx::is_co_contra_pair(const basic & other) const
345 // like is_equal_same_type(), but tests for different covariant status
346 GINAC_ASSERT(is_of_type(other,idx));
347 const idx & o=static_cast<const idx &>(const_cast<basic &>(other));
349 if (covariant==o.covariant) return false;
350 if (symbolic!=o.symbolic) return false;
351 if (symbolic && o.symbolic) return serial==o.serial;
352 return value==o.value;
355 /** Toggle co-/contravariance of index. */
356 ex idx::toggle_covariant(void) const
358 idx * i_copy=static_cast<idx *>(duplicate());
359 i_copy->covariant = !i_copy->covariant;
360 i_copy->clearflag(status_flags::hash_calculated);
361 return i_copy->setflag(status_flags::dynallocated);
365 // non-virtual functions in this class
371 // static member variables
376 unsigned idx::next_serial=0;
383 const std::type_info & typeid_idx = typeid(some_idx);
389 /** Bring a vector of indices into a canonic order. This operation only makes
390 * sense if the object carrying these indices is either symmetric or totally
391 * antisymmetric with respect to the indices.
393 * @param iv Index vector
394 * @param antisymmetric Whether the object carrying the indices is antisymmetric (symmetric otherwise)
395 * @return the sign introduced by the reordering of the indices. For symmetric
396 * objects this is always +1. For antisymmetric objects this is either
397 * +1 or -1 or 0 (if two equal indices were encountered). If the index
398 * vector was unchanged this function returns INT_MAX. */
399 int canonicalize_indices(exvector & iv, bool antisymmetric)
402 // nothing do to for 0 or 1 indices
406 bool something_changed=false;
409 // simple bubble sort algorithm should be sufficient for the small number of indices needed
410 exvector::const_iterator last_idx=iv.end();
411 exvector::const_iterator next_to_last_idx=iv.end()-1;
412 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
413 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
414 int cmpval=(*it1).compare(*it2);
417 something_changed=true;
418 if (antisymmetric) sig=-sig;
419 } else if ((cmpval==0) && antisymmetric) {
420 something_changed=true;
426 return something_changed ? sig : INT_MAX;
429 /** Build a vector of indices as the set intersection of two other index
430 * vectors (i.e. the returned vector contains the indices which appear in
431 * both source vectors). */
432 exvector idx_intersect(const exvector & iv1, const exvector & iv2)
434 // Create union vector
436 iv_union.reserve(iv1.size() + iv2.size());
437 iv_union.insert(iv_union.end(), iv1.begin(), iv1.end());
438 iv_union.insert(iv_union.end(), iv2.begin(), iv2.end());
441 canonicalize_indices(iv_union);
443 // Look for duplicates
444 exvector iv_intersect;
445 exvector::const_iterator cit = iv_union.begin(), citend = iv_union.end();
449 while (cit != citend) {
450 if (e.is_equal(*cit)) {
451 iv_intersect.push_back(e);
454 } while (cit != citend && e.is_equal(*cit));
463 /** Given a vector iv3 of three indices and a vector iv2 of two indices
464 * where iv2 is a subset of iv3, return the (free) index that is in iv3
465 * but not in iv2 and the sign introduced by permuting that index to the
468 * @param iv3 Vector of 3 indices
469 * @param iv2 Vector of 2 indices, must be a subset of iv3
470 * @param sig Returns the sign introduced by permuting the free index to the
471 * front if the object carrying the indices was antisymmetric (if
472 * it's symmetric, you can just ignore the returned value).
473 * @return the free index (the one that is in iv3 but not in iv2) */
474 ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2, int * sig)
476 // match (return value,iv2) to iv3 by permuting indices
477 // iv3 is always cyclic
479 GINAC_ASSERT(iv3.size()==3);
480 GINAC_ASSERT(iv2.size()==2);
484 #define TEST_PERMUTATION(A,B,C,P) \
485 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
490 TEST_PERMUTATION(0,1,2, 1);
491 TEST_PERMUTATION(0,2,1, -1);
492 TEST_PERMUTATION(1,0,2, -1);
493 TEST_PERMUTATION(1,2,0, 1);
494 TEST_PERMUTATION(2,0,1, 1);
495 TEST_PERMUTATION(2,1,0, -1);
496 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
499 /** Substitute one index in a vector of expressions.
501 * @param v Vector to substitute in (will be modified)
502 * @param is Index being substituted
503 * @param ir Index to replace by
504 * @return number of performed substitutions */
505 unsigned subs_index_in_exvector(exvector & v, const ex & is, const ex & ir)
507 exvector::iterator it;
508 unsigned replacements=0;
509 unsigned current_replacements;
511 GINAC_ASSERT(is_ex_of_type(is,idx));
512 GINAC_ASSERT(is_ex_of_type(ir,idx));
514 for (it=v.begin(); it!=v.end(); ++it) {
515 current_replacements=count_index(*it,is);
516 if (current_replacements>0) {
517 (*it)=(*it).subs(is==ir);
519 replacements += current_replacements;
524 /** Count number of times a given index appears in the index vector of an
527 * @param e Indexed object
528 * @param i Index to look for
529 * @return number of times the index was found */
530 unsigned count_index(const ex & e, const ex & i)
532 exvector idxv=e.get_indices();
534 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
535 if ((*cit).is_equal(i)) count++;
540 /** Substitute multiple indices in an expression.
542 * @param e Expression to substitute in
543 * @param idxv_subs Vector of indices being substituted
544 * @param idxv_repl Vector of indices to replace by (1:1 correspondence to idxv_subs)
545 * @return expression with substituted indices */
546 ex subs_indices(const ex & e, const exvector & idxv_subs, const exvector & idxv_repl)
548 GINAC_ASSERT(idxv_subs.size()==idxv_repl.size());
550 for (unsigned i=0; i<idxv_subs.size(); ++i) {
551 res=res.subs(idxv_subs[i]==idxv_repl[i]);
556 #ifndef NO_NAMESPACE_GINAC
558 #endif // ndef NO_NAMESPACE_GINAC