3 * Implementation of GiNaC's symbolic objects. */
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
34 GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(symbol, basic)
37 // default ctor, dtor, copy ctor assignment operator and helpers
40 symbol::symbol() : inherited(TINFO_symbol), serial(next_serial++)
42 debugmsg("symbol default ctor", LOGLEVEL_CONSTRUCT);
43 name = autoname_prefix()+ToString(serial);
44 asexinfop = new assigned_ex_info;
45 setflag(status_flags::evaluated | status_flags::expanded);
48 /** For use by copy ctor and assignment operator. */
49 void symbol::copy(const symbol & other)
51 inherited::copy(other);
53 serial = other.serial;
54 asexinfop = other.asexinfop;
55 ++asexinfop->refcount;
58 void symbol::destroy(bool call_parent)
60 if (--asexinfop->refcount == 0)
63 inherited::destroy(call_parent);
72 symbol::symbol(const symbol & other)
74 debugmsg("symbol copy ctor", LOGLEVEL_CONSTRUCT);
78 symbol::symbol(const std::string & initname) : inherited(TINFO_symbol)
80 debugmsg("symbol ctor from string", LOGLEVEL_CONSTRUCT);
82 serial = next_serial++;
83 asexinfop = new assigned_ex_info;
84 setflag(status_flags::evaluated | status_flags::expanded);
91 /** Construct object from archive_node. */
92 symbol::symbol(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
94 debugmsg("symbol ctor from archive_node", LOGLEVEL_CONSTRUCT);
95 serial = next_serial++;
96 if (!(n.find_string("name", name)))
97 name = autoname_prefix() + ToString(serial);
98 asexinfop = new assigned_ex_info;
99 setflag(status_flags::evaluated);
102 /** Unarchive the object. */
103 ex symbol::unarchive(const archive_node &n, const lst &sym_lst)
105 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
107 // If symbol is in sym_lst, return the existing symbol
108 for (unsigned i=0; i<sym_lst.nops(); i++) {
109 if (is_ex_of_type(sym_lst.op(i), symbol) && (ex_to_symbol(sym_lst.op(i)).name == ex_to_symbol(s).name))
110 return sym_lst.op(i);
115 /** Archive the object. */
116 void symbol::archive(archive_node &n) const
118 inherited::archive(n);
119 n.add_string("name", name);
123 // functions overriding virtual functions from bases classes
128 basic *symbol::duplicate() const
130 debugmsg("symbol duplicate", LOGLEVEL_DUPLICATE);
131 return new symbol(*this);
134 void symbol::print(std::ostream & os, unsigned upper_precedence) const
136 debugmsg("symbol print",LOGLEVEL_PRINT);
140 void symbol::printraw(std::ostream & os) const
142 debugmsg("symbol printraw",LOGLEVEL_PRINT);
143 os << class_name() << "(" << "name=" << name << ",serial=" << serial
144 << ",hash=" << hashvalue << ",flags=" << flags << ")";
147 void symbol::printtree(std::ostream & os, unsigned indent) const
149 debugmsg("symbol printtree",LOGLEVEL_PRINT);
150 os << std::string(indent,' ') << name << " (symbol): "
151 << "serial=" << serial
152 << ", hash=" << hashvalue
153 << " (0x" << std::hex << hashvalue << std::dec << ")"
154 << ", flags=" << flags << std::endl;
157 void symbol::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
159 debugmsg("symbol print csrc", LOGLEVEL_PRINT);
163 bool symbol::info(unsigned inf) const
165 if (inf==info_flags::symbol) return true;
166 if (inf==info_flags::polynomial ||
167 inf==info_flags::integer_polynomial ||
168 inf==info_flags::cinteger_polynomial ||
169 inf==info_flags::rational_polynomial ||
170 inf==info_flags::crational_polynomial ||
171 inf==info_flags::rational_function)
174 return inherited::info(inf);
177 ex symbol::expand(unsigned options) const
182 bool symbol::has(const ex & other) const
184 if (this->is_equal(*other.bp))
190 int symbol::degree(const ex & s) const
192 return is_equal(*s.bp) ? 1 : 0;
195 int symbol::ldegree(const ex & s) const
197 return is_equal(*s.bp) ? 1 : 0;
200 ex symbol::coeff(const ex & s, int n) const
203 return n==1 ? _ex1() : _ex0();
205 return n==0 ? *this : _ex0();
208 ex symbol::eval(int level) const
210 if (level == -max_recursion_level)
211 throw(std::runtime_error("max recursion level reached"));
213 if (asexinfop->is_assigned) {
214 setflag(status_flags::evaluated);
216 return (asexinfop->assigned_expression);
218 return (asexinfop->assigned_expression).eval(level);
224 ex symbol::subs(const lst & ls, const lst & lr) const
226 GINAC_ASSERT(ls.nops() == lr.nops());
228 for (unsigned i=0; i<ls.nops(); i++) {
229 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
230 if (compare_same_type(ex_to_symbol(ls.op(i)))==0)
239 /** Implementation of ex::diff() for single differentiation of a symbol.
243 ex symbol::derivative(const symbol & s) const
245 if (compare_same_type(s))
251 int symbol::compare_same_type(const basic & other) const
253 GINAC_ASSERT(is_of_type(other,symbol));
254 const symbol *o = static_cast<const symbol *>(&other);
255 if (serial==o->serial) return 0;
256 return serial < o->serial ? -1 : 1;
259 bool symbol::is_equal_same_type(const basic & other) const
261 GINAC_ASSERT(is_of_type(other,symbol));
262 const symbol *o = static_cast<const symbol *>(&other);
263 return serial==o->serial;
266 unsigned symbol::return_type(void) const
268 return return_types::commutative;
271 unsigned symbol::return_type_tinfo(void) const
276 unsigned symbol::calchash(void) const
278 // this is where the schoolbook method
279 // (golden_ratio_hash(tinfo()) ^ serial)
280 // is not good enough yet...
281 hashvalue = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
282 setflag(status_flags::hash_calculated);
287 // virtual functions which can be overridden by derived classes
293 // non-virtual functions in this class
298 void symbol::assign(const ex & value)
300 asexinfop->is_assigned = 1;
301 asexinfop->assigned_expression = value;
302 clearflag(status_flags::evaluated | status_flags::expanded);
305 void symbol::unassign(void)
307 if (asexinfop->is_assigned) {
308 asexinfop->is_assigned = 0;
309 asexinfop->assigned_expression = _ex0();
311 setflag(status_flags::evaluated | status_flags::expanded);
316 /** Symbols not constructed with a string get one assigned using this
317 * prefix and a number. */
318 std::string & symbol::autoname_prefix(void)
320 static std::string *s = new std::string("symbol");
325 // static member variables
330 unsigned symbol::next_serial = 0;
333 // subclass assigned_ex_info
336 /** Default ctor. Defaults to unassigned. */
337 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)