]> www.ginac.de Git - ginac.git/blobdiff - ginac/ex.h
- added ex::to_rational() to convert general expression to rational expression
[ginac.git] / ginac / ex.h
index a39fe2e789e0fd9861e6d04abd0f5f4a76698359..bf8d0d6f45edb04246c8f4594b7d4d35620253b0 100644 (file)
 #define __GINAC_EX_H__
 
 #include <iostream>
-#include <ginac/basic.h>
-#include <ginac/operators.h>
+#include "basic.h"
+#include "operators.h"
 
-#ifndef NO_GINAC_NAMESPACE
+#ifndef NO_NAMESPACE_GINAC
 namespace GiNaC {
-#endif // ndef NO_GINAC_NAMESPACE
+#endif // ndef NO_NAMESPACE_GINAC
 
 class ex;
 class expand_options;
@@ -38,7 +38,9 @@ class status_flags;
 class symbol;
 class lst;
 
-extern const ex & _ex0(void);  /* FIXME: should this pollute headers? */
+// Sorry, this is the only constant to pollute the global scope, the other ones
+// are defined in utils.h and not visible from outside.
+extern const ex & _ex0(void);     //  single ex(numeric(0))
 
 // typedef vector<ex> exvector;
 
@@ -121,6 +123,14 @@ public:
 ;
 #endif // def INLINE_EX_CONSTRUCTORS
 
+#ifdef CINT_CONVERSION_WORKAROUND
+    // workaround to fix the missing automatic derived->basic->ex conversion
+    const ex & operator=(const basic & other)
+        {
+            return *this=ex(other);
+        }
+#endif // def CINT_CONVERSION_WORKAROUND 
+
     // other constructors
 public:
     ex(const basic & other)
@@ -135,12 +145,83 @@ public:
 ;
 #endif // def INLINE_EX_CONSTRUCTORS
     
-    ex(int i);
-    ex(unsigned int i);
-    ex(long i);
-    ex(unsigned long i);
-    ex(double const d);
+    ex(int i)
+#ifdef INLINE_EX_CONSTRUCTORS
+        {
+            construct_from_int(i);
+#ifdef OBSCURE_CINT_HACK
+            update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+        }
+#else
+;
+#endif // def INLINE_EX_CONSTRUCTORS
+
+    ex(unsigned int i)
+#ifdef INLINE_EX_CONSTRUCTORS
+        {
+            construct_from_uint(i);
+#ifdef OBSCURE_CINT_HACK
+            update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+        }
+#else
+;
+#endif // def INLINE_EX_CONSTRUCTORS
+    
+    ex(long i)
+#ifdef INLINE_EX_CONSTRUCTORS
+        {
+            construct_from_long(i);
+#ifdef OBSCURE_CINT_HACK
+            update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+        }
+#else
+;
+#endif // def INLINE_EX_CONSTRUCTORS
+
+    ex(unsigned long i)
+#ifdef INLINE_EX_CONSTRUCTORS
+        {
+            construct_from_ulong(i);
+#ifdef OBSCURE_CINT_HACK
+            update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+        }
+#else
+;
+#endif // def INLINE_EX_CONSTRUCTORS
+    
+    ex(double const d)
+#ifdef INLINE_EX_CONSTRUCTORS
+        {
+            construct_from_double(d);
+#ifdef OBSCURE_CINT_HACK
+            update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+        }
+#else
+;
+#endif // def INLINE_EX_CONSTRUCTORS
 
+    /** 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. */
+    ex(const string &s, const ex &l)
+#ifdef INLINE_EX_CONSTRUCTORS
+        {
+            construct_from_string_and_lst(s, l);
+#ifdef OBSCURE_CINT_HACK
+            update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+        }
+#else
+;
+#endif // def INLINE_EX_CONSTRUCTORS
+
+    
     // functions overriding virtual functions from bases classes
     // none
     
@@ -160,34 +241,52 @@ public:
     unsigned nops() const;
     ex expand(unsigned options=0) const;
     bool has(const ex & other) const;
+#ifdef CINT_CONVERSION_WORKAROUND
+    bool has(basic const & other) const { return has(ex(other)); }
+#endif // def CINT_CONVERSION_WORKAROUND
     int degree(const symbol & s) const;
     int ldegree(const symbol & s) const;
     ex coeff(const symbol & s, int n=1) const;
     ex lcoeff(const symbol & s) const { return coeff(s, degree(s)); }
     ex tcoeff(const symbol & s) const { return coeff(s, ldegree(s)); }
-    ex numer(bool normalize = true) const;
-    ex denom(bool normalize = true) const;
+    ex numer(void) const;
+    ex denom(void) const;
     ex unit(const symbol &x) const;
     ex content(const symbol &x) const;
     numeric integer_content(void) const;
     ex primpart(const symbol &x) const;
     ex primpart(const symbol &x, const ex &cont) const;
+#ifdef CINT_CONVERSION_WORKAROUND
+    ex primpart(const symbol &x, const basic &cont) const { return primpart(x,ex(cont)); }
+#endif // def CINT_CONVERSION_WORKAROUND
     ex normal(int level = 0) const;
+    ex to_rational(lst &repl_lst) const;
     ex smod(const numeric &xi) const;
     numeric max_coefficient(void) const;
     ex collect(const symbol & s) const;
     ex eval(int level = 0) const;
     ex evalf(int level = 0) const;
     ex diff(const symbol & s, unsigned nth = 1) const;
-    ex series(const symbol & s, const ex & point, int order = 6) const;
+    ex series(const ex & r, int order) const;
+#ifdef CINT_CONVERSION_WORKAROUND
+    ex series(const basic & r, int order) const { return series(ex(r),order); }
+#endif // def CINT_CONVERSION_WORKAROUND
     ex subs(const lst & ls, const lst & lr) const;
     ex subs(const ex & e) const;
+#ifdef CINT_CONVERSION_WORKAROUND
+    ex subs(const basic & e) const { return subs(ex(e)); }
+#endif // def CINT_CONVERSION_WORKAROUND
     exvector get_indices(void) const;
     ex simplify_ncmul(const exvector & v) const;
     ex operator[](const ex & index) const;
+#ifdef CINT_CONVERSION_WORKAROUND
+    ex operator[](const basic & index) const { return operator[](ex(index)); }
+#endif // def CINT_CONVERSION_WORKAROUND
     ex operator[](int i) const;
     ex op(int i) const;
     ex & let_op(int i);
+    ex lhs(void) const;
+    ex rhs(void) const;
     int compare(const ex & other) const
 #ifdef INLINE_EX_CONSTRUCTORS
         {
@@ -202,6 +301,9 @@ public:
 #else
 ;
 #endif // def INLINE_EX_CONSTRUCTORS
+#ifdef CINT_CONVERSION_WORKAROUND
+    int compare(const basic & other) const { return compare(ex(other)); }
+#endif // def CINT_CONVERSION_WORKAROUND
     bool is_equal(const ex & other) const
 #ifdef INLINE_EX_CONSTRUCTORS
         {
@@ -216,6 +318,9 @@ public:
 #else
 ;
 #endif // def INLINE_EX_CONSTRUCTORS
+#ifdef CINT_CONVERSION_WORKAROUND
+    bool is_equal(const basic & other) const { return is_equal(ex(other)); }
+#endif // def CINT_CONVERSION_WORKAROUND
     bool is_zero(void) const {return compare(_ex0())==0;};
         
     unsigned return_type(void) const;
@@ -223,10 +328,25 @@ public:
     unsigned gethash(void) const;
 
     ex exadd(const ex & rh) const;
+#ifdef CINT_CONVERSION_WORKAROUND
+    ex exadd(const basic & rh) const { return exadd(ex(rh)); }
+#endif // def CINT_CONVERSION_WORKAROUND
     ex exmul(const ex & rh) const;
+#ifdef CINT_CONVERSION_WORKAROUND
+    ex exmul(const basic & rh) const { return exmul(ex(rh)); }
+#endif // def CINT_CONVERSION_WORKAROUND
     ex exncmul(const ex & rh) const;
+#ifdef CINT_CONVERSION_WORKAROUND
+    ex exncmul(const basic & rh) const { return exncmul(ex(rh)); }
+#endif // def CINT_CONVERSION_WORKAROUND
 private:
     void construct_from_basic(const basic & other);
+    void construct_from_int(int i);
+    void construct_from_uint(unsigned int i);
+    void construct_from_long(long i);
+    void construct_from_ulong(unsigned long i);
+    void construct_from_double(double d);
+    void construct_from_string_and_lst(const string &s, const ex &l);
     void makewriteable();
 
 #ifdef OBSCURE_CINT_HACK
@@ -250,6 +370,7 @@ protected:
             }
             last_created_or_assigned_bp=bp;
             ++last_created_or_assigned_bp->refcount;
+            last_created_or_assigned_exp=(long)(void *)(this);
         }
 #endif // def OBSCURE_CINT_HACK
 
@@ -258,7 +379,9 @@ protected:
 public:
     basic *bp;
 #ifdef OBSCURE_CINT_HACK
-    static basic *last_created_or_assigned_bp;
+    static basic * last_created_or_assigned_bp;
+    static basic * dummy_bp;
+    static long last_created_or_assigned_exp;
 #endif // def OBSCURE_CINT_HACK
 };
 
