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__
27 #include "operators.h"
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.
34 extern const ex & _ex0(void); ///< single ex(numeric(0))
38 class scalar_products;
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.) */
50 // default ctor, dtor, copy ctor assignment operator and helpers
55 ex & operator=(const ex & other);
58 ex(const basic & other);
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);
70 // functions overriding virtual functions from bases classes
73 // new virtual functions which can be overridden by derived classes
76 // non-virtual functions in this class
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 { return bp->info(inf); }
84 unsigned nops() const { return bp->nops(); }
85 ex expand(unsigned options=0) const;
86 bool has(const ex & other) const { return bp->has(other); }
87 int degree(const ex & s) const { return bp->degree(s); }
88 int ldegree(const ex & s) const { return bp->ldegree(s); }
89 ex coeff(const ex & s, int n = 1) const { return bp->coeff(s, n); }
90 ex lcoeff(const ex & s) const { return coeff(s, degree(s)); }
91 ex tcoeff(const ex & s) const { return coeff(s, ldegree(s)); }
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, bool distributed = false) const { return bp->collect(s, distributed); }
104 ex eval(int level = 0) const { return bp->eval(level); }
105 ex evalf(int level = 0) const { return bp->evalf(level); }
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 { return bp->subs(ls, lr); }
109 ex subs(const ex & e) const { return bp->subs(e); }
110 exvector get_free_indices(void) const { return bp->get_free_indices(); }
111 ex simplify_indexed(void) const;
112 ex simplify_indexed(const scalar_products & sp) const;
113 ex simplify_ncmul(const exvector & v) const { return bp->simplify_ncmul(v); }
114 ex operator[](const ex & index) const;
115 ex operator[](int i) const;
116 ex op(int i) const { return bp->op(i); }
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()); }
124 unsigned return_type(void) const { return bp->return_type(); }
125 unsigned return_type_tinfo(void) const { return bp->return_type_tinfo(); }
126 unsigned gethash(void) const { return bp->gethash(); }
128 void construct_from_basic(const basic & other);
129 void construct_from_int(int i);
130 void construct_from_uint(unsigned int i);
131 void construct_from_long(long i);
132 void construct_from_ulong(unsigned long i);
133 void construct_from_double(double d);
134 void construct_from_string_and_lst(const std::string &s, const ex &l);
135 void makewriteable();
137 #ifdef OBSCURE_CINT_HACK
139 static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
141 if (last_created_or_assigned_bp==0) return false;
142 if ((last_created_or_assigned_bp->flags &
143 status_flags::dynallocated)==0) return false;
144 if ((last_created_or_assigned_bp->flags &
145 status_flags::evaluated)==0) return false;
149 void update_last_created_or_assigned_bp(void)
151 if (last_created_or_assigned_bp!=0) {
152 if (--last_created_or_assigned_bp->refcount == 0) {
153 delete last_created_or_assigned_bp;
156 last_created_or_assigned_bp = bp;
157 ++last_created_or_assigned_bp->refcount;
158 last_created_or_assigned_exp = (long)(void *)(this);
160 #endif // def OBSCURE_CINT_HACK
165 basic *bp; ///< pointer to basic object managed by this
166 #ifdef OBSCURE_CINT_HACK
167 static basic * last_created_or_assigned_bp;
168 static basic * dummy_bp;
169 static long last_created_or_assigned_exp;
170 #endif // def OBSCURE_CINT_HACK
174 // performance-critical inlined method implementations
177 ex::ex() : bp(_ex0().bp)
179 /*debugmsg("ex default ctor",LOGLEVEL_CONSTRUCT);*/
180 GINAC_ASSERT(_ex0().bp!=0);
181 GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
184 #ifdef OBSCURE_CINT_HACK
185 update_last_created_or_assigned_bp();
186 #endif // def OBSCURE_CINT_HACK
192 /*debugmsg("ex dtor",LOGLEVEL_DESTRUCT);*/
194 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
195 if (--bp->refcount == 0)
200 ex::ex(const ex & other) : bp(other.bp)
202 /*debugmsg("ex copy ctor",LOGLEVEL_CONSTRUCT);*/
204 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
206 #ifdef OBSCURE_CINT_HACK
207 update_last_created_or_assigned_bp();
208 #endif // def OBSCURE_CINT_HACK
212 ex & ex::operator=(const ex & other)
214 /*debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);*/
216 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
217 GINAC_ASSERT(other.bp!=0);
218 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
219 // NB: must first increment other.bp->refcount, since other might be *this.
220 ++other.bp->refcount;
221 if (--bp->refcount==0)
224 #ifdef OBSCURE_CINT_HACK
225 update_last_created_or_assigned_bp();
226 #endif // def OBSCURE_CINT_HACK
231 ex::ex(const basic & other)
233 /*debugmsg("ex ctor from basic",LOGLEVEL_CONSTRUCT);*/
234 construct_from_basic(other);
235 #ifdef OBSCURE_CINT_HACK
236 update_last_created_or_assigned_bp();
237 #endif // def OBSCURE_CINT_HACK
243 /*debugmsg("ex ctor from int",LOGLEVEL_CONSTRUCT);*/
244 construct_from_int(i);
245 #ifdef OBSCURE_CINT_HACK
246 update_last_created_or_assigned_bp();
247 #endif // def OBSCURE_CINT_HACK
251 ex::ex(unsigned int i)
253 /*debugmsg("ex ctor from unsigned int",LOGLEVEL_CONSTRUCT);*/
254 construct_from_uint(i);
255 #ifdef OBSCURE_CINT_HACK
256 update_last_created_or_assigned_bp();
257 #endif // def OBSCURE_CINT_HACK
263 /*debugmsg("ex ctor from long",LOGLEVEL_CONSTRUCT);*/
264 construct_from_long(i);
265 #ifdef OBSCURE_CINT_HACK
266 update_last_created_or_assigned_bp();
267 #endif // def OBSCURE_CINT_HACK
271 ex::ex(unsigned long i)
273 /*debugmsg("ex ctor from unsigned long",LOGLEVEL_CONSTRUCT);*/
274 construct_from_ulong(i);
275 #ifdef OBSCURE_CINT_HACK
276 update_last_created_or_assigned_bp();
277 #endif // def OBSCURE_CINT_HACK
281 ex::ex(double const d)
283 /*debugmsg("ex ctor from double",LOGLEVEL_CONSTRUCT);*/
284 construct_from_double(d);
285 #ifdef OBSCURE_CINT_HACK
286 update_last_created_or_assigned_bp();
287 #endif // def OBSCURE_CINT_HACK
291 ex::ex(const std::string &s, const ex &l)
293 /*debugmsg("ex ctor from string,lst",LOGLEVEL_CONSTRUCT);*/
294 construct_from_string_and_lst(s, l);
295 #ifdef OBSCURE_CINT_HACK
296 update_last_created_or_assigned_bp();
297 #endif // def OBSCURE_CINT_HACK
301 int ex::compare(const ex & other) const
304 GINAC_ASSERT(other.bp!=0);
305 if (bp==other.bp) // trivial case: both expressions point to same basic
307 return bp->compare(*other.bp);
311 bool ex::is_equal(const ex & other) const
314 GINAC_ASSERT(other.bp!=0);
315 if (bp==other.bp) // trivial case: both expressions point to same basic
317 return bp->is_equal(*other.bp);
322 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
324 return e1.bp == e2.bp;
327 // wrapper functions around member functions
328 inline unsigned nops(const ex & thisex)
329 { return thisex.nops(); }
331 inline ex expand(const ex & thisex, unsigned options = 0)
332 { return thisex.expand(options); }
334 inline bool has(const ex & thisex, const ex & other)
335 { return thisex.has(other); }
337 inline int degree(const ex & thisex, const ex & s)
338 { return thisex.degree(s); }
340 inline int ldegree(const ex & thisex, const ex & s)
341 { return thisex.ldegree(s); }
343 inline ex coeff(const ex & thisex, const ex & s, int n=1)
344 { return thisex.coeff(s, n); }
346 inline ex numer(const ex & thisex)
347 { return thisex.numer(); }
349 inline ex denom(const ex & thisex)
350 { return thisex.denom(); }
352 inline ex normal(const ex & thisex, int level=0)
353 { return thisex.normal(level); }
355 inline ex to_rational(const ex & thisex, lst & repl_lst)
356 { return thisex.to_rational(repl_lst); }
358 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
359 { return thisex.collect(s, distributed); }
361 inline ex eval(const ex & thisex, int level = 0)
362 { return thisex.eval(level); }
364 inline ex evalf(const ex & thisex, int level = 0)
365 { return thisex.evalf(level); }
367 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
368 { return thisex.diff(s, nth); }
370 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
371 { return thisex.series(r, order, options); }
373 inline ex subs(const ex & thisex, const ex & e)
374 { return thisex.subs(e); }
376 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
377 { return thisex.subs(ls, lr); }
379 inline ex op(const ex & thisex, int i)
380 { return thisex.op(i); }
382 inline ex lhs(const ex & thisex)
383 { return thisex.lhs(); }
385 inline ex rhs(const ex & thisex)
386 { return thisex.rhs(); }
388 inline bool is_zero(const ex & thisex)
389 { return thisex.is_zero(); }
391 inline void swap(ex & e1, ex & e2)
396 #endif // ndef __GINAC_EX_H__