]> www.ginac.de Git - ginac.git/blobdiff - ginac/container.h
Added complex conjugation methods and GiNaC function "conjugate".
[ginac.git] / ginac / container.h
index e3465c46decb803392b255cc1c364c5df9b51d08..5d6fe3ac5ebb3e9a79b4728ba75345e5005d7eb2 100644 (file)
@@ -3,7 +3,7 @@
  *  Wrapper template for making GiNaC classes out of STL containers. */
 
 /*
- *  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
@@ -67,6 +67,63 @@ template <>
 inline void container_storage<std::vector>::reserve(std::vector<ex> & v, size_t n) { v.reserve(n); }
 
 
+/** Helper template to allow initialization of containers via an overloaded
+ *  comma operator (idea stolen from Blitz++). */
+template <typename T, typename STLT>
+class container_init {
+public:
+       container_init(STLT & s) : stlt(s) {}
+
+       container_init<T, STLT> operator,(const T & x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       // The following specializations produce much tighter code than the
+       // general case above
+
+       container_init<T, STLT> operator,(int x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       container_init<T, STLT> operator,(unsigned int x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       container_init<T, STLT> operator,(long x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       container_init<T, STLT> operator,(unsigned long x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       container_init<T, STLT> operator,(double x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       container_init<T, STLT> operator,(const symbol & x)
+       {
+               stlt.push_back(T(x));
+               return container_init<T, STLT>(stlt);
+       }
+
+private:
+       container_init();
+       STLT & stlt;
+};
+
 /** Wrapper template for making GiNaC classes out of STL containers. */
 template <template <class> class C>
 class container : public basic, public container_storage<C> {
@@ -81,6 +138,7 @@ public:
 protected:
        // helpers
        static unsigned get_tinfo() { return TINFO_fail; }
+       static unsigned get_default_flags() { return 0; }
        static char get_open_delim() { return '('; }
        static char get_close_delim() { return ')'; }
 
@@ -88,6 +146,8 @@ protected:
 public:
        container(STLT const & s, bool discardable = false) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
+
                if (discardable)
                        this->seq.swap(const_cast<STLT &>(s));
                else
@@ -96,23 +156,32 @@ public:
 
        explicit container(std::auto_ptr<STLT> vp) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                this->seq.swap(*vp);
        }
 
        container(exvector::const_iterator b, exvector::const_iterator e)
-        : inherited(get_tinfo()), container_storage<C>(b, e) {}
+        : inherited(get_tinfo()), container_storage<C>(b, e)
+       {
+               setflag(get_default_flags());
+       }
 
        explicit container(const ex & p1)
-        : inherited(get_tinfo()), container_storage<C>(1, p1) {}
+        : inherited(get_tinfo()), container_storage<C>(1, p1)
+       {
+               setflag(get_default_flags());
+       }
 
        container(const ex & p1, const ex & p2) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 2);
                this->seq.push_back(p1); this->seq.push_back(p2);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 3);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
        }
@@ -120,6 +189,7 @@ public:
        container(const ex & p1, const ex & p2, const ex & p3,
                           const ex & p4) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 4);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4);
@@ -128,6 +198,7 @@ public:
        container(const ex & p1, const ex & p2, const ex & p3,
                  const ex & p4, const ex & p5) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 5);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5);
@@ -136,6 +207,7 @@ public:
        container(const ex & p1, const ex & p2, const ex & p3,
                  const ex & p4, const ex & p5, const ex & p6) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 6);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
@@ -145,6 +217,7 @@ public:
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 7);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
@@ -155,6 +228,7 @@ public:
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7, const ex & p8) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 8);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
@@ -165,6 +239,7 @@ public:
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7, const ex & p8, const ex & p9) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 9);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
@@ -176,6 +251,7 @@ public:
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 10);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
@@ -188,6 +264,7 @@ public:
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10, const ex & p11) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 11);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
@@ -200,6 +277,7 @@ public:
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10, const ex & p11, const ex & p12) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 12);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
@@ -213,6 +291,7 @@ public:
                  const ex & p10, const ex & p11, const ex & p12,
                  const ex & p13) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 13);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
@@ -227,6 +306,7 @@ public:
                  const ex & p10, const ex & p11, const ex & p12,
                  const ex & p13, const ex & p14) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 14);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
@@ -241,6 +321,7 @@ public:
                  const ex & p10, const ex & p11, const ex & p12,
                  const ex & p13, const ex & p14, const ex & p15) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 15);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
@@ -256,6 +337,7 @@ public:
                  const ex & p13, const ex & p14, const ex & p15,
                  const ex & p16) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 16);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
                this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
@@ -265,6 +347,15 @@ public:
                this->seq.push_back(p16);
        }
 
+       // First step of initialization of container with a comma-separated
+       // sequence of expressions. Subsequent steps are handled by
+       // container_init<>::operator,().
+       container_init<ex, STLT> operator=(const ex & x)
+       {
+               this->seq.push_back(x);
+               return container_init<ex, STLT>(this->seq);
+       }
+
        // functions overriding virtual functions from base classes
 public:
        bool info(unsigned inf) const { return inherited::info(inf); }
@@ -276,6 +367,32 @@ public:
        ex subs(const exmap & m, unsigned options = 0) const;
 
 protected:
+       ex conjugate() const
+       {
+               STLT* newcont = 0;
+               for (const_iterator i=seq.begin(); i!=seq.end(); ++i) {
+                       if (newcont) {
+                               newcont->push_back(i->conjugate());
+                               continue;
+                       }
+                       ex x = i->conjugate();
+                       if (are_ex_trivially_equal(x, *i)) {
+                               continue;
+                       }
+                       newcont = new STLT;
+                       reserve (*newcont, seq.size());
+                       for (const_iterator j=seq.begin(); j!=i; ++j) {
+                               newcont->push_back(*j);
+                       }
+                       newcont->push_back(x);
+               }
+               if (newcont) {
+                       ex result = thiscontainer(*newcont);
+                       delete newcont;
+                       return result;
+               }
+               return *this;
+       }
        bool is_equal_same_type(const basic & other) const;
 
        // new virtual functions which can be overridden by derived classes
@@ -335,12 +452,17 @@ protected:
 
 /** Default constructor */
 template <template <class> class C>
-container<C>::container() : inherited(get_tinfo()) {}
+container<C>::container() : inherited(get_tinfo())
+{
+       setflag(get_default_flags());
+}
 
 /** Construct object from archive_node. */
 template <template <class> class C>
 container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
 {
+       setflag(get_default_flags());
+
        for (unsigned int i=0; true; i++) {
                ex e;
                if (n.find_ex("seq", e, sym_lst, i))