* Implementation of GiNaC's indices. */
/*
- * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
+ * GiNaC Copyright (C) 1999-2004 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
#include "lst.h"
#include "relational.h"
#include "operators.h"
-#include "print.h"
#include "archive.h"
#include "utils.h"
namespace GiNaC {
-GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
-GINAC_IMPLEMENT_REGISTERED_CLASS(varidx, idx)
-GINAC_IMPLEMENT_REGISTERED_CLASS(spinidx, varidx)
+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_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
// functions overriding virtual functions from base classes
//////////
-void idx::print(const print_context & c, unsigned level) const
-{
- if (is_a<print_tree>(c)) {
-
- c.s << std::string(level, ' ') << class_name()
- << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
- << std::endl;
- unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
- value.print(c, level + delta_indent);
- dim.print(c, level + delta_indent);
-
- } else {
-
- if (is_a<print_latex>(c))
- c.s << "{";
- else
- c.s << ".";
- 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 << "]";
- }
- if (is_a<print_latex>(c))
- c.s << "}";
+void idx::print_index(const print_context & c, unsigned level) const
+{
+ 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 << "]";
}
}
-void varidx::print(const print_context & c, unsigned level) const
+void idx::do_print(const print_context & c, unsigned level) const
{
- if (is_a<print_tree>(c)) {
+ c.s << ".";
+ print_index(c, level);
+}
- c.s << std::string(level, ' ') << class_name()
- << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
- << (covariant ? ", covariant" : ", contravariant")
- << std::endl;
- unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
- value.print(c, level + delta_indent);
- dim.print(c, level + delta_indent);
+void idx::do_print_latex(const print_latex & c, unsigned level) const
+{
+ c.s << "{";
+ print_index(c, level);
+ c.s << "}";
+}
- } else {
- if (is_a<print_latex>(c))
- c.s << "{";
- else {
- if (covariant)
- c.s << ".";
- else
- c.s << "~";
- }
- 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 << "]";
- }
- if (is_a<print_latex>(c))
- c.s << "}";
- }
+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);
}
-void spinidx::print(const print_context & c, unsigned level) const
+void varidx::do_print(const print_context & c, unsigned level) const
{
- if (is_a<print_tree>(c)) {
+ if (covariant)
+ c.s << ".";
+ else
+ c.s << "~";
+ print_index(c, level);
+}
- c.s << std::string(level, ' ') << class_name()
- << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
- << (covariant ? ", covariant" : ", contravariant")
- << (dotted ? ", dotted" : ", undotted")
- << std::endl;
- unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
- value.print(c, level + delta_indent);
- dim.print(c, level + delta_indent);
+void varidx::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")
+ << std::endl;
+ value.print(c, level + c.delta_indent);
+ dim.print(c, level + c.delta_indent);
+}
- } else {
+void spinidx::do_print(const print_context & c, unsigned level) const
+{
+ if (covariant)
+ c.s << ".";
+ else
+ c.s << "~";
+ if (dotted)
+ c.s << "*";
+ print_index(c, level);
+}
- bool is_tex = is_a<print_latex>(c);
- if (is_tex) {
- if (covariant)
- c.s << "_{";
- else
- c.s << "^{";
- } else {
- if (covariant)
- c.s << ".";
- else
- c.s << "~";
- }
- if (dotted) {
- if (is_tex)
- c.s << "\\dot{";
- else
- c.s << "*";
- }
- 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 (is_tex && dotted)
- c.s << "}";
- if (is_tex)
- c.s << "}";
- }
+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 << "}";
+}
+
+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);
}
bool idx::info(unsigned inf) const
ex idx::map(map_function & f) const
{
- idx *copy = duplicate();
- copy->setflag(status_flags::dynallocated);
- copy->clearflag(status_flags::hash_calculated);
- copy->value = f(value);
- return *copy;
+ 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;
+ }
}
/** Returns order relation between two indices of the same type. The order
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.
unsigned v = golden_ratio_hash(tinfo());
v = rotate_left(v);
v ^= value.gethash();
- v = rotate_left(v);
- v ^= dim.gethash();
// Store calculated hash value only if object is already evaluated
if (flags & status_flags::evaluated) {