]> www.ginac.de Git - ginac.git/blobdiff - ginac/ex.h
final (?) update for 1.1
[ginac.git] / ginac / ex.h
index ad5980075f720b10f86d41f51698b237ea813c37..3c8094b1b405b1717f5cc05ce8ae74f0563055a7 100644 (file)
@@ -3,7 +3,7 @@
  *  Interface to GiNaC's light-weight expression handles. */
 
 /*
- *  GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2003 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
@@ -27,7 +27,6 @@
 #include <functional>
 
 #include "basic.h"
-#include "operators.h"
 
 namespace GiNaC {
 
@@ -89,21 +88,33 @@ public:
        ex(long i);
        ex(unsigned long i);
        ex(double const d);
+
        /** Construct ex from string and a list of symbols. The input grammar is
-        *  similar to the GiNaC output format. All symbols to be used in the
-        *  expression must be specified in a lst in the second argument. Undefined
-        *  symbols and other parser errors will throw an exception. */
+        *  similar to the GiNaC output format. All symbols and indices to be used
+        *  in the expression must be specified in a lst in the second argument.
+        *  Undefined symbols and other parser errors will throw an exception. */
        ex(const std::string &s, const ex &l);
        
        // non-virtual functions in this class
 public:
-       void swap(ex & other);
+       /** Efficiently swap the contents of two expressions. */
+       void swap(ex & other)
+       {
+               GINAC_ASSERT(bp!=0);
+               GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+               GINAC_ASSERT(other.bp!=0);
+               GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
+       
+               basic * tmpbp = bp;
+               bp = other.bp;
+               other.bp = tmpbp;
+       }
+
        void print(const print_context & c, unsigned level = 0) const;
-       void printtree(std::ostream & os) const;
        void dbgprint(void) const;
        void dbgprinttree(void) const;
        bool info(unsigned inf) const { return bp->info(inf); }
-       unsigned nops() const { return bp->nops(); }
+       size_t nops() const { return bp->nops(); }
        ex expand(unsigned options=0) const;
        bool has(const ex & pattern) const { return bp->has(pattern); }
        ex map(map_function & f) const { return bp->map(f); }
@@ -123,7 +134,8 @@ public:
        ex primpart(const symbol &x) const;
        ex primpart(const symbol &x, const ex &cont) const;
        ex normal(int level = 0) const;
-       ex to_rational(lst &repl_lst) const { return bp->to_rational(repl_lst); }
+       ex to_rational(lst &repl_lst) const;
+       ex to_polynomial(lst &repl_lst) const;
        ex smod(const numeric &xi) const { return bp->smod(xi); }
        numeric max_coefficient(void) const;
        ex collect(const ex & s, bool distributed = false) const { return bp->collect(s, distributed); }
@@ -134,8 +146,8 @@ public:
        ex series(const ex & r, int order, unsigned options = 0) const;
        bool match(const ex & pattern) const;
        bool match(const ex & pattern, lst & repl_lst) const { return bp->match(pattern, repl_lst); }
-       ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const { return bp->subs(ls, lr, no_pattern); }
-       ex subs(const ex & e, bool no_pattern = false) const { return bp->subs(e, no_pattern); }
+       ex subs(const lst & ls, const lst & lr, unsigned options = 0) const { return bp->subs(ls, lr, options); }
+       ex subs(const ex & e, unsigned options = 0) const { return bp->subs(e, options); }
        exvector get_free_indices(void) const { return bp->get_free_indices(); }
        ex simplify_indexed(void) const;
        ex simplify_indexed(const scalar_products & sp) const;
@@ -145,11 +157,13 @@ public:
        ex antisymmetrize(const lst & l) const;
        ex symmetrize_cyclic(void) const;
        ex symmetrize_cyclic(const lst & l) const;
-       ex simplify_ncmul(const exvector & v) const { return bp->simplify_ncmul(v); }
-       ex operator[](const ex & index) const;
-       ex operator[](int i) const;
-       ex op(int i) const { return bp->op(i); }
-       ex & let_op(int i);
+       ex eval_ncmul(const exvector & v) const { return bp->eval_ncmul(v); }
+       ex op(size_t i) const { return bp->op(i); }
+       ex operator[](const ex & index) const { return (*bp)[index]; }
+       ex operator[](size_t i) const { return (*bp)[i]; }
+       ex & let_op(size_t i);
+       ex & operator[](const ex & index);
+       ex & operator[](size_t i);
        ex lhs(void) const;
        ex rhs(void) const;
        int compare(const ex & other) const;
@@ -356,7 +370,7 @@ inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
 }
 
 // wrapper functions around member functions
-inline unsigned nops(const ex & thisex)
+inline size_t nops(const ex & thisex)
 { return thisex.nops(); }
 
 inline ex expand(const ex & thisex, unsigned options = 0)
@@ -392,6 +406,9 @@ inline ex normal(const ex & thisex, int level=0)
 inline ex to_rational(const ex & thisex, lst & repl_lst)
 { return thisex.to_rational(repl_lst); }
 
+inline ex to_polynomial(const ex & thisex, lst & repl_lst)
+{ return thisex.to_polynomial(repl_lst); }
+
 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
 { return thisex.collect(s, distributed); }
 
@@ -413,11 +430,11 @@ inline ex series(const ex & thisex, const ex & r, int order, unsigned options =
 inline bool match(const ex & thisex, const ex & pattern, lst & repl_lst)
 { return thisex.match(pattern, repl_lst); }
 
-inline ex subs(const ex & thisex, const ex & e)
-{ return thisex.subs(e); }
+inline ex subs(const ex & thisex, const ex & e, unsigned options = 0)
+{ return thisex.subs(e, options); }
 
-inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
-{ return thisex.subs(ls, lr); }
+inline ex subs(const ex & thisex, const lst & ls, const lst & lr, unsigned options = 0)
+{ return thisex.subs(ls, lr, options); }
 
 inline ex simplify_indexed(const ex & thisex)
 { return thisex.simplify_indexed(); }
@@ -443,7 +460,7 @@ inline ex symmetrize_cyclic(const ex & thisex)
 inline ex symmetrize_cyclic(const ex & thisex, const lst & l)
 { return thisex.symmetrize_cyclic(l); }
 
-inline ex op(const ex & thisex, int i)
+inline ex op(const ex & thisex, size_t i)
 { return thisex.op(i); }
 
 inline ex lhs(const ex & thisex)
@@ -458,6 +475,10 @@ inline bool is_zero(const ex & thisex)
 inline void swap(ex & e1, ex & e2)
 { e1.swap(e2); }
 
+// This makes STL algorithms use the more efficient swap operation for ex objects
+inline void iter_swap(std::vector<ex>::iterator i1, std::vector<ex>::iterator i2)
+{ i1->swap(*i2); }
+
 
 /* Function objects for STL sort() etc. */
 struct ex_is_less : public std::binary_function<ex, ex, bool> {
@@ -468,6 +489,10 @@ struct ex_is_equal : public std::binary_function<ex, ex, bool> {
        bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
 };
 
+struct op0_is_equal : public std::binary_function<ex, ex, bool> {
+       bool operator() (const ex &lh, const ex &rh) const { return lh.op(0).is_equal(rh.op(0)); }
+};
+
 struct ex_swap : public std::binary_function<ex, ex, void> {
        void operator() (ex &lh, ex &rh) const { lh.swap(rh); }
 };