3 * Implementation of GiNaC's ABC. */
6 * GiNaC Copyright (C) 1999-2001 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);
89 #ifndef INLINE_BASIC_CONSTRUCTORS
90 basic::basic(unsigned ti) : flags(0), refcount(0), tinfo_key(ti)
92 debugmsg("basic constructor with tinfo_key", LOGLEVEL_CONSTRUCT);
101 /** Construct object from archive_node. */
102 basic::basic(const archive_node &n, const lst &sym_lst) : flags(0), refcount(0)
104 debugmsg("basic constructor from archive_node", LOGLEVEL_CONSTRUCT);
106 // Reconstruct tinfo_key from class name
107 std::string class_name;
108 if (n.find_string("class", class_name))
109 tinfo_key = find_tinfo_key(class_name);
111 throw (std::runtime_error("archive node contains no class name"));
114 /** Unarchive the object. */
115 ex basic::unarchive(const archive_node &n, const lst &sym_lst)
117 return (new basic(n, sym_lst))->setflag(status_flags::dynallocated);
120 /** Archive the object. */
121 void basic::archive(archive_node &n) const
123 n.add_string("class", class_name());
127 // functions overriding virtual functions from bases classes
133 // new virtual functions which can be overridden by derived classes
138 /** Output to stream formatted to be useful as ginsh input. */
139 void basic::print(std::ostream & os, unsigned upper_precedence) const
141 debugmsg("basic print",LOGLEVEL_PRINT);
142 os << "[basic object]";
145 /** Output to stream in ugly raw format, so brave developers can have a look
146 * at the underlying structure. */
147 void basic::printraw(std::ostream & os) const
149 debugmsg("basic printraw",LOGLEVEL_PRINT);
150 os << "[basic object]";
153 /** Output to stream formatted in tree- (indented-) form, so developers can
154 * have a look at the underlying structure. */
155 void basic::printtree(std::ostream & os, unsigned indent) const
157 debugmsg("basic printtree",LOGLEVEL_PRINT);
158 os << std::string(indent,' ') << "type=" << class_name()
159 << ", hash=" << hashvalue
160 << " (0x" << std::hex << hashvalue << std::dec << ")"
161 << ", flags=" << flags
162 << ", nops=" << nops() << std::endl;
163 for (unsigned i=0; i<nops(); ++i) {
164 op(i).printtree(os,indent+delta_indent);
168 /** Output to stream formatted as C-source.
170 * @param os a stream for output
171 * @param type variable type (one of the csrc_types)
172 * @param upper_precedence operator precedence of caller
173 * @see ex::printcsrc */
174 void basic::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
176 debugmsg("basic print csrc", LOGLEVEL_PRINT);
179 /** Little wrapper arount print to be called within a debugger. */
180 void basic::dbgprint(void) const
183 std::cerr << std::endl;
186 /** Little wrapper arount printtree to be called within a debugger. */
187 void basic::dbgprinttree(void) const
189 printtree(std::cerr,0);
192 basic * basic::duplicate() const
194 debugmsg("basic duplicate",LOGLEVEL_DUPLICATE);
195 return new basic(*this);
198 /** Information about the object.
200 * @see class info_flags */
201 bool basic::info(unsigned inf) const
203 return false; // all possible properties are false for basic objects
206 /** Number of operands/members. */
207 unsigned basic::nops() const
209 // iterating from 0 to nops() on atomic objects should be an empty loop,
210 // and accessing their elements is a range error. Container objects should
215 /** Return operand/member at position i. */
216 ex basic::op(int i) const
218 return (const_cast<basic *>(this))->let_op(i);
221 /** Return modifyable operand/member at position i. */
222 ex & basic::let_op(int i)
224 throw(std::out_of_range("op() out of range"));
227 ex basic::operator[](const ex & index) const
229 if (is_exactly_of_type(*index.bp,numeric))
230 return op(static_cast<const numeric &>(*index.bp).to_int());
232 throw(std::invalid_argument("non-numeric indices not supported by this type"));
235 ex basic::operator[](int i) const
240 /** Search ocurrences. An object 'has' an expression if it is the expression
241 * itself or one of the children 'has' it. */
242 bool basic::has(const ex & other) const
244 GINAC_ASSERT(other.bp!=0);
245 if (is_equal(*other.bp)) return true;
247 for (unsigned i=0; i<nops(); i++) {
248 if (op(i).has(other)) return true;
254 /** Return degree of highest power in symbol s. */
255 int basic::degree(const symbol & s) const
260 /** Return degree of lowest power in symbol s. */
261 int basic::ldegree(const symbol & s) const
266 /** Return coefficient of degree n in symbol s. */
267 ex basic::coeff(const symbol & s, int n) const
269 return n==0 ? *this : _ex0();
272 /** Sort expression in terms of powers of some symbol.
273 * @param s symbol to sort in. */
274 ex basic::collect(const symbol & s) const
277 int ldeg = this->ldegree(s);
278 int deg = this->degree(s);
279 for (int n=ldeg; n<=deg; n++) {
280 x += this->coeff(s,n)*power(s,n);
285 /* Perform automatic symbolic evaluations on expression. */
286 ex basic::eval(int level) const
288 // There is nothing to do for basic objects:
292 /** Evaluate object numerically. */
293 ex basic::evalf(int level) const
295 // There is nothing to do for basic objects:
299 /* Substitute a set of symbols. */
300 ex basic::subs(const lst & ls, const lst & lr) const
305 /** Default interface of nth derivative ex::diff(s, n). It should be called
306 * instead of ::derivative(s) for first derivatives and for nth derivatives it
307 * just recurses down.
309 * @param s symbol to differentiate in
310 * @param nth order of differentiation
312 ex basic::diff(const symbol & s, unsigned nth) const
314 // trivial: zeroth derivative
318 // evaluate unevaluated *this before differentiating
319 if (!(flags & status_flags::evaluated))
320 return ex(*this).diff(s, nth);
322 ex ndiff = this->derivative(s);
323 while (!ndiff.is_zero() && // stop differentiating zeros
325 ndiff = ndiff.diff(s);
331 exvector basic::get_indices(void) const
333 return exvector(); // return an empty exvector
336 ex basic::simplify_ncmul(const exvector & v) const
338 return simplified_ncmul(v);
343 /** Default implementation of ex::diff(). It simply throws an error message.
345 * @exception logic_error (differentiation not supported by this type)
347 ex basic::derivative(const symbol & s) const
349 throw(std::logic_error("differentiation not supported by this type"));
352 /** Returns order relation between two objects of same type. Needs to be
353 * implemented by each class. */
354 int basic::compare_same_type(const basic & other) const
356 return compare_pointers(this, &other);
359 /** Returns true if two objects of same type are equal. Normally needs
360 * not be reimplemented as long as it wasn't overwritten by some parent
361 * class, since it just calls complare_same_type(). */
362 bool basic::is_equal_same_type(const basic & other) const
364 return compare_same_type(other)==0;
367 unsigned basic::return_type(void) const
369 return return_types::commutative;
372 unsigned basic::return_type_tinfo(void) const
377 unsigned basic::calchash(void) const
379 unsigned v=golden_ratio_hash(tinfo());
380 for (unsigned i=0; i<nops(); i++) {
382 v ^= (const_cast<basic *>(this))->op(i).gethash();
387 // store calculated hash value only if object is already evaluated
388 if (flags & status_flags::evaluated) {
389 setflag(status_flags::hash_calculated);
396 /** Expand expression, i.e. multiply it out and return the result as a new
398 ex basic::expand(unsigned options) const
400 return this->setflag(status_flags::expanded);
405 // non-virtual functions in this class
410 /** Substitute symbols in expression and return the result as a new expression.
411 * There are two valid types of replacement arguments: 1) a relational like
412 * symbol==ex and 2) a list of relationals lst(symbol1==ex1,symbol2==ex2,...),
413 * which is converted to subs(lst(symbol1,symbol2,...),lst(ex1,ex2,...)).
414 * In addition, an object of class idx can be used instead of a symbol. */
415 ex basic::subs(const ex & e) const
417 if (e.info(info_flags::relation_equal)) {
420 if (!e.info(info_flags::list)) {
421 throw(std::invalid_argument("basic::subs(ex): argument must be a list"));
425 for (unsigned i=0; i<e.nops(); i++) {
426 if (!e.op(i).info(info_flags::relation_equal)) {
427 throw(std::invalid_argument("basic::subs(ex): argument must be a list or equations"));
429 if (!e.op(i).op(0).info(info_flags::symbol)) {
430 if (!e.op(i).op(0).info(info_flags::idx)) {
431 throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol or an idx"));
434 ls.append(e.op(i).op(0));
435 lr.append(e.op(i).op(1));
440 /** Compare objects to establish canonical order.
441 * All compare functions return: -1 for *this less than other, 0 equal,
443 int basic::compare(const basic & other) const
445 unsigned hash_this = gethash();
446 unsigned hash_other = other.gethash();
448 if (hash_this<hash_other) return -1;
449 if (hash_this>hash_other) return 1;
451 unsigned typeid_this = tinfo();
452 unsigned typeid_other = other.tinfo();
454 if (typeid_this<typeid_other) {
456 cout << "hash collision, different types: "
457 << *this << " and " << other << endl;
458 this->printraw(cout);
460 other.printraw(cout);
465 if (typeid_this>typeid_other) {
467 cout << "hash collision, different types: "
468 << *this << " and " << other << endl;
469 this->printraw(cout);
471 other.printraw(cout);
477 GINAC_ASSERT(typeid(*this)==typeid(other));
479 int cmpval=compare_same_type(other);
480 if ((cmpval!=0)&&(hash_this<0x80000000U)) {
482 cout << "hash collision, same type: "
483 << *this << " and " << other << endl;
484 this->printraw(cout);
486 other.printraw(cout);
493 /** Test for equality. */
494 bool basic::is_equal(const basic & other) const
496 unsigned hash_this = gethash();
497 unsigned hash_other = other.gethash();
499 if (hash_this!=hash_other) return false;
501 unsigned typeid_this = tinfo();
502 unsigned typeid_other = other.tinfo();
504 if (typeid_this!=typeid_other) return false;
506 GINAC_ASSERT(typeid(*this)==typeid(other));
508 return is_equal_same_type(other);
513 /** Stop further evaluation.
514 * @see basic::eval */
515 const basic & basic::hold(void) const
517 return setflag(status_flags::evaluated);
520 void basic::ensure_if_modifiable(void) const
523 throw(std::runtime_error("cannot modify multiply referenced object"));
528 // static member variables
533 unsigned basic::precedence = 70;
534 unsigned basic::delta_indent = 4;
540 int max_recursion_level=1024;
542 #ifndef NO_NAMESPACE_GINAC
544 #endif // ndef NO_NAMESPACE_GINAC