]> www.ginac.de Git - ginac.git/blob - ginac/basic.cpp
8317f8129adb7134b2a5b625d96ba93862befa2c
[ginac.git] / ginac / basic.cpp
1 /** @file basic.cpp
2  *
3  *  Implementation of GiNaC's ABC.
4  *
5  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <iostream>
23 #include <typeinfo>
24 #include <stdexcept>
25
26 #include "ginac.h"
27 #include "utils.h"
28
29 //////////
30 // default constructor, destructor, copy constructor assignment operator and helpers
31 //////////
32
33 // public
34
35 #ifndef INLINE_BASIC_CONSTRUCTORS
36 basic::basic() : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
37 {
38     debugmsg("basic default constructor",LOGLEVEL_CONSTRUCT);
39     // nothing to do
40 }
41
42 basic::~basic() 
43 {
44     debugmsg("basic destructor",LOGLEVEL_DESTRUCT);
45     destroy(0);
46     ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
47 }
48
49 basic::basic(basic const & other) : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
50 {
51     debugmsg("basic copy constructor",LOGLEVEL_CONSTRUCT);
52     copy(other);
53 }
54 #endif
55
56 basic const & basic::operator=(basic const & other)
57 {
58     debugmsg("basic operator=",LOGLEVEL_ASSIGNMENT);
59     if (this != &other) {
60         destroy(1);
61         copy(other);
62     }
63     return *this;
64 }
65
66 // protected
67
68 #if 0
69 void basic::copy(basic const & other)
70 {
71     flags=other.flags & ~ status_flags::dynallocated;
72     hashvalue=other.hashvalue;
73     tinfo_key=other.tinfo_key;
74 }
75 #endif
76
77 //////////
78 // other constructors
79 //////////
80
81 #ifndef INLINE_BASIC_CONSTRUCTORS
82 basic::basic(unsigned ti) : flags(0), refcount(0), tinfo_key(ti)
83 {
84     debugmsg("basic constructor with tinfo_key",LOGLEVEL_CONSTRUCT);
85     // nothing to do
86 }
87 #endif
88
89 //////////
90 // functions overriding virtual functions from bases classes
91 //////////
92
93 // none
94
95 //////////
96 // new virtual functions which can be overridden by derived classes
97 //////////
98
99 // public
100
101 basic * basic::duplicate() const
102 {
103     debugmsg("basic duplicate",LOGLEVEL_DUPLICATE);
104     return new basic(*this);
105 }
106
107 bool basic::info(unsigned inf) const
108 {
109     return false; // all possible properties are false for basic objects
110 }
111
112 int basic::nops() const
113 {
114     return 0;
115 }
116
117 ex basic::op(int const i) const
118 {
119     return (const_cast<basic *>(this))->let_op(i);
120 }
121
122 ex & basic::let_op(int const i)
123 {
124     throw(std::out_of_range("op() out of range"));
125 }
126
127 ex basic::operator[](ex const & index) const
128 {
129     if (is_exactly_of_type(*index.bp,numeric)) {
130         return op(static_cast<numeric const &>(*index.bp).to_int());
131     }
132     throw(std::invalid_argument("non-numeric indices not supported by this type"));
133 }
134
135 ex basic::operator[](int const i) const
136 {
137     return op(i);
138 }
139
140 bool basic::has(ex const & other) const
141 {
142     ASSERT(other.bp!=0);
143     if (is_equal(*other.bp)) return true;
144     if (nops()>0) {
145         for (int i=0; i<nops(); i++) {
146             if (op(i).has(other)) return true;
147         }
148     }
149     return false;
150 }
151
152 int basic::degree(symbol const & s) const
153 {
154     return 0;
155 }
156
157 int basic::ldegree(symbol const & s) const
158 {
159     return 0;
160 }
161
162 ex basic::coeff(symbol const & s, int const n) const
163 {
164     return n==0 ? *this : exZERO();
165 }
166
167 ex basic::collect(symbol const & s) const
168 {
169     ex x;
170     int ldeg=ldegree(s);
171     int deg=degree(s);
172     for (int n=ldeg; n<=deg; n++) {
173         x += coeff(s,n)*power(s,n);
174     }
175     return x;
176 }
177
178 ex basic::eval(int level) const
179 {
180     return this->hold();
181 }
182
183 ex basic::evalf(int level) const
184 {
185     return *this;
186 }
187
188 ex basic::subs(lst const & ls, lst const & lr) const
189 {
190     return *this;
191 }
192
193 exvector basic::get_indices(void) const
194 {
195     return exvector(); // return an empty exvector
196 }
197
198 ex basic::simplify_ncmul(exvector const & v) const
199 {
200     return simplified_ncmul(v);
201 }
202
203 // protected
204
205 int basic::compare_same_type(basic const & other) const
206 {
207     return compare_pointers(this, &other);
208 }
209
210 bool basic::is_equal_same_type(basic const & other) const
211 {
212     return compare_same_type(other)==0;
213 }
214
215 unsigned basic::return_type(void) const
216 {
217     return return_types::commutative;
218 }
219
220 unsigned basic::return_type_tinfo(void) const
221 {
222     return tinfo();
223 }
224
225 unsigned basic::calchash(void) const
226 {
227     unsigned v=golden_ratio_hash(tinfo());
228     for (int i=0; i<nops(); i++) {
229         v=rotate_left_31(v);
230         v ^= (const_cast<basic *>(this))->let_op(i).gethash();
231     }
232
233     v = v & 0x7FFFFFFFU;
234     
235     // store calculated hash value only if object is already evaluated
236     if (flags & status_flags::evaluated) {
237         setflag(status_flags::hash_calculated);
238         hashvalue=v;
239     }
240
241     return v;
242 }
243
244 ex basic::expand(unsigned options) const
245 {
246     return this->setflag(status_flags::expanded);
247 }
248
249 //////////
250 // non-virtual functions in this class
251 //////////
252
253 // public
254
255 ex basic::subs(ex const & e) const
256 {
257     // accept 2 types of replacement expressions:
258     //   - symbol==ex
259     //   - lst(symbol1==ex1,symbol2==ex2,...)
260     // convert to subs(lst(symbol1,symbol2,...),lst(ex1,ex2,...))
261     // additionally, idx can be used instead of symbol
262     if (e.info(info_flags::relation_equal)) {
263         return subs(lst(e));
264     }
265     if (!e.info(info_flags::list)) {
266         throw(std::invalid_argument("basic::subs(ex): argument must be a list"));
267     }
268     lst ls;
269     lst lr;
270     for (int i=0; i<e.nops(); i++) {
271         if (!e.op(i).info(info_flags::relation_equal)) {
272             throw(std::invalid_argument("basic::subs(ex): argument must be a list or equations"));
273         }
274         if (!e.op(i).op(0).info(info_flags::symbol)) {
275             if (!e.op(i).op(0).info(info_flags::idx)) {
276                 throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol or an idx"));
277             }
278         }
279         ls.append(e.op(i).op(0));
280         lr.append(e.op(i).op(1));
281     }
282     return subs(ls,lr);
283 }
284
285 // compare functions to sort expressions canonically
286 // all compare functions return: -1 for *this less than other, 0 equal, 1 greater
287
288 /*
289 int basic::compare(basic const & other) const
290 {
291     const type_info & typeid_this = typeid(*this);
292     const type_info & typeid_other = typeid(other);
293
294     if (typeid_this==typeid_other) {
295         return compare_same_type(other);
296     }
297
298     // special rule: sort numeric() to end
299     if (typeid_this==typeid_numeric) return 1;
300     if (typeid_other==typeid_numeric) return -1;
301
302     // otherwise: sort according to type_info order (arbitrary, but well defined)
303     return typeid_this.before(typeid_other) ? -1 : 1;
304 }
305 */
306
307 int basic::compare(basic const & other) const
308 {
309     unsigned hash_this = gethash();
310     unsigned hash_other = other.gethash();
311
312     if (hash_this<hash_other) return -1;
313     if (hash_this>hash_other) return 1;
314
315     unsigned typeid_this = tinfo();
316     unsigned typeid_other = other.tinfo();
317
318     if (typeid_this<typeid_other) {
319         /*
320         cout << "hash collision, different types: " 
321              << *this << " and " << other << endl;
322         this->printraw(cout);
323         cout << " and ";
324         other.printraw(cout);
325         cout << endl;
326         */
327         return -1;
328     }
329     if (typeid_this>typeid_other) {
330         /*
331         cout << "hash collision, different types: " 
332              << *this << " and " << other << endl;
333         this->printraw(cout);
334         cout << " and ";
335         other.printraw(cout);
336         cout << endl;
337         */
338         return 1;
339     }
340
341     ASSERT(typeid(*this)==typeid(other));
342
343     int cmpval=compare_same_type(other);
344     if ((cmpval!=0)&&(hash_this<0x80000000U)) {
345         /*
346         cout << "hash collision, same type: " 
347              << *this << " and " << other << endl;
348         this->printraw(cout);
349         cout << " and ";
350         other.printraw(cout);
351         cout << endl;
352         */
353     }
354     return cmpval;
355 }
356
357 bool basic::is_equal(basic const & other) const
358 {
359     unsigned hash_this = gethash();
360     unsigned hash_other = other.gethash();
361
362     if (hash_this!=hash_other) return false;
363
364     unsigned typeid_this = tinfo();
365     unsigned typeid_other = other.tinfo();
366
367     if (typeid_this!=typeid_other) return false;
368
369     ASSERT(typeid(*this)==typeid(other));
370
371     return is_equal_same_type(other);
372 }
373
374 // protected
375
376 basic const & basic::hold(void) const
377 {
378     return setflag(status_flags::evaluated);
379 }
380
381 void basic::ensure_if_modifiable(void) const
382 {
383     if (refcount>1) {
384         throw(std::runtime_error("cannot modify multiply referenced object"));
385     }
386 }
387
388 //////////
389 // static member variables
390 //////////
391
392 // protected
393
394 unsigned basic::precedence=70;
395 unsigned basic::delta_indent=4;
396
397 //////////
398 // global constants
399 //////////
400
401 const basic some_basic;
402 type_info const & typeid_basic=typeid(some_basic);
403
404 //////////
405 // global variables
406 //////////
407
408 int max_recursion_level=1024;