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
36 // default constructor, destructor, copy constructor assignment operator and helpers
41 #ifndef INLINE_BASIC_CONSTRUCTORS
42 basic::basic() : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
44 debugmsg("basic default constructor",LOGLEVEL_CONSTRUCT);
50 debugmsg("basic destructor",LOGLEVEL_DESTRUCT);
52 ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
55 basic::basic(basic const & other) : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
57 debugmsg("basic copy constructor",LOGLEVEL_CONSTRUCT);
62 basic const & basic::operator=(basic const & other)
64 debugmsg("basic operator=",LOGLEVEL_ASSIGNMENT);
75 void basic::copy(basic const & other)
77 flags=other.flags & ~ status_flags::dynallocated;
78 hashvalue=other.hashvalue;
79 tinfo_key=other.tinfo_key;
87 #ifndef INLINE_BASIC_CONSTRUCTORS
88 basic::basic(unsigned ti) : flags(0), refcount(0), tinfo_key(ti)
90 debugmsg("basic constructor with tinfo_key",LOGLEVEL_CONSTRUCT);
96 // functions overriding virtual functions from bases classes
102 // new virtual functions which can be overridden by derived classes
107 basic * basic::duplicate() const
109 debugmsg("basic duplicate",LOGLEVEL_DUPLICATE);
110 return new basic(*this);
113 bool basic::info(unsigned inf) const
115 return false; // all possible properties are false for basic objects
118 int basic::nops() const
123 ex basic::op(int const i) const
125 return (const_cast<basic *>(this))->let_op(i);
128 ex & basic::let_op(int const i)
130 throw(std::out_of_range("op() out of range"));
133 ex basic::operator[](ex const & index) const
135 if (is_exactly_of_type(*index.bp,numeric)) {
136 return op(static_cast<numeric const &>(*index.bp).to_int());
138 throw(std::invalid_argument("non-numeric indices not supported by this type"));
141 ex basic::operator[](int const i) const
146 bool basic::has(ex const & other) const
149 if (is_equal(*other.bp)) return true;
151 for (int i=0; i<nops(); i++) {
152 if (op(i).has(other)) return true;
158 int basic::degree(symbol const & s) const
163 int basic::ldegree(symbol const & s) const
168 ex basic::coeff(symbol const & s, int const n) const
170 return n==0 ? *this : exZERO();
173 ex basic::collect(symbol const & s) const
178 for (int n=ldeg; n<=deg; n++) {
179 x += coeff(s,n)*power(s,n);
184 ex basic::eval(int level) const
189 ex basic::evalf(int level) const
194 ex basic::subs(lst const & ls, lst const & lr) const
199 exvector basic::get_indices(void) const
201 return exvector(); // return an empty exvector
204 ex basic::simplify_ncmul(exvector const & v) const
206 return simplified_ncmul(v);
211 int basic::compare_same_type(basic const & other) const
213 return compare_pointers(this, &other);
216 bool basic::is_equal_same_type(basic const & other) const
218 return compare_same_type(other)==0;
221 unsigned basic::return_type(void) const
223 return return_types::commutative;
226 unsigned basic::return_type_tinfo(void) const
231 unsigned basic::calchash(void) const
233 unsigned v=golden_ratio_hash(tinfo());
234 for (int i=0; i<nops(); i++) {
236 v ^= (const_cast<basic *>(this))->let_op(i).gethash();
241 // store calculated hash value only if object is already evaluated
242 if (flags & status_flags::evaluated) {
243 setflag(status_flags::hash_calculated);
250 ex basic::expand(unsigned options) const
252 return this->setflag(status_flags::expanded);
256 // non-virtual functions in this class
261 ex basic::subs(ex const & e) const
263 // accept 2 types of replacement expressions:
265 // - lst(symbol1==ex1,symbol2==ex2,...)
266 // convert to subs(lst(symbol1,symbol2,...),lst(ex1,ex2,...))
267 // additionally, idx can be used instead of symbol
268 if (e.info(info_flags::relation_equal)) {
271 if (!e.info(info_flags::list)) {
272 throw(std::invalid_argument("basic::subs(ex): argument must be a list"));
276 for (int i=0; i<e.nops(); i++) {
277 if (!e.op(i).info(info_flags::relation_equal)) {
278 throw(std::invalid_argument("basic::subs(ex): argument must be a list or equations"));
280 if (!e.op(i).op(0).info(info_flags::symbol)) {
281 if (!e.op(i).op(0).info(info_flags::idx)) {
282 throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol or an idx"));
285 ls.append(e.op(i).op(0));
286 lr.append(e.op(i).op(1));
291 // compare functions to sort expressions canonically
292 // all compare functions return: -1 for *this less than other, 0 equal, 1 greater
295 int basic::compare(basic const & other) const
297 const type_info & typeid_this = typeid(*this);
298 const type_info & typeid_other = typeid(other);
300 if (typeid_this==typeid_other) {
301 return compare_same_type(other);
304 // special rule: sort numeric() to end
305 if (typeid_this==typeid_numeric) return 1;
306 if (typeid_other==typeid_numeric) return -1;
308 // otherwise: sort according to type_info order (arbitrary, but well defined)
309 return typeid_this.before(typeid_other) ? -1 : 1;
313 int basic::compare(basic const & other) const
315 unsigned hash_this = gethash();
316 unsigned hash_other = other.gethash();
318 if (hash_this<hash_other) return -1;
319 if (hash_this>hash_other) return 1;
321 unsigned typeid_this = tinfo();
322 unsigned typeid_other = other.tinfo();
324 if (typeid_this<typeid_other) {
326 cout << "hash collision, different types: "
327 << *this << " and " << other << endl;
328 this->printraw(cout);
330 other.printraw(cout);
335 if (typeid_this>typeid_other) {
337 cout << "hash collision, different types: "
338 << *this << " and " << other << endl;
339 this->printraw(cout);
341 other.printraw(cout);
347 ASSERT(typeid(*this)==typeid(other));
349 int cmpval=compare_same_type(other);
350 if ((cmpval!=0)&&(hash_this<0x80000000U)) {
352 cout << "hash collision, same type: "
353 << *this << " and " << other << endl;
354 this->printraw(cout);
356 other.printraw(cout);
363 bool basic::is_equal(basic const & other) const
365 unsigned hash_this = gethash();
366 unsigned hash_other = other.gethash();
368 if (hash_this!=hash_other) return false;
370 unsigned typeid_this = tinfo();
371 unsigned typeid_other = other.tinfo();
373 if (typeid_this!=typeid_other) return false;
375 ASSERT(typeid(*this)==typeid(other));
377 return is_equal_same_type(other);
382 basic const & basic::hold(void) const
384 return setflag(status_flags::evaluated);
387 void basic::ensure_if_modifiable(void) const
390 throw(std::runtime_error("cannot modify multiply referenced object"));
395 // static member variables
400 unsigned basic::precedence=70;
401 unsigned basic::delta_indent=4;
407 const basic some_basic;
408 type_info const & typeid_basic=typeid(some_basic);
414 int max_recursion_level=1024;