if (num == 0)
return *this;
- basic *copy = duplicate();
- copy->setflag(status_flags::dynallocated);
- copy->clearflag(status_flags::hash_calculated | status_flags::expanded);
- for (size_t i=0; i<num; i++)
- copy->let_op(i) = f(copy->op(i));
- return *copy;
+ basic *copy = NULL;
+ for (size_t i=0; i<num; i++) {
+ const ex & o = op(i);
+ const ex & n = f(o);
+ if (!are_ex_trivially_equal(o, n)) {
+ if (copy == NULL)
+ copy = duplicate();
+ copy->let_op(i) = n;
+ }
+ }
+
+ if (copy) {
+ copy->setflag(status_flags::dynallocated);
+ copy->clearflag(status_flags::hash_calculated | status_flags::expanded);
+ return *copy;
+ } else
+ return *this;
}
/** Return degree of highest power in object s. */
struct expand_map_function : public map_function {
unsigned options;
expand_map_function(unsigned o) : options(o) {}
- ex operator()(const ex & e) { return expand(e, options); }
+ ex operator()(const ex & e) { return e.expand(options); }
};
/** Expand expression, i.e. multiply it out and return the result as a new
tinfo_key = TINFO_clifford;
}
-clifford::clifford(unsigned char rl, const ex & metr, const exvector & v, bool discardable) : inherited(sy_none(), v, discardable), representation_label(rl), metric(metr)
+clifford::clifford(unsigned char rl, const ex & metr, const exvector & v, bool discardable) : inherited(not_symmetric(), v, discardable), representation_label(rl), metric(metr)
{
tinfo_key = TINFO_clifford;
}
-clifford::clifford(unsigned char rl, const ex & metr, std::auto_ptr<exvector> vp) : inherited(sy_none(), vp), representation_label(rl), metric(metr)
+clifford::clifford(unsigned char rl, const ex & metr, std::auto_ptr<exvector> vp) : inherited(not_symmetric(), vp), representation_label(rl), metric(metr)
{
tinfo_key = TINFO_clifford;
}
ex dirac_ONE(unsigned char rl)
{
- return clifford(diracone(), rl);
+ static ex ONE = (new diracone)->setflag(status_flags::dynallocated);
+ return clifford(ONE, rl);
}
ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
ex dirac_gamma(const ex & mu, unsigned char rl)
{
+ static ex gamma = (new diracgamma)->setflag(status_flags::dynallocated);
+
if (!is_a<varidx>(mu))
throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
ex dim = ex_to<idx>(mu).get_dim();
- return clifford(diracgamma(), mu, lorentz_g(varidx((new symbol)->setflag(status_flags::dynallocated), dim),varidx((new symbol)->setflag(status_flags::dynallocated), dim)), rl);
+ return clifford(gamma, mu, lorentz_g(varidx((new symbol)->setflag(status_flags::dynallocated), dim),varidx((new symbol)->setflag(status_flags::dynallocated), dim)), rl);
}
ex dirac_gamma5(unsigned char rl)
{
- return clifford(diracgamma5(), rl);
+ static ex gamma5 = (new diracgamma5)->setflag(status_flags::dynallocated);
+ return clifford(gamma5, rl);
}
ex dirac_gammaL(unsigned char rl)
{
- return clifford(diracgammaL(), rl);
+ static ex gammaL = (new diracgammaL)->setflag(status_flags::dynallocated);
+ return clifford(gammaL, rl);
}
ex dirac_gammaR(unsigned char rl)
{
- return clifford(diracgammaR(), rl);
+ static ex gammaR = (new diracgammaR)->setflag(status_flags::dynallocated);
+ return clifford(gammaR, rl);
}
ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
tinfo_key = TINFO_color;
}
-color::color(unsigned char rl, const exvector & v, bool discardable) : inherited(sy_none(), v, discardable), representation_label(rl)
+color::color(unsigned char rl, const exvector & v, bool discardable) : inherited(not_symmetric(), v, discardable), representation_label(rl)
{
tinfo_key = TINFO_color;
}
-color::color(unsigned char rl, std::auto_ptr<exvector> vp) : inherited(sy_none(), vp), representation_label(rl)
+color::color(unsigned char rl, std::auto_ptr<exvector> vp) : inherited(not_symmetric(), vp), representation_label(rl)
{
tinfo_key = TINFO_color;
}
ex color_ONE(unsigned char rl)
{
- return color(su3one(), rl);
+ static ex ONE = (new su3one)->setflag(status_flags::dynallocated);
+ return color(ONE, rl);
}
ex color_T(const ex & a, unsigned char rl)
{
+ static ex t = (new su3t)->setflag(status_flags::dynallocated);
+
if (!is_a<idx>(a))
throw(std::invalid_argument("indices of color_T must be of type idx"));
if (!ex_to<idx>(a).get_dim().is_equal(8))
throw(std::invalid_argument("index dimension for color_T must be 8"));
- return color(su3t(), a, rl);
+ return color(t, a, rl);
}
ex color_f(const ex & a, const ex & b, const ex & c)
{
+ static ex f = (new su3f)->setflag(status_flags::dynallocated);
+
if (!is_a<idx>(a) || !is_a<idx>(b) || !is_a<idx>(c))
throw(std::invalid_argument("indices of color_f must be of type idx"));
if (!ex_to<idx>(a).get_dim().is_equal(8) || !ex_to<idx>(b).get_dim().is_equal(8) || !ex_to<idx>(c).get_dim().is_equal(8))
throw(std::invalid_argument("index dimension for color_f must be 8"));
- return indexed(su3f(), sy_anti(), a, b, c);
+ return indexed(f, antisymmetric3(), a, b, c);
}
ex color_d(const ex & a, const ex & b, const ex & c)
{
+ static ex d = (new su3d)->setflag(status_flags::dynallocated);
+
if (!is_a<idx>(a) || !is_a<idx>(b) || !is_a<idx>(c))
throw(std::invalid_argument("indices of color_d must be of type idx"));
if (!ex_to<idx>(a).get_dim().is_equal(8) || !ex_to<idx>(b).get_dim().is_equal(8) || !ex_to<idx>(c).get_dim().is_equal(8))
throw(std::invalid_argument("index dimension for color_d must be 8"));
- return indexed(su3d(), sy_symm(), a, b, c);
+ return indexed(d, symmetric3(), a, b, c);
}
ex color_h(const ex & a, const ex & b, const ex & c)
template <template <class> class C>
void container<C>::do_print_tree(const print_tree & c, unsigned level) const
{
- c.s << std::string(level, ' ') << class_name()
+ c.s << std::string(level, ' ') << class_name() << " @" << this
<< std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
<< ", nops=" << nops()
<< std::endl;
std::auto_ptr<typename container<C>::STLT> container<C>::subschildren(const exmap & m, unsigned options) const
{
// returns a NULL pointer if nothing had to be substituted
- // returns a pointer to a newly created epvector otherwise
- // (and relinquishes responsibility for the epvector)
+ // returns a pointer to a newly created STLT otherwise
+ // (and relinquishes responsibility for the STLT)
const_iterator cit = this->seq.begin(), end = this->seq.end();
while (cit != end) {
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
// default constructor
//////////
-indexed::indexed() : symtree(sy_none())
+indexed::indexed() : symtree(not_symmetric())
{
tinfo_key = TINFO_indexed;
}
// other constructors
//////////
-indexed::indexed(const ex & b) : inherited(b), symtree(sy_none())
+indexed::indexed(const ex & b) : inherited(b), symtree(not_symmetric())
{
tinfo_key = TINFO_indexed;
validate();
}
-indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symtree(sy_none())
+indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symtree(not_symmetric())
{
tinfo_key = TINFO_indexed;
validate();
}
-indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(sy_none())
+indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(not_symmetric())
{
tinfo_key = TINFO_indexed;
validate();
}
-indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(sy_none())
+indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(not_symmetric())
{
tinfo_key = TINFO_indexed;
validate();
}
-indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symtree(sy_none())
+indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symtree(not_symmetric())
{
tinfo_key = TINFO_indexed;
validate();
validate();
}
-indexed::indexed(const ex & b, const exvector & v) : inherited(b), symtree(sy_none())
+indexed::indexed(const ex & b, const exvector & v) : inherited(b), symtree(not_symmetric())
{
seq.insert(seq.end(), v.begin(), v.end());
tinfo_key = TINFO_indexed;
symtree = sy_anti();
break;
default:
- symtree = sy_none();
+ symtree = not_symmetric();
break;
}
const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
ex ncmul::expand(unsigned options) const
{
// First, expand the children
- exvector expanded_seq = expandchildren(options);
+ std::auto_ptr<exvector> vp = expandchildren(options);
+ const exvector &expanded_seq = vp.get() ? *vp : this->seq;
// Now, look for all the factors that are sums and remember their
// position and number of terms.
}
// If there are no sums, we are done
- if (number_of_adds == 0)
- return (new ncmul(expanded_seq, true))->
- setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
+ if (number_of_adds == 0) {
+ if (vp.get())
+ return (new ncmul(vp))->
+ setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
+ else
+ return *this;
+ }
// Now, form all possible products of the terms of the sums with the
// remaining factors, and add them together
// non-virtual functions in this class
//////////
-exvector ncmul::expandchildren(unsigned options) const
+std::auto_ptr<exvector> ncmul::expandchildren(unsigned options) const
{
- exvector s;
- s.reserve(seq.size());
- exvector::const_iterator it = seq.begin(), itend = seq.end();
- while (it != itend) {
- s.push_back(it->expand(options));
- it++;
+ const_iterator cit = this->seq.begin(), end = this->seq.end();
+ while (cit != end) {
+ const ex & expanded_ex = cit->expand(options);
+ if (!are_ex_trivially_equal(*cit, expanded_ex)) {
+
+ // copy first part of seq which hasn't changed
+ std::auto_ptr<exvector> s(new exvector(this->seq.begin(), cit));
+ reserve(*s, this->seq.size());
+
+ // insert changed element
+ s->push_back(expanded_ex);
+ ++cit;
+
+ // copy rest
+ while (cit != end) {
+ s->push_back(cit->expand(options));
+ ++cit;
+ }
+
+ return s;
+ }
+
+ ++cit;
}
- return s;
+
+ return std::auto_ptr<exvector>(0); // nothing has changed
}
const exvector & ncmul::get_factors() const
void do_print_csrc(const print_context & c, unsigned level) const;
size_t count_factors(const ex & e) const;
void append_factors(exvector & v, const ex & e) const;
- exvector expandchildren(unsigned options) const;
+ std::auto_ptr<exvector> expandchildren(unsigned options) const;
public:
const exvector & get_factors() const;
};
ex power::map(map_function & f) const
{
- return (new power(f(basis), f(exponent)))->setflag(status_flags::dynallocated);
+ const ex &mapped_basis = f(basis);
+ const ex &mapped_exponent = f(exponent);
+
+ if (!are_ex_trivially_equal(basis, mapped_basis)
+ || !are_ex_trivially_equal(exponent, mapped_exponent))
+ return (new power(mapped_basis, mapped_exponent))->setflag(status_flags::dynallocated);
+ else
+ return *this;
}
int power::degree(const ex & s) const
ex relational::map(map_function & f) const
{
- return (new relational(f(lh), f(rh), o))->setflag(status_flags::dynallocated);
+ const ex &mapped_lh = f(lh);
+ const ex &mapped_rh = f(rh);
+
+ if (!are_ex_trivially_equal(lh, mapped_lh)
+ || !are_ex_trivially_equal(rh, mapped_rh))
+ return (new relational(mapped_lh, mapped_rh, o))->setflag(status_flags::dynallocated);
+ else
+ return *this;
}
ex relational::eval(int level) const
// global functions
//////////
+static const symmetry & index0()
+{
+ static ex s = (new symmetry(0))->setflag(status_flags::dynallocated);
+ return ex_to<symmetry>(s);
+}
+
+static const symmetry & index1()
+{
+ static ex s = (new symmetry(1))->setflag(status_flags::dynallocated);
+ return ex_to<symmetry>(s);
+}
+
+static const symmetry & index2()
+{
+ static ex s = (new symmetry(2))->setflag(status_flags::dynallocated);
+ return ex_to<symmetry>(s);
+}
+
+static const symmetry & index3()
+{
+ static ex s = (new symmetry(3))->setflag(status_flags::dynallocated);
+ return ex_to<symmetry>(s);
+}
+
+const symmetry & not_symmetric()
+{
+ static ex s = (new symmetry)->setflag(status_flags::dynallocated);
+ return ex_to<symmetry>(s);
+}
+
+const symmetry & symmetric2()
+{
+ static ex s = (new symmetry(symmetry::symmetric, index0(), index1()))->setflag(status_flags::dynallocated);
+ return ex_to<symmetry>(s);
+}
+
+const symmetry & symmetric3()
+{
+ static ex s = (new symmetry(symmetry::symmetric, index0(), index1()))->add(index2()).setflag(status_flags::dynallocated);
+ return ex_to<symmetry>(s);
+}
+
+const symmetry & symmetric4()
+{
+ static ex s = (new symmetry(symmetry::symmetric, index0(), index1()))->add(index2()).add(index3()).setflag(status_flags::dynallocated);
+ return ex_to<symmetry>(s);
+}
+
+const symmetry & antisymmetric2()
+{
+ static ex s = (new symmetry(symmetry::antisymmetric, index0(), index1()))->setflag(status_flags::dynallocated);
+ return ex_to<symmetry>(s);
+}
+
+const symmetry & antisymmetric3()
+{
+ static ex s = (new symmetry(symmetry::antisymmetric, index0(), index1()))->add(index2()).setflag(status_flags::dynallocated);
+ return ex_to<symmetry>(s);
+}
+
+const symmetry & antisymmetric4()
+{
+ static ex s = (new symmetry(symmetry::antisymmetric, index0(), index1()))->add(index2()).add(index3()).setflag(status_flags::dynallocated);
+ return ex_to<symmetry>(s);
+}
+
class sy_is_less : public std::binary_function<ex, ex, bool> {
exvector::iterator v;
inline symmetry sy_cycl(const symmetry &c1, const symmetry &c2, const symmetry &c3) { return symmetry(symmetry::cyclic, c1, c2).add(c3); }
inline symmetry sy_cycl(const symmetry &c1, const symmetry &c2, const symmetry &c3, const symmetry &c4) { return symmetry(symmetry::cyclic, c1, c2).add(c3).add(c4); }
+// These return references to preallocated common symmetries (similar to
+// the numeric flyweights).
+const symmetry & not_symmetric();
+const symmetry & symmetric2();
+const symmetry & symmetric3();
+const symmetry & symmetric4();
+const symmetry & antisymmetric2();
+const symmetry & antisymmetric3();
+const symmetry & antisymmetric4();
+
/** Canonicalize the order of elements of an expression vector, according to
* the symmetry properties defined in a symmetry tree.
*
ex delta_tensor(const ex & i1, const ex & i2)
{
+ static ex delta = (new tensdelta)->setflag(status_flags::dynallocated);
+
if (!is_a<idx>(i1) || !is_a<idx>(i2))
throw(std::invalid_argument("indices of delta tensor must be of type idx"));
- return indexed(tensdelta(), sy_symm(), i1, i2);
+ return indexed(delta, symmetric2(), i1, i2);
}
ex metric_tensor(const ex & i1, const ex & i2)
{
+ static ex metric = (new tensmetric)->setflag(status_flags::dynallocated);
+
if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
- return indexed(tensmetric(), sy_symm(), i1, i2);
+ return indexed(metric, symmetric2(), i1, i2);
}
ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
{
+ static ex metric_neg = (new minkmetric(false))->setflag(status_flags::dynallocated);
+ static ex metric_pos = (new minkmetric(true))->setflag(status_flags::dynallocated);
+
if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
- return indexed(minkmetric(pos_sig), sy_symm(), i1, i2);
+ return indexed(pos_sig ? metric_pos : metric_neg, symmetric2(), i1, i2);
}
ex spinor_metric(const ex & i1, const ex & i2)
{
+ static ex metric = (new spinmetric)->setflag(status_flags::dynallocated);
+
if (!is_a<spinidx>(i1) || !is_a<spinidx>(i2))
throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
if (!ex_to<idx>(i1).get_dim().is_equal(2) || !ex_to<idx>(i2).get_dim().is_equal(2))
throw(std::runtime_error("index dimension for spinor metric must be 2"));
- return indexed(spinmetric(), sy_anti(), i1, i2);
+ return indexed(metric, antisymmetric2(), i1, i2);
}
ex epsilon_tensor(const ex & i1, const ex & i2)
{
+ static ex epsilon = (new tensepsilon)->setflag(status_flags::dynallocated);
+
if (!is_a<idx>(i1) || !is_a<idx>(i2))
throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
if (!ex_to<idx>(i1).get_dim().is_equal(_ex2))
throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
- return indexed(tensepsilon(), sy_anti(), i1, i2);
+ return indexed(epsilon, antisymmetric2(), i1, i2);
}
ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
{
+ static ex epsilon = (new tensepsilon)->setflag(status_flags::dynallocated);
+
if (!is_a<idx>(i1) || !is_a<idx>(i2) || !is_a<idx>(i3))
throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
if (!ex_to<idx>(i1).get_dim().is_equal(_ex3))
throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
- return indexed(tensepsilon(), sy_anti(), i1, i2, i3);
+ return indexed(epsilon, antisymmetric3(), i1, i2, i3);
}
ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
{
+ static ex epsilon_neg = (new tensepsilon(true, false))->setflag(status_flags::dynallocated);
+ static ex epsilon_pos = (new tensepsilon(true, true))->setflag(status_flags::dynallocated);
+
if (!is_a<varidx>(i1) || !is_a<varidx>(i2) || !is_a<varidx>(i3) || !is_a<varidx>(i4))
throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
if (!ex_to<idx>(i1).get_dim().is_equal(_ex4))
throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
- return indexed(tensepsilon(true, pos_sig), sy_anti(), i1, i2, i3, i4);
+ return indexed(pos_sig ? epsilon_pos : epsilon_neg, antisymmetric4(), i1, i2, i3, i4);
}
} // namespace GiNaC
// Helper macros for class implementations (mostly useful for trivial classes)
#define DEFAULT_CTOR(classname) \
-classname::classname() : inherited(TINFO_##classname) {}
+classname::classname() : inherited(TINFO_##classname) { setflag(status_flags::evaluated | status_flags::expanded); }
#define DEFAULT_UNARCHIVE(classname) \
ex classname::unarchive(const archive_node &n, lst &sym_lst) \