3 * Implementation of GiNaC's indices. */
6 * GiNaC Copyright (C) 1999-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "relational.h"
31 #include "operators.h"
37 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(idx, basic,
38 print_func<print_context>(&idx::do_print).
39 print_func<print_latex>(&idx::do_print_latex).
40 print_func<print_csrc>(&idx::do_print_csrc).
41 print_func<print_tree>(&idx::do_print_tree))
43 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(varidx, idx,
44 print_func<print_context>(&varidx::do_print).
45 print_func<print_latex>(&varidx::do_print_latex).
46 print_func<print_tree>(&varidx::do_print_tree))
48 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(spinidx, varidx,
49 print_func<print_context>(&spinidx::do_print).
50 print_func<print_latex>(&spinidx::do_print_latex).
51 print_func<print_tree>(&spinidx::do_print_tree))
54 // default constructor
57 idx::idx() : inherited(&idx::tinfo_static) {}
59 varidx::varidx() : covariant(false)
61 tinfo_key = &varidx::tinfo_static;
64 spinidx::spinidx() : dotted(false)
66 tinfo_key = &spinidx::tinfo_static;
73 idx::idx(const ex & v, const ex & d) : inherited(&idx::tinfo_static), value(v), dim(d)
76 if (!dim.info(info_flags::posint))
77 throw(std::invalid_argument("dimension of space must be a positive integer"));
80 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
82 tinfo_key = &varidx::tinfo_static;
85 spinidx::spinidx(const ex & v, const ex & d, bool cov, bool dot) : inherited(v, d, cov), dotted(dot)
87 tinfo_key = &spinidx::tinfo_static;
94 idx::idx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
96 n.find_ex("value", value, sym_lst);
97 n.find_ex("dim", dim, sym_lst);
100 varidx::varidx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
102 n.find_bool("covariant", covariant);
105 spinidx::spinidx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
107 n.find_bool("dotted", dotted);
110 void idx::archive(archive_node &n) const
112 inherited::archive(n);
113 n.add_ex("value", value);
114 n.add_ex("dim", dim);
117 void varidx::archive(archive_node &n) const
119 inherited::archive(n);
120 n.add_bool("covariant", covariant);
123 void spinidx::archive(archive_node &n) const
125 inherited::archive(n);
126 n.add_bool("dotted", dotted);
129 DEFAULT_UNARCHIVE(idx)
130 DEFAULT_UNARCHIVE(varidx)
131 DEFAULT_UNARCHIVE(spinidx)
134 // functions overriding virtual functions from base classes
137 void idx::print_index(const print_context & c, unsigned level) const
139 bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(value));
145 if (c.options & print_options::print_index_dimensions) {
152 void idx::do_print(const print_context & c, unsigned level) const
155 print_index(c, level);
158 void idx::do_print_latex(const print_latex & c, unsigned level) const
161 print_index(c, level);
165 void idx::do_print_csrc(const print_csrc & c, unsigned level) const
168 if (value.info(info_flags::integer))
169 c.s << ex_to<numeric>(value).to_int();
175 void idx::do_print_tree(const print_tree & c, unsigned level) const
177 c.s << std::string(level, ' ') << class_name() << " @" << this
178 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
180 value.print(c, level + c.delta_indent);
181 dim.print(c, level + c.delta_indent);
184 void varidx::do_print(const print_context & c, unsigned level) const
190 print_index(c, level);
193 void varidx::do_print_tree(const print_tree & c, unsigned level) const
195 c.s << std::string(level, ' ') << class_name() << " @" << this
196 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
197 << (covariant ? ", covariant" : ", contravariant")
199 value.print(c, level + c.delta_indent);
200 dim.print(c, level + c.delta_indent);
203 void spinidx::do_print(const print_context & c, unsigned level) const
211 print_index(c, level);
214 void spinidx::do_print_latex(const print_latex & c, unsigned level) const
220 print_index(c, level);
224 void spinidx::do_print_tree(const print_tree & c, unsigned level) const
226 c.s << std::string(level, ' ') << class_name() << " @" << this
227 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
228 << (covariant ? ", covariant" : ", contravariant")
229 << (dotted ? ", dotted" : ", undotted")
231 value.print(c, level + c.delta_indent);
232 dim.print(c, level + c.delta_indent);
235 bool idx::info(unsigned inf) const
238 case info_flags::idx:
239 case info_flags::has_indices:
242 return inherited::info(inf);
245 size_t idx::nops() const
247 // don't count the dimension as that is not really a sub-expression
251 ex idx::op(size_t i) const
253 GINAC_ASSERT(i == 0);
257 ex idx::map(map_function & f) const
259 const ex &mapped_value = f(value);
260 if (are_ex_trivially_equal(value, mapped_value))
263 idx *copy = duplicate();
264 copy->setflag(status_flags::dynallocated);
265 copy->clearflag(status_flags::hash_calculated);
266 copy->value = mapped_value;
271 /** Returns order relation between two indices of the same type. The order
272 * must be such that dummy indices lie next to each other. */
273 int idx::compare_same_type(const basic & other) const
275 GINAC_ASSERT(is_a<idx>(other));
276 const idx &o = static_cast<const idx &>(other);
278 int cmpval = value.compare(o.value);
281 return dim.compare(o.dim);
284 bool idx::match_same_type(const basic & other) const
286 GINAC_ASSERT(is_a<idx>(other));
287 const idx &o = static_cast<const idx &>(other);
289 return dim.is_equal(o.dim);
292 int varidx::compare_same_type(const basic & other) const
294 GINAC_ASSERT(is_a<varidx>(other));
295 const varidx &o = static_cast<const varidx &>(other);
297 int cmpval = inherited::compare_same_type(other);
301 // Check variance last so dummy indices will end up next to each other
302 if (covariant != o.covariant)
303 return covariant ? -1 : 1;
308 bool varidx::match_same_type(const basic & other) const
310 GINAC_ASSERT(is_a<varidx>(other));
311 const varidx &o = static_cast<const varidx &>(other);
313 if (covariant != o.covariant)
316 return inherited::match_same_type(other);
319 int spinidx::compare_same_type(const basic & other) const
321 GINAC_ASSERT(is_a<spinidx>(other));
322 const spinidx &o = static_cast<const spinidx &>(other);
324 // Check dottedness first so dummy indices will end up next to each other
325 if (dotted != o.dotted)
326 return dotted ? -1 : 1;
328 int cmpval = inherited::compare_same_type(other);
335 bool spinidx::match_same_type(const basic & other) const
337 GINAC_ASSERT(is_a<spinidx>(other));
338 const spinidx &o = static_cast<const spinidx &>(other);
340 if (dotted != o.dotted)
342 return inherited::match_same_type(other);
345 unsigned idx::calchash() const
347 // NOTE: The code in simplify_indexed() assumes that canonically
348 // ordered sequences of indices have the two members of dummy index
349 // pairs lying next to each other. The hash values for indices must
350 // be devised accordingly. The easiest (only?) way to guarantee the
351 // desired ordering is to make indices with the same value have equal
352 // hash keys. That is, the hash values must not depend on the index
353 // dimensions or other attributes (variance etc.).
354 // The compare_same_type() methods will take care of the rest.
355 const void* this_tinfo = (const void*)(typeid(*this).name());
356 unsigned v = golden_ratio_hash((p_int)this_tinfo);
358 v ^= value.gethash();
360 // Store calculated hash value only if object is already evaluated
361 if (flags & status_flags::evaluated) {
362 setflag(status_flags::hash_calculated);
369 /** By default, basic::evalf would evaluate the index value but we don't want
370 * a.1 to become a.(1.0). */
371 ex idx::evalf(int level) const
376 ex idx::subs(const exmap & m, unsigned options) const
378 // First look for index substitutions
379 exmap::const_iterator it = m.find(*this);
382 // Substitution index->index
383 if (is_a<idx>(it->second) || (options & subs_options::really_subs_idx))
386 // Otherwise substitute value
387 idx *i_copy = duplicate();
388 i_copy->value = it->second;
389 i_copy->clearflag(status_flags::hash_calculated);
390 return i_copy->setflag(status_flags::dynallocated);
393 // None, substitute objects in value (not in dimension)
394 const ex &subsed_value = value.subs(m, options);
395 if (are_ex_trivially_equal(value, subsed_value))
398 idx *i_copy = duplicate();
399 i_copy->value = subsed_value;
400 i_copy->clearflag(status_flags::hash_calculated);
401 return i_copy->setflag(status_flags::dynallocated);
404 /** Implementation of ex::diff() for an index always returns 0.
407 ex idx::derivative(const symbol & s) const
413 // new virtual functions
416 bool idx::is_dummy_pair_same_type(const basic & other) const
418 const idx &o = static_cast<const idx &>(other);
420 // Only pure symbols form dummy pairs, "2n+1" doesn't
421 if (!is_a<symbol>(value))
424 // Value must be equal, of course
425 if (!value.is_equal(o.value))
428 // Dimensions need not be equal but must be comparable (so we can
429 // determine the minimum dimension of contractions)
430 if (dim.is_equal(o.dim))
433 return is_exactly_a<numeric>(dim) || is_exactly_a<numeric>(o.dim);
436 bool varidx::is_dummy_pair_same_type(const basic & other) const
438 const varidx &o = static_cast<const varidx &>(other);
440 // Variance must be opposite
441 if (covariant == o.covariant)
444 return inherited::is_dummy_pair_same_type(other);
447 bool spinidx::is_dummy_pair_same_type(const basic & other) const
449 const spinidx &o = static_cast<const spinidx &>(other);
451 // Dottedness must be the same
452 if (dotted != o.dotted)
455 return inherited::is_dummy_pair_same_type(other);
460 // non-virtual functions
463 ex idx::replace_dim(const ex & new_dim) const
465 idx *i_copy = duplicate();
466 i_copy->dim = new_dim;
467 i_copy->clearflag(status_flags::hash_calculated);
468 return i_copy->setflag(status_flags::dynallocated);
471 ex idx::minimal_dim(const idx & other) const
473 return GiNaC::minimal_dim(dim, other.dim);
476 ex varidx::toggle_variance() const
478 varidx *i_copy = duplicate();
479 i_copy->covariant = !i_copy->covariant;
480 i_copy->clearflag(status_flags::hash_calculated);
481 return i_copy->setflag(status_flags::dynallocated);
484 ex spinidx::toggle_dot() const
486 spinidx *i_copy = duplicate();
487 i_copy->dotted = !i_copy->dotted;
488 i_copy->clearflag(status_flags::hash_calculated);
489 return i_copy->setflag(status_flags::dynallocated);
492 ex spinidx::toggle_variance_dot() const
494 spinidx *i_copy = duplicate();
495 i_copy->covariant = !i_copy->covariant;
496 i_copy->dotted = !i_copy->dotted;
497 i_copy->clearflag(status_flags::hash_calculated);
498 return i_copy->setflag(status_flags::dynallocated);
505 bool is_dummy_pair(const idx & i1, const idx & i2)
507 // The indices must be of exactly the same type
508 if (typeid(i1) != typeid(i2))
511 // Same type, let the indices decide whether they are paired
512 return i1.is_dummy_pair_same_type(i2);
515 bool is_dummy_pair(const ex & e1, const ex & e2)
517 // The expressions must be indices
518 if (!is_a<idx>(e1) || !is_a<idx>(e2))
521 return is_dummy_pair(ex_to<idx>(e1), ex_to<idx>(e2));
524 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
529 // No indices? Then do nothing
533 // Only one index? Then it is a free one if it's not numeric
534 if (itend - it == 1) {
535 if (ex_to<idx>(*it).is_symbolic())
536 out_free.push_back(*it);
540 // Sort index vector. This will cause dummy indices come to lie next
541 // to each other (because the sort order is defined to guarantee this).
542 exvector v(it, itend);
543 shaker_sort(v.begin(), v.end(), ex_is_less(), ex_swap());
545 // Find dummy pairs and free indices
546 it = v.begin(); itend = v.end();
547 exvector::const_iterator last = it++;
548 while (it != itend) {
549 if (is_dummy_pair(*it, *last)) {
550 out_dummy.push_back(*last);
555 if (!it->is_equal(*last) && ex_to<idx>(*last).is_symbolic())
556 out_free.push_back(*last);
560 if (ex_to<idx>(*last).is_symbolic())
561 out_free.push_back(*last);
564 ex minimal_dim(const ex & dim1, const ex & dim2)
566 if (dim1.is_equal(dim2) || dim1 < dim2 || (is_exactly_a<numeric>(dim1) && !is_a<numeric>(dim2)))
568 else if (dim1 > dim2 || (!is_a<numeric>(dim1) && is_exactly_a<numeric>(dim2)))
571 std::ostringstream s;
572 s << "minimal_dim(): index dimensions " << dim1 << " and " << dim2 << " cannot be ordered";
573 throw (std::runtime_error(s.str()));