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
35 GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(symbol, basic)
38 // default ctor, dtor, copy ctor assignment operator and helpers
41 symbol::symbol() : inherited(TINFO_symbol), serial(next_serial++)
43 debugmsg("symbol default ctor", LOGLEVEL_CONSTRUCT);
44 name = autoname_prefix()+ToString(serial);
45 asexinfop = new assigned_ex_info;
46 setflag(status_flags::evaluated | status_flags::expanded);
49 /** For use by copy ctor and assignment operator. */
50 void symbol::copy(const symbol & other)
52 inherited::copy(other);
54 serial = other.serial;
55 asexinfop = other.asexinfop;
56 ++asexinfop->refcount;
59 void symbol::destroy(bool call_parent)
61 if (--asexinfop->refcount == 0)
64 inherited::destroy(call_parent);
73 symbol::symbol(const symbol & other)
75 debugmsg("symbol copy ctor", LOGLEVEL_CONSTRUCT);
79 symbol::symbol(const std::string & initname) : inherited(TINFO_symbol)
81 debugmsg("symbol ctor from string", LOGLEVEL_CONSTRUCT);
83 serial = next_serial++;
84 asexinfop = new assigned_ex_info;
85 setflag(status_flags::evaluated | status_flags::expanded);
92 /** Construct object from archive_node. */
93 symbol::symbol(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
95 debugmsg("symbol ctor from archive_node", LOGLEVEL_CONSTRUCT);
96 serial = next_serial++;
97 if (!(n.find_string("name", name)))
98 name = autoname_prefix() + ToString(serial);
99 asexinfop = new assigned_ex_info;
100 setflag(status_flags::evaluated);
103 /** Unarchive the object. */
104 ex symbol::unarchive(const archive_node &n, const lst &sym_lst)
106 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
108 // If symbol is in sym_lst, return the existing symbol
109 for (unsigned i=0; i<sym_lst.nops(); i++) {
110 if (is_ex_of_type(sym_lst.op(i), symbol) && (ex_to_symbol(sym_lst.op(i)).name == ex_to_symbol(s).name))
111 return sym_lst.op(i);
116 /** Archive the object. */
117 void symbol::archive(archive_node &n) const
119 inherited::archive(n);
120 n.add_string("name", name);
124 // functions overriding virtual functions from bases classes
129 basic *symbol::duplicate() const
131 debugmsg("symbol duplicate", LOGLEVEL_DUPLICATE);
132 return new symbol(*this);
135 void symbol::print(const print_context & c, unsigned level) const
137 debugmsg("symbol print", LOGLEVEL_PRINT);
139 if (is_of_type(c, print_tree)) {
141 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
142 << ", serial=" << serial
143 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
150 bool symbol::info(unsigned inf) const
152 if (inf==info_flags::symbol) return true;
153 if (inf==info_flags::polynomial ||
154 inf==info_flags::integer_polynomial ||
155 inf==info_flags::cinteger_polynomial ||
156 inf==info_flags::rational_polynomial ||
157 inf==info_flags::crational_polynomial ||
158 inf==info_flags::rational_function)
161 return inherited::info(inf);
164 ex symbol::expand(unsigned options) const
169 bool symbol::has(const ex & other) const
171 if (this->is_equal(*other.bp))
177 int symbol::degree(const ex & s) const
179 return is_equal(*s.bp) ? 1 : 0;
182 int symbol::ldegree(const ex & s) const
184 return is_equal(*s.bp) ? 1 : 0;
187 ex symbol::coeff(const ex & s, int n) const
190 return n==1 ? _ex1() : _ex0();
192 return n==0 ? *this : _ex0();
195 ex symbol::eval(int level) const
197 if (level == -max_recursion_level)
198 throw(std::runtime_error("max recursion level reached"));
200 if (asexinfop->is_assigned) {
201 setflag(status_flags::evaluated);
203 return (asexinfop->assigned_expression);
205 return (asexinfop->assigned_expression).eval(level);
213 /** Implementation of ex::diff() for single differentiation of a symbol.
217 ex symbol::derivative(const symbol & s) const
219 if (compare_same_type(s))
225 int symbol::compare_same_type(const basic & other) const
227 GINAC_ASSERT(is_of_type(other,symbol));
228 const symbol *o = static_cast<const symbol *>(&other);
229 if (serial==o->serial) return 0;
230 return serial < o->serial ? -1 : 1;
233 bool symbol::is_equal_same_type(const basic & other) const
235 GINAC_ASSERT(is_of_type(other,symbol));
236 const symbol *o = static_cast<const symbol *>(&other);
237 return serial==o->serial;
240 unsigned symbol::return_type(void) const
242 return return_types::commutative;
245 unsigned symbol::return_type_tinfo(void) const
250 unsigned symbol::calchash(void) const
252 // this is where the schoolbook method
253 // (golden_ratio_hash(tinfo()) ^ serial)
254 // is not good enough yet...
255 hashvalue = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
256 setflag(status_flags::hash_calculated);
261 // virtual functions which can be overridden by derived classes
267 // non-virtual functions in this class
272 void symbol::assign(const ex & value)
274 asexinfop->is_assigned = 1;
275 asexinfop->assigned_expression = value;
276 clearflag(status_flags::evaluated | status_flags::expanded);
279 void symbol::unassign(void)
281 if (asexinfop->is_assigned) {
282 asexinfop->is_assigned = 0;
283 asexinfop->assigned_expression = _ex0();
285 setflag(status_flags::evaluated | status_flags::expanded);
290 /** Symbols not constructed with a string get one assigned using this
291 * prefix and a number. */
292 std::string & symbol::autoname_prefix(void)
294 static std::string *s = new std::string("symbol");
299 // static member variables
304 unsigned symbol::next_serial = 0;
307 // subclass assigned_ex_info
310 /** Default ctor. Defaults to unassigned. */
311 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)