]> www.ginac.de Git - ginac.git/blob - ginac/ex.h
- switched to automake build environment
[ginac.git] / ginac / ex.h
1 /** @file ex.h
2  *
3  *  Interface to GiNaC's light-weight expression handles. */
4
5 #ifndef _EX_H_
6 #define _EX_H_
7
8 #include <iostream>
9
10 class ex;
11 class expand_options;
12 class status_flags;
13
14 #include "basic.h"
15 #include "debugmsg.h"
16 #include "flags.h"
17
18 class symbol;
19 class lst;
20
21 typedef vector<ex> exvector;
22
23 // enum definitions
24
25 ex const & exZERO(void);
26 ex const & exONE(void);
27 ex const & exTWO(void);
28 ex const & exTHREE(void);
29 ex const & exMINUSONE(void);
30 ex const & exHALF(void);
31 ex const & exMINUSHALF(void);
32
33 #define INLINE_EX_CONSTRUCTORS
34
35 /** Lightweight interface to GiNaC's symbolic objects. Basically all it does is
36  *  to hold a pointer to the other objects, manage the reference counting and
37  *  provide methods for manipulation of these objects. */
38 class ex
39 {
40     friend class basic;
41
42 // member functions
43
44     // default constructor, destructor, copy constructor assignment operator and helpers
45 public:
46     ex()
47 #ifdef INLINE_EX_CONSTRUCTORS
48     : bp(exZERO().bp)
49         {
50             debugmsg("ex default constructor",LOGLEVEL_CONSTRUCT);
51             ASSERT(exZERO().bp!=0);
52             ASSERT(exZERO().bp->flags & status_flags::dynallocated);
53             ASSERT(bp!=0);
54             ++bp->refcount;
55         }
56 #else
57 ;
58 #endif // def INLINE_EX_CONSTRUCTORS
59
60     ~ex()
61 #ifdef INLINE_EX_CONSTRUCTORS
62         {
63             debugmsg("ex destructor",LOGLEVEL_DESTRUCT);
64             ASSERT(bp!=0);
65             ASSERT(bp->flags & status_flags::dynallocated);
66             if (--bp->refcount == 0) {
67                 delete bp;
68             }
69         }
70 #else
71 ;
72 #endif // def INLINE_EX_CONSTRUCTORS
73         
74     ex(ex const & other)
75 #ifdef INLINE_EX_CONSTRUCTORS
76     : bp(other.bp)
77         {
78             debugmsg("ex copy constructor",LOGLEVEL_CONSTRUCT);
79             ASSERT(bp!=0);
80             ASSERT((bp->flags) & status_flags::dynallocated);
81             ++bp->refcount;
82         }
83 #else
84 ;
85 #endif // def INLINE_EX_CONSTRUCTORS
86         
87     ex const & operator=(ex const & other)
88 #ifdef INLINE_EX_CONSTRUCTORS
89         {
90             debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);
91             ASSERT(bp!=0);
92             ASSERT(bp->flags & status_flags::dynallocated);
93             ASSERT(other.bp!=0);
94             ASSERT(other.bp->flags & status_flags::dynallocated);
95             ++other.bp->refcount;
96             basic * tmpbp=other.bp;
97             if (--bp->refcount==0) {
98                 delete bp;
99             }
100             bp=tmpbp;
101             return *this;
102         }
103 #else
104 ;
105 #endif // def INLINE_EX_CONSTRUCTORS
106
107     // other constructors
108 public:
109     ex(basic const & other)
110 #ifdef INLINE_EX_CONSTRUCTORS
111     {
112         debugmsg("ex constructor from basic",LOGLEVEL_CONSTRUCT);
113         construct_from_basic(other);
114     }
115 #else
116 ;
117 #endif // def INLINE_EX_CONSTRUCTORS
118     
119     ex(int const i);
120     ex(unsigned int const i);
121     ex(long const i);
122     ex(unsigned long const i);
123     ex(double const d);
124
125     // functions overriding virtual functions from bases classes
126     // none
127     
128     // new virtual functions which can be overridden by derived classes
129     // none
130
131     // non-virtual functions in this class
132 public:
133     void swap(ex & other);
134     void printraw(ostream & os) const;
135     void printtree(ostream & os, unsigned indent=0) const;
136     void print(ostream & os, unsigned upper_precedence=0) const;
137     void printcsrc(ostream & os, unsigned type, const char *var_name) const;
138     void dbgprint(void) const;
139     void dbgprinttree(void) const;
140     bool info(unsigned inf) const;
141     int nops() const;
142     ex expand(unsigned options=0) const;
143     bool has(ex const & other) const;
144     int degree(symbol const & s) const;
145     int ldegree(symbol const & s) const;
146     ex coeff(symbol const & s, int const n=1) const;
147     ex lcoeff(symbol const & s) const { return coeff(s, degree(s)); }
148     ex tcoeff(symbol const & s) const { return coeff(s, ldegree(s)); }
149     ex numer(bool normalize = true) const;
150     ex denom(bool normalize = true) const;
151     ex unit(const symbol &x) const;
152     ex content(const symbol &x) const;
153     numeric integer_content(void) const;
154     ex primpart(const symbol &x) const;
155     ex primpart(const symbol &x, const ex &cont) const;
156     ex normal(int level = 0) const;
157     ex smod(const numeric &xi) const;
158     numeric max_coefficient(void) const;
159     ex collect(symbol const & s) const;
160     ex eval(int level = 0) const;
161     ex evalf(int level = 0) const;
162     ex diff(symbol const & s, unsigned nth = 1) const;
163     ex series(symbol const & s, ex const & point, int order = 6) const;
164     ex subs(lst const & ls, lst const & lr) const;
165     ex subs(ex const & e) const;
166     exvector get_indices(void) const;
167     ex simplify_ncmul(exvector const & v) const;
168     ex operator[](ex const & index) const;
169     ex operator[](int const i) const;
170     ex op(int const i) const;
171     ex & let_op(int const i);
172     int compare(ex const & other) const
173 #ifdef INLINE_EX_CONSTRUCTORS
174         {
175             ASSERT(bp!=0);
176             ASSERT(other.bp!=0);
177             if (bp==other.bp) {
178                 // special case: both expression point to same basic, trivially equal
179                 return 0; 
180             }
181             return bp->compare(*other.bp);
182         }
183 #else
184 ;
185 #endif // def INLINE_EX_CONSTRUCTORS
186     bool is_equal(ex const & other) const
187 #ifdef INLINE_EX_CONSTRUCTORS
188         {
189             ASSERT(bp!=0);
190             ASSERT(other.bp!=0);
191             if (bp==other.bp) {
192                 // special case: both expression point to same basic, trivially equal
193                 return true; 
194             }
195             return bp->is_equal(*other.bp);
196         }
197 #else
198 ;
199 #endif // def INLINE_EX_CONSTRUCTORS
200     bool is_zero(void) const {return compare(exZERO()) == 0;};
201         
202     unsigned return_type(void) const;
203     unsigned return_type_tinfo(void) const;
204     unsigned gethash(void) const;
205
206     ex exadd(ex const & rh) const;
207     ex exmul(ex const & rh) const;
208     ex exncmul(ex const & rh) const;
209 private:
210     void construct_from_basic(basic const & other);
211     void makewriteable();
212
213 // member variables
214
215 public:
216     basic *bp;
217
218 };
219
220 // wrapper functions around member functions
221 inline int nops(ex const & thisex)
222 { return thisex.nops(); }
223
224 inline ex expand(ex const & thisex, unsigned options = 0)
225 { return thisex.expand(options); }
226
227 inline bool has(ex const & thisex, ex const & other)
228 { return thisex.has(other); }
229
230 inline int degree(ex const & thisex, symbol const & s)
231 { return thisex.degree(s); }
232
233 inline int ldegree(ex const & thisex, symbol const & s)
234 { return thisex.ldegree(s); }
235
236 inline ex coeff(ex const & thisex, symbol const & s, int const n=1)
237 { return thisex.coeff(s, n); }
238
239 inline ex numer(ex const & thisex, bool normalize = true)
240 { return thisex.numer(normalize); }
241
242 inline ex denom(ex const & thisex, bool normalize = true)
243 { return thisex.denom(normalize); }
244
245 inline ex normal(ex const & thisex, int level=0)
246 { return thisex.normal(level); }
247
248 inline ex collect(ex const & thisex, symbol const & s)
249 { return thisex.collect(s); }
250
251 inline ex eval(ex const & thisex, int level = 0)
252 { return thisex.eval(level); }
253
254 inline ex evalf(ex const & thisex, int level = 0)
255 { return thisex.evalf(level); }
256
257 inline ex diff(ex const & thisex, symbol const & s, unsigned nth = 1)
258 { return thisex.diff(s, nth); }
259
260 inline ex subs(ex const & thisex, ex const & e)
261 { return thisex.subs(e); }
262
263 inline ex subs(ex const & thisex, lst const & ls, lst const & lr)
264 { return thisex.subs(ls, lr); }
265
266 inline void swap(ex & e1, ex & e2)
267 { e1.swap(e2); }
268
269 #endif // ndef _EX_H_