3 * Implementation of GiNaC's symbolic objects. */
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
33 #ifndef NO_NAMESPACE_GINAC
35 #endif // ndef NO_NAMESPACE_GINAC
37 GINAC_IMPLEMENT_REGISTERED_CLASS(symbol, basic)
40 // default constructor, destructor, copy constructor assignment operator and helpers
43 symbol::symbol() : inherited(TINFO_symbol)
45 debugmsg("symbol default constructor", LOGLEVEL_CONSTRUCT);
46 serial = next_serial++;
47 name = autoname_prefix()+ToString(serial);
48 asexinfop = new assigned_ex_info;
49 setflag(status_flags::evaluated);
54 debugmsg("symbol destructor", LOGLEVEL_DESTRUCT);
58 symbol::symbol(const symbol & other)
60 debugmsg("symbol copy constructor", LOGLEVEL_CONSTRUCT);
64 void symbol::copy(const symbol & other)
66 inherited::copy(other);
68 serial = other.serial;
69 asexinfop = other.asexinfop;
70 ++asexinfop->refcount;
73 void symbol::destroy(bool call_parent)
75 if (--asexinfop->refcount == 0) {
79 inherited::destroy(call_parent);
83 // how should the following be interpreted?
87 // probably as: x=ex(y);
95 symbol::symbol(const std::string & initname) : inherited(TINFO_symbol)
97 debugmsg("symbol constructor from string", LOGLEVEL_CONSTRUCT);
99 serial = next_serial++;
100 asexinfop = new assigned_ex_info;
101 setflag(status_flags::evaluated);
108 /** Construct object from archive_node. */
109 symbol::symbol(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
111 debugmsg("symbol constructor from archive_node", LOGLEVEL_CONSTRUCT);
112 serial = next_serial++;
113 if (!(n.find_string("name", name)))
114 name = autoname_prefix() + ToString(serial);
115 asexinfop = new assigned_ex_info;
116 setflag(status_flags::evaluated);
119 /** Unarchive the object. */
120 ex symbol::unarchive(const archive_node &n, const lst &sym_lst)
122 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
124 // If symbol is in sym_lst, return the existing symbol
125 for (unsigned i=0; i<sym_lst.nops(); i++) {
126 if (is_ex_of_type(sym_lst.op(i), symbol) && (ex_to_symbol(sym_lst.op(i)).name == ex_to_symbol(s).name))
127 return sym_lst.op(i);
132 /** Archive the object. */
133 void symbol::archive(archive_node &n) const
135 inherited::archive(n);
136 n.add_string("name", name);
140 // functions overriding virtual functions from bases classes
145 basic *symbol::duplicate() const
147 debugmsg("symbol duplicate", LOGLEVEL_DUPLICATE);
148 return new symbol(*this);
151 void symbol::print(ostream & os, unsigned upper_precedence) const
153 debugmsg("symbol print",LOGLEVEL_PRINT);
157 void symbol::printraw(ostream & os) const
159 debugmsg("symbol printraw",LOGLEVEL_PRINT);
160 os << "symbol(" << "name=" << name << ",serial=" << serial
161 << ",hash=" << hashvalue << ",flags=" << flags << ")";
164 void symbol::printtree(ostream & os, unsigned indent) const
166 debugmsg("symbol printtree",LOGLEVEL_PRINT);
167 os << std::string(indent,' ') << name << " (symbol): "
168 << "serial=" << serial
169 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
170 << ", flags=" << flags << endl;
173 void symbol::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
175 debugmsg("symbol print csrc", LOGLEVEL_PRINT);
179 bool symbol::info(unsigned inf) const
181 if (inf==info_flags::symbol) return true;
182 if (inf==info_flags::polynomial ||
183 inf==info_flags::integer_polynomial ||
184 inf==info_flags::cinteger_polynomial ||
185 inf==info_flags::rational_polynomial ||
186 inf==info_flags::crational_polynomial ||
187 inf==info_flags::rational_function) {
190 return inherited::info(inf);
194 ex symbol::expand(unsigned options) const
199 bool symbol::has(const ex & other) const
201 if (is_equal(*other.bp)) return true;
205 int symbol::degree(const symbol & s) const
207 return compare_same_type(s)==0 ? 1 : 0;
210 int symbol::ldegree(const symbol & s) const
212 return compare_same_type(s)==0 ? 1 : 0;
215 ex symbol::coeff(const symbol & s, int n) const
217 if (compare_same_type(s)==0) {
218 return n==1 ? _ex1() : _ex0();
220 return n==0 ? *this : _ex0();
224 ex symbol::eval(int level) const
226 if (level == -max_recursion_level) {
227 throw(std::runtime_error("max recursion level reached"));
230 if (asexinfop->is_assigned) {
231 setflag(status_flags::evaluated);
233 return (asexinfop->assigned_expression);
235 return (asexinfop->assigned_expression).eval(level);
242 ex symbol::subs(const lst & ls, const lst & lr) const
244 GINAC_ASSERT(ls.nops()==lr.nops());
245 #ifdef DO_GINAC_ASSERT
246 for (unsigned i=0; i<ls.nops(); i++) {
247 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
248 is_ex_of_type(ls.op(i),idx));
250 #endif // def DO_GINAC_ASSERT
252 for (unsigned i=0; i<ls.nops(); i++) {
253 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
254 if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
262 /** Implementation of ex::diff() for single differentiation of a symbol.
266 ex symbol::derivative(const symbol & s) const
268 if (compare_same_type(s)) {
275 int symbol::compare_same_type(const basic & other) const
277 GINAC_ASSERT(is_of_type(other,symbol));
278 const symbol *o = static_cast<const symbol *>(&other);
279 if (serial==o->serial) return 0;
280 return serial < o->serial ? -1 : 1;
283 bool symbol::is_equal_same_type(const basic & other) const
285 GINAC_ASSERT(is_of_type(other,symbol));
286 const symbol *o = static_cast<const symbol *>(&other);
287 return serial==o->serial;
290 unsigned symbol::return_type(void) const
292 return return_types::commutative;
295 unsigned symbol::return_type_tinfo(void) const
300 unsigned symbol::calchash(void) const
302 // return golden_ratio_hash(tinfo()) ^ serial;
303 hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
304 setflag(status_flags::hash_calculated);
309 // virtual functions which can be overridden by derived classes
315 // non-virtual functions in this class
320 void symbol::assign(const ex & value)
322 asexinfop->is_assigned=1;
323 asexinfop->assigned_expression=value;
324 clearflag(status_flags::evaluated);
327 void symbol::unassign(void)
329 if (asexinfop->is_assigned) {
330 asexinfop->is_assigned=0;
331 asexinfop->assigned_expression=_ex0();
333 setflag(status_flags::evaluated);
338 std::string & symbol::autoname_prefix(void)
340 static std::string * s=new std::string("symbol");
345 // static member variables
350 unsigned symbol::next_serial=0;
352 // string const symbol::autoname_prefix="symbol";
358 const symbol some_symbol;
359 const type_info & typeid_symbol=typeid(some_symbol);
362 // subclass assigned_ex_info
365 /** Default ctor. Defaults to unassigned. */
366 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)
370 #ifndef NO_NAMESPACE_GINAC
372 #endif // ndef NO_NAMESPACE_GINAC