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
31 // default constructor, destructor, copy constructor assignment operator and helpers
34 symbol::symbol() : basic(TINFO_symbol)
36 debugmsg("symbol default constructor",LOGLEVEL_CONSTRUCT);
38 name=autoname_prefix()+ToString(serial);
39 asexinfop=new assigned_ex_info;
40 setflag(status_flags::evaluated);
45 debugmsg("symbol destructor",LOGLEVEL_DESTRUCT);
49 symbol::symbol(symbol const & other)
51 debugmsg("symbol copy constructor",LOGLEVEL_CONSTRUCT);
55 void symbol::copy(symbol const & other)
60 asexinfop=other.asexinfop;
61 ++asexinfop->refcount;
64 void symbol::destroy(bool call_parent)
66 if (--asexinfop->refcount == 0) {
70 basic::destroy(call_parent);
74 // how should the following be interpreted?
78 // probably as: x=ex(y);
86 symbol::symbol(string const & initname) : basic(TINFO_symbol)
88 debugmsg("symbol constructor from string",LOGLEVEL_CONSTRUCT);
91 asexinfop=new assigned_ex_info;
92 setflag(status_flags::evaluated);
96 // functions overriding virtual functions from bases classes
101 basic * symbol::duplicate() const
103 debugmsg("symbol duplicate",LOGLEVEL_DUPLICATE);
104 return new symbol(*this);
107 bool symbol::info(unsigned inf) const
109 if (inf==info_flags::symbol) return true;
110 if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial || inf==info_flags::rational_function) {
113 return basic::info(inf);
117 ex symbol::expand(unsigned options) const
122 bool symbol::has(ex const & other) const
124 if (is_equal(*other.bp)) return true;
128 int symbol::degree(symbol const & s) const
130 return compare_same_type(s)==0 ? 1 : 0;
133 int symbol::ldegree(symbol const & s) const
135 return compare_same_type(s)==0 ? 1 : 0;
138 ex symbol::coeff(symbol const & s, int const n) const
140 if (compare_same_type(s)==0) {
141 return n==1 ? exONE() : exZERO();
143 return n==0 ? *this : exZERO();
147 ex symbol::eval(int level) const
149 if (level == -max_recursion_level) {
150 throw(std::runtime_error("max recursion level reached"));
153 if (asexinfop->is_assigned) {
154 setflag(status_flags::evaluated);
156 return (asexinfop->assigned_expression);
158 return (asexinfop->assigned_expression).eval(level);
165 ex symbol::subs(lst const & ls, lst const & lr) const
167 ASSERT(ls.nops()==lr.nops());
169 for (int i=0; i<ls.nops(); i++) {
170 ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
171 is_ex_of_type(ls.op(i),idx));
173 #endif // def DOASSERT
175 for (int i=0; i<ls.nops(); i++) {
176 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
177 if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
185 int symbol::compare_same_type(basic const & other) const
187 ASSERT(is_of_type(other,symbol));
188 const symbol *o = static_cast<const symbol *>(&other);
189 if (serial==o->serial) return 0;
190 return serial < o->serial ? -1 : 1;
193 bool symbol::is_equal_same_type(basic const & other) const
195 ASSERT(is_of_type(other,symbol));
196 const symbol *o = static_cast<const symbol *>(&other);
197 return serial==o->serial;
200 unsigned symbol::return_type(void) const
202 return return_types::commutative;
205 unsigned symbol::return_type_tinfo(void) const
210 unsigned symbol::calchash(void) const
212 // return golden_ratio_hash(tinfo()) ^ serial;
213 hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
214 setflag(status_flags::hash_calculated);
219 // virtual functions which can be overridden by derived classes
225 // non-virtual functions in this class
230 void symbol::assign(ex const & value)
232 asexinfop->is_assigned=1;
233 asexinfop->assigned_expression=value;
234 clearflag(status_flags::evaluated);
237 void symbol::unassign(void)
239 if (asexinfop->is_assigned) {
240 asexinfop->is_assigned=0;
241 asexinfop->assigned_expression=exZERO();
243 setflag(status_flags::evaluated);
248 string & symbol::autoname_prefix(void)
250 static string * s=new string("symbol");
255 // static member variables
260 unsigned symbol::next_serial=0;
262 // string const symbol::autoname_prefix="symbol";
268 const symbol some_symbol;
269 type_info const & typeid_symbol=typeid(some_symbol);
272 // subclass assigned_ex_info
275 /** Default ctor. Defaults to unassigned. */
276 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)