3 * Implementation of GiNaC's light-weight expression handles. */
6 * GiNaC Copyright (C) 1999 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
33 #ifndef NO_GINAC_NAMESPACE
35 #endif // ndef NO_GINAC_NAMESPACE
38 // default constructor, destructor, copy constructor assignment operator and helpers
43 #ifndef INLINE_EX_CONSTRUCTORS
45 ex::ex() : bp(exZERO().bp)
47 debugmsg("ex default constructor",LOGLEVEL_CONSTRUCT);
48 GINAC_ASSERT(exZERO().bp!=0);
49 GINAC_ASSERT(exZERO().bp->flags & status_flags::dynallocated);
56 debugmsg("ex destructor",LOGLEVEL_DESTRUCT);
58 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
59 if (--bp->refcount == 0) {
64 ex::ex(ex const & other) : bp(other.bp)
66 debugmsg("ex copy constructor",LOGLEVEL_CONSTRUCT);
68 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
72 ex const & ex::operator=(ex const & other)
74 debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);
76 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
77 GINAC_ASSERT(other.bp!=0);
78 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
80 basic * tmpbp=other.bp;
81 if (--bp->refcount==0) {
88 #endif // ndef INLINE_EX_CONSTRUCTORS
96 #ifndef INLINE_EX_CONSTRUCTORS
97 ex::ex(basic const & other)
99 debugmsg("ex constructor from basic",LOGLEVEL_CONSTRUCT);
100 construct_from_basic(other);
106 debugmsg("ex constructor from int",LOGLEVEL_CONSTRUCT);
107 construct_from_basic(numeric(i));
110 ex::ex(unsigned int const i)
112 debugmsg("ex constructor from unsigned int",LOGLEVEL_CONSTRUCT);
113 construct_from_basic(numeric(i));
118 debugmsg("ex constructor from long",LOGLEVEL_CONSTRUCT);
119 construct_from_basic(numeric(i));
122 ex::ex(unsigned long const i)
124 debugmsg("ex constructor from unsigned long",LOGLEVEL_CONSTRUCT);
125 construct_from_basic(numeric(i));
128 ex::ex(double const d)
130 debugmsg("ex constructor from double",LOGLEVEL_CONSTRUCT);
131 construct_from_basic(numeric(d));
135 // functions overriding virtual functions from bases classes
141 // new virtual functions which can be overridden by derived classes
147 // non-virtual functions in this class
152 void ex::swap(ex & other)
154 debugmsg("ex swap",LOGLEVEL_MEMBER_FUNCTION);
157 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
158 GINAC_ASSERT(other.bp!=0);
159 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
166 bool ex::info(unsigned inf) const
168 if (inf == info_flags::normal_form) {
170 // Polynomials are in normal form
171 if (info(info_flags::polynomial))
174 // polynomial^(-int) is in normal form
175 if (is_ex_exactly_of_type(*this, power))
176 return op(1).info(info_flags::negint);
178 // polynomial^(int) * polynomial^(int) * ... is in normal form
179 if (!is_ex_exactly_of_type(*this, mul))
181 for (int i=0; i<nops(); i++) {
182 if (is_ex_exactly_of_type(op(i), power)) {
183 if (!op(i).op(1).info(info_flags::integer))
185 if (!op(i).op(0).info(info_flags::polynomial))
188 if (!op(i).info(info_flags::polynomial))
193 return bp->info(inf);
203 ex ex::expand(unsigned options) const
206 return bp->expand(options);
209 bool ex::has(ex const & other) const
212 return bp->has(other);
215 int ex::degree(symbol const & s) const
218 return bp->degree(s);
221 int ex::ldegree(symbol const & s) const
224 return bp->ldegree(s);
227 ex ex::coeff(symbol const & s, int const n) const
230 return bp->coeff(s,n);
233 ex ex::numer(bool normalize) const
236 if (normalize && !info(info_flags::normal_form))
242 if (n.info(info_flags::polynomial))
246 if (is_ex_exactly_of_type(n, power) && n.op(1).info(info_flags::negint))
249 // something^(int) * something^(int) * ...
250 if (!is_ex_exactly_of_type(n, mul))
253 for (int i=0; i<n.nops(); i++) {
254 if (!is_ex_exactly_of_type(n.op(i), power) || !n.op(i).op(1).info(info_flags::negint))
260 ex ex::denom(bool normalize) const
263 if (normalize && !info(info_flags::normal_form))
269 if (n.info(info_flags::polynomial))
273 if (is_ex_exactly_of_type(n, power) && n.op(1).info(info_flags::negint))
274 return power(n.op(0), -(n.op(1)));
276 // something^(int) * something^(int) * ...
277 if (!is_ex_exactly_of_type(n, mul))
280 for (int i=0; i<n.nops(); i++) {
281 if (is_ex_exactly_of_type(n.op(i), power) && n.op(i).op(1).info(info_flags::negint))
282 res *= power(n.op(i), -1);
287 ex ex::collect(symbol const & s) const
290 return bp->collect(s);
293 ex ex::eval(int level) const
296 return bp->eval(level);
299 ex ex::evalf(int level) const
302 return bp->evalf(level);
305 ex ex::subs(lst const & ls, lst const & lr) const
308 return bp->subs(ls,lr);
311 ex ex::subs(ex const & e) const
317 exvector ex::get_indices(void) const
320 return bp->get_indices();
323 ex ex::simplify_ncmul(exvector const & v) const
326 return bp->simplify_ncmul(v);
329 ex ex::operator[](ex const & index) const
331 debugmsg("ex operator[ex]",LOGLEVEL_OPERATOR);
336 ex ex::operator[](int const i) const
338 debugmsg("ex operator[int]",LOGLEVEL_OPERATOR);
343 ex ex::op(int const i) const
345 debugmsg("ex op()",LOGLEVEL_MEMBER_FUNCTION);
350 ex & ex::let_op(int const i)
352 debugmsg("ex let_op()",LOGLEVEL_MEMBER_FUNCTION);
355 return bp->let_op(i);
358 #ifndef INLINE_EX_CONSTRUCTORS
359 int ex::compare(ex const & other) const
362 GINAC_ASSERT(other.bp!=0);
364 // special case: both expression point to same basic, trivially equal
367 return bp->compare(*other.bp);
369 #endif // ndef INLINE_EX_CONSTRUCTORS
371 #ifndef INLINE_EX_CONSTRUCTORS
372 bool ex::is_equal(ex const & other) const
375 GINAC_ASSERT(other.bp!=0);
377 // special case: both expression point to same basic, trivially equal
380 return bp->is_equal(*other.bp);
382 #endif // ndef INLINE_EX_CONSTRUCTORS
384 unsigned ex::return_type(void) const
387 return bp->return_type();
390 unsigned ex::return_type_tinfo(void) const
393 return bp->return_type_tinfo();
396 unsigned ex::gethash(void) const
399 return bp->gethash();
402 ex ex::exadd(ex const & rh) const
404 return (new add(*this,rh))->setflag(status_flags::dynallocated);
407 ex ex::exmul(ex const & rh) const
409 return (new mul(*this,rh))->setflag(status_flags::dynallocated);
412 ex ex::exncmul(ex const & rh) const
414 return (new ncmul(*this,rh))->setflag(status_flags::dynallocated);
419 void ex::makewriteable()
421 debugmsg("ex makewriteable",LOGLEVEL_MEMBER_FUNCTION);
423 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
424 if (bp->refcount > 1) {
425 basic * bp2=bp->duplicate();
427 bp2->setflag(status_flags::dynallocated);
431 GINAC_ASSERT(bp->refcount == 1);
434 void ex::construct_from_basic(basic const & other)
436 if ( (other.flags & status_flags::evaluated)==0 ) {
437 // cf. copy constructor
438 ex const & tmpex = other.eval(1); // evaluate only one (top) level
441 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
443 if ((other.flags & status_flags::dynallocated)&&(other.refcount==0)) {
444 delete &const_cast<basic &>(other);
447 if (other.flags & status_flags::dynallocated) {
448 bp=&const_cast<basic &>(other);
450 bp=other.duplicate();
451 bp->setflag(status_flags::dynallocated);
454 // bp->clearflag(status_flags::evaluated);
458 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
462 // static member variables
468 // functions which are not member functions
477 ex const & exZERO(void)
479 static ex * eZERO=new ex(numZERO());
483 ex const & exONE(void)
485 static ex * eONE=new ex(numONE());
489 ex const & exTWO(void)
491 static ex * eTWO=new ex(numTWO());
495 ex const & exTHREE(void)
497 static ex * eTHREE=new ex(numTHREE());
501 ex const & exMINUSONE(void)
503 static ex * eMINUSONE=new ex(numMINUSONE());
507 ex const & exHALF(void)
509 static ex * eHALF=new ex(ex(1)/ex(2));
513 ex const & exMINUSHALF(void)
515 static ex * eMINUSHALF=new ex(numeric(-1,2));
519 #ifndef NO_GINAC_NAMESPACE
521 #endif // ndef NO_GINAC_NAMESPACE