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"
33 // Sorry, this is the only constant to pollute the global scope, the other ones
34 // are defined in utils.h and not visible from outside.
36 extern const ex & _ex0(void); ///< single ex(numeric(0))
40 class scalar_products;
43 /** Lightweight wrapper for GiNaC's symbolic objects. Basically all it does is
44 * to hold a pointer to the other objects, manage the reference counting and
45 * provide methods for manipulation of these objects. (Some people call such
46 * a thing a proxy class.) */
49 friend class archive_node;
50 friend bool are_ex_trivially_equal(const ex &, const ex &);
51 template<class T> friend const T &ex_to(const ex &);
52 template<class T> friend bool is_a(const ex &);
53 template<class T> friend bool is_exactly_a(const ex &);
57 // default ctor, dtor, copy ctor assignment operator and helpers
62 ex & operator=(const ex & other);
65 ex(const basic & other);
71 /** Construct ex from string and a list of symbols. The input grammar is
72 * similar to the GiNaC output format. All symbols to be used in the
73 * expression must be specified in a lst in the second argument. Undefined
74 * symbols and other parser errors will throw an exception. */
75 ex(const std::string &s, const ex &l);
77 // non-virtual functions in this class
79 void swap(ex & other);
80 void print(const print_context & c, unsigned level = 0) const;
81 void printtree(std::ostream & os) const;
82 void dbgprint(void) const;
83 void dbgprinttree(void) const;
84 bool info(unsigned inf) const { return bp->info(inf); }
85 unsigned nops() const { return bp->nops(); }
86 ex expand(unsigned options=0) const;
87 bool has(const ex & pattern) const { return bp->has(pattern); }
88 ex map(map_function & f) const { return bp->map(f); }
89 ex map(ex (*f)(const ex & e)) const;
90 bool find(const ex & pattern, lst & found) const;
91 int degree(const ex & s) const { return bp->degree(s); }
92 int ldegree(const ex & s) const { return bp->ldegree(s); }
93 ex coeff(const ex & s, int n = 1) const { return bp->coeff(s, n); }
94 ex lcoeff(const ex & s) const { return coeff(s, degree(s)); }
95 ex tcoeff(const ex & s) const { return coeff(s, ldegree(s)); }
98 ex numer_denom(void) const;
99 ex unit(const symbol &x) const;
100 ex content(const symbol &x) const;
101 numeric integer_content(void) const;
102 ex primpart(const symbol &x) const;
103 ex primpart(const symbol &x, const ex &cont) const;
104 ex normal(int level = 0) const;
105 ex to_rational(lst &repl_lst) const { return bp->to_rational(repl_lst); }
106 ex smod(const numeric &xi) const { return bp->smod(xi); }
107 numeric max_coefficient(void) const;
108 ex collect(const ex & s, bool distributed = false) const { return bp->collect(s, distributed); }
109 ex eval(int level = 0) const { return bp->eval(level); }
110 ex evalf(int level = 0) const { return bp->evalf(level); }
111 ex evalm(void) const { return bp->evalm(); }
112 ex diff(const symbol & s, unsigned nth = 1) const;
113 ex series(const ex & r, int order, unsigned options = 0) const;
114 bool match(const ex & pattern) const;
115 bool match(const ex & pattern, lst & repl_lst) const { return bp->match(pattern, repl_lst); }
116 ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const { return bp->subs(ls, lr, no_pattern); }
117 ex subs(const ex & e, bool no_pattern = false) const { return bp->subs(e, no_pattern); }
118 exvector get_free_indices(void) const { return bp->get_free_indices(); }
119 ex simplify_indexed(void) const;
120 ex simplify_indexed(const scalar_products & sp) const;
121 ex symmetrize(void) const;
122 ex symmetrize(const lst & l) const;
123 ex antisymmetrize(void) const;
124 ex antisymmetrize(const lst & l) const;
125 ex symmetrize_cyclic(void) const;
126 ex symmetrize_cyclic(const lst & l) const;
127 ex simplify_ncmul(const exvector & v) const { return bp->simplify_ncmul(v); }
128 ex operator[](const ex & index) const;
129 ex operator[](int i) const;
130 ex op(int i) const { return bp->op(i); }
134 int compare(const ex & other) const;
135 bool is_equal(const ex & other) const;
136 bool is_zero(void) const { return is_equal(_ex0()); }
138 unsigned return_type(void) const { return bp->return_type(); }
139 unsigned return_type_tinfo(void) const { return bp->return_type_tinfo(); }
140 unsigned gethash(void) const { return bp->gethash(); }
142 void construct_from_basic(const basic & other);
143 void construct_from_int(int i);
144 void construct_from_uint(unsigned int i);
145 void construct_from_long(long i);
146 void construct_from_ulong(unsigned long i);
147 void construct_from_double(double d);
148 void construct_from_string_and_lst(const std::string &s, const ex &l);
149 void makewriteable();
151 #ifdef OBSCURE_CINT_HACK
153 static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
155 if (last_created_or_assigned_bp==0) return false;
156 if ((last_created_or_assigned_bp->flags &
157 status_flags::dynallocated)==0) return false;
158 if ((last_created_or_assigned_bp->flags &
159 status_flags::evaluated)==0) return false;
163 void update_last_created_or_assigned_bp(void)
165 if (last_created_or_assigned_bp!=0) {
166 if (--last_created_or_assigned_bp->refcount == 0) {
167 delete last_created_or_assigned_bp;
170 last_created_or_assigned_bp = bp;
171 ++last_created_or_assigned_bp->refcount;
172 last_created_or_assigned_exp = (long)(void *)(this);
174 #endif // def OBSCURE_CINT_HACK
179 basic *bp; ///< pointer to basic object managed by this, direct manipulation deprecated
180 #ifdef OBSCURE_CINT_HACK
181 static basic * last_created_or_assigned_bp;
182 static basic * dummy_bp;
183 static long last_created_or_assigned_exp;
184 #endif // def OBSCURE_CINT_HACK
188 // performance-critical inlined method implementations
191 ex::ex() : bp(_ex0().bp)
193 /*debugmsg("ex default ctor",LOGLEVEL_CONSTRUCT);*/
194 GINAC_ASSERT(_ex0().bp!=0);
195 GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
198 #ifdef OBSCURE_CINT_HACK
199 update_last_created_or_assigned_bp();
200 #endif // def OBSCURE_CINT_HACK
206 /*debugmsg("ex dtor",LOGLEVEL_DESTRUCT);*/
208 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
209 if (--bp->refcount == 0)
214 ex::ex(const ex & other) : bp(other.bp)
216 /*debugmsg("ex copy ctor",LOGLEVEL_CONSTRUCT);*/
218 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
220 #ifdef OBSCURE_CINT_HACK
221 update_last_created_or_assigned_bp();
222 #endif // def OBSCURE_CINT_HACK
226 ex & ex::operator=(const ex & other)
228 /*debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);*/
230 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
231 GINAC_ASSERT(other.bp!=0);
232 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
233 // NB: must first increment other.bp->refcount, since other might be *this.
234 ++other.bp->refcount;
235 if (--bp->refcount==0)
238 #ifdef OBSCURE_CINT_HACK
239 update_last_created_or_assigned_bp();
240 #endif // def OBSCURE_CINT_HACK
245 ex::ex(const basic & other)
247 /*debugmsg("ex ctor from basic",LOGLEVEL_CONSTRUCT);*/
248 construct_from_basic(other);
249 #ifdef OBSCURE_CINT_HACK
250 update_last_created_or_assigned_bp();
251 #endif // def OBSCURE_CINT_HACK
257 /*debugmsg("ex ctor from int",LOGLEVEL_CONSTRUCT);*/
258 construct_from_int(i);
259 #ifdef OBSCURE_CINT_HACK
260 update_last_created_or_assigned_bp();
261 #endif // def OBSCURE_CINT_HACK
265 ex::ex(unsigned int i)
267 /*debugmsg("ex ctor from unsigned int",LOGLEVEL_CONSTRUCT);*/
268 construct_from_uint(i);
269 #ifdef OBSCURE_CINT_HACK
270 update_last_created_or_assigned_bp();
271 #endif // def OBSCURE_CINT_HACK
277 /*debugmsg("ex ctor from long",LOGLEVEL_CONSTRUCT);*/
278 construct_from_long(i);
279 #ifdef OBSCURE_CINT_HACK
280 update_last_created_or_assigned_bp();
281 #endif // def OBSCURE_CINT_HACK
285 ex::ex(unsigned long i)
287 /*debugmsg("ex ctor from unsigned long",LOGLEVEL_CONSTRUCT);*/
288 construct_from_ulong(i);
289 #ifdef OBSCURE_CINT_HACK
290 update_last_created_or_assigned_bp();
291 #endif // def OBSCURE_CINT_HACK
295 ex::ex(double const d)
297 /*debugmsg("ex ctor from double",LOGLEVEL_CONSTRUCT);*/
298 construct_from_double(d);
299 #ifdef OBSCURE_CINT_HACK
300 update_last_created_or_assigned_bp();
301 #endif // def OBSCURE_CINT_HACK
305 ex::ex(const std::string &s, const ex &l)
307 /*debugmsg("ex ctor from string,lst",LOGLEVEL_CONSTRUCT);*/
308 construct_from_string_and_lst(s, l);
309 #ifdef OBSCURE_CINT_HACK
310 update_last_created_or_assigned_bp();
311 #endif // def OBSCURE_CINT_HACK
315 int ex::compare(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->compare(*other.bp);
325 bool ex::is_equal(const ex & other) const
328 GINAC_ASSERT(other.bp!=0);
329 if (bp==other.bp) // trivial case: both expressions point to same basic
331 return bp->is_equal(*other.bp);
336 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
338 return e1.bp == e2.bp;
341 // wrapper functions around member functions
342 inline unsigned nops(const ex & thisex)
343 { return thisex.nops(); }
345 inline ex expand(const ex & thisex, unsigned options = 0)
346 { return thisex.expand(options); }
348 inline bool has(const ex & thisex, const ex & pattern)
349 { return thisex.has(pattern); }
351 inline bool find(const ex & thisex, const ex & pattern, lst & found)
352 { return thisex.find(pattern, found); }
354 inline int degree(const ex & thisex, const ex & s)
355 { return thisex.degree(s); }
357 inline int ldegree(const ex & thisex, const ex & s)
358 { return thisex.ldegree(s); }
360 inline ex coeff(const ex & thisex, const ex & s, int n=1)
361 { return thisex.coeff(s, n); }
363 inline ex numer(const ex & thisex)
364 { return thisex.numer(); }
366 inline ex denom(const ex & thisex)
367 { return thisex.denom(); }
369 inline ex numer_denom(const ex & thisex)
370 { return thisex.numer_denom(); }
372 inline ex normal(const ex & thisex, int level=0)
373 { return thisex.normal(level); }
375 inline ex to_rational(const ex & thisex, lst & repl_lst)
376 { return thisex.to_rational(repl_lst); }
378 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
379 { return thisex.collect(s, distributed); }
381 inline ex eval(const ex & thisex, int level = 0)
382 { return thisex.eval(level); }
384 inline ex evalf(const ex & thisex, int level = 0)
385 { return thisex.evalf(level); }
387 inline ex evalm(const ex & thisex)
388 { return thisex.evalm(); }
390 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
391 { return thisex.diff(s, nth); }
393 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
394 { return thisex.series(r, order, options); }
396 inline bool match(const ex & thisex, const ex & pattern, lst & repl_lst)
397 { return thisex.match(pattern, repl_lst); }
399 inline ex subs(const ex & thisex, const ex & e)
400 { return thisex.subs(e); }
402 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
403 { return thisex.subs(ls, lr); }
405 inline ex simplify_indexed(const ex & thisex)
406 { return thisex.simplify_indexed(); }
408 inline ex simplify_indexed(const ex & thisex, const scalar_products & sp)
409 { return thisex.simplify_indexed(sp); }
411 inline ex symmetrize(const ex & thisex)
412 { return thisex.symmetrize(); }
414 inline ex symmetrize(const ex & thisex, const lst & l)
415 { return thisex.symmetrize(l); }
417 inline ex antisymmetrize(const ex & thisex)
418 { return thisex.antisymmetrize(); }
420 inline ex antisymmetrize(const ex & thisex, const lst & l)
421 { return thisex.antisymmetrize(l); }
423 inline ex symmetrize_cyclic(const ex & thisex)
424 { return thisex.symmetrize_cyclic(); }
426 inline ex symmetrize_cyclic(const ex & thisex, const lst & l)
427 { return thisex.symmetrize_cyclic(l); }
429 inline ex op(const ex & thisex, int i)
430 { return thisex.op(i); }
432 inline ex lhs(const ex & thisex)
433 { return thisex.lhs(); }
435 inline ex rhs(const ex & thisex)
436 { return thisex.rhs(); }
438 inline bool is_zero(const ex & thisex)
439 { return thisex.is_zero(); }
441 inline void swap(ex & e1, ex & e2)
445 /* Function objects for STL sort() etc. */
446 struct ex_is_less : public std::binary_function<ex, ex, bool> {
447 bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
450 struct ex_is_equal : public std::binary_function<ex, ex, bool> {
451 bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
454 struct ex_swap : public std::binary_function<ex, ex, void> {
455 void operator() (ex &lh, ex &rh) const { lh.swap(rh); }
459 /* Convert function pointer to function object suitable for map(). */
460 class pointer_to_map_function : public map_function {
462 ex (*ptr)(const ex &);
464 explicit pointer_to_map_function(ex (*x)(const ex &)) : ptr(x) {}
465 ex operator()(const ex & e) { return ptr(e); }
469 class pointer_to_map_function_1arg : public map_function {
471 ex (*ptr)(const ex &, T1);
474 explicit pointer_to_map_function_1arg(ex (*x)(const ex &, T1), T1 a1) : ptr(x), arg1(a1) {}
475 ex operator()(const ex & e) { return ptr(e, arg1); }
478 template<class T1, class T2>
479 class pointer_to_map_function_2args : public map_function {
481 ex (*ptr)(const ex &, T1, T2);
485 explicit pointer_to_map_function_2args(ex (*x)(const ex &, T1, T2), T1 a1, T2 a2) : ptr(x), arg1(a1), arg2(a2) {}
486 ex operator()(const ex & e) { return ptr(e, arg1, arg2); }
489 template<class T1, class T2, class T3>
490 class pointer_to_map_function_3args : public map_function {
492 ex (*ptr)(const ex &, T1, T2, T3);
497 explicit pointer_to_map_function_3args(ex (*x)(const ex &, T1, T2, T3), T1 a1, T2 a2, T3 a3) : ptr(x), arg1(a1), arg2(a2), arg3(a3) {}
498 ex operator()(const ex & e) { return ptr(e, arg1, arg2, arg3); }
501 inline ex ex::map(ex (*f)(const ex & e)) const
503 pointer_to_map_function fcn(f);
510 #endif // ndef __GINAC_EX_H__