02d20e8894f1308e14850fced01d4cd43a8f0c68
[ginac.git] / ginac / ex.cpp
1 /** @file ex.cpp
2  *
3  *  Implementation of GiNaC's light-weight expression handles. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <iostream>
24 #include <stdexcept>
25
26 #include "ex.h"
27 #include "add.h"
28 #include "mul.h"
29 #include "ncmul.h"
30 #include "numeric.h"
31 #include "power.h"
32 #include "relational.h"
33 #include "indexed.h"
34 #include "input_lexer.h"
35 #include "debugmsg.h"
36 #include "utils.h"
37
38 namespace GiNaC {
39
40 //////////
41 // other ctors
42 //////////
43
44 // none (all inlined)
45
46 //////////
47 // functions overriding virtual functions from bases classes
48 //////////
49
50 // none
51
52 //////////
53 // new virtual functions which can be overridden by derived classes
54 //////////
55
56 // none
57
58 //////////
59 // non-virtual functions in this class
60 //////////
61
62 // public
63
64 /** Efficiently swap the contents of two expressions. */
65 void ex::swap(ex & other)
66 {
67         debugmsg("ex swap",LOGLEVEL_MEMBER_FUNCTION);
68
69         GINAC_ASSERT(bp!=0);
70         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
71         GINAC_ASSERT(other.bp!=0);
72         GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
73         
74         basic * tmpbp = bp;
75         bp = other.bp;
76         other.bp = tmpbp;
77 }
78
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
85 {
86         debugmsg("ex print", LOGLEVEL_PRINT);
87         GINAC_ASSERT(bp!=0);
88         bp->print(c, level);
89 }
90
91 /** Print expression to stream in a tree-like format suitable for debugging. */
92 void ex::printtree(std::ostream & os) const
93 {
94         debugmsg("ex printtree", LOGLEVEL_PRINT);
95         GINAC_ASSERT(bp!=0);
96         bp->print(print_tree(os));
97 }
98
99 /** Little wrapper arount print to be called within a debugger. */
100 void ex::dbgprint(void) const
101 {
102         debugmsg("ex dbgprint", LOGLEVEL_PRINT);
103         GINAC_ASSERT(bp!=0);
104         bp->dbgprint();
105 }
106
107 /** Little wrapper arount printtree to be called within a debugger. */
108 void ex::dbgprinttree(void) const
109 {
110         debugmsg("ex dbgprinttree", LOGLEVEL_PRINT);
111         GINAC_ASSERT(bp!=0);
112         bp->dbgprinttree();
113 }
114
115 ex ex::expand(unsigned options) const
116 {
117         GINAC_ASSERT(bp!=0);
118         if (bp->flags & status_flags::expanded)
119                 return *bp;
120         else
121                 return bp->expand(options);
122 }
123
124 /** Compute partial derivative of an expression.
125  *
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
130 {
131         GINAC_ASSERT(bp!=0);
132
133         if (!nth)
134                 return *this;
135         else
136                 return bp->diff(s, nth);
137 }
138
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.
142  *
143  *  @return simplified expression */
144 ex ex::simplify_indexed(void) const
145 {
146         return GiNaC::simplify_indexed(*this);
147 }
148
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.
153  *
154  *  @param sp Scalar products to be replaced automatically
155  *  @return simplified expression */
156 ex ex::simplify_indexed(const scalar_products & sp) const
157 {
158         return GiNaC::simplify_indexed(*this, sp);
159 }
160
161 ex ex::operator[](const ex & index) const
162 {
163         debugmsg("ex operator[ex]",LOGLEVEL_OPERATOR);
164         GINAC_ASSERT(bp!=0);
165         return (*bp)[index];
166 }
167
168 ex ex::operator[](int i) const
169 {
170         debugmsg("ex operator[int]",LOGLEVEL_OPERATOR);
171         GINAC_ASSERT(bp!=0);
172         return (*bp)[i];
173 }
174
175 /** Return modifyable operand/member at position i. */
176 ex & ex::let_op(int i)
177 {
178         debugmsg("ex let_op()",LOGLEVEL_MEMBER_FUNCTION);
179         makewriteable();
180         GINAC_ASSERT(bp!=0);
181         return bp->let_op(i);
182 }
183
184 /** Left hand side of relational expression. */
185 ex ex::lhs(void) const
186 {
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();
191 }
192
193 /** Right hand side of relational expression. */
194 ex ex::rhs(void) const
195 {
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();
200 }
201
202 // private
203
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()
207 {
208         debugmsg("ex makewriteable",LOGLEVEL_MEMBER_FUNCTION);
209         GINAC_ASSERT(bp!=0);
210         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
211         if (bp->refcount > 1) {
212                 basic * bp2 = bp->duplicate();
213                 ++bp2->refcount;
214                 bp2->setflag(status_flags::dynallocated);
215                 --bp->refcount;
216                 bp = bp2;
217         }
218         GINAC_ASSERT(bp->refcount==1);
219 }
220
221 /** Ctor from basic implementation.
222  *  @see ex::ex(const basic &) */
223 void ex::construct_from_basic(const basic & other)
224 {
225         if ((other.flags & status_flags::evaluated)==0) {
226                 // cf. copy ctor
227                 const ex & tmpex = other.eval(1); // evaluate only one (top) level
228                 bp = tmpex.bp;
229                 GINAC_ASSERT(bp!=0);
230                 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
231                 ++bp->refcount;
232                 if ((other.flags & status_flags::dynallocated)&&(other.refcount==0))
233                         delete &const_cast<basic &>(other);
234         } else {
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);
238                 } else {
239                         // create a duplicate on the heap:
240                         bp = other.duplicate();
241                         bp->setflag(status_flags::dynallocated);
242                 }
243                 GINAC_ASSERT(bp!=0);
244                 ++bp->refcount;
245         }
246         GINAC_ASSERT(bp!=0);
247         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
248 }
249
250 void ex::construct_from_int(int i)
251 {
252         switch (i) {  // some tiny efficiency-hack
253         case -2:
254                 bp = _ex_2().bp;
255                 ++bp->refcount;
256                 break;
257         case -1:
258                 bp = _ex_1().bp;
259                 ++bp->refcount;
260                 break;
261         case 0:
262                 bp = _ex0().bp;
263                 ++bp->refcount;
264                 break;
265         case 1:
266                 bp = _ex1().bp;
267                 ++bp->refcount;
268                 break;
269         case 2:
270                 bp = _ex2().bp;
271                 ++bp->refcount;
272                 break;
273         default:
274                 bp = new numeric(i);
275                 bp->setflag(status_flags::dynallocated);
276                 ++bp->refcount;
277                 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
278                 GINAC_ASSERT(bp->refcount==1);
279         }
280 }
281         
282 void ex::construct_from_uint(unsigned int i)
283 {
284         switch (i) {  // some tiny efficiency-hack
285         case 0:
286                 bp = _ex0().bp;
287                 ++bp->refcount;
288                 break;
289         case 1:
290                 bp = _ex1().bp;
291                 ++bp->refcount;
292                 break;
293         case 2:
294                 bp = _ex2().bp;
295                 ++bp->refcount;
296                 break;
297         default:
298                 bp = new numeric(i);
299                 bp->setflag(status_flags::dynallocated);
300                 ++bp->refcount;
301                 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
302                 GINAC_ASSERT(bp->refcount==1);
303         }
304 }
305         
306 void ex::construct_from_long(long i)
307 {
308         switch (i) {  // some tiny efficiency-hack
309         case -2:
310                 bp = _ex_2().bp;
311                 ++bp->refcount;
312                 break;
313         case -1:
314                 bp = _ex_1().bp;
315                 ++bp->refcount;
316                 break;
317         case 0:
318                 bp = _ex0().bp;
319                 ++bp->refcount;
320                 break;
321         case 1:
322                 bp = _ex1().bp;
323                 ++bp->refcount;
324                 break;
325         case 2:
326                 bp = _ex2().bp;
327                 ++bp->refcount;
328                 break;
329         default:
330                 bp = new numeric(i);
331                 bp->setflag(status_flags::dynallocated);
332                 ++bp->refcount;
333                 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
334                 GINAC_ASSERT(bp->refcount==1);
335         }
336 }
337         
338 void ex::construct_from_ulong(unsigned long i)
339 {
340         switch (i) {  // some tiny efficiency-hack
341         case 0:
342                 bp = _ex0().bp;
343                 ++bp->refcount;
344                 break;
345         case 1:
346                 bp = _ex1().bp;
347                 ++bp->refcount;
348                 break;
349         case 2:
350                 bp = _ex2().bp;
351                 ++bp->refcount;
352                 break;
353         default:
354                 bp = new numeric(i);
355                 bp->setflag(status_flags::dynallocated);
356                 ++bp->refcount;
357                 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
358                 GINAC_ASSERT(bp->refcount==1);
359         }
360 }
361         
362 void ex::construct_from_double(double d)
363 {
364         bp = new numeric(d);
365         bp->setflag(status_flags::dynallocated);
366         ++bp->refcount;
367         GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
368         GINAC_ASSERT(bp->refcount==1);
369 }
370
371 void ex::construct_from_string_and_lst(const std::string &s, const ex &l)
372 {
373         set_lexer_string(s);
374         set_lexer_symbols(l);
375         ginac_yyrestart(NULL);
376         if (ginac_yyparse())
377                 throw (std::runtime_error(get_parser_error()));
378         else {
379                 bp = parsed_ex.bp;
380                 GINAC_ASSERT(bp!=0);
381                 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
382                 ++bp->refcount;
383         }
384 }
385         
386 //////////
387 // static member variables
388 //////////
389
390 // none
391
392 //////////
393 // functions which are not member functions
394 //////////
395
396 // none
397
398 //////////
399 // global functions
400 //////////
401
402 // none
403
404
405 } // namespace GiNaC