3 * Implementation of GiNaC's ABC. */
6 * GiNaC Copyright (C) 1999 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
37 // default constructor, destructor, copy constructor assignment operator and helpers
42 #ifndef INLINE_BASIC_CONSTRUCTORS
43 basic::basic() : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
45 debugmsg("basic default constructor",LOGLEVEL_CONSTRUCT);
51 debugmsg("basic destructor",LOGLEVEL_DESTRUCT);
53 ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
56 basic::basic(basic const & other) : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
58 debugmsg("basic copy constructor",LOGLEVEL_CONSTRUCT);
63 basic const & basic::operator=(basic const & other)
65 debugmsg("basic operator=",LOGLEVEL_ASSIGNMENT);
76 void basic::copy(basic const & other)
78 flags=other.flags & ~ status_flags::dynallocated;
79 hashvalue=other.hashvalue;
80 tinfo_key=other.tinfo_key;
88 #ifndef INLINE_BASIC_CONSTRUCTORS
89 basic::basic(unsigned ti) : flags(0), refcount(0), tinfo_key(ti)
91 debugmsg("basic constructor with tinfo_key",LOGLEVEL_CONSTRUCT);
97 // functions overriding virtual functions from bases classes
103 // new virtual functions which can be overridden by derived classes
108 basic * basic::duplicate() const
110 debugmsg("basic duplicate",LOGLEVEL_DUPLICATE);
111 return new basic(*this);
114 bool basic::info(unsigned inf) const
116 return false; // all possible properties are false for basic objects
119 int basic::nops() const
124 ex basic::op(int const i) const
126 return (const_cast<basic *>(this))->let_op(i);
129 ex & basic::let_op(int const i)
131 throw(std::out_of_range("op() out of range"));
134 ex basic::operator[](ex const & index) const
136 if (is_exactly_of_type(*index.bp,numeric)) {
137 return op(static_cast<numeric const &>(*index.bp).to_int());
139 throw(std::invalid_argument("non-numeric indices not supported by this type"));
142 ex basic::operator[](int const i) const
147 bool basic::has(ex const & other) const
150 if (is_equal(*other.bp)) return true;
152 for (int i=0; i<nops(); i++) {
153 if (op(i).has(other)) return true;
159 int basic::degree(symbol const & s) const
164 int basic::ldegree(symbol const & s) const
169 ex basic::coeff(symbol const & s, int const n) const
171 return n==0 ? *this : exZERO();
174 ex basic::collect(symbol const & s) const
179 for (int n=ldeg; n<=deg; n++) {
180 x += coeff(s,n)*power(s,n);
185 ex basic::eval(int level) const
190 ex basic::evalf(int level) const
195 ex basic::subs(lst const & ls, lst const & lr) const
200 exvector basic::get_indices(void) const
202 return exvector(); // return an empty exvector
205 ex basic::simplify_ncmul(exvector const & v) const
207 return simplified_ncmul(v);
212 int basic::compare_same_type(basic const & other) const
214 return compare_pointers(this, &other);
217 bool basic::is_equal_same_type(basic const & other) const
219 return compare_same_type(other)==0;
222 unsigned basic::return_type(void) const
224 return return_types::commutative;
227 unsigned basic::return_type_tinfo(void) const
232 unsigned basic::calchash(void) const
234 unsigned v=golden_ratio_hash(tinfo());
235 for (int i=0; i<nops(); i++) {
237 v ^= (const_cast<basic *>(this))->let_op(i).gethash();
242 // store calculated hash value only if object is already evaluated
243 if (flags & status_flags::evaluated) {
244 setflag(status_flags::hash_calculated);
251 ex basic::expand(unsigned options) const
253 return this->setflag(status_flags::expanded);
257 // non-virtual functions in this class
262 ex basic::subs(ex const & e) const
264 // accept 2 types of replacement expressions:
266 // - lst(symbol1==ex1,symbol2==ex2,...)
267 // convert to subs(lst(symbol1,symbol2,...),lst(ex1,ex2,...))
268 // additionally, idx can be used instead of symbol
269 if (e.info(info_flags::relation_equal)) {
272 if (!e.info(info_flags::list)) {
273 throw(std::invalid_argument("basic::subs(ex): argument must be a list"));
277 for (int i=0; i<e.nops(); i++) {
278 if (!e.op(i).info(info_flags::relation_equal)) {
279 throw(std::invalid_argument("basic::subs(ex): argument must be a list or equations"));
281 if (!e.op(i).op(0).info(info_flags::symbol)) {
282 if (!e.op(i).op(0).info(info_flags::idx)) {
283 throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol or an idx"));
286 ls.append(e.op(i).op(0));
287 lr.append(e.op(i).op(1));
292 // compare functions to sort expressions canonically
293 // all compare functions return: -1 for *this less than other, 0 equal, 1 greater
296 int basic::compare(basic const & other) const
298 const type_info & typeid_this = typeid(*this);
299 const type_info & typeid_other = typeid(other);
301 if (typeid_this==typeid_other) {
302 return compare_same_type(other);
305 // special rule: sort numeric() to end
306 if (typeid_this==typeid_numeric) return 1;
307 if (typeid_other==typeid_numeric) return -1;
309 // otherwise: sort according to type_info order (arbitrary, but well defined)
310 return typeid_this.before(typeid_other) ? -1 : 1;
314 int basic::compare(basic const & other) const
316 unsigned hash_this = gethash();
317 unsigned hash_other = other.gethash();
319 if (hash_this<hash_other) return -1;
320 if (hash_this>hash_other) return 1;
322 unsigned typeid_this = tinfo();
323 unsigned typeid_other = other.tinfo();
325 if (typeid_this<typeid_other) {
327 cout << "hash collision, different types: "
328 << *this << " and " << other << endl;
329 this->printraw(cout);
331 other.printraw(cout);
336 if (typeid_this>typeid_other) {
338 cout << "hash collision, different types: "
339 << *this << " and " << other << endl;
340 this->printraw(cout);
342 other.printraw(cout);
348 ASSERT(typeid(*this)==typeid(other));
350 int cmpval=compare_same_type(other);
351 if ((cmpval!=0)&&(hash_this<0x80000000U)) {
353 cout << "hash collision, same type: "
354 << *this << " and " << other << endl;
355 this->printraw(cout);
357 other.printraw(cout);
364 bool basic::is_equal(basic const & other) const
366 unsigned hash_this = gethash();
367 unsigned hash_other = other.gethash();
369 if (hash_this!=hash_other) return false;
371 unsigned typeid_this = tinfo();
372 unsigned typeid_other = other.tinfo();
374 if (typeid_this!=typeid_other) return false;
376 ASSERT(typeid(*this)==typeid(other));
378 return is_equal_same_type(other);
383 basic const & basic::hold(void) const
385 return setflag(status_flags::evaluated);
388 void basic::ensure_if_modifiable(void) const
391 throw(std::runtime_error("cannot modify multiply referenced object"));
396 // static member variables
401 unsigned basic::precedence=70;
402 unsigned basic::delta_indent=4;
408 const basic some_basic;
409 type_info const & typeid_basic=typeid(some_basic);
415 int max_recursion_level=1024;