3 * Interface to GiNaC's light-weight expression handles. */
6 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
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.
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.
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
23 #ifndef __GINAC_EX_H__
24 #define __GINAC_EX_H__
28 #include "operators.h"
30 #ifndef NO_NAMESPACE_GINAC
32 #endif // ndef NO_NAMESPACE_GINAC
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))
45 // typedef vector<ex> exvector;
47 #define INLINE_EX_CONSTRUCTORS
49 /** Lightweight wrapper for GiNaC's symbolic objects. Basically all it does is
50 * to hold a pointer to the other objects, manage the reference counting and
51 * provide methods for manipulation of these objects. */
58 // default constructor, destructor, copy constructor assignment operator and helpers
61 #ifdef INLINE_EX_CONSTRUCTORS
64 GINAC_ASSERT(_ex0().bp!=0);
65 GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
68 #ifdef OBSCURE_CINT_HACK
69 update_last_created_or_assigned_bp();
70 #endif // def OBSCURE_CINT_HACK
74 #endif // def INLINE_EX_CONSTRUCTORS
77 #ifdef INLINE_EX_CONSTRUCTORS
80 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
81 if (--bp->refcount == 0) {
87 #endif // def INLINE_EX_CONSTRUCTORS
90 #ifdef INLINE_EX_CONSTRUCTORS
94 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
96 #ifdef OBSCURE_CINT_HACK
97 update_last_created_or_assigned_bp();
98 #endif // def OBSCURE_CINT_HACK
102 #endif // def INLINE_EX_CONSTRUCTORS
104 const ex & operator=(const ex & other)
105 #ifdef INLINE_EX_CONSTRUCTORS
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) {
117 #ifdef OBSCURE_CINT_HACK
118 update_last_created_or_assigned_bp();
119 #endif // def OBSCURE_CINT_HACK
124 #endif // def INLINE_EX_CONSTRUCTORS
126 #ifdef CINT_CONVERSION_WORKAROUND
127 // workaround to fix the missing automatic derived->basic->ex conversion
128 const ex & operator=(const basic & other)
130 return *this=ex(other);
132 #endif // def CINT_CONVERSION_WORKAROUND
134 // other constructors
136 ex(const basic & other)
137 #ifdef INLINE_EX_CONSTRUCTORS
139 construct_from_basic(other);
140 #ifdef OBSCURE_CINT_HACK
141 update_last_created_or_assigned_bp();
142 #endif // def OBSCURE_CINT_HACK
146 #endif // def INLINE_EX_CONSTRUCTORS
149 #ifdef INLINE_EX_CONSTRUCTORS
151 construct_from_int(i);
152 #ifdef OBSCURE_CINT_HACK
153 update_last_created_or_assigned_bp();
154 #endif // def OBSCURE_CINT_HACK
158 #endif // def INLINE_EX_CONSTRUCTORS
161 #ifdef INLINE_EX_CONSTRUCTORS
163 construct_from_uint(i);
164 #ifdef OBSCURE_CINT_HACK
165 update_last_created_or_assigned_bp();
166 #endif // def OBSCURE_CINT_HACK
170 #endif // def INLINE_EX_CONSTRUCTORS
173 #ifdef INLINE_EX_CONSTRUCTORS
175 construct_from_long(i);
176 #ifdef OBSCURE_CINT_HACK
177 update_last_created_or_assigned_bp();
178 #endif // def OBSCURE_CINT_HACK
182 #endif // def INLINE_EX_CONSTRUCTORS
185 #ifdef INLINE_EX_CONSTRUCTORS
187 construct_from_ulong(i);
188 #ifdef OBSCURE_CINT_HACK
189 update_last_created_or_assigned_bp();
190 #endif // def OBSCURE_CINT_HACK
194 #endif // def INLINE_EX_CONSTRUCTORS
197 #ifdef INLINE_EX_CONSTRUCTORS
199 construct_from_double(d);
200 #ifdef OBSCURE_CINT_HACK
201 update_last_created_or_assigned_bp();
202 #endif // def OBSCURE_CINT_HACK
206 #endif // def INLINE_EX_CONSTRUCTORS
208 /** Construct ex from string and a list of symbols. The input grammar is
209 * similar to the GiNaC output format. All symbols to be used in the
210 * expression must be specified in a lst in the second argument. Undefined
211 * symbols and other parser errors will throw an exception. */
212 ex(const string &s, const ex &l)
213 #ifdef INLINE_EX_CONSTRUCTORS
215 construct_from_string_and_lst(s, l);
216 #ifdef OBSCURE_CINT_HACK
217 update_last_created_or_assigned_bp();
218 #endif // def OBSCURE_CINT_HACK
222 #endif // def INLINE_EX_CONSTRUCTORS
225 // functions overriding virtual functions from bases classes
228 // new virtual functions which can be overridden by derived classes
231 // non-virtual functions in this class
233 void swap(ex & other);
234 void printraw(ostream & os) const;
235 void printtree(ostream & os, unsigned indent=0) const;
236 void print(ostream & os, unsigned upper_precedence=0) const;
237 void printcsrc(ostream & os, unsigned type, const char *var_name) const;
238 void dbgprint(void) const;
239 void dbgprinttree(void) const;
240 bool info(unsigned inf) const;
241 unsigned nops() const;
242 ex expand(unsigned options=0) const;
243 bool has(const ex & other) const;
244 #ifdef CINT_CONVERSION_WORKAROUND
245 bool has(basic const & other) const { return has(ex(other)); }
246 #endif // def CINT_CONVERSION_WORKAROUND
247 int degree(const symbol & s) const;
248 int ldegree(const symbol & s) const;
249 ex coeff(const symbol & s, int n=1) const;
250 ex lcoeff(const symbol & s) const { return coeff(s, degree(s)); }
251 ex tcoeff(const symbol & s) const { return coeff(s, ldegree(s)); }
252 ex numer(void) const;
253 ex denom(void) const;
254 ex unit(const symbol &x) const;
255 ex content(const symbol &x) const;
256 numeric integer_content(void) const;
257 ex primpart(const symbol &x) const;
258 ex primpart(const symbol &x, const ex &cont) const;
259 #ifdef CINT_CONVERSION_WORKAROUND
260 ex primpart(const symbol &x, const basic &cont) const { return primpart(x,ex(cont)); }
261 #endif // def CINT_CONVERSION_WORKAROUND
262 ex normal(int level = 0) const;
263 ex to_rational(lst &repl_lst) const;
264 ex smod(const numeric &xi) const;
265 numeric max_coefficient(void) const;
266 ex collect(const symbol & s) const;
267 ex eval(int level = 0) const;
268 ex evalf(int level = 0) const;
269 ex diff(const symbol & s, unsigned nth = 1) const;
270 ex series(const ex & r, int order) const;
271 #ifdef CINT_CONVERSION_WORKAROUND
272 ex series(const basic & r, int order) const { return series(ex(r),order); }
273 #endif // def CINT_CONVERSION_WORKAROUND
274 ex subs(const lst & ls, const lst & lr) const;
275 ex subs(const ex & e) const;
276 #ifdef CINT_CONVERSION_WORKAROUND
277 ex subs(const basic & e) const { return subs(ex(e)); }
278 #endif // def CINT_CONVERSION_WORKAROUND
279 exvector get_indices(void) const;
280 ex simplify_ncmul(const exvector & v) const;
281 ex operator[](const ex & index) const;
282 #ifdef CINT_CONVERSION_WORKAROUND
283 ex operator[](const basic & index) const { return operator[](ex(index)); }
284 #endif // def CINT_CONVERSION_WORKAROUND
285 ex operator[](int i) const;
290 int compare(const ex & other) const
291 #ifdef INLINE_EX_CONSTRUCTORS
294 GINAC_ASSERT(other.bp!=0);
296 // special case: both expression point to same basic, trivially equal
299 return bp->compare(*other.bp);
303 #endif // def INLINE_EX_CONSTRUCTORS
304 #ifdef CINT_CONVERSION_WORKAROUND
305 int compare(const basic & other) const { return compare(ex(other)); }
306 #endif // def CINT_CONVERSION_WORKAROUND
307 bool is_equal(const ex & other) const
308 #ifdef INLINE_EX_CONSTRUCTORS
311 GINAC_ASSERT(other.bp!=0);
313 // special case: both expression point to same basic, trivially equal
316 return bp->is_equal(*other.bp);
320 #endif // def INLINE_EX_CONSTRUCTORS
321 #ifdef CINT_CONVERSION_WORKAROUND
322 bool is_equal(const basic & other) const { return is_equal(ex(other)); }
323 #endif // def CINT_CONVERSION_WORKAROUND
324 bool is_zero(void) const {return compare(_ex0())==0;};
326 unsigned return_type(void) const;
327 unsigned return_type_tinfo(void) const;
328 unsigned gethash(void) const;
330 ex exadd(const ex & rh) const;
331 #ifdef CINT_CONVERSION_WORKAROUND
332 ex exadd(const basic & rh) const { return exadd(ex(rh)); }
333 #endif // def CINT_CONVERSION_WORKAROUND
334 ex exmul(const ex & rh) const;
335 #ifdef CINT_CONVERSION_WORKAROUND
336 ex exmul(const basic & rh) const { return exmul(ex(rh)); }
337 #endif // def CINT_CONVERSION_WORKAROUND
338 ex exncmul(const ex & rh) const;
339 #ifdef CINT_CONVERSION_WORKAROUND
340 ex exncmul(const basic & rh) const { return exncmul(ex(rh)); }
341 #endif // def CINT_CONVERSION_WORKAROUND
343 void construct_from_basic(const basic & other);
344 void construct_from_int(int i);
345 void construct_from_uint(unsigned int i);
346 void construct_from_long(long i);
347 void construct_from_ulong(unsigned long i);
348 void construct_from_double(double d);
349 void construct_from_string_and_lst(const string &s, const ex &l);
350 void makewriteable();
352 #ifdef OBSCURE_CINT_HACK
354 static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
356 if (last_created_or_assigned_bp==0) return false;
357 if ((last_created_or_assigned_bp->flags &
358 status_flags::dynallocated)==0) return false;
359 if ((last_created_or_assigned_bp->flags &
360 status_flags::evaluated)==0) return false;
364 void update_last_created_or_assigned_bp(void)
366 if (last_created_or_assigned_bp!=0) {
367 if (--last_created_or_assigned_bp->refcount == 0) {
368 delete last_created_or_assigned_bp;
371 last_created_or_assigned_bp=bp;
372 ++last_created_or_assigned_bp->refcount;
373 last_created_or_assigned_exp=(long)(void *)(this);
375 #endif // def OBSCURE_CINT_HACK
381 #ifdef OBSCURE_CINT_HACK
382 static basic * last_created_or_assigned_bp;
383 static basic * dummy_bp;
384 static long last_created_or_assigned_exp;
385 #endif // def OBSCURE_CINT_HACK
389 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
391 return e1.bp == e2.bp;
394 // wrapper functions around member functions
395 inline unsigned nops(const ex & thisex)
396 { return thisex.nops(); }
398 inline ex expand(const ex & thisex, unsigned options = 0)
399 { return thisex.expand(options); }
401 inline bool has(const ex & thisex, const ex & other)
402 { return thisex.has(other); }
404 inline int degree(const ex & thisex, const symbol & s)
405 { return thisex.degree(s); }
407 inline int ldegree(const ex & thisex, const symbol & s)
408 { return thisex.ldegree(s); }
410 inline ex coeff(const ex & thisex, const symbol & s, int n=1)
411 { return thisex.coeff(s, n); }
413 inline ex numer(const ex & thisex)
414 { return thisex.numer(); }
416 inline ex denom(const ex & thisex)
417 { return thisex.denom(); }
419 inline ex normal(const ex & thisex, int level=0)
420 { return thisex.normal(level); }
422 inline ex to_rational(const ex & thisex, lst & repl_lst)
423 { return thisex.to_rational(repl_lst); }
425 inline ex collect(const ex & thisex, const symbol & s)
426 { return thisex.collect(s); }
428 inline ex eval(const ex & thisex, int level = 0)
429 { return thisex.eval(level); }
431 inline ex evalf(const ex & thisex, int level = 0)
432 { return thisex.evalf(level); }
434 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
435 { return thisex.diff(s, nth); }
437 inline ex series(const ex & thisex, const ex & r, int order)
438 { return thisex.series(r, order); }
440 inline ex subs(const ex & thisex, const ex & e)
441 { return thisex.subs(e); }
443 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
444 { return thisex.subs(ls, lr); }
446 inline ex op(const ex & thisex, int i)
447 { return thisex.op(i); }
449 inline ex lhs(const ex & thisex)
450 { return thisex.lhs(); }
452 inline ex rhs(const ex & thisex)
453 { return thisex.rhs(); }
455 inline bool is_zero(const ex & thisex)
456 { return thisex.is_zero(); }
458 inline void swap(ex & e1, ex & e2)
461 #ifndef NO_NAMESPACE_GINAC
463 #endif // ndef NO_NAMESPACE_GINAC
465 #endif // ndef __GINAC_EX_H__