3 * Implementation of GiNaC's ABC.
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
30 // default constructor, destructor, copy constructor assignment operator and helpers
35 #ifndef INLINE_BASIC_CONSTRUCTORS
36 basic::basic() : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
38 debugmsg("basic default constructor",LOGLEVEL_CONSTRUCT);
44 debugmsg("basic destructor",LOGLEVEL_DESTRUCT);
46 ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
49 basic::basic(basic const & other) : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
51 debugmsg("basic copy constructor",LOGLEVEL_CONSTRUCT);
56 basic const & basic::operator=(basic const & other)
58 debugmsg("basic operator=",LOGLEVEL_ASSIGNMENT);
69 void basic::copy(basic const & other)
71 flags=other.flags & ~ status_flags::dynallocated;
72 hashvalue=other.hashvalue;
73 tinfo_key=other.tinfo_key;
81 #ifndef INLINE_BASIC_CONSTRUCTORS
82 basic::basic(unsigned ti) : flags(0), refcount(0), tinfo_key(ti)
84 debugmsg("basic constructor with tinfo_key",LOGLEVEL_CONSTRUCT);
90 // functions overriding virtual functions from bases classes
96 // new virtual functions which can be overridden by derived classes
101 basic * basic::duplicate() const
103 debugmsg("basic duplicate",LOGLEVEL_DUPLICATE);
104 return new basic(*this);
107 bool basic::info(unsigned inf) const
109 return false; // all possible properties are false for basic objects
112 int basic::nops() const
117 ex basic::op(int const i) const
119 return (const_cast<basic *>(this))->let_op(i);
122 ex & basic::let_op(int const i)
124 throw(std::out_of_range("op() out of range"));
127 ex basic::operator[](ex const & index) const
129 if (is_exactly_of_type(*index.bp,numeric)) {
130 return op(static_cast<numeric const &>(*index.bp).to_int());
132 throw(std::invalid_argument("non-numeric indices not supported by this type"));
135 ex basic::operator[](int const i) const
140 bool basic::has(ex const & other) const
143 if (is_equal(*other.bp)) return true;
145 for (int i=0; i<nops(); i++) {
146 if (op(i).has(other)) return true;
152 int basic::degree(symbol const & s) const
157 int basic::ldegree(symbol const & s) const
162 ex basic::coeff(symbol const & s, int const n) const
164 return n==0 ? *this : exZERO();
167 ex basic::collect(symbol const & s) const
172 for (int n=ldeg; n<=deg; n++) {
173 x += coeff(s,n)*power(s,n);
178 ex basic::eval(int level) const
183 ex basic::evalf(int level) const
188 ex basic::subs(lst const & ls, lst const & lr) const
193 exvector basic::get_indices(void) const
195 return exvector(); // return an empty exvector
198 ex basic::simplify_ncmul(exvector const & v) const
200 return simplified_ncmul(v);
205 int basic::compare_same_type(basic const & other) const
207 return compare_pointers(this, &other);
210 bool basic::is_equal_same_type(basic const & other) const
212 return compare_same_type(other)==0;
215 unsigned basic::return_type(void) const
217 return return_types::commutative;
220 unsigned basic::return_type_tinfo(void) const
225 unsigned basic::calchash(void) const
227 unsigned v=golden_ratio_hash(tinfo());
228 for (int i=0; i<nops(); i++) {
230 v ^= (const_cast<basic *>(this))->let_op(i).gethash();
235 // store calculated hash value only if object is already evaluated
236 if (flags & status_flags::evaluated) {
237 setflag(status_flags::hash_calculated);
244 ex basic::expand(unsigned options) const
246 return this->setflag(status_flags::expanded);
250 // non-virtual functions in this class
255 ex basic::subs(ex const & e) const
257 // accept 2 types of replacement expressions:
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)) {
265 if (!e.info(info_flags::list)) {
266 throw(std::invalid_argument("basic::subs(ex): argument must be a list"));
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"));
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"));
279 ls.append(e.op(i).op(0));
280 lr.append(e.op(i).op(1));
285 // compare functions to sort expressions canonically
286 // all compare functions return: -1 for *this less than other, 0 equal, 1 greater
289 int basic::compare(basic const & other) const
291 const type_info & typeid_this = typeid(*this);
292 const type_info & typeid_other = typeid(other);
294 if (typeid_this==typeid_other) {
295 return compare_same_type(other);
298 // special rule: sort numeric() to end
299 if (typeid_this==typeid_numeric) return 1;
300 if (typeid_other==typeid_numeric) return -1;
302 // otherwise: sort according to type_info order (arbitrary, but well defined)
303 return typeid_this.before(typeid_other) ? -1 : 1;
307 int basic::compare(basic const & other) const
309 unsigned hash_this = gethash();
310 unsigned hash_other = other.gethash();
312 if (hash_this<hash_other) return -1;
313 if (hash_this>hash_other) return 1;
315 unsigned typeid_this = tinfo();
316 unsigned typeid_other = other.tinfo();
318 if (typeid_this<typeid_other) {
320 cout << "hash collision, different types: "
321 << *this << " and " << other << endl;
322 this->printraw(cout);
324 other.printraw(cout);
329 if (typeid_this>typeid_other) {
331 cout << "hash collision, different types: "
332 << *this << " and " << other << endl;
333 this->printraw(cout);
335 other.printraw(cout);
341 ASSERT(typeid(*this)==typeid(other));
343 int cmpval=compare_same_type(other);
344 if ((cmpval!=0)&&(hash_this<0x80000000U)) {
346 cout << "hash collision, same type: "
347 << *this << " and " << other << endl;
348 this->printraw(cout);
350 other.printraw(cout);
357 bool basic::is_equal(basic const & other) const
359 unsigned hash_this = gethash();
360 unsigned hash_other = other.gethash();
362 if (hash_this!=hash_other) return false;
364 unsigned typeid_this = tinfo();
365 unsigned typeid_other = other.tinfo();
367 if (typeid_this!=typeid_other) return false;
369 ASSERT(typeid(*this)==typeid(other));
371 return is_equal_same_type(other);
376 basic const & basic::hold(void) const
378 return setflag(status_flags::evaluated);
381 void basic::ensure_if_modifiable(void) const
384 throw(std::runtime_error("cannot modify multiply referenced object"));
389 // static member variables
394 unsigned basic::precedence=70;
395 unsigned basic::delta_indent=4;
401 const basic some_basic;
402 type_info const & typeid_basic=typeid(some_basic);
408 int max_recursion_level=1024;