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 const 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 printraw(std::ostream & os) const;
80 void printtree(std::ostream & os, unsigned indent=0) const;
81 void print(std::ostream & os, unsigned upper_precedence=0) const;
82 void printcsrc(std::ostream & os, unsigned type, const char *var_name) const;
83 void dbgprint(void) const;
84 void dbgprinttree(void) const;
85 bool info(unsigned inf) const;
86 unsigned nops() const;
87 ex expand(unsigned options=0) const;
88 bool has(const ex & other) const;
89 int degree(const symbol & s) const;
90 int ldegree(const symbol & s) const;
91 ex coeff(const symbol & s, int n=1) const;
92 ex lcoeff(const symbol & s) const { return coeff(s, degree(s)); }
93 ex tcoeff(const symbol & s) const { return coeff(s, ldegree(s)); }
96 ex unit(const symbol &x) const;
97 ex content(const symbol &x) const;
98 numeric integer_content(void) const;
99 ex primpart(const symbol &x) const;
100 ex primpart(const symbol &x, const ex &cont) const;
101 ex normal(int level = 0) const;
102 ex to_rational(lst &repl_lst) const;
103 ex smod(const numeric &xi) const;
104 numeric max_coefficient(void) const;
105 ex collect(const symbol & s) const;
106 ex eval(int level = 0) const;
107 ex evalf(int level = 0) const;
108 ex diff(const symbol & s, unsigned nth = 1) const;
109 ex series(const ex & r, int order, unsigned options = 0) const;
110 ex subs(const lst & ls, const lst & lr) const;
111 ex subs(const ex & e) const;
112 exvector get_free_indices(void) const;
113 ex simplify_indexed(void) const;
114 ex simplify_indexed(const scalar_products & sp) const;
115 ex simplify_ncmul(const exvector & v) const;
116 ex operator[](const ex & index) const;
117 ex operator[](int i) const;
122 int compare(const ex & other) const;
123 bool is_equal(const ex & other) const;
124 bool is_zero(void) const { return is_equal(_ex0()); }
126 unsigned return_type(void) const;
127 unsigned return_type_tinfo(void) const;
128 unsigned gethash(void) const;
130 ex exadd(const ex & rh) const;
131 ex exmul(const ex & rh) const;
132 ex exncmul(const ex & rh) const;
134 void construct_from_basic(const basic & other);
135 void construct_from_int(int i);
136 void construct_from_uint(unsigned int i);
137 void construct_from_long(long i);
138 void construct_from_ulong(unsigned long i);
139 void construct_from_double(double d);
140 void construct_from_string_and_lst(const std::string &s, const ex &l);
141 void makewriteable();
143 #ifdef OBSCURE_CINT_HACK
145 static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
147 if (last_created_or_assigned_bp==0) return false;
148 if ((last_created_or_assigned_bp->flags &
149 status_flags::dynallocated)==0) return false;
150 if ((last_created_or_assigned_bp->flags &
151 status_flags::evaluated)==0) return false;
155 void update_last_created_or_assigned_bp(void)
157 if (last_created_or_assigned_bp!=0) {
158 if (--last_created_or_assigned_bp->refcount == 0) {
159 delete last_created_or_assigned_bp;
162 last_created_or_assigned_bp = bp;
163 ++last_created_or_assigned_bp->refcount;
164 last_created_or_assigned_exp = (long)(void *)(this);
166 #endif // def OBSCURE_CINT_HACK
171 basic *bp; ///< pointer to basic object managed by this
172 #ifdef OBSCURE_CINT_HACK
173 static basic * last_created_or_assigned_bp;
174 static basic * dummy_bp;
175 static long last_created_or_assigned_exp;
176 #endif // def OBSCURE_CINT_HACK
180 // performance-critical inlined method implementations
183 ex::ex() : bp(_ex0().bp)
185 /*debugmsg("ex default ctor",LOGLEVEL_CONSTRUCT);*/
186 GINAC_ASSERT(_ex0().bp!=0);
187 GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
190 #ifdef OBSCURE_CINT_HACK
191 update_last_created_or_assigned_bp();
192 #endif // def OBSCURE_CINT_HACK
198 /*debugmsg("ex dtor",LOGLEVEL_DESTRUCT);*/
200 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
201 if (--bp->refcount == 0)
206 ex::ex(const ex & other) : bp(other.bp)
208 /*debugmsg("ex copy ctor",LOGLEVEL_CONSTRUCT);*/
210 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
212 #ifdef OBSCURE_CINT_HACK
213 update_last_created_or_assigned_bp();
214 #endif // def OBSCURE_CINT_HACK
218 const ex & ex::operator=(const ex & other)
220 /*debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);*/
222 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
223 GINAC_ASSERT(other.bp!=0);
224 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
225 if (--bp->refcount==0)
227 ++other.bp->refcount;
229 #ifdef OBSCURE_CINT_HACK
230 update_last_created_or_assigned_bp();
231 #endif // def OBSCURE_CINT_HACK
236 ex::ex(const basic & other)
238 /*debugmsg("ex ctor from basic",LOGLEVEL_CONSTRUCT);*/
239 construct_from_basic(other);
240 #ifdef OBSCURE_CINT_HACK
241 update_last_created_or_assigned_bp();
242 #endif // def OBSCURE_CINT_HACK
248 /*debugmsg("ex ctor from int",LOGLEVEL_CONSTRUCT);*/
249 construct_from_int(i);
250 #ifdef OBSCURE_CINT_HACK
251 update_last_created_or_assigned_bp();
252 #endif // def OBSCURE_CINT_HACK
256 ex::ex(unsigned int i)
258 /*debugmsg("ex ctor from unsigned int",LOGLEVEL_CONSTRUCT);*/
259 construct_from_uint(i);
260 #ifdef OBSCURE_CINT_HACK
261 update_last_created_or_assigned_bp();
262 #endif // def OBSCURE_CINT_HACK
268 /*debugmsg("ex ctor from long",LOGLEVEL_CONSTRUCT);*/
269 construct_from_long(i);
270 #ifdef OBSCURE_CINT_HACK
271 update_last_created_or_assigned_bp();
272 #endif // def OBSCURE_CINT_HACK
276 ex::ex(unsigned long i)
278 /*debugmsg("ex ctor from unsigned long",LOGLEVEL_CONSTRUCT);*/
279 construct_from_ulong(i);
280 #ifdef OBSCURE_CINT_HACK
281 update_last_created_or_assigned_bp();
282 #endif // def OBSCURE_CINT_HACK
286 ex::ex(double const d)
288 /*debugmsg("ex ctor from double",LOGLEVEL_CONSTRUCT);*/
289 construct_from_double(d);
290 #ifdef OBSCURE_CINT_HACK
291 update_last_created_or_assigned_bp();
292 #endif // def OBSCURE_CINT_HACK
296 ex::ex(const std::string &s, const ex &l)
298 /*debugmsg("ex ctor from string,lst",LOGLEVEL_CONSTRUCT);*/
299 construct_from_string_and_lst(s, l);
300 #ifdef OBSCURE_CINT_HACK
301 update_last_created_or_assigned_bp();
302 #endif // def OBSCURE_CINT_HACK
306 int ex::compare(const ex & other) const
309 GINAC_ASSERT(other.bp!=0);
310 if (bp==other.bp) // trivial case: both expressions point to same basic
312 return bp->compare(*other.bp);
316 bool ex::is_equal(const ex & other) const
319 GINAC_ASSERT(other.bp!=0);
320 if (bp==other.bp) // trivial case: both expressions point to same basic
322 return bp->is_equal(*other.bp);
327 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
329 return e1.bp == e2.bp;
332 // wrapper functions around member functions
333 inline unsigned nops(const ex & thisex)
334 { return thisex.nops(); }
336 inline ex expand(const ex & thisex, unsigned options = 0)
337 { return thisex.expand(options); }
339 inline bool has(const ex & thisex, const ex & other)
340 { return thisex.has(other); }
342 inline int degree(const ex & thisex, const symbol & s)
343 { return thisex.degree(s); }
345 inline int ldegree(const ex & thisex, const symbol & s)
346 { return thisex.ldegree(s); }
348 inline ex coeff(const ex & thisex, const symbol & s, int n=1)
349 { return thisex.coeff(s, n); }
351 inline ex numer(const ex & thisex)
352 { return thisex.numer(); }
354 inline ex denom(const ex & thisex)
355 { return thisex.denom(); }
357 inline ex normal(const ex & thisex, int level=0)
358 { return thisex.normal(level); }
360 inline ex to_rational(const ex & thisex, lst & repl_lst)
361 { return thisex.to_rational(repl_lst); }
363 inline ex collect(const ex & thisex, const symbol & s)
364 { return thisex.collect(s); }
366 inline ex eval(const ex & thisex, int level = 0)
367 { return thisex.eval(level); }
369 inline ex evalf(const ex & thisex, int level = 0)
370 { return thisex.evalf(level); }
372 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
373 { return thisex.diff(s, nth); }
375 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
376 { return thisex.series(r, order, options); }
378 inline ex subs(const ex & thisex, const ex & e)
379 { return thisex.subs(e); }
381 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
382 { return thisex.subs(ls, lr); }
384 inline ex op(const ex & thisex, int i)
385 { return thisex.op(i); }
387 inline ex lhs(const ex & thisex)
388 { return thisex.lhs(); }
390 inline ex rhs(const ex & thisex)
391 { return thisex.rhs(); }
393 inline bool is_zero(const ex & thisex)
394 { return thisex.is_zero(); }
396 inline void swap(ex & e1, ex & e2)
401 #endif // ndef __GINAC_EX_H__