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