3 * Implementation of GiNaC's symbolic objects. */
6 * GiNaC Copyright (C) 1999 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
32 #ifndef NO_GINAC_NAMESPACE
34 #endif // ndef NO_GINAC_NAMESPACE
37 // default constructor, destructor, copy constructor assignment operator and helpers
40 symbol::symbol() : basic(TINFO_symbol)
42 debugmsg("symbol default constructor",LOGLEVEL_CONSTRUCT);
44 name=autoname_prefix()+ToString(serial);
45 asexinfop=new assigned_ex_info;
46 setflag(status_flags::evaluated);
51 debugmsg("symbol destructor",LOGLEVEL_DESTRUCT);
55 symbol::symbol(symbol const & other)
57 debugmsg("symbol copy constructor",LOGLEVEL_CONSTRUCT);
61 void symbol::copy(symbol const & other)
66 asexinfop=other.asexinfop;
67 ++asexinfop->refcount;
70 void symbol::destroy(bool call_parent)
72 if (--asexinfop->refcount == 0) {
76 basic::destroy(call_parent);
80 // how should the following be interpreted?
84 // probably as: x=ex(y);
92 symbol::symbol(string const & initname) : basic(TINFO_symbol)
94 debugmsg("symbol constructor from string",LOGLEVEL_CONSTRUCT);
97 asexinfop=new assigned_ex_info;
98 setflag(status_flags::evaluated);
102 // functions overriding virtual functions from bases classes
107 basic * symbol::duplicate() const
109 debugmsg("symbol duplicate",LOGLEVEL_DUPLICATE);
110 return new symbol(*this);
113 void symbol::print(ostream & os, unsigned upper_precedence) const
115 debugmsg("symbol print",LOGLEVEL_PRINT);
119 void symbol::printraw(ostream & os) const
121 debugmsg("symbol printraw",LOGLEVEL_PRINT);
122 os << "symbol(" << "name=" << name << ",serial=" << serial
123 << ",hash=" << hashvalue << ",flags=" << flags << ")";
126 void symbol::printtree(ostream & os, unsigned indent) const
128 debugmsg("symbol printtree",LOGLEVEL_PRINT);
129 os << string(indent,' ') << name << " (symbol): "
130 << "serial=" << serial
131 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
132 << ", flags=" << flags << endl;
135 void symbol::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
137 debugmsg("symbol print csrc", LOGLEVEL_PRINT);
141 bool symbol::info(unsigned inf) const
143 if (inf==info_flags::symbol) return true;
144 if (inf==info_flags::polynomial ||
145 inf==info_flags::integer_polynomial ||
146 inf==info_flags::cinteger_polynomial ||
147 inf==info_flags::rational_polynomial ||
148 inf==info_flags::crational_polynomial ||
149 inf==info_flags::rational_function) {
152 return basic::info(inf);
156 ex symbol::expand(unsigned options) const
161 bool symbol::has(ex const & other) const
163 if (is_equal(*other.bp)) return true;
167 int symbol::degree(symbol const & s) const
169 return compare_same_type(s)==0 ? 1 : 0;
172 int symbol::ldegree(symbol const & s) const
174 return compare_same_type(s)==0 ? 1 : 0;
177 ex symbol::coeff(symbol const & s, int const n) const
179 if (compare_same_type(s)==0) {
180 return n==1 ? _ex1() : _ex0();
182 return n==0 ? *this : _ex0();
186 ex symbol::eval(int level) const
188 if (level == -max_recursion_level) {
189 throw(std::runtime_error("max recursion level reached"));
192 if (asexinfop->is_assigned) {
193 setflag(status_flags::evaluated);
195 return (asexinfop->assigned_expression);
197 return (asexinfop->assigned_expression).eval(level);
204 ex symbol::subs(lst const & ls, lst const & lr) const
206 GINAC_ASSERT(ls.nops()==lr.nops());
207 #ifdef DO_GINAC_ASSERT
208 for (int i=0; i<ls.nops(); i++) {
209 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
210 is_ex_of_type(ls.op(i),idx));
212 #endif // def DO_GINAC_ASSERT
214 for (int i=0; i<ls.nops(); i++) {
215 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
216 if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
224 int symbol::compare_same_type(basic const & other) const
226 GINAC_ASSERT(is_of_type(other,symbol));
227 const symbol *o = static_cast<const symbol *>(&other);
228 if (serial==o->serial) return 0;
229 return serial < o->serial ? -1 : 1;
232 bool symbol::is_equal_same_type(basic const & other) const
234 GINAC_ASSERT(is_of_type(other,symbol));
235 const symbol *o = static_cast<const symbol *>(&other);
236 return serial==o->serial;
239 unsigned symbol::return_type(void) const
241 return return_types::commutative;
244 unsigned symbol::return_type_tinfo(void) const
249 unsigned symbol::calchash(void) const
251 // return golden_ratio_hash(tinfo()) ^ serial;
252 hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
253 setflag(status_flags::hash_calculated);
258 // virtual functions which can be overridden by derived classes
264 // non-virtual functions in this class
269 void symbol::assign(ex const & value)
271 asexinfop->is_assigned=1;
272 asexinfop->assigned_expression=value;
273 clearflag(status_flags::evaluated);
276 void symbol::unassign(void)
278 if (asexinfop->is_assigned) {
279 asexinfop->is_assigned=0;
280 asexinfop->assigned_expression=_ex0();
282 setflag(status_flags::evaluated);
287 string & symbol::autoname_prefix(void)
289 static string * s=new string("symbol");
294 // static member variables
299 unsigned symbol::next_serial=0;
301 // string const symbol::autoname_prefix="symbol";
307 const symbol some_symbol;
308 type_info const & typeid_symbol=typeid(some_symbol);
311 // subclass assigned_ex_info
314 /** Default ctor. Defaults to unassigned. */
315 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)
319 #ifndef NO_GINAC_NAMESPACE
321 #endif // ndef NO_GINAC_NAMESPACE