3 * Interface to GiNaC's light-weight expression handles. */
6 * GiNaC Copyright (C) 1999-2001 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
125 // other constructors
127 ex(const basic & other)
128 #ifdef INLINE_EX_CONSTRUCTORS
130 construct_from_basic(other);
131 #ifdef OBSCURE_CINT_HACK
132 update_last_created_or_assigned_bp();
133 #endif // def OBSCURE_CINT_HACK
137 #endif // def INLINE_EX_CONSTRUCTORS
140 #ifdef INLINE_EX_CONSTRUCTORS
142 construct_from_int(i);
143 #ifdef OBSCURE_CINT_HACK
144 update_last_created_or_assigned_bp();
145 #endif // def OBSCURE_CINT_HACK
149 #endif // def INLINE_EX_CONSTRUCTORS
152 #ifdef INLINE_EX_CONSTRUCTORS
154 construct_from_uint(i);
155 #ifdef OBSCURE_CINT_HACK
156 update_last_created_or_assigned_bp();
157 #endif // def OBSCURE_CINT_HACK
161 #endif // def INLINE_EX_CONSTRUCTORS
164 #ifdef INLINE_EX_CONSTRUCTORS
166 construct_from_long(i);
167 #ifdef OBSCURE_CINT_HACK
168 update_last_created_or_assigned_bp();
169 #endif // def OBSCURE_CINT_HACK
173 #endif // def INLINE_EX_CONSTRUCTORS
176 #ifdef INLINE_EX_CONSTRUCTORS
178 construct_from_ulong(i);
179 #ifdef OBSCURE_CINT_HACK
180 update_last_created_or_assigned_bp();
181 #endif // def OBSCURE_CINT_HACK
185 #endif // def INLINE_EX_CONSTRUCTORS
188 #ifdef INLINE_EX_CONSTRUCTORS
190 construct_from_double(d);
191 #ifdef OBSCURE_CINT_HACK
192 update_last_created_or_assigned_bp();
193 #endif // def OBSCURE_CINT_HACK
197 #endif // def INLINE_EX_CONSTRUCTORS
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
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
213 #endif // def INLINE_EX_CONSTRUCTORS
216 // functions overriding virtual functions from bases classes
219 // new virtual functions which can be overridden by derived classes
222 // non-virtual functions in this class
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;
266 int compare(const ex & other) const
267 #ifdef INLINE_EX_CONSTRUCTORS
270 GINAC_ASSERT(other.bp!=0);
272 // special case: both expression point to same basic, trivially equal
275 return bp->compare(*other.bp);
279 #endif // def INLINE_EX_CONSTRUCTORS
280 bool is_equal(const ex & other) const
281 #ifdef INLINE_EX_CONSTRUCTORS
284 GINAC_ASSERT(other.bp!=0);
286 // special case: both expression point to same basic, trivially equal
289 return bp->is_equal(*other.bp);
293 #endif // def INLINE_EX_CONSTRUCTORS
294 bool is_zero(void) const {return compare(_ex0())==0;};
296 unsigned return_type(void) const;
297 unsigned return_type_tinfo(void) const;
298 unsigned gethash(void) const;
300 ex exadd(const ex & rh) const;
301 ex exmul(const ex & rh) const;
302 ex exncmul(const ex & rh) const;
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();
313 #ifdef OBSCURE_CINT_HACK
315 static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
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;
325 void update_last_created_or_assigned_bp(void)
327 if (last_created_or_assigned_bp!=0) {
328 if (--last_created_or_assigned_bp->refcount == 0) {
329 delete last_created_or_assigned_bp;
332 last_created_or_assigned_bp=bp;
333 ++last_created_or_assigned_bp->refcount;
334 last_created_or_assigned_exp=(long)(void *)(this);
336 #endif // def OBSCURE_CINT_HACK
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
350 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
352 return e1.bp == e2.bp;
355 // wrapper functions around member functions
356 inline unsigned nops(const ex & thisex)
357 { return thisex.nops(); }
359 inline ex expand(const ex & thisex, unsigned options = 0)
360 { return thisex.expand(options); }
362 inline bool has(const ex & thisex, const ex & other)
363 { return thisex.has(other); }
365 inline int degree(const ex & thisex, const symbol & s)
366 { return thisex.degree(s); }
368 inline int ldegree(const ex & thisex, const symbol & s)
369 { return thisex.ldegree(s); }
371 inline ex coeff(const ex & thisex, const symbol & s, int n=1)
372 { return thisex.coeff(s, n); }
374 inline ex numer(const ex & thisex)
375 { return thisex.numer(); }
377 inline ex denom(const ex & thisex)
378 { return thisex.denom(); }
380 inline ex normal(const ex & thisex, int level=0)
381 { return thisex.normal(level); }
383 inline ex to_rational(const ex & thisex, lst & repl_lst)
384 { return thisex.to_rational(repl_lst); }
386 inline ex collect(const ex & thisex, const symbol & s)
387 { return thisex.collect(s); }
389 inline ex eval(const ex & thisex, int level = 0)
390 { return thisex.eval(level); }
392 inline ex evalf(const ex & thisex, int level = 0)
393 { return thisex.evalf(level); }
395 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
396 { return thisex.diff(s, nth); }
398 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
399 { return thisex.series(r, order, options); }
401 inline ex subs(const ex & thisex, const ex & e)
402 { return thisex.subs(e); }
404 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
405 { return thisex.subs(ls, lr); }
407 inline ex op(const ex & thisex, int i)
408 { return thisex.op(i); }
410 inline ex lhs(const ex & thisex)
411 { return thisex.lhs(); }
413 inline ex rhs(const ex & thisex)
414 { return thisex.rhs(); }
416 inline bool is_zero(const ex & thisex)
417 { return thisex.is_zero(); }
419 inline void swap(ex & e1, ex & e2)
422 #ifndef NO_NAMESPACE_GINAC
424 #endif // ndef NO_NAMESPACE_GINAC
426 #endif // ndef __GINAC_EX_H__