3 * Implementation of GiNaC's symbolic objects.
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 // default constructor, destructor, copy constructor assignment operator and helpers
33 symbol::symbol() : basic(TINFO_symbol)
35 debugmsg("symbol default constructor",LOGLEVEL_CONSTRUCT);
37 name=autoname_prefix()+ToString(serial);
38 asexinfop=new assigned_ex_info;
39 setflag(status_flags::evaluated);
44 debugmsg("symbol destructor",LOGLEVEL_DESTRUCT);
48 symbol::symbol(symbol const & other)
50 debugmsg("symbol copy constructor",LOGLEVEL_CONSTRUCT);
54 void symbol::copy(symbol const & other)
59 asexinfop=other.asexinfop;
60 ++asexinfop->refcount;
63 void symbol::destroy(bool call_parent)
65 if (--asexinfop->refcount == 0) {
69 basic::destroy(call_parent);
73 // how should the following be interpreted?
77 // probably as: x=ex(y);
85 symbol::symbol(string const & initname) : basic(TINFO_symbol)
87 debugmsg("symbol constructor from string",LOGLEVEL_CONSTRUCT);
90 asexinfop=new assigned_ex_info;
91 setflag(status_flags::evaluated);
95 // functions overriding virtual functions from bases classes
100 basic * symbol::duplicate() const
102 debugmsg("symbol duplicate",LOGLEVEL_DUPLICATE);
103 return new symbol(*this);
106 bool symbol::info(unsigned inf) const
108 if (inf==info_flags::symbol) return true;
109 if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial || inf==info_flags::rational_function) {
112 return basic::info(inf);
116 ex symbol::expand(unsigned options) const
121 bool symbol::has(ex const & other) const
123 if (is_equal(*other.bp)) return true;
127 int symbol::degree(symbol const & s) const
129 return compare_same_type(s)==0 ? 1 : 0;
132 int symbol::ldegree(symbol const & s) const
134 return compare_same_type(s)==0 ? 1 : 0;
137 ex symbol::coeff(symbol const & s, int const n) const
139 if (compare_same_type(s)==0) {
140 return n==1 ? exONE() : exZERO();
142 return n==0 ? *this : exZERO();
146 ex symbol::eval(int level) const
148 if (level == -max_recursion_level) {
149 throw(std::runtime_error("max recursion level reached"));
152 if (asexinfop->is_assigned) {
153 setflag(status_flags::evaluated);
155 return (asexinfop->assigned_expression);
157 return (asexinfop->assigned_expression).eval(level);
164 ex symbol::subs(lst const & ls, lst const & lr) const
166 ASSERT(ls.nops()==lr.nops());
168 for (int i=0; i<ls.nops(); i++) {
169 ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
170 is_ex_of_type(ls.op(i),idx));
172 #endif // def DOASSERT
174 for (int i=0; i<ls.nops(); i++) {
175 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
176 if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
184 int symbol::compare_same_type(basic const & other) const
186 ASSERT(is_of_type(other,symbol));
187 const symbol *o = static_cast<const symbol *>(&other);
188 if (serial==o->serial) return 0;
189 return serial < o->serial ? -1 : 1;
192 bool symbol::is_equal_same_type(basic const & other) const
194 ASSERT(is_of_type(other,symbol));
195 const symbol *o = static_cast<const symbol *>(&other);
196 return serial==o->serial;
199 unsigned symbol::return_type(void) const
201 return return_types::commutative;
204 unsigned symbol::return_type_tinfo(void) const
209 unsigned symbol::calchash(void) const
211 // return golden_ratio_hash(tinfo()) ^ serial;
212 hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
213 setflag(status_flags::hash_calculated);
218 // virtual functions which can be overridden by derived classes
224 // non-virtual functions in this class
229 void symbol::assign(ex const & value)
231 asexinfop->is_assigned=1;
232 asexinfop->assigned_expression=value;
233 clearflag(status_flags::evaluated);
236 void symbol::unassign(void)
238 if (asexinfop->is_assigned) {
239 asexinfop->is_assigned=0;
240 asexinfop->assigned_expression=exZERO();
242 setflag(status_flags::evaluated);
247 string & symbol::autoname_prefix(void)
249 static string * s=new string("symbol");
254 // static member variables
259 unsigned symbol::next_serial=0;
261 // string const symbol::autoname_prefix="symbol";
267 const symbol some_symbol;
268 type_info const & typeid_symbol=typeid(some_symbol);
271 // subclass assigned_ex_info
274 /** Default ctor. Defaults to unassigned. */
275 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)