@@ -287,15 +410,18 @@ inline int ldegree(const ex & thisex, const symbol & s)
 inline ex coeff(const ex & thisex, const symbol & s, int n=1)
 { return thisex.coeff(s, n); }
 
-inline ex numer(const ex & thisex, bool normalize = true)
-{ return thisex.numer(normalize); }
+inline ex numer(const ex & thisex)
+{ return thisex.numer(); }
 
-inline ex denom(const ex & thisex, bool normalize = true)
-{ return thisex.denom(normalize); }
+inline ex denom(const ex & thisex)
+{ return thisex.denom(); }
 
 inline ex normal(const ex & thisex, int level=0)
 { return thisex.normal(level); }
 
+inline ex to_rational(const ex & thisex, lst & repl_lst)
+{ return thisex.to_rational(repl_lst); }
+
 inline ex collect(const ex & thisex, const symbol & s)
 { return thisex.collect(s); }
 
@@ -308,8 +434,8 @@ inline ex evalf(const ex & thisex, int level = 0)
 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
 { return thisex.diff(s, nth); }
 
-inline ex series(const ex & thisex, const symbol & s, const ex & point, int order = 6)
-{ return thisex.series(s, point, order); }
+inline ex series(const ex & thisex, const ex & r, int order)
+{ return thisex.series(r, order); }
 
 inline ex subs(const ex & thisex, const ex & e)
 { return thisex.subs(e); }
@@ -317,12 +443,24 @@ inline ex subs(const ex & thisex, const ex & e)
 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
 { return thisex.subs(ls, lr); }
 
+inline ex op(const ex & thisex, int i)
+{ return thisex.op(i); }
+
+inline ex lhs(const ex & thisex)
+{ return thisex.lhs(); }
+
+inline ex rhs(const ex & thisex)
+{ return thisex.rhs(); }
+
+inline bool is_zero(const ex & thisex)
+{ return thisex.is_zero(); }
+
 inline void swap(ex & e1, ex & e2)
 { e1.swap(e2); }
 
-#ifndef NO_GINAC_NAMESPACE
+#ifndef NO_NAMESPACE_GINAC
 } // namespace GiNaC
-#endif // ndef NO_GINAC_NAMESPACE
+#endif // ndef NO_NAMESPACE_GINAC
 
 #endif // ndef __GINAC_EX_H__