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 numer_denom(void) const;
95 ex unit(const symbol &x) const;
96 ex content(const symbol &x) const;
97 numeric integer_content(void) const;
98 ex primpart(const symbol &x) const;
99 ex primpart(const symbol &x, const ex &cont) const;
100 ex normal(int level = 0) const;
101 ex to_rational(lst &repl_lst) const;
102 ex smod(const numeric &xi) const;
103 numeric max_coefficient(void) const;
104 ex collect(const ex & s, bool distributed = false) const { return bp->collect(s, distributed); }
105 ex eval(int level = 0) const { return bp->eval(level); }
106 ex evalf(int level = 0) const { return bp->evalf(level); }
107 ex diff(const symbol & s, unsigned nth = 1) const;
108 ex series(const ex & r, int order, unsigned options = 0) const;
109 bool match(const ex & pattern) const;
110 bool match(const ex & pattern, lst & repl_lst) const { return bp->match(pattern, repl_lst); }
111 ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const { return bp->subs(ls, lr, no_pattern); }
112 ex subs(const ex & e, bool no_pattern = false) const { return bp->subs(e, no_pattern); }
113 exvector get_free_indices(void) const { return bp->get_free_indices(); }
114 ex simplify_indexed(void) const;
115 ex simplify_indexed(const scalar_products & sp) const;
116 ex symmetrize(void) const;
117 ex symmetrize(const lst & l) const;
118 ex antisymmetrize(void) const;
119 ex antisymmetrize(const lst & l) const;
120 ex simplify_ncmul(const exvector & v) const { return bp->simplify_ncmul(v); }
121 ex operator[](const ex & index) const;
122 ex operator[](int i) const;
123 ex op(int i) const { return bp->op(i); }
127 int compare(const ex & other) const;
128 bool is_equal(const ex & other) const;
129 bool is_zero(void) const { return is_equal(_ex0()); }
131 unsigned return_type(void) const { return bp->return_type(); }
132 unsigned return_type_tinfo(void) const { return bp->return_type_tinfo(); }
133 unsigned gethash(void) const { return bp->gethash(); }
135 void construct_from_basic(const basic & other);
136 void construct_from_int(int i);
137 void construct_from_uint(unsigned int i);
138 void construct_from_long(long i);
139 void construct_from_ulong(unsigned long i);
140 void construct_from_double(double d);
141 void construct_from_string_and_lst(const std::string &s, const ex &l);
142 void makewriteable();
144 #ifdef OBSCURE_CINT_HACK
146 static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
148 if (last_created_or_assigned_bp==0) return false;
149 if ((last_created_or_assigned_bp->flags &
150 status_flags::dynallocated)==0) return false;
151 if ((last_created_or_assigned_bp->flags &
152 status_flags::evaluated)==0) return false;
156 void update_last_created_or_assigned_bp(void)
158 if (last_created_or_assigned_bp!=0) {
159 if (--last_created_or_assigned_bp->refcount == 0) {
160 delete last_created_or_assigned_bp;
163 last_created_or_assigned_bp = bp;
164 ++last_created_or_assigned_bp->refcount;
165 last_created_or_assigned_exp = (long)(void *)(this);
167 #endif // def OBSCURE_CINT_HACK
172 basic *bp; ///< pointer to basic object managed by this
173 #ifdef OBSCURE_CINT_HACK
174 static basic * last_created_or_assigned_bp;
175 static basic * dummy_bp;
176 static long last_created_or_assigned_exp;
177 #endif // def OBSCURE_CINT_HACK
181 // performance-critical inlined method implementations
184 ex::ex() : bp(_ex0().bp)
186 /*debugmsg("ex default ctor",LOGLEVEL_CONSTRUCT);*/
187 GINAC_ASSERT(_ex0().bp!=0);
188 GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
191 #ifdef OBSCURE_CINT_HACK
192 update_last_created_or_assigned_bp();
193 #endif // def OBSCURE_CINT_HACK
199 /*debugmsg("ex dtor",LOGLEVEL_DESTRUCT);*/
201 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
202 if (--bp->refcount == 0)
207 ex::ex(const ex & other) : bp(other.bp)
209 /*debugmsg("ex copy ctor",LOGLEVEL_CONSTRUCT);*/
211 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
213 #ifdef OBSCURE_CINT_HACK
214 update_last_created_or_assigned_bp();
215 #endif // def OBSCURE_CINT_HACK
219 ex & ex::operator=(const ex & other)
221 /*debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);*/
223 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
224 GINAC_ASSERT(other.bp!=0);
225 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
226 // NB: must first increment other.bp->refcount, since other might be *this.
227 ++other.bp->refcount;
228 if (--bp->refcount==0)
231 #ifdef OBSCURE_CINT_HACK
232 update_last_created_or_assigned_bp();
233 #endif // def OBSCURE_CINT_HACK
238 ex::ex(const basic & other)
240 /*debugmsg("ex ctor from basic",LOGLEVEL_CONSTRUCT);*/
241 construct_from_basic(other);
242 #ifdef OBSCURE_CINT_HACK
243 update_last_created_or_assigned_bp();
244 #endif // def OBSCURE_CINT_HACK
250 /*debugmsg("ex ctor from int",LOGLEVEL_CONSTRUCT);*/
251 construct_from_int(i);
252 #ifdef OBSCURE_CINT_HACK
253 update_last_created_or_assigned_bp();
254 #endif // def OBSCURE_CINT_HACK
258 ex::ex(unsigned int i)
260 /*debugmsg("ex ctor from unsigned int",LOGLEVEL_CONSTRUCT);*/
261 construct_from_uint(i);
262 #ifdef OBSCURE_CINT_HACK
263 update_last_created_or_assigned_bp();
264 #endif // def OBSCURE_CINT_HACK
270 /*debugmsg("ex ctor from long",LOGLEVEL_CONSTRUCT);*/
271 construct_from_long(i);
272 #ifdef OBSCURE_CINT_HACK
273 update_last_created_or_assigned_bp();
274 #endif // def OBSCURE_CINT_HACK
278 ex::ex(unsigned long i)
280 /*debugmsg("ex ctor from unsigned long",LOGLEVEL_CONSTRUCT);*/
281 construct_from_ulong(i);
282 #ifdef OBSCURE_CINT_HACK
283 update_last_created_or_assigned_bp();
284 #endif // def OBSCURE_CINT_HACK
288 ex::ex(double const d)
290 /*debugmsg("ex ctor from double",LOGLEVEL_CONSTRUCT);*/
291 construct_from_double(d);
292 #ifdef OBSCURE_CINT_HACK
293 update_last_created_or_assigned_bp();
294 #endif // def OBSCURE_CINT_HACK
298 ex::ex(const std::string &s, const ex &l)
300 /*debugmsg("ex ctor from string,lst",LOGLEVEL_CONSTRUCT);*/
301 construct_from_string_and_lst(s, l);
302 #ifdef OBSCURE_CINT_HACK
303 update_last_created_or_assigned_bp();
304 #endif // def OBSCURE_CINT_HACK
308 int ex::compare(const ex & other) const
311 GINAC_ASSERT(other.bp!=0);
312 if (bp==other.bp) // trivial case: both expressions point to same basic
314 return bp->compare(*other.bp);
318 bool ex::is_equal(const ex & other) const
321 GINAC_ASSERT(other.bp!=0);
322 if (bp==other.bp) // trivial case: both expressions point to same basic
324 return bp->is_equal(*other.bp);
329 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
331 return e1.bp == e2.bp;
334 // wrapper functions around member functions
335 inline unsigned nops(const ex & thisex)
336 { return thisex.nops(); }
338 inline ex expand(const ex & thisex, unsigned options = 0)
339 { return thisex.expand(options); }
341 inline bool has(const ex & thisex, const ex & other)
342 { return thisex.has(other); }
344 inline int degree(const ex & thisex, const ex & s)
345 { return thisex.degree(s); }
347 inline int ldegree(const ex & thisex, const ex & s)
348 { return thisex.ldegree(s); }
350 inline ex coeff(const ex & thisex, const ex & s, int n=1)
351 { return thisex.coeff(s, n); }
353 inline ex numer(const ex & thisex)
354 { return thisex.numer(); }
356 inline ex denom(const ex & thisex)
357 { return thisex.denom(); }
359 inline ex numer_denom(const ex & thisex)
360 { return thisex.numer_denom(); }
362 inline ex normal(const ex & thisex, int level=0)
363 { return thisex.normal(level); }
365 inline ex to_rational(const ex & thisex, lst & repl_lst)
366 { return thisex.to_rational(repl_lst); }
368 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
369 { return thisex.collect(s, distributed); }
371 inline ex eval(const ex & thisex, int level = 0)
372 { return thisex.eval(level); }
374 inline ex evalf(const ex & thisex, int level = 0)
375 { return thisex.evalf(level); }
377 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
378 { return thisex.diff(s, nth); }
380 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
381 { return thisex.series(r, order, options); }
383 inline bool match(const ex & thisex, const ex & pattern, lst & repl_lst)
384 { return thisex.match(pattern, repl_lst); }
386 inline ex subs(const ex & thisex, const ex & e)
387 { return thisex.subs(e); }
389 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
390 { return thisex.subs(ls, lr); }
392 inline ex simplify_indexed(const ex & thisex)
393 { return thisex.simplify_indexed(); }
395 inline ex simplify_indexed(const ex & thisex, const scalar_products & sp)
396 { return thisex.simplify_indexed(sp); }
398 inline ex symmetrize(const ex & thisex)
399 { return thisex.symmetrize(); }
401 inline ex symmetrize(const ex & thisex, const lst & l)
402 { return thisex.symmetrize(l); }
404 inline ex antisymmetrize(const ex & thisex)
405 { return thisex.antisymmetrize(); }
407 inline ex antisymmetrize(const ex & thisex, const lst & l)
408 { return thisex.antisymmetrize(l); }
410 inline ex op(const ex & thisex, int i)
411 { return thisex.op(i); }
413 inline ex lhs(const ex & thisex)
414 { return thisex.lhs(); }
416 inline ex rhs(const ex & thisex)
417 { return thisex.rhs(); }
419 inline bool is_zero(const ex & thisex)
420 { return thisex.is_zero(); }
422 inline void swap(ex & e1, ex & e2)
427 #endif // ndef __GINAC_EX_H__