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. */
49 // default ctor, dtor, copy ctor assignment operator and helpers
54 const ex & operator=(const ex & other);
57 ex(const basic & other);
63 /** Construct ex from string and a list of symbols. The input grammar is
64 * similar to the GiNaC output format. All symbols to be used in the
65 * expression must be specified in a lst in the second argument. Undefined
66 * symbols and other parser errors will throw an exception. */
67 ex(const std::string &s, const ex &l);
69 // functions overriding virtual functions from bases classes
72 // new virtual functions which can be overridden by derived classes
75 // non-virtual functions in this class
77 void swap(ex & other);
78 void printraw(std::ostream & os) const;
79 void printtree(std::ostream & os, unsigned indent=0) const;
80 void print(std::ostream & os, unsigned upper_precedence=0) const;
81 void printcsrc(std::ostream & os, unsigned type, const char *var_name) const;
82 void dbgprint(void) const;
83 void dbgprinttree(void) const;
84 bool info(unsigned inf) const;
85 unsigned nops() const;
86 ex expand(unsigned options=0) const;
87 bool has(const ex & other) const;
88 int degree(const symbol & s) const;
89 int ldegree(const symbol & s) const;
90 ex coeff(const symbol & s, int n=1) const;
91 ex lcoeff(const symbol & s) const { return coeff(s, degree(s)); }
92 ex tcoeff(const symbol & s) const { return coeff(s, ldegree(s)); }
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 symbol & s) const;
105 ex eval(int level = 0) const;
106 ex evalf(int level = 0) const;
107 ex diff(const symbol & s, unsigned nth = 1) const;
108 ex series(const ex & r, int order, unsigned options = 0) const;
109 ex subs(const lst & ls, const lst & lr) const;
110 ex subs(const ex & e) const;
111 exvector get_free_indices(void) const;
112 ex simplify_indexed(void) const;
113 ex simplify_indexed(const scalar_products & sp) const;
114 ex simplify_ncmul(const exvector & v) const;
115 ex operator[](const ex & index) const;
116 ex operator[](int i) const;
121 int compare(const ex & other) const;
122 bool is_equal(const ex & other) const;
123 bool is_zero(void) const { return is_equal(_ex0()); }
125 unsigned return_type(void) const;
126 unsigned return_type_tinfo(void) const;
127 unsigned gethash(void) const;
129 ex exadd(const ex & rh) const;
130 ex exmul(const ex & rh) const;
131 ex exncmul(const ex & rh) const;
133 void construct_from_basic(const basic & other);
134 void construct_from_int(int i);
135 void construct_from_uint(unsigned int i);
136 void construct_from_long(long i);
137 void construct_from_ulong(unsigned long i);
138 void construct_from_double(double d);
139 void construct_from_string_and_lst(const std::string &s, const ex &l);
140 void makewriteable();
142 #ifdef OBSCURE_CINT_HACK
144 static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
146 if (last_created_or_assigned_bp==0) return false;
147 if ((last_created_or_assigned_bp->flags &
148 status_flags::dynallocated)==0) return false;
149 if ((last_created_or_assigned_bp->flags &
150 status_flags::evaluated)==0) return false;
154 void update_last_created_or_assigned_bp(void)
156 if (last_created_or_assigned_bp!=0) {
157 if (--last_created_or_assigned_bp->refcount == 0) {
158 delete last_created_or_assigned_bp;
161 last_created_or_assigned_bp = bp;
162 ++last_created_or_assigned_bp->refcount;
163 last_created_or_assigned_exp = (long)(void *)(this);
165 #endif // def OBSCURE_CINT_HACK
170 basic *bp; ///< pointer to basic object managed by this
171 #ifdef OBSCURE_CINT_HACK
172 static basic * last_created_or_assigned_bp;
173 static basic * dummy_bp;
174 static long last_created_or_assigned_exp;
175 #endif // def OBSCURE_CINT_HACK
179 // performance-critical inlined method implementations
182 ex::ex() : bp(_ex0().bp)
184 /*debugmsg("ex default ctor",LOGLEVEL_CONSTRUCT);*/
185 GINAC_ASSERT(_ex0().bp!=0);
186 GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
189 #ifdef OBSCURE_CINT_HACK
190 update_last_created_or_assigned_bp();
191 #endif // def OBSCURE_CINT_HACK
197 /*debugmsg("ex dtor",LOGLEVEL_DESTRUCT);*/
199 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
200 if (--bp->refcount == 0)
205 ex::ex(const ex & other) : bp(other.bp)
207 /*debugmsg("ex copy ctor",LOGLEVEL_CONSTRUCT);*/
209 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
211 #ifdef OBSCURE_CINT_HACK
212 update_last_created_or_assigned_bp();
213 #endif // def OBSCURE_CINT_HACK
217 const ex & ex::operator=(const ex & other)
219 /*debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);*/
221 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
222 GINAC_ASSERT(other.bp!=0);
223 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
224 if (--bp->refcount==0)
226 ++other.bp->refcount;
228 #ifdef OBSCURE_CINT_HACK
229 update_last_created_or_assigned_bp();
230 #endif // def OBSCURE_CINT_HACK
235 ex::ex(const basic & other)
237 /*debugmsg("ex ctor from basic",LOGLEVEL_CONSTRUCT);*/
238 construct_from_basic(other);
239 #ifdef OBSCURE_CINT_HACK
240 update_last_created_or_assigned_bp();
241 #endif // def OBSCURE_CINT_HACK
247 /*debugmsg("ex ctor from int",LOGLEVEL_CONSTRUCT);*/
248 construct_from_int(i);
249 #ifdef OBSCURE_CINT_HACK
250 update_last_created_or_assigned_bp();
251 #endif // def OBSCURE_CINT_HACK
255 ex::ex(unsigned int i)
257 /*debugmsg("ex ctor from unsigned int",LOGLEVEL_CONSTRUCT);*/
258 construct_from_uint(i);
259 #ifdef OBSCURE_CINT_HACK
260 update_last_created_or_assigned_bp();
261 #endif // def OBSCURE_CINT_HACK
267 /*debugmsg("ex ctor from long",LOGLEVEL_CONSTRUCT);*/
268 construct_from_long(i);
269 #ifdef OBSCURE_CINT_HACK
270 update_last_created_or_assigned_bp();
271 #endif // def OBSCURE_CINT_HACK
275 ex::ex(unsigned long i)
277 /*debugmsg("ex ctor from unsigned long",LOGLEVEL_CONSTRUCT);*/
278 construct_from_ulong(i);
279 #ifdef OBSCURE_CINT_HACK
280 update_last_created_or_assigned_bp();
281 #endif // def OBSCURE_CINT_HACK
285 ex::ex(double const d)
287 /*debugmsg("ex ctor from double",LOGLEVEL_CONSTRUCT);*/
288 construct_from_double(d);
289 #ifdef OBSCURE_CINT_HACK
290 update_last_created_or_assigned_bp();
291 #endif // def OBSCURE_CINT_HACK
295 ex::ex(const std::string &s, const ex &l)
297 /*debugmsg("ex ctor from string,lst",LOGLEVEL_CONSTRUCT);*/
298 construct_from_string_and_lst(s, l);
299 #ifdef OBSCURE_CINT_HACK
300 update_last_created_or_assigned_bp();
301 #endif // def OBSCURE_CINT_HACK
305 int ex::compare(const ex & other) const
308 GINAC_ASSERT(other.bp!=0);
309 if (bp==other.bp) // trivial case: both expressions point to same basic
311 return bp->compare(*other.bp);
315 bool ex::is_equal(const ex & other) const
318 GINAC_ASSERT(other.bp!=0);
319 if (bp==other.bp) // trivial case: both expressions point to same basic
321 return bp->is_equal(*other.bp);
326 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
328 return e1.bp == e2.bp;
331 // wrapper functions around member functions
332 inline unsigned nops(const ex & thisex)
333 { return thisex.nops(); }
335 inline ex expand(const ex & thisex, unsigned options = 0)
336 { return thisex.expand(options); }
338 inline bool has(const ex & thisex, const ex & other)
339 { return thisex.has(other); }
341 inline int degree(const ex & thisex, const symbol & s)
342 { return thisex.degree(s); }
344 inline int ldegree(const ex & thisex, const symbol & s)
345 { return thisex.ldegree(s); }
347 inline ex coeff(const ex & thisex, const symbol & s, int n=1)
348 { return thisex.coeff(s, n); }
350 inline ex numer(const ex & thisex)
351 { return thisex.numer(); }
353 inline ex denom(const ex & thisex)
354 { return thisex.denom(); }
356 inline ex normal(const ex & thisex, int level=0)
357 { return thisex.normal(level); }
359 inline ex to_rational(const ex & thisex, lst & repl_lst)
360 { return thisex.to_rational(repl_lst); }
362 inline ex collect(const ex & thisex, const symbol & s)
363 { return thisex.collect(s); }
365 inline ex eval(const ex & thisex, int level = 0)
366 { return thisex.eval(level); }
368 inline ex evalf(const ex & thisex, int level = 0)
369 { return thisex.evalf(level); }
371 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
372 { return thisex.diff(s, nth); }
374 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
375 { return thisex.series(r, order, options); }
377 inline ex subs(const ex & thisex, const ex & e)
378 { return thisex.subs(e); }
380 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
381 { return thisex.subs(ls, lr); }
383 inline ex op(const ex & thisex, int i)
384 { return thisex.op(i); }
386 inline ex lhs(const ex & thisex)
387 { return thisex.lhs(); }
389 inline ex rhs(const ex & thisex)
390 { return thisex.rhs(); }
392 inline bool is_zero(const ex & thisex)
393 { return thisex.is_zero(); }
395 inline void swap(ex & e1, ex & e2)
400 #endif // ndef __GINAC_EX_H__