3 * Implementation of GiNaC's ABC. */
13 // default constructor, destructor, copy constructor assignment operator and helpers
18 #ifndef INLINE_BASIC_CONSTRUCTORS
19 basic::basic() : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
21 debugmsg("basic default constructor",LOGLEVEL_CONSTRUCT);
27 debugmsg("basic destructor",LOGLEVEL_DESTRUCT);
29 ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
32 basic::basic(basic const & other) : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
34 debugmsg("basic copy constructor",LOGLEVEL_CONSTRUCT);
39 basic const & basic::operator=(basic const & other)
41 debugmsg("basic operator=",LOGLEVEL_ASSIGNMENT);
52 void basic::copy(basic const & other)
54 flags=other.flags & ~ status_flags::dynallocated;
55 hashvalue=other.hashvalue;
56 tinfo_key=other.tinfo_key;
64 #ifndef INLINE_BASIC_CONSTRUCTORS
65 basic::basic(unsigned ti) : flags(0), refcount(0), tinfo_key(ti)
67 debugmsg("basic constructor with tinfo_key",LOGLEVEL_CONSTRUCT);
73 // functions overriding virtual functions from bases classes
79 // new virtual functions which can be overridden by derived classes
84 basic * basic::duplicate() const
86 debugmsg("basic duplicate",LOGLEVEL_DUPLICATE);
87 return new basic(*this);
90 bool basic::info(unsigned inf) const
92 return false; // all possible properties are false for basic objects
95 int basic::nops() const
100 ex basic::op(int const i) const
102 return (const_cast<basic *>(this))->let_op(i);
105 ex & basic::let_op(int const i)
107 throw(std::out_of_range("op() out of range"));
110 ex basic::operator[](ex const & index) const
112 if (is_exactly_of_type(*index.bp,numeric)) {
113 return op(static_cast<numeric const &>(*index.bp).to_int());
115 throw(std::invalid_argument("non-numeric indices not supported by this type"));
118 ex basic::operator[](int const i) const
123 bool basic::has(ex const & other) const
126 if (is_equal(*other.bp)) return true;
128 for (int i=0; i<nops(); i++) {
129 if (op(i).has(other)) return true;
135 int basic::degree(symbol const & s) const
140 int basic::ldegree(symbol const & s) const
145 ex basic::coeff(symbol const & s, int const n) const
147 return n==0 ? *this : exZERO();
150 ex basic::collect(symbol const & s) const
155 for (int n=ldeg; n<=deg; n++) {
156 x += coeff(s,n)*power(s,n);
161 ex basic::eval(int level) const
166 ex basic::evalf(int level) const
171 ex basic::subs(lst const & ls, lst const & lr) const
176 exvector basic::get_indices(void) const
178 return exvector(); // return an empty exvector
181 ex basic::simplify_ncmul(exvector const & v) const
183 return simplified_ncmul(v);
188 int basic::compare_same_type(basic const & other) const
190 return compare_pointers(this, &other);
193 bool basic::is_equal_same_type(basic const & other) const
195 return compare_same_type(other)==0;
198 unsigned basic::return_type(void) const
200 return return_types::commutative;
203 unsigned basic::return_type_tinfo(void) const
208 unsigned basic::calchash(void) const
210 unsigned v=golden_ratio_hash(tinfo());
211 for (int i=0; i<nops(); i++) {
213 v ^= (const_cast<basic *>(this))->let_op(i).gethash();
218 // store calculated hash value only if object is already evaluated
219 if (flags & status_flags::evaluated) {
220 setflag(status_flags::hash_calculated);
227 ex basic::expand(unsigned options) const
229 return this->setflag(status_flags::expanded);
233 // non-virtual functions in this class
238 ex basic::subs(ex const & e) const
240 // accept 2 types of replacement expressions:
242 // - lst(symbol1==ex1,symbol2==ex2,...)
243 // convert to subs(lst(symbol1,symbol2,...),lst(ex1,ex2,...))
244 // additionally, idx can be used instead of symbol
245 if (e.info(info_flags::relation_equal)) {
248 if (!e.info(info_flags::list)) {
249 throw(std::invalid_argument("basic::subs(ex): argument must be a list"));
253 for (int i=0; i<e.nops(); i++) {
254 if (!e.op(i).info(info_flags::relation_equal)) {
255 throw(std::invalid_argument("basic::subs(ex): argument must be a list or equations"));
257 if (!e.op(i).op(0).info(info_flags::symbol)) {
258 if (!e.op(i).op(0).info(info_flags::idx)) {
259 throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol or an idx"));
262 ls.append(e.op(i).op(0));
263 lr.append(e.op(i).op(1));
268 // compare functions to sort expressions canonically
269 // all compare functions return: -1 for *this less than other, 0 equal, 1 greater
272 int basic::compare(basic const & other) const
274 const type_info & typeid_this = typeid(*this);
275 const type_info & typeid_other = typeid(other);
277 if (typeid_this==typeid_other) {
278 return compare_same_type(other);
281 // special rule: sort numeric() to end
282 if (typeid_this==typeid_numeric) return 1;
283 if (typeid_other==typeid_numeric) return -1;
285 // otherwise: sort according to type_info order (arbitrary, but well defined)
286 return typeid_this.before(typeid_other) ? -1 : 1;
290 int basic::compare(basic const & other) const
292 unsigned hash_this = gethash();
293 unsigned hash_other = other.gethash();
295 if (hash_this<hash_other) return -1;
296 if (hash_this>hash_other) return 1;
298 unsigned typeid_this = tinfo();
299 unsigned typeid_other = other.tinfo();
301 if (typeid_this<typeid_other) {
303 cout << "hash collision, different types: "
304 << *this << " and " << other << endl;
305 this->printraw(cout);
307 other.printraw(cout);
312 if (typeid_this>typeid_other) {
314 cout << "hash collision, different types: "
315 << *this << " and " << other << endl;
316 this->printraw(cout);
318 other.printraw(cout);
324 ASSERT(typeid(*this)==typeid(other));
326 int cmpval=compare_same_type(other);
327 if ((cmpval!=0)&&(hash_this<0x80000000U)) {
329 cout << "hash collision, same type: "
330 << *this << " and " << other << endl;
331 this->printraw(cout);
333 other.printraw(cout);
340 bool basic::is_equal(basic const & other) const
342 unsigned hash_this = gethash();
343 unsigned hash_other = other.gethash();
345 if (hash_this!=hash_other) return false;
347 unsigned typeid_this = tinfo();
348 unsigned typeid_other = other.tinfo();
350 if (typeid_this!=typeid_other) return false;
352 ASSERT(typeid(*this)==typeid(other));
354 return is_equal_same_type(other);
359 basic const & basic::hold(void) const
361 return setflag(status_flags::evaluated);
364 void basic::ensure_if_modifiable(void) const
367 throw(std::runtime_error("cannot modify multiply referenced object"));
372 // static member variables
377 unsigned basic::precedence=70;
378 unsigned basic::delta_indent=4;
384 const basic some_basic;
385 type_info const & typeid_basic=typeid(some_basic);
391 int max_recursion_level=1024;