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