]> www.ginac.de Git - ginac.git/blob - ginac/ex.h
something like 2/3*a is no longer printed as (2/3)*a
[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         const 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;
84         unsigned nops() const;
85         ex expand(unsigned options=0) const;
86         bool has(const ex & other) const;
87         int degree(const ex & s) const;
88         int ldegree(const ex & s) const;
89         ex coeff(const ex & s, int n=1) const;
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) const;
104         ex eval(int level = 0) const;
105         ex evalf(int level = 0) const;
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;
109         ex subs(const ex & e) const;
110         exvector get_free_indices(void) const;
111         ex simplify_indexed(void) const;
112         ex simplify_indexed(const scalar_products & sp) const;
113         ex simplify_ncmul(const exvector & v) const;
114         ex operator[](const ex & index) const;
115         ex operator[](int i) const;
116         ex op(int i) const;
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;
125         unsigned return_type_tinfo(void) const;
126         unsigned gethash(void) const;
127         
128         ex exadd(const ex & rh) const;
129         ex exmul(const ex & rh) const;
130 private:
131         void construct_from_basic(const basic & other);
132         void construct_from_int(int i);
133         void construct_from_uint(unsigned int i);
134         void construct_from_long(long i);
135         void construct_from_ulong(unsigned long i);
136         void construct_from_double(double d);
137         void construct_from_string_and_lst(const std::string &s, const ex &l);
138         void makewriteable();
139
140 #ifdef OBSCURE_CINT_HACK
141 public:
142         static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
143         {
144                 if (last_created_or_assigned_bp==0) return false;
145                 if ((last_created_or_assigned_bp->flags &
146                          status_flags::dynallocated)==0) return false;
147                 if ((last_created_or_assigned_bp->flags &
148                          status_flags::evaluated)==0) return false;
149                 return true;
150         }
151 protected:
152         void update_last_created_or_assigned_bp(void)
153         {
154                 if (last_created_or_assigned_bp!=0) {
155                         if (--last_created_or_assigned_bp->refcount == 0) {
156                                 delete last_created_or_assigned_bp;
157                         }
158                 }
159                 last_created_or_assigned_bp = bp;
160                 ++last_created_or_assigned_bp->refcount;
161                 last_created_or_assigned_exp = (long)(void *)(this);
162         }
163 #endif // def OBSCURE_CINT_HACK
164
165 // member variables
166
167 public:
168         basic *bp;      ///< pointer to basic object managed by this
169 #ifdef OBSCURE_CINT_HACK
170         static basic * last_created_or_assigned_bp;
171         static basic * dummy_bp;
172         static long last_created_or_assigned_exp;
173 #endif // def OBSCURE_CINT_HACK
174 };
175
176
177 // performance-critical inlined method implementations
178
179 inline
180 ex::ex() : bp(_ex0().bp)
181 {
182         /*debugmsg("ex default ctor",LOGLEVEL_CONSTRUCT);*/
183         GINAC_ASSERT(_ex0().bp!=0);
184         GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
185         GINAC_ASSERT(bp!=0);
186         ++bp->refcount;
187 #ifdef OBSCURE_CINT_HACK
188         update_last_created_or_assigned_bp();
189 #endif // def OBSCURE_CINT_HACK
190 }
191
192 inline
193 ex::~ex()
194 {
195         /*debugmsg("ex dtor",LOGLEVEL_DESTRUCT);*/
196         GINAC_ASSERT(bp!=0);
197         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
198         if (--bp->refcount == 0)
199                 delete bp;
200 }
201
202 inline
203 ex::ex(const ex & other) : bp(other.bp)
204 {
205         /*debugmsg("ex copy ctor",LOGLEVEL_CONSTRUCT);*/
206         GINAC_ASSERT(bp!=0);
207         GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
208         ++bp->refcount;
209 #ifdef OBSCURE_CINT_HACK
210         update_last_created_or_assigned_bp();
211 #endif // def OBSCURE_CINT_HACK
212 }
213
214 inline
215 const ex & ex::operator=(const ex & other)
216 {
217         /*debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);*/
218         GINAC_ASSERT(bp!=0);
219         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
220         GINAC_ASSERT(other.bp!=0);
221         GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
222         if (--bp->refcount==0)
223                 delete bp;
224         ++other.bp->refcount;
225         bp = other.bp;
226 #ifdef OBSCURE_CINT_HACK
227         update_last_created_or_assigned_bp();
228 #endif // def OBSCURE_CINT_HACK
229         return *this;
230 }
231
232 inline
233 ex::ex(const basic & other)
234 {
235         /*debugmsg("ex ctor from basic",LOGLEVEL_CONSTRUCT);*/
236         construct_from_basic(other);
237 #ifdef OBSCURE_CINT_HACK
238         update_last_created_or_assigned_bp();
239 #endif // def OBSCURE_CINT_HACK
240 }
241
242 inline
243 ex::ex(int i)
244 {
245         /*debugmsg("ex ctor from int",LOGLEVEL_CONSTRUCT);*/
246         construct_from_int(i);
247 #ifdef OBSCURE_CINT_HACK
248         update_last_created_or_assigned_bp();
249 #endif // def OBSCURE_CINT_HACK
250 }
251
252 inline
253 ex::ex(unsigned int i)
254 {
255         /*debugmsg("ex ctor from unsigned int",LOGLEVEL_CONSTRUCT);*/
256         construct_from_uint(i);
257 #ifdef OBSCURE_CINT_HACK
258         update_last_created_or_assigned_bp();
259 #endif // def OBSCURE_CINT_HACK
260 }
261
262 inline
263 ex::ex(long i)
264 {
265         /*debugmsg("ex ctor from long",LOGLEVEL_CONSTRUCT);*/
266         construct_from_long(i);
267 #ifdef OBSCURE_CINT_HACK
268         update_last_created_or_assigned_bp();
269 #endif // def OBSCURE_CINT_HACK
270 }
271
272 inline
273 ex::ex(unsigned long i)
274 {
275         /*debugmsg("ex ctor from unsigned long",LOGLEVEL_CONSTRUCT);*/
276         construct_from_ulong(i);
277 #ifdef OBSCURE_CINT_HACK
278         update_last_created_or_assigned_bp();
279 #endif // def OBSCURE_CINT_HACK
280 }
281
282 inline
283 ex::ex(double const d)
284 {
285         /*debugmsg("ex ctor from double",LOGLEVEL_CONSTRUCT);*/
286         construct_from_double(d);
287 #ifdef OBSCURE_CINT_HACK
288         update_last_created_or_assigned_bp();
289 #endif // def OBSCURE_CINT_HACK
290 }
291
292 inline
293 ex::ex(const std::string &s, const ex &l)
294 {
295         /*debugmsg("ex ctor from string,lst",LOGLEVEL_CONSTRUCT);*/
296         construct_from_string_and_lst(s, l);
297 #ifdef OBSCURE_CINT_HACK
298         update_last_created_or_assigned_bp();
299 #endif // def OBSCURE_CINT_HACK
300 }
301
302 inline
303 int ex::compare(const ex & other) const
304 {
305         GINAC_ASSERT(bp!=0);
306         GINAC_ASSERT(other.bp!=0);
307         if (bp==other.bp)  // trivial case: both expressions point to same basic
308                 return 0;
309         return bp->compare(*other.bp);
310 }
311
312 inline
313 bool ex::is_equal(const ex & other) const
314 {
315         GINAC_ASSERT(bp!=0);
316         GINAC_ASSERT(other.bp!=0);
317         if (bp==other.bp)  // trivial case: both expressions point to same basic
318                 return true;
319         return bp->is_equal(*other.bp);
320 }
321
322
323 // utility functions
324 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
325 {
326         return e1.bp == e2.bp;
327 }
328
329 // wrapper functions around member functions
330 inline unsigned nops(const ex & thisex)
331 { return thisex.nops(); }
332
333 inline ex expand(const ex & thisex, unsigned options = 0)
334 { return thisex.expand(options); }
335
336 inline bool has(const ex & thisex, const ex & other)
337 { return thisex.has(other); }
338
339 inline int degree(const ex & thisex, const ex & s)
340 { return thisex.degree(s); }
341
342 inline int ldegree(const ex & thisex, const ex & s)
343 { return thisex.ldegree(s); }
344
345 inline ex coeff(const ex & thisex, const ex & s, int n=1)
346 { return thisex.coeff(s, n); }
347
348 inline ex numer(const ex & thisex)
349 { return thisex.numer(); }
350
351 inline ex denom(const ex & thisex)
352 { return thisex.denom(); }
353
354 inline ex normal(const ex & thisex, int level=0)
355 { return thisex.normal(level); }
356
357 inline ex to_rational(const ex & thisex, lst & repl_lst)
358 { return thisex.to_rational(repl_lst); }
359
360 inline ex collect(const ex & thisex, const ex & s)
361 { return thisex.collect(s); }
362
363 inline ex eval(const ex & thisex, int level = 0)
364 { return thisex.eval(level); }
365
366 inline ex evalf(const ex & thisex, int level = 0)
367 { return thisex.evalf(level); }
368
369 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
370 { return thisex.diff(s, nth); }
371
372 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
373 { return thisex.series(r, order, options); }
374
375 inline ex subs(const ex & thisex, const ex & e)
376 { return thisex.subs(e); }
377
378 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
379 { return thisex.subs(ls, lr); }
380
381 inline ex op(const ex & thisex, int i)
382 { return thisex.op(i); }
383
384 inline ex lhs(const ex & thisex)
385 { return thisex.lhs(); }
386
387 inline ex rhs(const ex & thisex)
388 { return thisex.rhs(); }
389
390 inline bool is_zero(const ex & thisex)
391 { return thisex.is_zero(); }
392
393 inline void swap(ex & e1, ex & e2)
394 { e1.swap(e2); }
395
396 } // namespace GiNaC
397
398 #endif // ndef __GINAC_EX_H__