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