- indentation is now done with tabs
[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-2000 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 "basic.h"
28 #include "operators.h"
29
30 #ifndef NO_NAMESPACE_GINAC
31 namespace GiNaC {
32 #endif // ndef NO_NAMESPACE_GINAC
33
34 class ex;
35 class expand_options;
36 class status_flags;
37
38 class symbol;
39 class lst;
40
41 // Sorry, this is the only constant to pollute the global scope, the other ones
42 // are defined in utils.h and not visible from outside.
43 extern const ex & _ex0(void);     //  single ex(numeric(0))
44
45 #define INLINE_EX_CONSTRUCTORS
46
47 /** Lightweight wrapper for GiNaC's symbolic objects.  Basically all it does is
48  *  to hold a pointer to the other objects, manage the reference counting and
49  *  provide methods for manipulation of these objects. */
50 class ex
51 {
52         friend class basic;
53
54 // member functions
55
56         // default constructor, destructor, copy constructor assignment operator and helpers
57 public:
58         ex()
59 #ifdef INLINE_EX_CONSTRUCTORS
60         : bp(_ex0().bp)
61                 {
62                         GINAC_ASSERT(_ex0().bp!=0);
63                         GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
64                         GINAC_ASSERT(bp!=0);
65                         ++bp->refcount;
66 #ifdef OBSCURE_CINT_HACK
67                         update_last_created_or_assigned_bp();
68 #endif // def OBSCURE_CINT_HACK
69                 }
70 #else
71 ;
72 #endif // def INLINE_EX_CONSTRUCTORS
73
74         ~ex()
75 #ifdef INLINE_EX_CONSTRUCTORS
76                 {
77                         GINAC_ASSERT(bp!=0);
78                         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
79                         if (--bp->refcount == 0) {
80                                 delete bp;
81                         }
82                 }
83 #else
84 ;
85 #endif // def INLINE_EX_CONSTRUCTORS
86                 
87         ex(const ex & other)
88 #ifdef INLINE_EX_CONSTRUCTORS
89         : bp(other.bp)
90                 {
91                         GINAC_ASSERT(bp!=0);
92                         GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
93                         ++bp->refcount;
94 #ifdef OBSCURE_CINT_HACK
95                         update_last_created_or_assigned_bp();
96 #endif // def OBSCURE_CINT_HACK
97                 }
98 #else
99 ;
100 #endif // def INLINE_EX_CONSTRUCTORS
101                 
102         const ex & operator=(const ex & 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 #ifdef OBSCURE_CINT_HACK
116                         update_last_created_or_assigned_bp();
117 #endif // def OBSCURE_CINT_HACK
118                         return *this;
119                 }
120 #else
121 ;
122 #endif // def INLINE_EX_CONSTRUCTORS
123
124
125         // other constructors
126 public:
127         ex(const basic & other)
128 #ifdef INLINE_EX_CONSTRUCTORS
129                 {
130                         construct_from_basic(other);
131 #ifdef OBSCURE_CINT_HACK
132                         update_last_created_or_assigned_bp();
133 #endif // def OBSCURE_CINT_HACK
134                 }
135 #else
136 ;
137 #endif // def INLINE_EX_CONSTRUCTORS
138         
139         ex(int i)
140 #ifdef INLINE_EX_CONSTRUCTORS
141                 {
142                         construct_from_int(i);
143 #ifdef OBSCURE_CINT_HACK
144                         update_last_created_or_assigned_bp();
145 #endif // def OBSCURE_CINT_HACK
146                 }
147 #else
148 ;
149 #endif // def INLINE_EX_CONSTRUCTORS
150
151         ex(unsigned int i)
152 #ifdef INLINE_EX_CONSTRUCTORS
153                 {
154                         construct_from_uint(i);
155 #ifdef OBSCURE_CINT_HACK
156                         update_last_created_or_assigned_bp();
157 #endif // def OBSCURE_CINT_HACK
158                 }
159 #else
160 ;
161 #endif // def INLINE_EX_CONSTRUCTORS
162         
163         ex(long i)
164 #ifdef INLINE_EX_CONSTRUCTORS
165                 {
166                         construct_from_long(i);
167 #ifdef OBSCURE_CINT_HACK
168                         update_last_created_or_assigned_bp();
169 #endif // def OBSCURE_CINT_HACK
170                 }
171 #else
172 ;
173 #endif // def INLINE_EX_CONSTRUCTORS
174
175         ex(unsigned long i)
176 #ifdef INLINE_EX_CONSTRUCTORS
177                 {
178                         construct_from_ulong(i);
179 #ifdef OBSCURE_CINT_HACK
180                         update_last_created_or_assigned_bp();
181 #endif // def OBSCURE_CINT_HACK
182                 }
183 #else
184 ;
185 #endif // def INLINE_EX_CONSTRUCTORS
186         
187         ex(double const d)
188 #ifdef INLINE_EX_CONSTRUCTORS
189                 {
190                         construct_from_double(d);
191 #ifdef OBSCURE_CINT_HACK
192                         update_last_created_or_assigned_bp();
193 #endif // def OBSCURE_CINT_HACK
194                 }
195 #else
196 ;
197 #endif // def INLINE_EX_CONSTRUCTORS
198
199         /** Construct ex from string and a list of symbols. The input grammar is
200          *  similar to the GiNaC output format. All symbols to be used in the
201          *  expression must be specified in a lst in the second argument. Undefined
202          *  symbols and other parser errors will throw an exception. */
203         ex(const std::string &s, const ex &l)
204 #ifdef INLINE_EX_CONSTRUCTORS
205                 {
206                         construct_from_string_and_lst(s, l);
207 #ifdef OBSCURE_CINT_HACK
208                         update_last_created_or_assigned_bp();
209 #endif // def OBSCURE_CINT_HACK
210                 }
211 #else
212 ;
213 #endif // def INLINE_EX_CONSTRUCTORS
214
215         
216         // functions overriding virtual functions from bases classes
217         // none
218         
219         // new virtual functions which can be overridden by derived classes
220         // none
221
222         // non-virtual functions in this class
223 public:
224         void swap(ex & other);
225         void printraw(std::ostream & os) const;
226         void printtree(std::ostream & os, unsigned indent=0) const;
227         void print(std::ostream & os, unsigned upper_precedence=0) const;
228         void printcsrc(std::ostream & os, unsigned type, const char *var_name) const;
229         void dbgprint(void) const;
230         void dbgprinttree(void) const;
231         bool info(unsigned inf) const;
232         unsigned nops() const;
233         ex expand(unsigned options=0) const;
234         bool has(const ex & other) const;
235         int degree(const symbol & s) const;
236         int ldegree(const symbol & s) const;
237         ex coeff(const symbol & s, int n=1) const;
238         ex lcoeff(const symbol & s) const { return coeff(s, degree(s)); }
239         ex tcoeff(const symbol & s) const { return coeff(s, ldegree(s)); }
240         ex numer(void) const;
241         ex denom(void) const;
242         ex unit(const symbol &x) const;
243         ex content(const symbol &x) const;
244         numeric integer_content(void) const;
245         ex primpart(const symbol &x) const;
246         ex primpart(const symbol &x, const ex &cont) const;
247         ex normal(int level = 0) const;
248         ex to_rational(lst &repl_lst) const;
249         ex smod(const numeric &xi) const;
250         numeric max_coefficient(void) const;
251         ex collect(const symbol & s) const;
252         ex eval(int level = 0) const;
253         ex evalf(int level = 0) const;
254         ex diff(const symbol & s, unsigned nth = 1) const;
255         ex series(const ex & r, int order, unsigned options = 0) const;
256         ex subs(const lst & ls, const lst & lr) const;
257         ex subs(const ex & e) const;
258         exvector get_indices(void) const;
259         ex simplify_ncmul(const exvector & v) const;
260         ex operator[](const ex & index) const;
261         ex operator[](int i) const;
262         ex op(int i) const;
263         ex & let_op(int i);
264         ex lhs(void) const;
265         ex rhs(void) const;
266         int compare(const ex & other) const
267 #ifdef INLINE_EX_CONSTRUCTORS
268                 {
269                         GINAC_ASSERT(bp!=0);
270                         GINAC_ASSERT(other.bp!=0);
271                         if (bp==other.bp) {
272                                 // special case: both expression point to same basic, trivially equal
273                                 return 0; 
274                         }
275                         return bp->compare(*other.bp);
276                 }
277 #else
278 ;
279 #endif // def INLINE_EX_CONSTRUCTORS
280         bool is_equal(const ex & other) const
281 #ifdef INLINE_EX_CONSTRUCTORS
282                 {
283                         GINAC_ASSERT(bp!=0);
284                         GINAC_ASSERT(other.bp!=0);
285                         if (bp==other.bp) {
286                                 // special case: both expression point to same basic, trivially equal
287                                 return true; 
288                         }
289                         return bp->is_equal(*other.bp);
290                 }
291 #else
292 ;
293 #endif // def INLINE_EX_CONSTRUCTORS
294         bool is_zero(void) const {return compare(_ex0())==0;};
295                 
296         unsigned return_type(void) const;
297         unsigned return_type_tinfo(void) const;
298         unsigned gethash(void) const;
299
300         ex exadd(const ex & rh) const;
301         ex exmul(const ex & rh) const;
302         ex exncmul(const ex & rh) const;
303 private:
304         void construct_from_basic(const basic & other);
305         void construct_from_int(int i);
306         void construct_from_uint(unsigned int i);
307         void construct_from_long(long i);
308         void construct_from_ulong(unsigned long i);
309         void construct_from_double(double d);
310         void construct_from_string_and_lst(const std::string &s, const ex &l);
311         void makewriteable();
312
313 #ifdef OBSCURE_CINT_HACK
314 public:
315         static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
316                 {
317                         if (last_created_or_assigned_bp==0) return false;
318                         if ((last_created_or_assigned_bp->flags &
319                                  status_flags::dynallocated)==0) return false;
320                         if ((last_created_or_assigned_bp->flags &
321                                  status_flags::evaluated)==0) return false;
322                         return true;
323                 }
324 protected:
325         void update_last_created_or_assigned_bp(void)
326                 {
327                         if (last_created_or_assigned_bp!=0) {
328                                 if (--last_created_or_assigned_bp->refcount == 0) {
329                                         delete last_created_or_assigned_bp;
330                                 }
331                         }
332                         last_created_or_assigned_bp=bp;
333                         ++last_created_or_assigned_bp->refcount;
334                         last_created_or_assigned_exp=(long)(void *)(this);
335                 }
336 #endif // def OBSCURE_CINT_HACK
337
338 // member variables
339
340 public:
341         basic *bp;
342 #ifdef OBSCURE_CINT_HACK
343         static basic * last_created_or_assigned_bp;
344         static basic * dummy_bp;
345         static long last_created_or_assigned_exp;
346 #endif // def OBSCURE_CINT_HACK
347 };
348
349 // utility functions
350 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
351 {
352         return e1.bp == e2.bp;
353 }
354
355 // wrapper functions around member functions
356 inline unsigned nops(const ex & thisex)
357 { return thisex.nops(); }
358
359 inline ex expand(const ex & thisex, unsigned options = 0)
360 { return thisex.expand(options); }
361
362 inline bool has(const ex & thisex, const ex & other)
363 { return thisex.has(other); }
364
365 inline int degree(const ex & thisex, const symbol & s)
366 { return thisex.degree(s); }
367
368 inline int ldegree(const ex & thisex, const symbol & s)
369 { return thisex.ldegree(s); }
370
371 inline ex coeff(const ex & thisex, const symbol & s, int n=1)
372 { return thisex.coeff(s, n); }
373
374 inline ex numer(const ex & thisex)
375 { return thisex.numer(); }
376
377 inline ex denom(const ex & thisex)
378 { return thisex.denom(); }
379
380 inline ex normal(const ex & thisex, int level=0)
381 { return thisex.normal(level); }
382
383 inline ex to_rational(const ex & thisex, lst & repl_lst)
384 { return thisex.to_rational(repl_lst); }
385
386 inline ex collect(const ex & thisex, const symbol & s)
387 { return thisex.collect(s); }
388
389 inline ex eval(const ex & thisex, int level = 0)
390 { return thisex.eval(level); }
391
392 inline ex evalf(const ex & thisex, int level = 0)
393 { return thisex.evalf(level); }
394
395 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
396 { return thisex.diff(s, nth); }
397
398 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
399 { return thisex.series(r, order, options); }
400
401 inline ex subs(const ex & thisex, const ex & e)
402 { return thisex.subs(e); }
403
404 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
405 { return thisex.subs(ls, lr); }
406
407 inline ex op(const ex & thisex, int i)
408 { return thisex.op(i); }
409
410 inline ex lhs(const ex & thisex)
411 { return thisex.lhs(); }
412
413 inline ex rhs(const ex & thisex)
414 { return thisex.rhs(); }
415
416 inline bool is_zero(const ex & thisex)
417 { return thisex.is_zero(); }
418
419 inline void swap(ex & e1, ex & e2)
420 { e1.swap(e2); }
421
422 #ifndef NO_NAMESPACE_GINAC
423 } // namespace GiNaC
424 #endif // ndef NO_NAMESPACE_GINAC
425
426 #endif // ndef __GINAC_EX_H__
427