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(std::ostream & os, unsigned upper_precedence) const
153 debugmsg("symbol print",LOGLEVEL_PRINT);
157 void symbol::printraw(std::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(std::ostream & os, unsigned indent) const
166 debugmsg("symbol printtree",LOGLEVEL_PRINT);
167 os << std::string(indent,' ') << name << " (symbol): "
168 << "serial=" << serial
169 << ", hash=" << hashvalue
170 << " (0x" << std::hex << hashvalue << std::dec << ")"
171 << ", flags=" << flags << std::endl;
174 void symbol::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
176 debugmsg("symbol print csrc", LOGLEVEL_PRINT);
180 bool symbol::info(unsigned inf) const
182 if (inf==info_flags::symbol) return true;
183 if (inf==info_flags::polynomial ||
184 inf==info_flags::integer_polynomial ||
185 inf==info_flags::cinteger_polynomial ||
186 inf==info_flags::rational_polynomial ||
187 inf==info_flags::crational_polynomial ||
188 inf==info_flags::rational_function) {
191 return inherited::info(inf);
195 ex symbol::expand(unsigned options) const
200 bool symbol::has(const ex & other) const
202 if (is_equal(*other.bp)) return true;
206 int symbol::degree(const symbol & s) const
208 return compare_same_type(s)==0 ? 1 : 0;
211 int symbol::ldegree(const symbol & s) const
213 return compare_same_type(s)==0 ? 1 : 0;
216 ex symbol::coeff(const symbol & s, int n) const
218 if (compare_same_type(s)==0) {
219 return n==1 ? _ex1() : _ex0();
221 return n==0 ? *this : _ex0();
225 ex symbol::eval(int level) const
227 if (level == -max_recursion_level) {
228 throw(std::runtime_error("max recursion level reached"));
231 if (asexinfop->is_assigned) {
232 setflag(status_flags::evaluated);
234 return (asexinfop->assigned_expression);
236 return (asexinfop->assigned_expression).eval(level);
243 ex symbol::subs(const lst & ls, const lst & lr) const
245 GINAC_ASSERT(ls.nops()==lr.nops());
246 #ifdef DO_GINAC_ASSERT
247 for (unsigned i=0; i<ls.nops(); i++) {
248 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
249 is_ex_of_type(ls.op(i),idx));
251 #endif // def DO_GINAC_ASSERT
253 for (unsigned i=0; i<ls.nops(); i++) {
254 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
255 if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
263 /** Implementation of ex::diff() for single differentiation of a symbol.
267 ex symbol::derivative(const symbol & s) const
269 if (compare_same_type(s)) {
276 int symbol::compare_same_type(const basic & other) const
278 GINAC_ASSERT(is_of_type(other,symbol));
279 const symbol *o = static_cast<const symbol *>(&other);
280 if (serial==o->serial) return 0;
281 return serial < o->serial ? -1 : 1;
284 bool symbol::is_equal_same_type(const basic & other) const
286 GINAC_ASSERT(is_of_type(other,symbol));
287 const symbol *o = static_cast<const symbol *>(&other);
288 return serial==o->serial;
291 unsigned symbol::return_type(void) const
293 return return_types::commutative;
296 unsigned symbol::return_type_tinfo(void) const
301 unsigned symbol::calchash(void) const
303 // return golden_ratio_hash(tinfo()) ^ serial;
304 hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
305 setflag(status_flags::hash_calculated);
310 // virtual functions which can be overridden by derived classes
316 // non-virtual functions in this class
321 void symbol::assign(const ex & value)
323 asexinfop->is_assigned=1;
324 asexinfop->assigned_expression=value;
325 clearflag(status_flags::evaluated);
328 void symbol::unassign(void)
330 if (asexinfop->is_assigned) {
331 asexinfop->is_assigned=0;
332 asexinfop->assigned_expression=_ex0();
334 setflag(status_flags::evaluated);
339 std::string & symbol::autoname_prefix(void)
341 static std::string * s=new std::string("symbol");
346 // static member variables
351 unsigned symbol::next_serial=0;
353 // std::string const symbol::autoname_prefix="symbol";
359 const symbol some_symbol;
360 const type_info & typeid_symbol=typeid(some_symbol);
363 // subclass assigned_ex_info
366 /** Default ctor. Defaults to unassigned. */
367 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)
371 #ifndef NO_NAMESPACE_GINAC
373 #endif // ndef NO_NAMESPACE_GINAC