3 * Implementation of 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
32 #include "relational.h"
34 #include "input_lexer.h"
47 // functions overriding virtual functions from bases classes
53 // new virtual functions which can be overridden by derived classes
59 // non-virtual functions in this class
64 /** Efficiently swap the contents of two expressions. */
65 void ex::swap(ex & other)
67 debugmsg("ex swap",LOGLEVEL_MEMBER_FUNCTION);
70 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
71 GINAC_ASSERT(other.bp!=0);
72 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
79 /** Print expression to stream. The formatting of the output is determined
80 * by the kind of print_context object that is passed. Possible formattings
81 * include ginsh-parsable output (the default), tree-like output for
82 * debugging, and C++ source.
83 * @see print_context */
84 void ex::print(const print_context & c, unsigned level) const
86 debugmsg("ex print", LOGLEVEL_PRINT);
91 /** Print expression to stream in a tree-like format suitable for debugging. */
92 void ex::printtree(std::ostream & os) const
94 debugmsg("ex printtree", LOGLEVEL_PRINT);
96 bp->print(print_tree(os));
99 /** Little wrapper arount print to be called within a debugger. */
100 void ex::dbgprint(void) const
102 debugmsg("ex dbgprint", LOGLEVEL_PRINT);
107 /** Little wrapper arount printtree to be called within a debugger. */
108 void ex::dbgprinttree(void) const
110 debugmsg("ex dbgprinttree", LOGLEVEL_PRINT);
115 ex ex::expand(unsigned options) const
118 if (bp->flags & status_flags::expanded)
121 return bp->expand(options);
124 /** Compute partial derivative of an expression.
126 * @param s symbol by which the expression is derived
127 * @param nth order of derivative (default 1)
128 * @return partial derivative as a new expression */
129 ex ex::diff(const symbol & s, unsigned nth) const
136 return bp->diff(s, nth);
139 /** Simplify/canonicalize expression containing indexed objects. This
140 * performs contraction of dummy indices where possible and checks whether
141 * the free indices in sums are consistent.
143 * @return simplified expression */
144 ex ex::simplify_indexed(void) const
146 return GiNaC::simplify_indexed(*this);
149 /** Simplify/canonicalize expression containing indexed objects. This
150 * performs contraction of dummy indices where possible, checks whether
151 * the free indices in sums are consistent, and automatically replaces
152 * scalar products by known values if desired.
154 * @param sp Scalar products to be replaced automatically
155 * @return simplified expression */
156 ex ex::simplify_indexed(const scalar_products & sp) const
158 return GiNaC::simplify_indexed(*this, sp);
161 ex ex::operator[](const ex & index) const
163 debugmsg("ex operator[ex]",LOGLEVEL_OPERATOR);
168 ex ex::operator[](int i) const
170 debugmsg("ex operator[int]",LOGLEVEL_OPERATOR);
175 /** Return modifyable operand/member at position i. */
176 ex & ex::let_op(int i)
178 debugmsg("ex let_op()",LOGLEVEL_MEMBER_FUNCTION);
181 return bp->let_op(i);
184 /** Left hand side of relational expression. */
185 ex ex::lhs(void) const
187 debugmsg("ex lhs()",LOGLEVEL_MEMBER_FUNCTION);
188 if (!is_ex_of_type(*this,relational))
189 throw std::runtime_error("ex::lhs(): not a relation");
190 return (*static_cast<relational *>(bp)).lhs();
193 /** Right hand side of relational expression. */
194 ex ex::rhs(void) const
196 debugmsg("ex rhs()",LOGLEVEL_MEMBER_FUNCTION);
197 if (!is_ex_of_type(*this,relational))
198 throw std::runtime_error("ex::rhs(): not a relation");
199 return (*static_cast<relational *>(bp)).rhs();
204 /** Make this ex writable (if more than one ex handle the same basic) by
205 * unlinking the object and creating an unshared copy of it. */
206 void ex::makewriteable()
208 debugmsg("ex makewriteable",LOGLEVEL_MEMBER_FUNCTION);
210 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
211 if (bp->refcount > 1) {
212 basic * bp2 = bp->duplicate();
214 bp2->setflag(status_flags::dynallocated);
218 GINAC_ASSERT(bp->refcount==1);
221 /** Ctor from basic implementation.
222 * @see ex::ex(const basic &) */
223 void ex::construct_from_basic(const basic & other)
225 if ((other.flags & status_flags::evaluated)==0) {
227 const ex & tmpex = other.eval(1); // evaluate only one (top) level
230 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
232 if ((other.flags & status_flags::dynallocated)&&(other.refcount==0))
233 delete &const_cast<basic &>(other);
235 if (other.flags & status_flags::dynallocated) {
236 // ok, it is already on the heap, so just copy bp:
237 bp = &const_cast<basic &>(other);
239 // create a duplicate on the heap:
240 bp = other.duplicate();
241 bp->setflag(status_flags::dynallocated);
247 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
250 void ex::construct_from_int(int i)
252 switch (i) { // some tiny efficiency-hack
275 bp->setflag(status_flags::dynallocated);
277 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
278 GINAC_ASSERT(bp->refcount==1);
282 void ex::construct_from_uint(unsigned int i)
284 switch (i) { // some tiny efficiency-hack
299 bp->setflag(status_flags::dynallocated);
301 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
302 GINAC_ASSERT(bp->refcount==1);
306 void ex::construct_from_long(long i)
308 switch (i) { // some tiny efficiency-hack
331 bp->setflag(status_flags::dynallocated);
333 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
334 GINAC_ASSERT(bp->refcount==1);
338 void ex::construct_from_ulong(unsigned long i)
340 switch (i) { // some tiny efficiency-hack
355 bp->setflag(status_flags::dynallocated);
357 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
358 GINAC_ASSERT(bp->refcount==1);
362 void ex::construct_from_double(double d)
365 bp->setflag(status_flags::dynallocated);
367 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
368 GINAC_ASSERT(bp->refcount==1);
371 void ex::construct_from_string_and_lst(const std::string &s, const ex &l)
374 set_lexer_symbols(l);
375 ginac_yyrestart(NULL);
377 throw (std::runtime_error(get_parser_error()));
381 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
387 // static member variables
393 // functions which are not member functions