]> www.ginac.de Git - ginac.git/blob - ginac/ex.cpp
some functions that took a "const symbol &" now take a "const ex &"
[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-2003 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 "lst.h"
33 #include "relational.h"
34 #include "input_lexer.h"
35 #include "utils.h"
36
37 namespace GiNaC {
38
39 //////////
40 // other constructors
41 //////////
42
43 // none (all inlined)
44
45 //////////
46 // non-virtual functions in this class
47 //////////
48
49 // public
50         
51 /** Print expression to stream. The formatting of the output is determined
52  *  by the kind of print_context object that is passed. Possible formattings
53  *  include ginsh-parsable output (the default), tree-like output for
54  *  debugging, and C++ source.
55  *  @see print_context */
56 void ex::print(const print_context & c, unsigned level) const
57 {
58         bp->print(c, level);
59 }
60
61 /** Little wrapper arount print to be called within a debugger. */
62 void ex::dbgprint() const
63 {
64         bp->dbgprint();
65 }
66
67 /** Little wrapper arount printtree to be called within a debugger. */
68 void ex::dbgprinttree() const
69 {
70         bp->dbgprinttree();
71 }
72
73 ex ex::expand(unsigned options) const
74 {
75         if (options == 0 && (bp->flags & status_flags::expanded)) // The "expanded" flag only covers the standard options; someone might want to re-expand with different options
76                 return *bp;
77         else
78                 return bp->expand(options);
79 }
80
81 /** Compute partial derivative of an expression.
82  *
83  *  @param s  symbol by which the expression is derived
84  *  @param nth  order of derivative (default 1)
85  *  @return partial derivative as a new expression */
86 ex ex::diff(const symbol & s, unsigned nth) const
87 {
88         if (!nth)
89                 return *this;
90         else
91                 return bp->diff(s, nth);
92 }
93
94 /** Check whether expression matches a specified pattern. */
95 bool ex::match(const ex & pattern) const
96 {
97         lst repl_lst;
98         return bp->match(pattern, repl_lst);
99 }
100
101 /** Find all occurrences of a pattern. The found matches are appended to
102  *  the "found" list. If the expression itself matches the pattern, the
103  *  children are not further examined. This function returns true when any
104  *  matches were found. */
105 bool ex::find(const ex & pattern, lst & found) const
106 {
107         if (match(pattern)) {
108                 found.append(*this);
109                 found.sort();
110                 found.unique();
111                 return true;
112         }
113         bool any_found = false;
114         for (size_t i=0; i<nops(); i++)
115                 if (op(i).find(pattern, found))
116                         any_found = true;
117         return any_found;
118 }
119
120 /** Substitute objects in an expression (syntactic substitution) and return
121  *  the result as a new expression. */
122 ex ex::subs(const lst & ls, const lst & lr, unsigned options) const
123 {
124         GINAC_ASSERT(ls.nops() == lr.nops());
125
126         // Convert the lists to a map
127         exmap m;
128         for (lst::const_iterator its = ls.begin(), itr = lr.begin(); its != ls.end(); ++its, ++itr) {
129                 m.insert(std::make_pair(*its, *itr));
130
131                 // Search for products and powers in the expressions to be substituted
132                 // (for an optimization in expairseq::subs())
133                 if (is_exactly_a<mul>(*its) || is_exactly_a<power>(*its))
134                         options |= subs_options::pattern_is_product;
135         }
136         if (!(options & subs_options::pattern_is_product))
137                 options |= subs_options::pattern_is_not_product;
138
139         return bp->subs(m, options);
140 }
141
142 /** Substitute objects in an expression (syntactic substitution) and return
143  *  the result as a new expression.  There are two valid types of
144  *  replacement arguments: 1) a relational like object==ex and 2) a list of
145  *  relationals lst(object1==ex1,object2==ex2,...). */
146 ex ex::subs(const ex & e, unsigned options) const
147 {
148         if (e.info(info_flags::relation_equal)) {
149                 exmap m;
150                 const ex & s = e.lhs();
151                 m.insert(std::make_pair(s, e.rhs()));
152                 if (is_exactly_a<mul>(s) || is_exactly_a<power>(s))
153                         options |= subs_options::pattern_is_product;
154                 return bp->subs(m, options);
155         } else if (!e.info(info_flags::list))
156                 throw(std::invalid_argument("basic::subs(ex): argument must be a list"));
157
158         // Convert the list to a map
159         exmap m;
160         GINAC_ASSERT(is_a<lst>(e));
161         for (lst::const_iterator it = ex_to<lst>(e).begin(); it != ex_to<lst>(e).end(); ++it) {
162                 ex r = *it;
163                 if (!r.info(info_flags::relation_equal))
164                         throw(std::invalid_argument("basic::subs(ex): argument must be a list of equations"));
165                 const ex & s = r.lhs();
166                 m.insert(std::make_pair(s, r.rhs()));
167
168                 // Search for products and powers in the expressions to be substituted
169                 // (for an optimization in expairseq::subs())
170                 if (is_exactly_a<mul>(s) || is_exactly_a<power>(s))
171                         options |= subs_options::pattern_is_product;
172         }
173         if (!(options & subs_options::pattern_is_product))
174                 options |= subs_options::pattern_is_not_product;
175
176         return bp->subs(m, options);
177 }
178
179 /** Traverse expression tree with given visitor, preorder traversal. */
180 void ex::traverse_preorder(visitor & v) const
181 {
182         accept(v);
183
184         size_t n = nops();
185         for (size_t i = 0; i < n; ++i)
186                 op(i).traverse_preorder(v);
187 }
188
189 /** Traverse expression tree with given visitor, postorder traversal. */
190 void ex::traverse_postorder(visitor & v) const
191 {
192         size_t n = nops();
193         for (size_t i = 0; i < n; ++i)
194                 op(i).traverse_postorder(v);
195
196         accept(v);
197 }
198
199 /** Return modifyable operand/member at position i. */
200 ex & ex::let_op(size_t i)
201 {
202         makewriteable();
203         return bp->let_op(i);
204 }
205
206 ex & ex::operator[](const ex & index)
207 {
208         makewriteable();
209         return (*bp)[index];
210 }
211
212 ex & ex::operator[](size_t i)
213 {
214         makewriteable();
215         return (*bp)[i];
216 }
217
218 /** Left hand side of relational expression. */
219 ex ex::lhs() const
220 {
221         if (!is_a<relational>(*this))
222                 throw std::runtime_error("ex::lhs(): not a relation");
223         return bp->op(0);
224 }
225
226 /** Right hand side of relational expression. */
227 ex ex::rhs() const
228 {
229         if (!is_a<relational>(*this))
230                 throw std::runtime_error("ex::rhs(): not a relation");
231         return bp->op(1);
232 }
233
234 // private
235
236 /** Make this ex writable (if more than one ex handle the same basic) by 
237  *  unlinking the object and creating an unshared copy of it. */
238 void ex::makewriteable()
239 {
240         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
241         bp.makewritable();
242         GINAC_ASSERT(bp->refcount == 1);
243 }
244
245 /** Share equal objects between expressions.
246  *  @see ex::compare(const basic &) */
247 void ex::share(const ex & other) const
248 {
249         if ((bp->flags & status_flags::not_shareable) || (other.bp->flags & status_flags::not_shareable))
250                 return;
251
252         if (bp->refcount <= other.bp->refcount)
253                 bp = other.bp;
254         else
255                 other.bp = bp;
256 }
257
258 /** Helper function for the ex-from-basic constructor. This is where GiNaC's
259  *  automatic evaluator and memory management are implemented.
260  *  @see ex::ex(const basic &) */
261 ptr<basic> ex::construct_from_basic(const basic & other)
262 {
263         if (!(other.flags & status_flags::evaluated)) {
264
265                 // The object is not yet evaluated, so call eval() to evaluate
266                 // the top level. This will return either
267                 //  a) the original object with status_flags::evaluated set (when the
268                 //     eval() implementation calls hold())
269                 // or
270                 //  b) a different expression.
271                 //
272                 // eval() returns an ex, not a basic&, so this will go through
273                 // construct_from_basic() a second time. In case a) we end up in
274                 // the "else" branch below. In case b) we end up here again and
275                 // apply eval() once more. The recursion stops when eval() calls
276                 // hold() or returns an object that already has its "evaluated"
277                 // flag set, such as a symbol or a numeric.
278                 const ex & tmpex = other.eval(1);
279
280                 // Eventually, the eval() recursion goes through the "else" branch
281                 // below, which assures that the object pointed to by tmpex.bp is
282                 // allocated on the heap (either it was already on the heap or it
283                 // is a heap-allocated duplicate of another object).
284                 GINAC_ASSERT(tmpex.bp->flags & status_flags::dynallocated); 
285
286                 // If the original object is not referenced but heap-allocated,
287                 // it means that eval() hit case b) above. The original object is
288                 // no longer needed (it evaluated into something different), so we
289                 // delete it (because nobody else will).
290                 if ((other.refcount==0) && (other.flags & status_flags::dynallocated))
291                         delete &other; // yes, you can apply delete to a const pointer
292
293                 // We can't return a basic& here because the tmpex is destroyed as
294                 // soon as we leave the function, which would deallocate the
295                 // evaluated object.
296                 return tmpex.bp;
297
298         } else {
299
300                 // The easy case: making an "ex" out of an evaluated object.
301                 if (other.flags & status_flags::dynallocated) {
302
303                         // The object is already heap-allocated, so we can just make
304                         // another reference to it.
305                         return ptr<basic>(const_cast<basic &>(other));
306
307                 } else {
308
309                         // The object is not heap-allocated, so we create a duplicate
310                         // on the heap.
311                         basic *bp = other.duplicate();
312                         bp->setflag(status_flags::dynallocated);
313                         GINAC_ASSERT(bp->refcount == 0);
314                         return bp;
315                 }
316         }
317 }
318
319 basic & ex::construct_from_int(int i)
320 {
321         switch (i) {  // prefer flyweights over new objects
322         case -12:
323                 return const_cast<numeric &>(_num_12);
324         case -11:
325                 return const_cast<numeric &>(_num_11);
326         case -10:
327                 return const_cast<numeric &>(_num_10);
328         case -9:
329                 return const_cast<numeric &>(_num_9);
330         case -8:
331                 return const_cast<numeric &>(_num_8);
332         case -7:
333                 return const_cast<numeric &>(_num_7);
334         case -6:
335                 return const_cast<numeric &>(_num_6);
336         case -5:
337                 return const_cast<numeric &>(_num_5);
338         case -4:
339                 return const_cast<numeric &>(_num_4);
340         case -3:
341                 return const_cast<numeric &>(_num_3);
342         case -2:
343                 return const_cast<numeric &>(_num_2);
344         case -1:
345                 return const_cast<numeric &>(_num_1);
346         case 0:
347                 return const_cast<numeric &>(_num0);
348         case 1:
349                 return const_cast<numeric &>(_num1);
350         case 2:
351                 return const_cast<numeric &>(_num2);
352         case 3:
353                 return const_cast<numeric &>(_num3);
354         case 4:
355                 return const_cast<numeric &>(_num4);
356         case 5:
357                 return const_cast<numeric &>(_num5);
358         case 6:
359                 return const_cast<numeric &>(_num6);
360         case 7:
361                 return const_cast<numeric &>(_num7);
362         case 8:
363                 return const_cast<numeric &>(_num8);
364         case 9:
365                 return const_cast<numeric &>(_num9);
366         case 10:
367                 return const_cast<numeric &>(_num10);
368         case 11:
369                 return const_cast<numeric &>(_num11);
370         case 12:
371                 return const_cast<numeric &>(_num12);
372         default:
373                 basic *bp = new numeric(i);
374                 bp->setflag(status_flags::dynallocated);
375                 GINAC_ASSERT(bp->refcount == 0);
376                 return *bp;
377         }
378 }
379         
380 basic & ex::construct_from_uint(unsigned int i)
381 {
382         switch (i) {  // prefer flyweights over new objects
383         case 0:
384                 return const_cast<numeric &>(_num0);
385         case 1:
386                 return const_cast<numeric &>(_num1);
387         case 2:
388                 return const_cast<numeric &>(_num2);
389         case 3:
390                 return const_cast<numeric &>(_num3);
391         case 4:
392                 return const_cast<numeric &>(_num4);
393         case 5:
394                 return const_cast<numeric &>(_num5);
395         case 6:
396                 return const_cast<numeric &>(_num6);
397         case 7:
398                 return const_cast<numeric &>(_num7);
399         case 8:
400                 return const_cast<numeric &>(_num8);
401         case 9:
402                 return const_cast<numeric &>(_num9);
403         case 10:
404                 return const_cast<numeric &>(_num10);
405         case 11:
406                 return const_cast<numeric &>(_num11);
407         case 12:
408                 return const_cast<numeric &>(_num12);
409         default:
410                 basic *bp = new numeric(i);
411                 bp->setflag(status_flags::dynallocated);
412                 GINAC_ASSERT(bp->refcount == 0);
413                 return *bp;
414         }
415 }
416         
417 basic & ex::construct_from_long(long i)
418 {
419         switch (i) {  // prefer flyweights over new objects
420         case -12:
421                 return const_cast<numeric &>(_num_12);
422         case -11:
423                 return const_cast<numeric &>(_num_11);
424         case -10:
425                 return const_cast<numeric &>(_num_10);
426         case -9:
427                 return const_cast<numeric &>(_num_9);
428         case -8:
429                 return const_cast<numeric &>(_num_8);
430         case -7:
431                 return const_cast<numeric &>(_num_7);
432         case -6:
433                 return const_cast<numeric &>(_num_6);
434         case -5:
435                 return const_cast<numeric &>(_num_5);
436         case -4:
437                 return const_cast<numeric &>(_num_4);
438         case -3:
439                 return const_cast<numeric &>(_num_3);
440         case -2:
441                 return const_cast<numeric &>(_num_2);
442         case -1:
443                 return const_cast<numeric &>(_num_1);
444         case 0:
445                 return const_cast<numeric &>(_num0);
446         case 1:
447                 return const_cast<numeric &>(_num1);
448         case 2:
449                 return const_cast<numeric &>(_num2);
450         case 3:
451                 return const_cast<numeric &>(_num3);
452         case 4:
453                 return const_cast<numeric &>(_num4);
454         case 5:
455                 return const_cast<numeric &>(_num5);
456         case 6:
457                 return const_cast<numeric &>(_num6);
458         case 7:
459                 return const_cast<numeric &>(_num7);
460         case 8:
461                 return const_cast<numeric &>(_num8);
462         case 9:
463                 return const_cast<numeric &>(_num9);
464         case 10:
465                 return const_cast<numeric &>(_num10);
466         case 11:
467                 return const_cast<numeric &>(_num11);
468         case 12:
469                 return const_cast<numeric &>(_num12);
470         default:
471                 basic *bp = new numeric(i);
472                 bp->setflag(status_flags::dynallocated);
473                 GINAC_ASSERT(bp->refcount == 0);
474                 return *bp;
475         }
476 }
477         
478 basic & ex::construct_from_ulong(unsigned long i)
479 {
480         switch (i) {  // prefer flyweights over new objects
481         case 0:
482                 return const_cast<numeric &>(_num0);
483         case 1:
484                 return const_cast<numeric &>(_num1);
485         case 2:
486                 return const_cast<numeric &>(_num2);
487         case 3:
488                 return const_cast<numeric &>(_num3);
489         case 4:
490                 return const_cast<numeric &>(_num4);
491         case 5:
492                 return const_cast<numeric &>(_num5);
493         case 6:
494                 return const_cast<numeric &>(_num6);
495         case 7:
496                 return const_cast<numeric &>(_num7);
497         case 8:
498                 return const_cast<numeric &>(_num8);
499         case 9:
500                 return const_cast<numeric &>(_num9);
501         case 10:
502                 return const_cast<numeric &>(_num10);
503         case 11:
504                 return const_cast<numeric &>(_num11);
505         case 12:
506                 return const_cast<numeric &>(_num12);
507         default:
508                 basic *bp = new numeric(i);
509                 bp->setflag(status_flags::dynallocated);
510                 GINAC_ASSERT(bp->refcount == 0);
511                 return *bp;
512         }
513 }
514         
515 basic & ex::construct_from_double(double d)
516 {
517         basic *bp = new numeric(d);
518         bp->setflag(status_flags::dynallocated);
519         GINAC_ASSERT(bp->refcount == 0);
520         return *bp;
521 }
522
523 ptr<basic> ex::construct_from_string_and_lst(const std::string &s, const ex &l)
524 {
525         set_lexer_string(s);
526         set_lexer_symbols(l);
527         ginac_yyrestart(NULL);
528         if (ginac_yyparse())
529                 throw (std::runtime_error(get_parser_error()));
530         else
531                 return parsed_ex.bp;
532 }
533         
534 //////////
535 // static member variables
536 //////////
537
538 // none
539
540 //////////
541 // functions which are not member functions
542 //////////
543
544 // none
545
546 //////////
547 // global functions
548 //////////
549
550 // none
551
552
553 } // namespace GiNaC