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
34 #ifndef NO_GINAC_NAMESPACE
36 #endif // ndef NO_GINAC_NAMESPACE
39 // default constructor, destructor, copy constructor assignment operator and helpers
44 #ifndef INLINE_EX_CONSTRUCTORS
46 ex::ex() : bp(ex0().bp)
48 debugmsg("ex default constructor",LOGLEVEL_CONSTRUCT);
49 GINAC_ASSERT(ex0().bp!=0);
50 GINAC_ASSERT(ex0().bp->flags & status_flags::dynallocated);
57 debugmsg("ex destructor",LOGLEVEL_DESTRUCT);
59 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
60 if (--bp->refcount == 0) {
65 ex::ex(ex const & other) : bp(other.bp)
67 debugmsg("ex copy constructor",LOGLEVEL_CONSTRUCT);
69 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
73 ex const & ex::operator=(ex const & other)
75 debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);
77 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
78 GINAC_ASSERT(other.bp!=0);
79 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
81 basic * tmpbp=other.bp;
82 if (--bp->refcount==0) {
89 #endif // ndef INLINE_EX_CONSTRUCTORS
97 #ifndef INLINE_EX_CONSTRUCTORS
98 ex::ex(basic const & other)
100 debugmsg("ex constructor from basic",LOGLEVEL_CONSTRUCT);
101 construct_from_basic(other);
107 debugmsg("ex constructor from int",LOGLEVEL_CONSTRUCT);
108 construct_from_basic(numeric(i));
111 ex::ex(unsigned int const i)
113 debugmsg("ex constructor from unsigned int",LOGLEVEL_CONSTRUCT);
114 construct_from_basic(numeric(i));
119 debugmsg("ex constructor from long",LOGLEVEL_CONSTRUCT);
120 construct_from_basic(numeric(i));
123 ex::ex(unsigned long const i)
125 debugmsg("ex constructor from unsigned long",LOGLEVEL_CONSTRUCT);
126 construct_from_basic(numeric(i));
129 ex::ex(double const d)
131 debugmsg("ex constructor from double",LOGLEVEL_CONSTRUCT);
132 construct_from_basic(numeric(d));
136 // functions overriding virtual functions from bases classes
142 // new virtual functions which can be overridden by derived classes
148 // non-virtual functions in this class
153 /** Swap the contents of two expressions. */
154 void ex::swap(ex & other)
156 debugmsg("ex swap",LOGLEVEL_MEMBER_FUNCTION);
159 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
160 GINAC_ASSERT(other.bp!=0);
161 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
168 /** Output formatted to be useful as ginsh input. */
169 void ex::print(ostream & os, unsigned upper_precedence) const
171 debugmsg("ex print",LOGLEVEL_PRINT);
173 bp->print(os,upper_precedence);
176 void ex::printraw(ostream & os) const
178 debugmsg("ex printraw",LOGLEVEL_PRINT);
185 void ex::printtree(ostream & os, unsigned indent) const
187 debugmsg("ex printtree",LOGLEVEL_PRINT);
189 // os << "refcount=" << bp->refcount << " ";
190 bp->printtree(os,indent);
193 /** Print expression as a C++ statement. The output looks like
194 * "<type> <var_name> = <expression>;". The "type" parameter has an effect
195 * on how number literals are printed.
197 * @param os output stream
198 * @param type variable type (one of the csrc_types)
199 * @param var_name variable name to be printed */
200 void ex::printcsrc(ostream & os, unsigned type, const char *var_name) const
202 debugmsg("ex print csrc", LOGLEVEL_PRINT);
205 case csrc_types::ctype_float:
208 case csrc_types::ctype_double:
211 case csrc_types::ctype_cl_N:
215 os << var_name << " = ";
216 bp->printcsrc(os, type, 0);
220 /** Little wrapper arount print to be called within a debugger. */
221 void ex::dbgprint(void) const
223 debugmsg("ex dbgprint",LOGLEVEL_PRINT);
228 /** Little wrapper arount printtree to be called within a debugger. */
229 void ex::dbgprinttree(void) const
231 debugmsg("ex dbgprinttree",LOGLEVEL_PRINT);
236 bool ex::info(unsigned inf) const
238 if (inf == info_flags::normal_form) {
240 // Polynomials are in normal form
241 if (info(info_flags::polynomial))
244 // polynomial^(-int) is in normal form
245 if (is_ex_exactly_of_type(*this, power))
246 return op(1).info(info_flags::negint);
248 // polynomial^(int) * polynomial^(int) * ... is in normal form
249 if (!is_ex_exactly_of_type(*this, mul))
251 for (int i=0; i<nops(); i++) {
252 if (is_ex_exactly_of_type(op(i), power)) {
253 if (!op(i).op(1).info(info_flags::integer))
255 if (!op(i).op(0).info(info_flags::polynomial))
258 if (!op(i).info(info_flags::polynomial))
263 return bp->info(inf);
273 ex ex::expand(unsigned options) const
276 return bp->expand(options);
279 bool ex::has(ex const & other) const
282 return bp->has(other);
285 int ex::degree(symbol const & s) const
288 return bp->degree(s);
291 int ex::ldegree(symbol const & s) const
294 return bp->ldegree(s);
297 ex ex::coeff(symbol const & s, int const n) const
300 return bp->coeff(s,n);
303 ex ex::numer(bool normalize) const
306 if (normalize && !info(info_flags::normal_form))
312 if (n.info(info_flags::polynomial))
316 if (is_ex_exactly_of_type(n, power) && n.op(1).info(info_flags::negint))
319 // something^(int) * something^(int) * ...
320 if (!is_ex_exactly_of_type(n, mul))
323 for (int i=0; i<n.nops(); i++) {
324 if (!is_ex_exactly_of_type(n.op(i), power) || !n.op(i).op(1).info(info_flags::negint))
330 ex ex::denom(bool normalize) const
333 if (normalize && !info(info_flags::normal_form))
339 if (n.info(info_flags::polynomial))
343 if (is_ex_exactly_of_type(n, power) && n.op(1).info(info_flags::negint))
344 return power(n.op(0), -(n.op(1)));
346 // something^(int) * something^(int) * ...
347 if (!is_ex_exactly_of_type(n, mul))
350 for (int i=0; i<n.nops(); i++) {
351 if (is_ex_exactly_of_type(n.op(i), power) && n.op(i).op(1).info(info_flags::negint))
352 res *= power(n.op(i), -1);
357 ex ex::collect(symbol const & s) const
360 return bp->collect(s);
363 ex ex::eval(int level) const
366 return bp->eval(level);
369 ex ex::evalf(int level) const
372 return bp->evalf(level);
375 ex ex::subs(lst const & ls, lst const & lr) const
378 return bp->subs(ls,lr);
381 ex ex::subs(ex const & e) const
387 exvector ex::get_indices(void) const
390 return bp->get_indices();
393 ex ex::simplify_ncmul(exvector const & v) const
396 return bp->simplify_ncmul(v);
399 ex ex::operator[](ex const & index) const
401 debugmsg("ex operator[ex]",LOGLEVEL_OPERATOR);
406 ex ex::operator[](int const i) const
408 debugmsg("ex operator[int]",LOGLEVEL_OPERATOR);
413 ex ex::op(int const i) const
415 debugmsg("ex op()",LOGLEVEL_MEMBER_FUNCTION);
420 ex & ex::let_op(int const i)
422 debugmsg("ex let_op()",LOGLEVEL_MEMBER_FUNCTION);
425 return bp->let_op(i);
428 #ifndef INLINE_EX_CONSTRUCTORS
429 int ex::compare(ex const & other) const
432 GINAC_ASSERT(other.bp!=0);
434 // special case: both expression point to same basic, trivially equal
437 return bp->compare(*other.bp);
439 #endif // ndef INLINE_EX_CONSTRUCTORS
441 #ifndef INLINE_EX_CONSTRUCTORS
442 bool ex::is_equal(ex const & other) const
445 GINAC_ASSERT(other.bp!=0);
447 // special case: both expression point to same basic, trivially equal
450 return bp->is_equal(*other.bp);
452 #endif // ndef INLINE_EX_CONSTRUCTORS
454 unsigned ex::return_type(void) const
457 return bp->return_type();
460 unsigned ex::return_type_tinfo(void) const
463 return bp->return_type_tinfo();
466 unsigned ex::gethash(void) const
469 return bp->gethash();
472 ex ex::exadd(ex const & rh) const
474 return (new add(*this,rh))->setflag(status_flags::dynallocated);
477 ex ex::exmul(ex const & rh) const
479 return (new mul(*this,rh))->setflag(status_flags::dynallocated);
482 ex ex::exncmul(ex const & rh) const
484 return (new ncmul(*this,rh))->setflag(status_flags::dynallocated);
489 void ex::makewriteable()
491 debugmsg("ex makewriteable",LOGLEVEL_MEMBER_FUNCTION);
493 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
494 if (bp->refcount > 1) {
495 basic * bp2=bp->duplicate();
497 bp2->setflag(status_flags::dynallocated);
501 GINAC_ASSERT(bp->refcount == 1);
504 void ex::construct_from_basic(basic const & other)
506 if ((other.flags & status_flags::evaluated)==0) {
507 // cf. copy constructor
508 ex const & tmpex = other.eval(1); // evaluate only one (top) level
511 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
513 if ((other.flags & status_flags::dynallocated)&&(other.refcount==0)) {
514 delete &const_cast<basic &>(other);
517 if (other.flags & status_flags::dynallocated) {
518 bp=&const_cast<basic &>(other);
520 bp=other.duplicate();
521 bp->setflag(status_flags::dynallocated);
524 // bp->clearflag(status_flags::evaluated);
528 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
532 // static member variables
538 // functions which are not member functions
550 #ifndef NO_GINAC_NAMESPACE
552 #endif // ndef NO_GINAC_NAMESPACE