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 #ifndef NO_GINAC_NAMESPACE
39 #endif // ndef NO_GINAC_NAMESPACE
42 // default constructor, destructor, copy constructor assignment operator and helpers
47 #ifndef INLINE_BASIC_CONSTRUCTORS
48 basic::basic() : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
50 debugmsg("basic default constructor",LOGLEVEL_CONSTRUCT);
56 debugmsg("basic destructor",LOGLEVEL_DESTRUCT);
58 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
61 basic::basic(basic const & other) : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
63 debugmsg("basic copy constructor",LOGLEVEL_CONSTRUCT);
68 basic const & basic::operator=(basic const & other)
70 debugmsg("basic operator=",LOGLEVEL_ASSIGNMENT);
81 void basic::copy(basic const & other)
83 flags=other.flags & ~ status_flags::dynallocated;
84 hashvalue=other.hashvalue;
85 tinfo_key=other.tinfo_key;
93 #ifndef INLINE_BASIC_CONSTRUCTORS
94 basic::basic(unsigned ti) : flags(0), refcount(0), tinfo_key(ti)
96 debugmsg("basic constructor with tinfo_key",LOGLEVEL_CONSTRUCT);
102 // functions overriding virtual functions from bases classes
108 // new virtual functions which can be overridden by derived classes
113 basic * basic::duplicate() const
115 debugmsg("basic duplicate",LOGLEVEL_DUPLICATE);
116 return new basic(*this);
119 bool basic::info(unsigned inf) const
121 return false; // all possible properties are false for basic objects
124 int basic::nops() const
129 ex basic::op(int const i) const
131 return (const_cast<basic *>(this))->let_op(i);
134 ex & basic::let_op(int const i)
136 throw(std::out_of_range("op() out of range"));
139 ex basic::operator[](ex const & index) const
141 if (is_exactly_of_type(*index.bp,numeric)) {
142 return op(static_cast<numeric const &>(*index.bp).to_int());
144 throw(std::invalid_argument("non-numeric indices not supported by this type"));
147 ex basic::operator[](int const i) const
152 bool basic::has(ex const & other) const
154 GINAC_ASSERT(other.bp!=0);
155 if (is_equal(*other.bp)) return true;
157 for (int i=0; i<nops(); i++) {
158 if (op(i).has(other)) return true;
164 int basic::degree(symbol const & s) const
169 int basic::ldegree(symbol const & s) const
174 ex basic::coeff(symbol const & s, int const n) const
176 return n==0 ? *this : exZERO();
179 ex basic::collect(symbol const & s) const
184 for (int n=ldeg; n<=deg; n++) {
185 x += coeff(s,n)*power(s,n);
190 ex basic::eval(int level) const
195 ex basic::evalf(int level) const
200 ex basic::subs(lst const & ls, lst const & lr) const
205 exvector basic::get_indices(void) const
207 return exvector(); // return an empty exvector
210 ex basic::simplify_ncmul(exvector const & v) const
212 return simplified_ncmul(v);
217 int basic::compare_same_type(basic const & other) const
219 return compare_pointers(this, &other);
222 bool basic::is_equal_same_type(basic const & other) const
224 return compare_same_type(other)==0;
227 unsigned basic::return_type(void) const
229 return return_types::commutative;
232 unsigned basic::return_type_tinfo(void) const
237 unsigned basic::calchash(void) const
239 unsigned v=golden_ratio_hash(tinfo());
240 for (int i=0; i<nops(); i++) {
242 v ^= (const_cast<basic *>(this))->let_op(i).gethash();
247 // store calculated hash value only if object is already evaluated
248 if (flags & status_flags::evaluated) {
249 setflag(status_flags::hash_calculated);
256 ex basic::expand(unsigned options) const
258 return this->setflag(status_flags::expanded);
262 // non-virtual functions in this class
267 ex basic::subs(ex const & e) const
269 // accept 2 types of replacement expressions:
271 // - lst(symbol1==ex1,symbol2==ex2,...)
272 // convert to subs(lst(symbol1,symbol2,...),lst(ex1,ex2,...))
273 // additionally, idx can be used instead of symbol
274 if (e.info(info_flags::relation_equal)) {
277 if (!e.info(info_flags::list)) {
278 throw(std::invalid_argument("basic::subs(ex): argument must be a list"));
282 for (int i=0; i<e.nops(); i++) {
283 if (!e.op(i).info(info_flags::relation_equal)) {
284 throw(std::invalid_argument("basic::subs(ex): argument must be a list or equations"));
286 if (!e.op(i).op(0).info(info_flags::symbol)) {
287 if (!e.op(i).op(0).info(info_flags::idx)) {
288 throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol or an idx"));
291 ls.append(e.op(i).op(0));
292 lr.append(e.op(i).op(1));
297 // compare functions to sort expressions canonically
298 // all compare functions return: -1 for *this less than other, 0 equal, 1 greater
301 int basic::compare(basic const & other) const
303 const type_info & typeid_this = typeid(*this);
304 const type_info & typeid_other = typeid(other);
306 if (typeid_this==typeid_other) {
307 return compare_same_type(other);
310 // special rule: sort numeric() to end
311 if (typeid_this==typeid_numeric) return 1;
312 if (typeid_other==typeid_numeric) return -1;
314 // otherwise: sort according to type_info order (arbitrary, but well defined)
315 return typeid_this.before(typeid_other) ? -1 : 1;
319 int basic::compare(basic const & other) const
321 unsigned hash_this = gethash();
322 unsigned hash_other = other.gethash();
324 if (hash_this<hash_other) return -1;
325 if (hash_this>hash_other) return 1;
327 unsigned typeid_this = tinfo();
328 unsigned typeid_other = other.tinfo();
330 if (typeid_this<typeid_other) {
332 cout << "hash collision, different types: "
333 << *this << " and " << other << endl;
334 this->printraw(cout);
336 other.printraw(cout);
341 if (typeid_this>typeid_other) {
343 cout << "hash collision, different types: "
344 << *this << " and " << other << endl;
345 this->printraw(cout);
347 other.printraw(cout);
353 GINAC_ASSERT(typeid(*this)==typeid(other));
355 int cmpval=compare_same_type(other);
356 if ((cmpval!=0)&&(hash_this<0x80000000U)) {
358 cout << "hash collision, same type: "
359 << *this << " and " << other << endl;
360 this->printraw(cout);
362 other.printraw(cout);
369 bool basic::is_equal(basic const & other) const
371 unsigned hash_this = gethash();
372 unsigned hash_other = other.gethash();
374 if (hash_this!=hash_other) return false;
376 unsigned typeid_this = tinfo();
377 unsigned typeid_other = other.tinfo();
379 if (typeid_this!=typeid_other) return false;
381 GINAC_ASSERT(typeid(*this)==typeid(other));
383 return is_equal_same_type(other);
388 basic const & basic::hold(void) const
390 return setflag(status_flags::evaluated);
393 void basic::ensure_if_modifiable(void) const
396 throw(std::runtime_error("cannot modify multiply referenced object"));
401 // static member variables
406 unsigned basic::precedence=70;
407 unsigned basic::delta_indent=4;
413 const basic some_basic;
414 type_info const & typeid_basic=typeid(some_basic);
420 int max_recursion_level=1024;
422 #ifndef NO_GINAC_NAMESPACE
424 #endif // ndef NO_GINAC_NAMESPACE