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 #define INLINE_EX_CONSTRUCTORS
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. */
56 // default constructor, destructor, copy constructor assignment operator and helpers
59 #ifdef INLINE_EX_CONSTRUCTORS
62 GINAC_ASSERT(_ex0().bp!=0);
63 GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
66 #ifdef OBSCURE_CINT_HACK
67 update_last_created_or_assigned_bp();
68 #endif // def OBSCURE_CINT_HACK
72 #endif // def INLINE_EX_CONSTRUCTORS
75 #ifdef INLINE_EX_CONSTRUCTORS
78 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
79 if (--bp->refcount == 0) {
85 #endif // def INLINE_EX_CONSTRUCTORS
88 #ifdef INLINE_EX_CONSTRUCTORS
92 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
94 #ifdef OBSCURE_CINT_HACK
95 update_last_created_or_assigned_bp();
96 #endif // def OBSCURE_CINT_HACK
100 #endif // def INLINE_EX_CONSTRUCTORS
102 const ex & operator=(const ex & other)
103 #ifdef INLINE_EX_CONSTRUCTORS
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) {
115 #ifdef OBSCURE_CINT_HACK
116 update_last_created_or_assigned_bp();
117 #endif // def OBSCURE_CINT_HACK
122 #endif // def INLINE_EX_CONSTRUCTORS
124 #ifdef CINT_CONVERSION_WORKAROUND
125 // workaround to fix the missing automatic derived->basic->ex conversion
126 const ex & operator=(const basic & other)
128 return *this=ex(other);
130 #endif // def CINT_CONVERSION_WORKAROUND
132 // other constructors
134 ex(const basic & other)
135 #ifdef INLINE_EX_CONSTRUCTORS
137 construct_from_basic(other);
138 #ifdef OBSCURE_CINT_HACK
139 update_last_created_or_assigned_bp();
140 #endif // def OBSCURE_CINT_HACK
144 #endif // def INLINE_EX_CONSTRUCTORS
147 #ifdef INLINE_EX_CONSTRUCTORS
149 construct_from_int(i);
150 #ifdef OBSCURE_CINT_HACK
151 update_last_created_or_assigned_bp();
152 #endif // def OBSCURE_CINT_HACK
156 #endif // def INLINE_EX_CONSTRUCTORS
159 #ifdef INLINE_EX_CONSTRUCTORS
161 construct_from_uint(i);
162 #ifdef OBSCURE_CINT_HACK
163 update_last_created_or_assigned_bp();
164 #endif // def OBSCURE_CINT_HACK
168 #endif // def INLINE_EX_CONSTRUCTORS
171 #ifdef INLINE_EX_CONSTRUCTORS
173 construct_from_long(i);
174 #ifdef OBSCURE_CINT_HACK
175 update_last_created_or_assigned_bp();
176 #endif // def OBSCURE_CINT_HACK
180 #endif // def INLINE_EX_CONSTRUCTORS
183 #ifdef INLINE_EX_CONSTRUCTORS
185 construct_from_ulong(i);
186 #ifdef OBSCURE_CINT_HACK
187 update_last_created_or_assigned_bp();
188 #endif // def OBSCURE_CINT_HACK
192 #endif // def INLINE_EX_CONSTRUCTORS
195 #ifdef INLINE_EX_CONSTRUCTORS
197 construct_from_double(d);
198 #ifdef OBSCURE_CINT_HACK
199 update_last_created_or_assigned_bp();
200 #endif // def OBSCURE_CINT_HACK
204 #endif // def INLINE_EX_CONSTRUCTORS
206 /** Construct ex from string and a list of symbols. The input grammar is
207 * similar to the GiNaC output format. All symbols to be used in the
208 * expression must be specified in a lst in the second argument. Undefined
209 * symbols and other parser errors will throw an exception. */
210 ex(const string &s, const ex &l)
211 #ifdef INLINE_EX_CONSTRUCTORS
213 construct_from_string_and_lst(s, l);
214 #ifdef OBSCURE_CINT_HACK
215 update_last_created_or_assigned_bp();
216 #endif // def OBSCURE_CINT_HACK
220 #endif // def INLINE_EX_CONSTRUCTORS
223 // functions overriding virtual functions from bases classes
226 // new virtual functions which can be overridden by derived classes
229 // non-virtual functions in this class
231 void swap(ex & other);
232 void printraw(ostream & os) const;
233 void printtree(ostream & os, unsigned indent=0) const;
234 void print(ostream & os, unsigned upper_precedence=0) const;
235 void printcsrc(ostream & os, unsigned type, const char *var_name) const;
236 void dbgprint(void) const;
237 void dbgprinttree(void) const;
238 bool info(unsigned inf) const;
239 unsigned nops() const;
240 ex expand(unsigned options=0) const;
241 bool has(const ex & other) const;
242 #ifdef CINT_CONVERSION_WORKAROUND
243 bool has(basic const & other) const { return has(ex(other)); }
244 #endif // def CINT_CONVERSION_WORKAROUND
245 int degree(const symbol & s) const;
246 int ldegree(const symbol & s) const;
247 ex coeff(const symbol & s, int n=1) const;
248 ex lcoeff(const symbol & s) const { return coeff(s, degree(s)); }
249 ex tcoeff(const symbol & s) const { return coeff(s, ldegree(s)); }
250 ex numer(void) const;
251 ex denom(void) const;
252 ex unit(const symbol &x) const;
253 ex content(const symbol &x) const;
254 numeric integer_content(void) const;
255 ex primpart(const symbol &x) const;
256 ex primpart(const symbol &x, const ex &cont) const;
257 #ifdef CINT_CONVERSION_WORKAROUND
258 ex primpart(const symbol &x, const basic &cont) const { return primpart(x,ex(cont)); }
259 #endif // def CINT_CONVERSION_WORKAROUND
260 ex normal(int level = 0) const;
261 ex to_rational(lst &repl_lst) const;
262 ex smod(const numeric &xi) const;
263 numeric max_coefficient(void) const;
264 ex collect(const symbol & s) const;
265 ex eval(int level = 0) const;
266 ex evalf(int level = 0) const;
267 ex diff(const symbol & s, unsigned nth = 1) const;
268 ex series(const ex & r, int order) const;
269 #ifdef CINT_CONVERSION_WORKAROUND
270 ex series(const basic & r, int order) const { return series(ex(r),order); }
271 #endif // def CINT_CONVERSION_WORKAROUND
272 ex subs(const lst & ls, const lst & lr) const;
273 ex subs(const ex & e) const;
274 #ifdef CINT_CONVERSION_WORKAROUND
275 ex subs(const basic & e) const { return subs(ex(e)); }
276 #endif // def CINT_CONVERSION_WORKAROUND
277 exvector get_indices(void) const;
278 ex simplify_ncmul(const exvector & v) const;
279 ex operator[](const ex & index) const;
280 #ifdef CINT_CONVERSION_WORKAROUND
281 ex operator[](const basic & index) const { return operator[](ex(index)); }
282 #endif // def CINT_CONVERSION_WORKAROUND
283 ex operator[](int i) const;
288 int compare(const ex & other) const
289 #ifdef INLINE_EX_CONSTRUCTORS
292 GINAC_ASSERT(other.bp!=0);
294 // special case: both expression point to same basic, trivially equal
297 return bp->compare(*other.bp);
301 #endif // def INLINE_EX_CONSTRUCTORS
302 #ifdef CINT_CONVERSION_WORKAROUND
303 int compare(const basic & other) const { return compare(ex(other)); }
304 #endif // def CINT_CONVERSION_WORKAROUND
305 bool is_equal(const ex & other) const
306 #ifdef INLINE_EX_CONSTRUCTORS
309 GINAC_ASSERT(other.bp!=0);
311 // special case: both expression point to same basic, trivially equal
314 return bp->is_equal(*other.bp);
318 #endif // def INLINE_EX_CONSTRUCTORS
319 #ifdef CINT_CONVERSION_WORKAROUND
320 bool is_equal(const basic & other) const { return is_equal(ex(other)); }
321 #endif // def CINT_CONVERSION_WORKAROUND
322 bool is_zero(void) const {return compare(_ex0())==0;};
324 unsigned return_type(void) const;
325 unsigned return_type_tinfo(void) const;
326 unsigned gethash(void) const;
328 ex exadd(const ex & rh) const;
329 #ifdef CINT_CONVERSION_WORKAROUND
330 ex exadd(const basic & rh) const { return exadd(ex(rh)); }
331 #endif // def CINT_CONVERSION_WORKAROUND
332 ex exmul(const ex & rh) const;
333 #ifdef CINT_CONVERSION_WORKAROUND
334 ex exmul(const basic & rh) const { return exmul(ex(rh)); }
335 #endif // def CINT_CONVERSION_WORKAROUND
336 ex exncmul(const ex & rh) const;
337 #ifdef CINT_CONVERSION_WORKAROUND
338 ex exncmul(const basic & rh) const { return exncmul(ex(rh)); }
339 #endif // def CINT_CONVERSION_WORKAROUND
341 void construct_from_basic(const basic & other);
342 void construct_from_int(int i);
343 void construct_from_uint(unsigned int i);
344 void construct_from_long(long i);
345 void construct_from_ulong(unsigned long i);
346 void construct_from_double(double d);
347 void construct_from_string_and_lst(const string &s, const ex &l);
348 void makewriteable();
350 #ifdef OBSCURE_CINT_HACK
352 static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
354 if (last_created_or_assigned_bp==0) return false;
355 if ((last_created_or_assigned_bp->flags &
356 status_flags::dynallocated)==0) return false;
357 if ((last_created_or_assigned_bp->flags &
358 status_flags::evaluated)==0) return false;
362 void update_last_created_or_assigned_bp(void)
364 if (last_created_or_assigned_bp!=0) {
365 if (--last_created_or_assigned_bp->refcount == 0) {
366 delete last_created_or_assigned_bp;
369 last_created_or_assigned_bp=bp;
370 ++last_created_or_assigned_bp->refcount;
371 last_created_or_assigned_exp=(long)(void *)(this);
373 #endif // def OBSCURE_CINT_HACK
379 #ifdef OBSCURE_CINT_HACK
380 static basic * last_created_or_assigned_bp;
381 static basic * dummy_bp;
382 static long last_created_or_assigned_exp;
383 #endif // def OBSCURE_CINT_HACK
387 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
389 return e1.bp == e2.bp;
392 // wrapper functions around member functions
393 inline unsigned nops(const ex & thisex)
394 { return thisex.nops(); }
396 inline ex expand(const ex & thisex, unsigned options = 0)
397 { return thisex.expand(options); }
399 inline bool has(const ex & thisex, const ex & other)
400 { return thisex.has(other); }
402 inline int degree(const ex & thisex, const symbol & s)
403 { return thisex.degree(s); }
405 inline int ldegree(const ex & thisex, const symbol & s)
406 { return thisex.ldegree(s); }
408 inline ex coeff(const ex & thisex, const symbol & s, int n=1)
409 { return thisex.coeff(s, n); }
411 inline ex numer(const ex & thisex)
412 { return thisex.numer(); }
414 inline ex denom(const ex & thisex)
415 { return thisex.denom(); }
417 inline ex normal(const ex & thisex, int level=0)
418 { return thisex.normal(level); }
420 inline ex to_rational(const ex & thisex, lst & repl_lst)
421 { return thisex.to_rational(repl_lst); }
423 inline ex collect(const ex & thisex, const symbol & s)
424 { return thisex.collect(s); }
426 inline ex eval(const ex & thisex, int level = 0)
427 { return thisex.eval(level); }
429 inline ex evalf(const ex & thisex, int level = 0)
430 { return thisex.evalf(level); }
432 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
433 { return thisex.diff(s, nth); }
435 inline ex series(const ex & thisex, const ex & r, int order)
436 { return thisex.series(r, order); }
438 inline ex subs(const ex & thisex, const ex & e)
439 { return thisex.subs(e); }
441 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
442 { return thisex.subs(ls, lr); }
444 inline ex op(const ex & thisex, int i)
445 { return thisex.op(i); }
447 inline ex lhs(const ex & thisex)
448 { return thisex.lhs(); }
450 inline ex rhs(const ex & thisex)
451 { return thisex.rhs(); }
453 inline bool is_zero(const ex & thisex)
454 { return thisex.is_zero(); }
456 inline void swap(ex & e1, ex & e2)
459 #ifndef NO_NAMESPACE_GINAC
461 #endif // ndef NO_NAMESPACE_GINAC
463 #endif // ndef __GINAC_EX_H__