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
38 // Sorry, this is the only constant to pollute the global scope, the other ones
39 // are defined in utils.h and not visible from outside.
40 extern const ex & _ex0(void); // single ex(numeric(0))
42 #define INLINE_EX_CONSTRUCTORS
44 /** Lightweight wrapper for GiNaC's symbolic objects. Basically all it does is
45 * to hold a pointer to the other objects, manage the reference counting and
46 * provide methods for manipulation of these objects. */
53 // default constructor, destructor, copy constructor assignment operator and helpers
56 #ifdef INLINE_EX_CONSTRUCTORS
59 GINAC_ASSERT(_ex0().bp!=0);
60 GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
63 #ifdef OBSCURE_CINT_HACK
64 update_last_created_or_assigned_bp();
65 #endif // def OBSCURE_CINT_HACK
69 #endif // def INLINE_EX_CONSTRUCTORS
72 #ifdef INLINE_EX_CONSTRUCTORS
75 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
76 if (--bp->refcount == 0) {
82 #endif // def INLINE_EX_CONSTRUCTORS
85 #ifdef INLINE_EX_CONSTRUCTORS
89 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
91 #ifdef OBSCURE_CINT_HACK
92 update_last_created_or_assigned_bp();
93 #endif // def OBSCURE_CINT_HACK
97 #endif // def INLINE_EX_CONSTRUCTORS
99 const ex & operator=(const ex & other)
100 #ifdef INLINE_EX_CONSTRUCTORS
103 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
104 GINAC_ASSERT(other.bp!=0);
105 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
106 ++other.bp->refcount;
107 basic * tmpbp=other.bp;
108 if (--bp->refcount==0) {
112 #ifdef OBSCURE_CINT_HACK
113 update_last_created_or_assigned_bp();
114 #endif // def OBSCURE_CINT_HACK
119 #endif // def INLINE_EX_CONSTRUCTORS
122 // other constructors
124 ex(const basic & other)
125 #ifdef INLINE_EX_CONSTRUCTORS
127 construct_from_basic(other);
128 #ifdef OBSCURE_CINT_HACK
129 update_last_created_or_assigned_bp();
130 #endif // def OBSCURE_CINT_HACK
134 #endif // def INLINE_EX_CONSTRUCTORS
137 #ifdef INLINE_EX_CONSTRUCTORS
139 construct_from_int(i);
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_uint(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_long(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_ulong(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_double(d);
188 #ifdef OBSCURE_CINT_HACK
189 update_last_created_or_assigned_bp();
190 #endif // def OBSCURE_CINT_HACK
194 #endif // def INLINE_EX_CONSTRUCTORS
196 /** Construct ex from string and a list of symbols. The input grammar is
197 * similar to the GiNaC output format. All symbols to be used in the
198 * expression must be specified in a lst in the second argument. Undefined
199 * symbols and other parser errors will throw an exception. */
200 ex(const std::string &s, const ex &l)
201 #ifdef INLINE_EX_CONSTRUCTORS
203 construct_from_string_and_lst(s, l);
204 #ifdef OBSCURE_CINT_HACK
205 update_last_created_or_assigned_bp();
206 #endif // def OBSCURE_CINT_HACK
210 #endif // def INLINE_EX_CONSTRUCTORS
213 // functions overriding virtual functions from bases classes
216 // new virtual functions which can be overridden by derived classes
219 // non-virtual functions in this class
221 void swap(ex & other);
222 void printraw(std::ostream & os) const;
223 void printtree(std::ostream & os, unsigned indent=0) const;
224 void print(std::ostream & os, unsigned upper_precedence=0) const;
225 void printcsrc(std::ostream & os, unsigned type, const char *var_name) const;
226 void dbgprint(void) const;
227 void dbgprinttree(void) const;
228 bool info(unsigned inf) const;
229 unsigned nops() const;
230 ex expand(unsigned options=0) const;
231 bool has(const ex & other) const;
232 int degree(const symbol & s) const;
233 int ldegree(const symbol & s) const;
234 ex coeff(const symbol & s, int n=1) const;
235 ex lcoeff(const symbol & s) const { return coeff(s, degree(s)); }
236 ex tcoeff(const symbol & s) const { return coeff(s, ldegree(s)); }
237 ex numer(void) const;
238 ex denom(void) const;
239 ex unit(const symbol &x) const;
240 ex content(const symbol &x) const;
241 numeric integer_content(void) const;
242 ex primpart(const symbol &x) const;
243 ex primpart(const symbol &x, const ex &cont) const;
244 ex normal(int level = 0) const;
245 ex to_rational(lst &repl_lst) const;
246 ex smod(const numeric &xi) const;
247 numeric max_coefficient(void) const;
248 ex collect(const symbol & s) const;
249 ex eval(int level = 0) const;
250 ex evalf(int level = 0) const;
251 ex diff(const symbol & s, unsigned nth = 1) const;
252 ex series(const ex & r, int order, unsigned options = 0) const;
253 ex subs(const lst & ls, const lst & lr) const;
254 ex subs(const ex & e) const;
255 exvector get_indices(void) const;
256 ex simplify_ncmul(const exvector & v) const;
257 ex operator[](const ex & index) const;
258 ex operator[](int i) const;
263 int compare(const ex & other) const
264 #ifdef INLINE_EX_CONSTRUCTORS
267 GINAC_ASSERT(other.bp!=0);
269 // special case: both expression point to same basic, trivially equal
272 return bp->compare(*other.bp);
276 #endif // def INLINE_EX_CONSTRUCTORS
277 bool is_equal(const ex & other) const
278 #ifdef INLINE_EX_CONSTRUCTORS
281 GINAC_ASSERT(other.bp!=0);
283 // special case: both expression point to same basic, trivially equal
286 return bp->is_equal(*other.bp);
290 #endif // def INLINE_EX_CONSTRUCTORS
291 bool is_zero(void) const {return compare(_ex0())==0;};
293 unsigned return_type(void) const;
294 unsigned return_type_tinfo(void) const;
295 unsigned gethash(void) const;
297 ex exadd(const ex & rh) const;
298 ex exmul(const ex & rh) const;
299 ex exncmul(const ex & rh) const;
301 void construct_from_basic(const basic & other);
302 void construct_from_int(int i);
303 void construct_from_uint(unsigned int i);
304 void construct_from_long(long i);
305 void construct_from_ulong(unsigned long i);
306 void construct_from_double(double d);
307 void construct_from_string_and_lst(const std::string &s, const ex &l);
308 void makewriteable();
310 #ifdef OBSCURE_CINT_HACK
312 static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
314 if (last_created_or_assigned_bp==0) return false;
315 if ((last_created_or_assigned_bp->flags &
316 status_flags::dynallocated)==0) return false;
317 if ((last_created_or_assigned_bp->flags &
318 status_flags::evaluated)==0) return false;
322 void update_last_created_or_assigned_bp(void)
324 if (last_created_or_assigned_bp!=0) {
325 if (--last_created_or_assigned_bp->refcount == 0) {
326 delete last_created_or_assigned_bp;
329 last_created_or_assigned_bp=bp;
330 ++last_created_or_assigned_bp->refcount;
331 last_created_or_assigned_exp=(long)(void *)(this);
333 #endif // def OBSCURE_CINT_HACK
339 #ifdef OBSCURE_CINT_HACK
340 static basic * last_created_or_assigned_bp;
341 static basic * dummy_bp;
342 static long last_created_or_assigned_exp;
343 #endif // def OBSCURE_CINT_HACK
347 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
349 return e1.bp == e2.bp;
352 // wrapper functions around member functions
353 inline unsigned nops(const ex & thisex)
354 { return thisex.nops(); }
356 inline ex expand(const ex & thisex, unsigned options = 0)
357 { return thisex.expand(options); }
359 inline bool has(const ex & thisex, const ex & other)
360 { return thisex.has(other); }
362 inline int degree(const ex & thisex, const symbol & s)
363 { return thisex.degree(s); }
365 inline int ldegree(const ex & thisex, const symbol & s)
366 { return thisex.ldegree(s); }
368 inline ex coeff(const ex & thisex, const symbol & s, int n=1)
369 { return thisex.coeff(s, n); }
371 inline ex numer(const ex & thisex)
372 { return thisex.numer(); }
374 inline ex denom(const ex & thisex)
375 { return thisex.denom(); }
377 inline ex normal(const ex & thisex, int level=0)
378 { return thisex.normal(level); }
380 inline ex to_rational(const ex & thisex, lst & repl_lst)
381 { return thisex.to_rational(repl_lst); }
383 inline ex collect(const ex & thisex, const symbol & s)
384 { return thisex.collect(s); }
386 inline ex eval(const ex & thisex, int level = 0)
387 { return thisex.eval(level); }
389 inline ex evalf(const ex & thisex, int level = 0)
390 { return thisex.evalf(level); }
392 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
393 { return thisex.diff(s, nth); }
395 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
396 { return thisex.series(r, order, options); }
398 inline ex subs(const ex & thisex, const ex & e)
399 { return thisex.subs(e); }
401 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
402 { return thisex.subs(ls, lr); }
404 inline ex op(const ex & thisex, int i)
405 { return thisex.op(i); }
407 inline ex lhs(const ex & thisex)
408 { return thisex.lhs(); }
410 inline ex rhs(const ex & thisex)
411 { return thisex.rhs(); }
413 inline bool is_zero(const ex & thisex)
414 { return thisex.is_zero(); }
416 inline void swap(ex & e1, ex & e2)
419 #ifndef NO_NAMESPACE_GINAC
421 #endif // ndef NO_NAMESPACE_GINAC
423 #endif // ndef __GINAC_EX_H__