]> www.ginac.de Git - ginac.git/commitdiff
synced to 1.2 (memory conservation)
authorChristian Bauer <Christian.Bauer@uni-mainz.de>
Thu, 10 Jun 2004 02:30:29 +0000 (02:30 +0000)
committerChristian Bauer <Christian.Bauer@uni-mainz.de>
Thu, 10 Jun 2004 02:30:29 +0000 (02:30 +0000)
14 files changed:
ginac/basic.cpp
ginac/clifford.cpp
ginac/color.cpp
ginac/container.h
ginac/idx.cpp
ginac/indexed.cpp
ginac/ncmul.cpp
ginac/ncmul.h
ginac/power.cpp
ginac/relational.cpp
ginac/symmetry.cpp
ginac/symmetry.h
ginac/tensor.cpp
ginac/utils.h

index 7a0634760354727c77aa06166e1922764b6f8338..17f265debbe27f9b5c35d07b0599ed108c6469c5 100644 (file)
@@ -307,12 +307,23 @@ ex basic::map(map_function & f) const
        if (num == 0)
                return *this;
 
        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. */
 }
 
 /** Return degree of highest power in object s. */
@@ -781,7 +792,7 @@ unsigned basic::calchash() const
 struct expand_map_function : public map_function {
        unsigned options;
        expand_map_function(unsigned o) : options(o) {}
 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
 };
 
 /** Expand expression, i.e. multiply it out and return the result as a new
index a3574029a4a6723767c9251db3e3c7200f6c743d..afc9989486bdee52218eac467e7eec32f6893329 100644 (file)
@@ -106,12 +106,12 @@ clifford::clifford(const ex & b, const ex & mu, const ex & metr, unsigned char r
        tinfo_key = TINFO_clifford;
 }
 
        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;
 }
 
 {
        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;
 }
 {
        tinfo_key = TINFO_clifford;
 }
@@ -634,7 +634,8 @@ ex diracgammaR::conjugate() const
 
 ex dirac_ONE(unsigned char rl)
 {
 
 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 clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
@@ -654,26 +655,31 @@ ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
 
 ex dirac_gamma(const ex & mu, 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();
        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)
 {
 }
 
 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)
 {
 }
 
 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)
 {
 }
 
 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)
 }
 
 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
index 600da082f06a575b41ddd5c63446422289680af3..e06eff44a43c659ef7bb7567f6dc04fdffc03961 100644 (file)
@@ -89,12 +89,12 @@ color::color(const ex & b, const ex & i1, unsigned char rl) : inherited(b, i1),
        tinfo_key = TINFO_color;
 }
 
        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;
 }
 
 {
        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;
 }
 {
        tinfo_key = TINFO_color;
 }
@@ -475,37 +475,44 @@ bool su3f::contract_with(exvector::iterator self, exvector::iterator other, exve
 
 ex color_ONE(unsigned char rl)
 {
 
 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)
 {
 }
 
 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"));
 
        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)
 {
 }
 
 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"));
 
        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)
 {
 }
 
 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"));
 
        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)
 }
 
 ex color_h(const ex & a, const ex & b, const ex & c)
index 11b5be0bbec78740b15b2d15dd5d0b68a71eb894..6161a20b9504c50b6997d78ea313fbd6faaf8d22 100644 (file)
@@ -502,7 +502,7 @@ void container<C>::do_print(const print_context & c, unsigned level) const
 template <template <class> class C>
 void container<C>::do_print_tree(const print_tree & c, unsigned level) const
 {
 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::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
            << ", nops=" << nops()
            << std::endl;
@@ -724,8 +724,8 @@ template <template <class> class C>
 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
 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) {
 
        const_iterator cit = this->seq.begin(), end = this->seq.end();
        while (cit != end) {
index 1aba28bf302c0c20abe672a718761bcc0c798fd5..c95c07c72fc87ef01655d157ef256e21a587bf78 100644 (file)
@@ -242,11 +242,16 @@ ex idx::op(size_t i) const
 
 ex idx::map(map_function & f) 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
 }
 
 /** Returns order relation between two indices of the same type. The order
index 40a6232c1ecdcb02f5f678acd616b79d21b76138..d1d5f7287b07d01a723f22adf0e21c37ffdcb15d 100644 (file)
@@ -48,7 +48,7 @@ GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(indexed, exprseq,
 // default constructor
 //////////
 
 // default constructor
 //////////
 
-indexed::indexed() : symtree(sy_none())
+indexed::indexed() : symtree(not_symmetric())
 {
        tinfo_key = TINFO_indexed;
 }
 {
        tinfo_key = TINFO_indexed;
 }
@@ -57,31 +57,31 @@ indexed::indexed() : symtree(sy_none())
 // other constructors
 //////////
 
 // 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();
 }
 
 {
        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();
 }
 
 {
        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();
 }
 
 {
        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();
 }
 
 {
        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();
 {
        tinfo_key = TINFO_indexed;
        validate();
@@ -105,7 +105,7 @@ indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex &
        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;
 {
        seq.insert(seq.end(), v.begin(), v.end());
        tinfo_key = TINFO_indexed;
@@ -152,7 +152,7 @@ indexed::indexed(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
                                symtree = sy_anti();
                                break;
                        default:
                                symtree = sy_anti();
                                break;
                        default:
-                               symtree = sy_none();
+                               symtree = not_symmetric();
                                break;
                }
                const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
                                break;
                }
                const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
index 005cc16f77369b29d67c9a93550a473d2ce4b566..3bc8b2145766e2755e6cc8060e10a9c892d2f4d2 100644 (file)
@@ -127,7 +127,8 @@ typedef std::vector<int> intvector;
 ex ncmul::expand(unsigned options) const
 {
        // First, expand the children
 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.
        
        // Now, look for all the factors that are sums and remember their
        // position and number of terms.
@@ -151,9 +152,13 @@ ex ncmul::expand(unsigned options) const
        }
 
        // If there are no sums, we are done
        }
 
        // 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
 
        // Now, form all possible products of the terms of the sums with the
        // remaining factors, and add them together
@@ -561,16 +566,34 @@ unsigned ncmul::return_type_tinfo() const
 // non-virtual functions in this class
 //////////
 
 // 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
 }
 
 const exvector & ncmul::get_factors() const
index 46a7aae1bd101da1f86cab802e0bb4d4bb6a9364..436ae8c018e900eb7ae0174bb588ed215e637b1f 100644 (file)
@@ -80,7 +80,7 @@ protected:
        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;
        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;
 };
 public:
        const exvector & get_factors() const;
 };
index aa0b082a7970b45faa10f4f06716e3bcae279c7b..309d529a69cffebb3825f96e2d1bcd13e6ff0916 100644 (file)
@@ -246,7 +246,14 @@ ex power::op(size_t i) const
 
 ex power::map(map_function & f) 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
 }
 
 int power::degree(const ex & s) const
index 87acde42fe24b89e975167da4dd243716d411fe3..86e2c2e8399bde1fbea5eb4db966e69aa2870f27 100644 (file)
@@ -164,7 +164,14 @@ ex relational::op(size_t i) const
 
 ex relational::map(map_function & f) 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
 }
 
 ex relational::eval(int level) const
index 637e5abe1c3c83faf59e8b7102441e79eb350382..84f6e6cf058016734a4ce4c5c30301f97fc7725a 100644 (file)
@@ -245,6 +245,72 @@ void symmetry::validate(unsigned n)
 // global functions
 //////////
 
 // 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;
 
 class sy_is_less : public std::binary_function<ex, ex, bool> {
        exvector::iterator v;
 
index a002885e82b6360fccb53ded09682e2d97f864e9..4ec444f8bd393b61088e981ad0bfafd35bc72ef4 100644 (file)
@@ -120,6 +120,16 @@ inline symmetry sy_cycl(const symmetry &c1, const symmetry &c2) { return symmetr
 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); }
 
 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.
  *
 /** Canonicalize the order of elements of an expression vector, according to
  *  the symmetry properties defined in a symmetry tree.
  *
index 399d3119ba742ba0b8d8cc21c8ef906ae5835664..a8c83b65ab20edeb18258411bd1e42dfa434788d 100644 (file)
@@ -534,40 +534,51 @@ bool tensepsilon::contract_with(exvector::iterator self, exvector::iterator othe
 
 ex delta_tensor(const ex & i1, const ex & i2)
 {
 
 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"));
 
        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)
 {
 }
 
 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"));
 
        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)
 {
 }
 
 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"));
 
        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)
 {
 }
 
 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"));
 
        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)
 {
 }
 
 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 (!is_a<idx>(i1) || !is_a<idx>(i2))
                throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
 
@@ -577,11 +588,13 @@ ex epsilon_tensor(const ex & i1, const ex & i2)
        if (!ex_to<idx>(i1).get_dim().is_equal(_ex2))
                throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
 
        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)
 {
 }
 
 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 (!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"));
 
@@ -591,11 +604,14 @@ ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
        if (!ex_to<idx>(i1).get_dim().is_equal(_ex3))
                throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
 
        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)
 {
 }
 
 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 (!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"));
 
@@ -605,7 +621,7 @@ ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool
        if (!ex_to<idx>(i1).get_dim().is_equal(_ex4))
                throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
 
        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
 }
 
 } // namespace GiNaC
index 9b8a7c94ec32cf54331d668b01ce36042efc28c5..117e84ca7e294a265192765597d288a998c26ee7 100644 (file)
@@ -440,7 +440,7 @@ extern const ex _ex120;
 // Helper macros for class implementations (mostly useful for trivial classes)
 
 #define DEFAULT_CTOR(classname) \
 // 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) \
 
 #define DEFAULT_UNARCHIVE(classname) \
 ex classname::unarchive(const archive_node &n, lst &sym_lst) \