]> www.ginac.de Git - ginac.git/blob - ginac/basic.cpp
bug fix for output of interactive expression in ginaccint
[ginac.git] / ginac / basic.cpp
1 /** @file basic.cpp
2  *
3  *  Implementation of GiNaC's ABC. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 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 <typeinfo>
25 #include <stdexcept>
26
27 #include "basic.h"
28 #include "ex.h"
29 #include "numeric.h"
30 #include "power.h"
31 #include "symbol.h"
32 #include "lst.h"
33 #include "ncmul.h"
34 #include "archive.h"
35 #include "utils.h"
36 #include "debugmsg.h"
37
38 #ifndef NO_NAMESPACE_GINAC
39 namespace GiNaC {
40 #endif // ndef NO_NAMESPACE_GINAC
41
42 GINAC_IMPLEMENT_REGISTERED_CLASS(basic, void)
43
44 //////////
45 // default constructor, destructor, copy constructor assignment operator and helpers
46 //////////
47
48 // public
49
50 #ifndef INLINE_BASIC_CONSTRUCTORS
51 basic::basic() : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
52 {
53     debugmsg("basic default constructor", LOGLEVEL_CONSTRUCT);
54     // nothing to do
55 }
56
57 basic::~basic() 
58 {
59     debugmsg("basic destructor", LOGLEVEL_DESTRUCT);
60     destroy(0);
61     GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
62 }
63
64 basic::basic(const basic & other) : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
65 {
66     debugmsg("basic copy constructor", LOGLEVEL_CONSTRUCT);
67     copy(other);
68 }
69 #endif
70
71 const basic & basic::operator=(const basic & other)
72 {
73     debugmsg("basic operator=", LOGLEVEL_ASSIGNMENT);
74     if (this != &other) {
75         destroy(1);
76         copy(other);
77     }
78     return *this;
79 }
80
81 // protected
82
83 #if 0
84 void basic::copy(const basic & other)
85 {
86     flags=other.flags & ~ status_flags::dynallocated;
87     hashvalue=other.hashvalue;
88     tinfo_key=other.tinfo_key;
89 }
90 #endif
91
92 //////////
93 // other constructors
94 //////////
95
96 #ifndef INLINE_BASIC_CONSTRUCTORS
97 basic::basic(unsigned ti) : flags(0), refcount(0), tinfo_key(ti)
98 {
99     debugmsg("basic constructor with tinfo_key", LOGLEVEL_CONSTRUCT);
100     // nothing to do
101 }
102 #endif
103
104 //////////
105 // archiving
106 //////////
107
108 /** Construct object from archive_node. */
109 basic::basic(const archive_node &n, const lst &sym_lst) : flags(0), refcount(0)
110 {
111     debugmsg("basic constructor from archive_node", LOGLEVEL_CONSTRUCT);
112
113     // Reconstruct tinfo_key from class name
114     string class_name;
115     if (n.find_string("class", class_name))
116         tinfo_key = find_tinfo_key(class_name);
117     else
118         throw (std::runtime_error("archive node contains no class name"));
119 }
120
121 /** Unarchive the object. */
122 ex basic::unarchive(const archive_node &n, const lst &sym_lst)
123 {
124     return (new basic(n, sym_lst))->setflag(status_flags::dynallocated);
125 }
126
127 /** Archive the object. */
128 void basic::archive(archive_node &n) const
129 {
130     n.add_string("class", class_name());
131 }
132
133 //////////
134 // functions overriding virtual functions from bases classes
135 //////////
136
137 // none
138
139 //////////
140 // new virtual functions which can be overridden by derived classes
141 //////////
142
143 // public
144
145 /** Output to stream formatted to be useful as ginsh input. */
146 void basic::print(ostream & os, unsigned upper_precedence) const
147 {
148     debugmsg("basic print",LOGLEVEL_PRINT);
149     os << "[basic object]";
150 }
151
152 /** Output to stream in ugly raw format, so brave developers can have a look
153  * at the underlying structure. */
154 void basic::printraw(ostream & os) const
155 {
156     debugmsg("basic printraw",LOGLEVEL_PRINT);
157     os << "[basic object]";
158 }
159
160 /** Output to stream formatted in tree- (indented-) form, so developers can
161  *  have a look at the underlying structure. */
162 void basic::printtree(ostream & os, unsigned indent) const
163 {
164     debugmsg("basic printtree",LOGLEVEL_PRINT);
165     os << string(indent,' ') << "type=" << typeid(*this).name()
166        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
167        << ", flags=" << flags
168        << ", nops=" << nops() << endl;
169     for (unsigned i=0; i<nops(); ++i) {
170         op(i).printtree(os,indent+delta_indent);
171     }
172 }
173
174 /** Output to stream formatted as C-source.
175  *
176  *  @param os a stream for output
177  *  @param type variable type (one of the csrc_types)
178  *  @param upper_precedence operator precedence of caller
179  *  @see ex::printcsrc */
180 void basic::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
181 {
182     debugmsg("basic print csrc", LOGLEVEL_PRINT);
183 }
184
185 /** Little wrapper arount print to be called within a debugger. */
186 void basic::dbgprint(void) const
187 {
188     print(cerr);
189     cerr << endl;
190 }
191
192 /** Little wrapper arount printtree to be called within a debugger. */
193 void basic::dbgprinttree(void) const
194 {
195     printtree(cerr,0);
196 }
197
198 basic * basic::duplicate() const
199 {
200     debugmsg("basic duplicate",LOGLEVEL_DUPLICATE);
201     return new basic(*this);
202 }
203
204 /** Information about the object.
205  *
206  *  @see class info_flags */
207 bool basic::info(unsigned inf) const
208 {
209     return false; // all possible properties are false for basic objects
210 }
211
212 /** Number of operands/members. */
213 unsigned basic::nops() const
214 {
215     return 0;
216 }
217
218 /** Return operand/member at position i. */
219 ex basic::op(int i) const
220 {
221     return (const_cast<basic *>(this))->let_op(i);
222 }
223
224 /** Return modifyable operand/member at position i. */
225 ex & basic::let_op(int i)
226 {
227     throw(std::out_of_range("op() out of range"));
228 }
229
230 ex basic::operator[](const ex & index) const
231 {
232     if (is_exactly_of_type(*index.bp,numeric)) {
233         return op(static_cast<const numeric &>(*index.bp).to_int());
234     }
235     throw(std::invalid_argument("non-numeric indices not supported by this type"));
236 }
237
238 ex basic::operator[](int i) const
239 {
240     return op(i);
241 }
242
243 bool basic::has(const ex & other) const
244 {
245     GINAC_ASSERT(other.bp!=0);
246     if (is_equal(*other.bp)) return true;
247     if (nops()>0) {
248         for (unsigned i=0; i<nops(); i++) {
249             if (op(i).has(other)) return true;
250         }
251     }
252     return false;
253 }
254
255 int basic::degree(const symbol & s) const
256 {
257     return 0;
258 }
259
260 int basic::ldegree(const symbol & s) const
261 {
262     return 0;
263 }
264
265 ex basic::coeff(const symbol & s, int n) const
266 {
267     return n==0 ? *this : _ex0();
268 }
269
270 ex basic::collect(const symbol & s) const
271 {
272     ex x;
273     int ldeg=ldegree(s);
274     int deg=degree(s);
275     for (int n=ldeg; n<=deg; n++) {
276         x += coeff(s,n)*power(s,n);
277     }
278     return x;
279 }
280
281 ex basic::eval(int level) const
282 {
283     return this->hold();
284 }
285
286 ex basic::evalf(int level) const
287 {
288     return *this;
289 }
290
291 ex basic::subs(const lst & ls, const lst & lr) const
292 {
293     return *this;
294 }
295
296 /** Default interface of nth derivative ex::diff(s, n).  It should be called
297  *  instead of ::derivative(s) for first derivatives and for nth derivatives it
298  *  just recurses down.
299  *
300  *  @param s symbol to differentiate in
301  *  @param nth order of differentiation
302  *  @see ex::diff */
303 ex basic::diff(const symbol & s, unsigned nth) const
304 {
305     // trivial: zeroth derivative
306     if (!nth)
307         return ex(*this);
308     
309     // evaluate unevalueted *this before differentiating
310     if (!(flags & status_flags::evaluated))
311         return ex(*this).diff(s, nth);
312     
313     ex ndiff = derivative(s);
314     while (!ndiff.is_zero() &&    // stop differentiating zeros
315            nth>1) {
316         ndiff = ndiff.diff(s);
317         --nth;
318     }
319     return ndiff;
320 }
321
322 exvector basic::get_indices(void) const
323 {
324     return exvector(); // return an empty exvector
325 }
326
327 ex basic::simplify_ncmul(const exvector & v) const
328 {
329     return simplified_ncmul(v);
330 }
331
332 // protected
333
334 /** Default implementation of ex::diff(). It simply throws an error message.
335  *
336  *  @exception logic_error (differentiation not supported by this type)
337  *  @see ex::diff */
338 ex basic::derivative(const symbol & s) const
339 {
340     throw(std::logic_error("differentiation not supported by this type"));
341 }
342
343 int basic::compare_same_type(const basic & other) const
344 {
345     return compare_pointers(this, &other);
346 }
347
348 bool basic::is_equal_same_type(const basic & other) const
349 {
350     return compare_same_type(other)==0;
351 }
352
353 unsigned basic::return_type(void) const
354 {
355     return return_types::commutative;
356 }
357
358 unsigned basic::return_type_tinfo(void) const
359 {
360     return tinfo();
361 }
362
363 unsigned basic::calchash(void) const
364 {
365     unsigned v=golden_ratio_hash(tinfo());
366     for (unsigned i=0; i<nops(); i++) {
367         v=rotate_left_31(v);
368         v ^= (const_cast<basic *>(this))->op(i).gethash();
369     }
370
371     v = v & 0x7FFFFFFFU;
372     
373     // store calculated hash value only if object is already evaluated
374     if (flags & status_flags::evaluated) {
375         setflag(status_flags::hash_calculated);
376         hashvalue=v;
377     }
378
379     return v;
380 }
381
382 ex basic::expand(unsigned options) const
383 {
384     return this->setflag(status_flags::expanded);
385 }
386
387
388 //////////
389 // non-virtual functions in this class
390 //////////
391
392 // public
393
394 ex basic::subs(const ex & e) const
395 {
396     // accept 2 types of replacement expressions:
397     //   - symbol==ex
398     //   - lst(symbol1==ex1,symbol2==ex2,...)
399     // convert to subs(lst(symbol1,symbol2,...),lst(ex1,ex2,...))
400     // additionally, idx can be used instead of symbol
401     if (e.info(info_flags::relation_equal)) {
402         return subs(lst(e));
403     }
404     if (!e.info(info_flags::list)) {
405         throw(std::invalid_argument("basic::subs(ex): argument must be a list"));
406     }
407     lst ls;
408     lst lr;
409     for (unsigned i=0; i<e.nops(); i++) {
410         if (!e.op(i).info(info_flags::relation_equal)) {
411             throw(std::invalid_argument("basic::subs(ex): argument must be a list or equations"));
412         }
413         if (!e.op(i).op(0).info(info_flags::symbol)) {
414             if (!e.op(i).op(0).info(info_flags::idx)) {
415                 throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol or an idx"));
416             }
417         }
418         ls.append(e.op(i).op(0));
419         lr.append(e.op(i).op(1));
420     }
421     return subs(ls,lr);
422 }
423
424 /** Compare objects to establish canonical order.
425  *  All compare functions return: -1 for *this less than other, 0 equal,
426  *  1 greater. */
427 int basic::compare(const basic & other) const
428 {
429     unsigned hash_this = gethash();
430     unsigned hash_other = other.gethash();
431
432     if (hash_this<hash_other) return -1;
433     if (hash_this>hash_other) return 1;
434
435     unsigned typeid_this = tinfo();
436     unsigned typeid_other = other.tinfo();
437
438     if (typeid_this<typeid_other) {
439         /*
440         cout << "hash collision, different types: " 
441              << *this << " and " << other << endl;
442         this->printraw(cout);
443         cout << " and ";
444         other.printraw(cout);
445         cout << endl;
446         */
447         return -1;
448     }
449     if (typeid_this>typeid_other) {
450         /*
451         cout << "hash collision, different types: " 
452              << *this << " and " << other << endl;
453         this->printraw(cout);
454         cout << " and ";
455         other.printraw(cout);
456         cout << endl;
457         */
458         return 1;
459     }
460
461     GINAC_ASSERT(typeid(*this)==typeid(other));
462
463     int cmpval=compare_same_type(other);
464     if ((cmpval!=0)&&(hash_this<0x80000000U)) {
465         /*
466         cout << "hash collision, same type: " 
467              << *this << " and " << other << endl;
468         this->printraw(cout);
469         cout << " and ";
470         other.printraw(cout);
471         cout << endl;
472         */
473     }
474     return cmpval;
475 }
476
477 bool basic::is_equal(const basic & other) const
478 {
479     unsigned hash_this = gethash();
480     unsigned hash_other = other.gethash();
481
482     if (hash_this!=hash_other) return false;
483
484     unsigned typeid_this = tinfo();
485     unsigned typeid_other = other.tinfo();
486
487     if (typeid_this!=typeid_other) return false;
488
489     GINAC_ASSERT(typeid(*this)==typeid(other));
490
491     return is_equal_same_type(other);
492 }
493
494 // protected
495
496 const basic & basic::hold(void) const
497 {
498     return setflag(status_flags::evaluated);
499 }
500
501 void basic::ensure_if_modifiable(void) const
502 {
503     if (refcount>1) {
504         throw(std::runtime_error("cannot modify multiply referenced object"));
505     }
506 }
507
508 //////////
509 // static member variables
510 //////////
511
512 // protected
513
514 unsigned basic::precedence=70;
515 unsigned basic::delta_indent=4;
516
517 //////////
518 // global constants
519 //////////
520
521 const basic some_basic;
522 const type_info & typeid_basic=typeid(some_basic);
523
524 //////////
525 // global variables
526 //////////
527
528 int max_recursion_level=1024;
529
530 #ifndef NO_NAMESPACE_GINAC
531 } // namespace GiNaC
532 #endif // ndef NO_NAMESPACE_GINAC