3 * Implementation of GiNaC's ABC. */
6 * GiNaC Copyright (C) 1999-2000 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
38 #ifndef NO_NAMESPACE_GINAC
40 #endif // ndef NO_NAMESPACE_GINAC
42 GINAC_IMPLEMENT_REGISTERED_CLASS(basic, void)
45 // default constructor, destructor, copy constructor assignment operator and helpers
50 #ifndef INLINE_BASIC_CONSTRUCTORS
51 basic::basic() : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
53 debugmsg("basic default constructor", LOGLEVEL_CONSTRUCT);
59 debugmsg("basic destructor", LOGLEVEL_DESTRUCT);
61 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
64 basic::basic(const basic & other) : flags(0), refcount(0), tinfo_key(TINFO_BASIC)
66 debugmsg("basic copy constructor", LOGLEVEL_CONSTRUCT);
71 const basic & basic::operator=(const basic & other)
73 debugmsg("basic operator=", LOGLEVEL_ASSIGNMENT);
84 void basic::copy(const basic & other)
86 flags=other.flags & ~ status_flags::dynallocated;
87 hashvalue=other.hashvalue;
88 tinfo_key=other.tinfo_key;
96 #ifndef INLINE_BASIC_CONSTRUCTORS
97 basic::basic(unsigned ti) : flags(0), refcount(0), tinfo_key(ti)
99 debugmsg("basic constructor with tinfo_key", LOGLEVEL_CONSTRUCT);
108 /** Construct object from archive_node. */
109 basic::basic(const archive_node &n, const lst &sym_lst) : flags(0), refcount(0)
111 debugmsg("basic constructor from archive_node", LOGLEVEL_CONSTRUCT);
113 // Reconstruct tinfo_key from class name
115 if (n.find_string("class", class_name))
116 tinfo_key = find_tinfo_key(class_name);
118 throw (std::runtime_error("archive node contains no class name"));
121 /** Unarchive the object. */
122 ex basic::unarchive(const archive_node &n, const lst &sym_lst)
124 return (new basic(n, sym_lst))->setflag(status_flags::dynallocated);
127 /** Archive the object. */
128 void basic::archive(archive_node &n) const
130 n.add_string("class", class_name());
134 // functions overriding virtual functions from bases classes
140 // new virtual functions which can be overridden by derived classes
145 /** Output to stream formatted to be useful as ginsh input. */
146 void basic::print(ostream & os, unsigned upper_precedence) const
148 debugmsg("basic print",LOGLEVEL_PRINT);
149 os << "[basic object]";
152 /** Output to stream in ugly raw format, so brave developers can have a look
153 * at the underlying structure. */
154 void basic::printraw(ostream & os) const
156 debugmsg("basic printraw",LOGLEVEL_PRINT);
157 os << "[basic object]";
160 /** Output to stream formatted in tree- (indented-) form, so developers can
161 * have a look at the underlying structure. */
162 void basic::printtree(ostream & os, unsigned indent) const
164 debugmsg("basic printtree",LOGLEVEL_PRINT);
165 os << string(indent,' ') << "type=" << typeid(*this).name()
166 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
167 << ", flags=" << flags
168 << ", nops=" << nops() << endl;
169 for (unsigned i=0; i<nops(); ++i) {
170 op(i).printtree(os,indent+delta_indent);
174 /** Output to stream formatted as C-source.
176 * @param os a stream for output
177 * @param type variable type (one of the csrc_types)
178 * @param upper_precedence operator precedence of caller
179 * @see ex::printcsrc */
180 void basic::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
182 debugmsg("basic print csrc", LOGLEVEL_PRINT);
185 /** Little wrapper arount print to be called within a debugger. */
186 void basic::dbgprint(void) const
192 /** Little wrapper arount printtree to be called within a debugger. */
193 void basic::dbgprinttree(void) const
198 basic * basic::duplicate() const
200 debugmsg("basic duplicate",LOGLEVEL_DUPLICATE);
201 return new basic(*this);
204 /** Information about the object.
206 * @see class info_flags */
207 bool basic::info(unsigned inf) const
209 return false; // all possible properties are false for basic objects
212 /** Number of operands/members. */
213 unsigned basic::nops() const
218 /** Return operand/member at position i. */
219 ex basic::op(int i) const
221 return (const_cast<basic *>(this))->let_op(i);
224 /** Return modifyable operand/member at position i. */
225 ex & basic::let_op(int i)
227 throw(std::out_of_range("op() out of range"));
230 ex basic::operator[](const ex & index) const
232 if (is_exactly_of_type(*index.bp,numeric))
233 return op(static_cast<const numeric &>(*index.bp).to_int());
235 throw(std::invalid_argument("non-numeric indices not supported by this type"));
238 ex basic::operator[](int i) const
243 /** Search ocurrences. An object 'has' an expression if it is the expression
244 * itself or one of the children 'has' it. */
245 bool basic::has(const ex & other) const
247 GINAC_ASSERT(other.bp!=0);
248 if (is_equal(*other.bp)) return true;
250 for (unsigned i=0; i<nops(); i++) {
251 if (op(i).has(other)) return true;
257 int basic::degree(const symbol & s) const
262 int basic::ldegree(const symbol & s) const
267 ex basic::coeff(const symbol & s, int n) const
269 return n==0 ? *this : _ex0();
272 ex basic::collect(const symbol & s) const
277 for (int n=ldeg; n<=deg; n++) {
278 x += coeff(s,n)*power(s,n);
283 ex basic::eval(int level) const
288 /** Evaluate object numerically. */
289 ex basic::evalf(int level) const
294 ex basic::subs(const lst & ls, const lst & lr) const
299 /** Default interface of nth derivative ex::diff(s, n). It should be called
300 * instead of ::derivative(s) for first derivatives and for nth derivatives it
301 * just recurses down.
303 * @param s symbol to differentiate in
304 * @param nth order of differentiation
306 ex basic::diff(const symbol & s, unsigned nth) const
308 // trivial: zeroth derivative
312 // evaluate unevalueted *this before differentiating
313 if (!(flags & status_flags::evaluated))
314 return ex(*this).diff(s, nth);
316 ex ndiff = derivative(s);
317 while (!ndiff.is_zero() && // stop differentiating zeros
319 ndiff = ndiff.diff(s);
325 exvector basic::get_indices(void) const
327 return exvector(); // return an empty exvector
330 ex basic::simplify_ncmul(const exvector & v) const
332 return simplified_ncmul(v);
337 /** Default implementation of ex::diff(). It simply throws an error message.
339 * @exception logic_error (differentiation not supported by this type)
341 ex basic::derivative(const symbol & s) const
343 throw(std::logic_error("differentiation not supported by this type"));
346 int basic::compare_same_type(const basic & other) const
348 return compare_pointers(this, &other);
351 bool basic::is_equal_same_type(const basic & other) const
353 return compare_same_type(other)==0;
356 unsigned basic::return_type(void) const
358 return return_types::commutative;
361 unsigned basic::return_type_tinfo(void) const
366 unsigned basic::calchash(void) const
368 unsigned v=golden_ratio_hash(tinfo());
369 for (unsigned i=0; i<nops(); i++) {
371 v ^= (const_cast<basic *>(this))->op(i).gethash();
376 // store calculated hash value only if object is already evaluated
377 if (flags & status_flags::evaluated) {
378 setflag(status_flags::hash_calculated);
385 ex basic::expand(unsigned options) const
387 return this->setflag(status_flags::expanded);
392 // non-virtual functions in this class
397 ex basic::subs(const ex & e) const
399 // accept 2 types of replacement expressions:
401 // - lst(symbol1==ex1,symbol2==ex2,...)
402 // convert to subs(lst(symbol1,symbol2,...),lst(ex1,ex2,...))
403 // additionally, idx can be used instead of symbol
404 if (e.info(info_flags::relation_equal)) {
407 if (!e.info(info_flags::list)) {
408 throw(std::invalid_argument("basic::subs(ex): argument must be a list"));
412 for (unsigned i=0; i<e.nops(); i++) {
413 if (!e.op(i).info(info_flags::relation_equal)) {
414 throw(std::invalid_argument("basic::subs(ex): argument must be a list or equations"));
416 if (!e.op(i).op(0).info(info_flags::symbol)) {
417 if (!e.op(i).op(0).info(info_flags::idx)) {
418 throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol or an idx"));
421 ls.append(e.op(i).op(0));
422 lr.append(e.op(i).op(1));
427 /** Compare objects to establish canonical order.
428 * All compare functions return: -1 for *this less than other, 0 equal,
430 int basic::compare(const basic & other) const
432 unsigned hash_this = gethash();
433 unsigned hash_other = other.gethash();
435 if (hash_this<hash_other) return -1;
436 if (hash_this>hash_other) return 1;
438 unsigned typeid_this = tinfo();
439 unsigned typeid_other = other.tinfo();
441 if (typeid_this<typeid_other) {
443 cout << "hash collision, different types: "
444 << *this << " and " << other << endl;
445 this->printraw(cout);
447 other.printraw(cout);
452 if (typeid_this>typeid_other) {
454 cout << "hash collision, different types: "
455 << *this << " and " << other << endl;
456 this->printraw(cout);
458 other.printraw(cout);
464 GINAC_ASSERT(typeid(*this)==typeid(other));
466 int cmpval=compare_same_type(other);
467 if ((cmpval!=0)&&(hash_this<0x80000000U)) {
469 cout << "hash collision, same type: "
470 << *this << " and " << other << endl;
471 this->printraw(cout);
473 other.printraw(cout);
480 bool basic::is_equal(const basic & other) const
482 unsigned hash_this = gethash();
483 unsigned hash_other = other.gethash();
485 if (hash_this!=hash_other) return false;
487 unsigned typeid_this = tinfo();
488 unsigned typeid_other = other.tinfo();
490 if (typeid_this!=typeid_other) return false;
492 GINAC_ASSERT(typeid(*this)==typeid(other));
494 return is_equal_same_type(other);
499 const basic & basic::hold(void) const
501 return setflag(status_flags::evaluated);
504 void basic::ensure_if_modifiable(void) const
507 throw(std::runtime_error("cannot modify multiply referenced object"));
512 // static member variables
517 unsigned basic::precedence=70;
518 unsigned basic::delta_indent=4;
524 const basic some_basic;
525 const type_info & typeid_basic=typeid(some_basic);
531 int max_recursion_level=1024;
533 #ifndef NO_NAMESPACE_GINAC
535 #endif // ndef NO_NAMESPACE_GINAC