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
36 // default constructor, destructor, copy constructor assignment operator and helpers
41 #ifndef INLINE_EX_CONSTRUCTORS
43 ex::ex() : bp(exZERO().bp)
45 debugmsg("ex default constructor",LOGLEVEL_CONSTRUCT);
46 ASSERT(exZERO().bp!=0);
47 ASSERT(exZERO().bp->flags & status_flags::dynallocated);
54 debugmsg("ex destructor",LOGLEVEL_DESTRUCT);
56 ASSERT(bp->flags & status_flags::dynallocated);
57 if (--bp->refcount == 0) {
62 ex::ex(ex const & other) : bp(other.bp)
64 debugmsg("ex copy constructor",LOGLEVEL_CONSTRUCT);
66 ASSERT((bp->flags) & status_flags::dynallocated);
70 ex const & ex::operator=(ex const & other)
72 debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);
74 ASSERT(bp->flags & status_flags::dynallocated);
76 ASSERT(other.bp->flags & status_flags::dynallocated);
78 basic * tmpbp=other.bp;
79 if (--bp->refcount==0) {
86 #endif // ndef INLINE_EX_CONSTRUCTORS
94 #ifndef INLINE_EX_CONSTRUCTORS
95 ex::ex(basic const & other)
97 debugmsg("ex constructor from basic",LOGLEVEL_CONSTRUCT);
98 construct_from_basic(other);
104 debugmsg("ex constructor from int",LOGLEVEL_CONSTRUCT);
105 construct_from_basic(numeric(i));
108 ex::ex(unsigned int const i)
110 debugmsg("ex constructor from unsigned int",LOGLEVEL_CONSTRUCT);
111 construct_from_basic(numeric(i));
116 debugmsg("ex constructor from long",LOGLEVEL_CONSTRUCT);
117 construct_from_basic(numeric(i));
120 ex::ex(unsigned long const i)
122 debugmsg("ex constructor from unsigned long",LOGLEVEL_CONSTRUCT);
123 construct_from_basic(numeric(i));
126 ex::ex(double const d)
128 debugmsg("ex constructor from double",LOGLEVEL_CONSTRUCT);
129 construct_from_basic(numeric(d));
133 // functions overriding virtual functions from bases classes
139 // new virtual functions which can be overridden by derived classes
145 // non-virtual functions in this class
150 void ex::swap(ex & other)
152 debugmsg("ex swap",LOGLEVEL_MEMBER_FUNCTION);
155 ASSERT(bp->flags & status_flags::dynallocated);
157 ASSERT(other.bp->flags & status_flags::dynallocated);
164 bool ex::info(unsigned inf) const
166 if (inf == info_flags::normal_form) {
168 // Polynomials are in normal form
169 if (info(info_flags::polynomial))
172 // polynomial^(-int) is in normal form
173 if (is_ex_exactly_of_type(*this, power))
174 return op(1).info(info_flags::negint);
176 // polynomial^(int) * polynomial^(int) * ... is in normal form
177 if (!is_ex_exactly_of_type(*this, mul))
179 for (int i=0; i<nops(); i++) {
180 if (is_ex_exactly_of_type(op(i), power)) {
181 if (!op(i).op(1).info(info_flags::integer))
183 if (!op(i).op(0).info(info_flags::polynomial))
186 if (!op(i).info(info_flags::polynomial))
191 return bp->info(inf);
201 ex ex::expand(unsigned options) const
204 return bp->expand(options);
207 bool ex::has(ex const & other) const
210 return bp->has(other);
213 int ex::degree(symbol const & s) const
216 return bp->degree(s);
219 int ex::ldegree(symbol const & s) const
222 return bp->ldegree(s);
225 ex ex::coeff(symbol const & s, int const n) const
228 return bp->coeff(s,n);
231 ex ex::numer(bool normalize) const
234 if (normalize && !info(info_flags::normal_form))
240 if (n.info(info_flags::polynomial))
244 if (is_ex_exactly_of_type(n, power) && n.op(1).info(info_flags::negint))
247 // something^(int) * something^(int) * ...
248 if (!is_ex_exactly_of_type(n, mul))
251 for (int i=0; i<n.nops(); i++) {
252 if (!is_ex_exactly_of_type(n.op(i), power) || !n.op(i).op(1).info(info_flags::negint))
258 ex ex::denom(bool normalize) const
261 if (normalize && !info(info_flags::normal_form))
267 if (n.info(info_flags::polynomial))
271 if (is_ex_exactly_of_type(n, power) && n.op(1).info(info_flags::negint))
272 return power(n.op(0), -(n.op(1)));
274 // something^(int) * something^(int) * ...
275 if (!is_ex_exactly_of_type(n, mul))
278 for (int i=0; i<n.nops(); i++) {
279 if (is_ex_exactly_of_type(n.op(i), power) && n.op(i).op(1).info(info_flags::negint))
280 res *= power(n.op(i), -1);
285 ex ex::collect(symbol const & s) const
288 return bp->collect(s);
291 ex ex::eval(int level) const
294 return bp->eval(level);
297 ex ex::evalf(int level) const
300 return bp->evalf(level);
303 ex ex::subs(lst const & ls, lst const & lr) const
306 return bp->subs(ls,lr);
309 ex ex::subs(ex const & e) const
315 exvector ex::get_indices(void) const
318 return bp->get_indices();
321 ex ex::simplify_ncmul(exvector const & v) const
324 return bp->simplify_ncmul(v);
327 ex ex::operator[](ex const & index) const
329 debugmsg("ex operator[ex]",LOGLEVEL_OPERATOR);
334 ex ex::operator[](int const i) const
336 debugmsg("ex operator[int]",LOGLEVEL_OPERATOR);
341 ex ex::op(int const i) const
343 debugmsg("ex op()",LOGLEVEL_MEMBER_FUNCTION);
348 ex & ex::let_op(int const i)
350 debugmsg("ex let_op()",LOGLEVEL_MEMBER_FUNCTION);
353 return bp->let_op(i);
356 #ifndef INLINE_EX_CONSTRUCTORS
357 int ex::compare(ex const & other) const
362 // special case: both expression point to same basic, trivially equal
365 return bp->compare(*other.bp);
367 #endif // ndef INLINE_EX_CONSTRUCTORS
369 #ifndef INLINE_EX_CONSTRUCTORS
370 bool ex::is_equal(ex const & other) const
375 // special case: both expression point to same basic, trivially equal
378 return bp->is_equal(*other.bp);
380 #endif // ndef INLINE_EX_CONSTRUCTORS
382 unsigned ex::return_type(void) const
385 return bp->return_type();
388 unsigned ex::return_type_tinfo(void) const
391 return bp->return_type_tinfo();
394 unsigned ex::gethash(void) const
397 return bp->gethash();
400 ex ex::exadd(ex const & rh) const
402 return (new add(*this,rh))->setflag(status_flags::dynallocated);
405 ex ex::exmul(ex const & rh) const
407 return (new mul(*this,rh))->setflag(status_flags::dynallocated);
410 ex ex::exncmul(ex const & rh) const
412 return (new ncmul(*this,rh))->setflag(status_flags::dynallocated);
417 void ex::makewriteable()
419 debugmsg("ex makewriteable",LOGLEVEL_MEMBER_FUNCTION);
421 ASSERT(bp->flags & status_flags::dynallocated);
422 if (bp->refcount > 1) {
423 basic * bp2=bp->duplicate();
425 bp2->setflag(status_flags::dynallocated);
429 ASSERT(bp->refcount == 1);
432 void ex::construct_from_basic(basic const & other)
434 if ( (other.flags & status_flags::evaluated)==0 ) {
435 // cf. copy constructor
436 ex const & tmpex = other.eval(1); // evaluate only one (top) level
439 ASSERT(bp->flags & status_flags::dynallocated);
441 if ((other.flags & status_flags::dynallocated)&&(other.refcount==0)) {
442 delete &const_cast<basic &>(other);
445 if (other.flags & status_flags::dynallocated) {
446 bp=&const_cast<basic &>(other);
448 bp=other.duplicate();
449 bp->setflag(status_flags::dynallocated);
452 // bp->clearflag(status_flags::evaluated);
456 ASSERT(bp->flags & status_flags::dynallocated);
460 // static member variables
466 // functions which are not member functions
475 ex const & exZERO(void)
477 static ex * eZERO=new ex(numZERO());
481 ex const & exONE(void)
483 static ex * eONE=new ex(numONE());
487 ex const & exTWO(void)
489 static ex * eTWO=new ex(numTWO());
493 ex const & exTHREE(void)
495 static ex * eTHREE=new ex(numTHREE());
499 ex const & exMINUSONE(void)
501 static ex * eMINUSONE=new ex(numMINUSONE());
505 ex const & exHALF(void)
507 static ex * eHALF=new ex(ex(1)/ex(2));
511 ex const & exMINUSHALF(void)
513 static ex * eMINUSHALF=new ex(numeric(-1,2));