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 // default constructor, destructor, copy constructor assignment operator and helpers
35 symbol::symbol() : basic(TINFO_symbol)
37 debugmsg("symbol default constructor",LOGLEVEL_CONSTRUCT);
39 name=autoname_prefix()+ToString(serial);
40 asexinfop=new assigned_ex_info;
41 setflag(status_flags::evaluated);
46 debugmsg("symbol destructor",LOGLEVEL_DESTRUCT);
50 symbol::symbol(symbol const & other)
52 debugmsg("symbol copy constructor",LOGLEVEL_CONSTRUCT);
56 void symbol::copy(symbol const & other)
61 asexinfop=other.asexinfop;
62 ++asexinfop->refcount;
65 void symbol::destroy(bool call_parent)
67 if (--asexinfop->refcount == 0) {
71 basic::destroy(call_parent);
75 // how should the following be interpreted?
79 // probably as: x=ex(y);
87 symbol::symbol(string const & initname) : basic(TINFO_symbol)
89 debugmsg("symbol constructor from string",LOGLEVEL_CONSTRUCT);
92 asexinfop=new assigned_ex_info;
93 setflag(status_flags::evaluated);
97 // functions overriding virtual functions from bases classes
102 basic * symbol::duplicate() const
104 debugmsg("symbol duplicate",LOGLEVEL_DUPLICATE);
105 return new symbol(*this);
108 bool symbol::info(unsigned inf) const
110 if (inf==info_flags::symbol) return true;
111 if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial || inf==info_flags::rational_function) {
114 return basic::info(inf);
118 ex symbol::expand(unsigned options) const
123 bool symbol::has(ex const & other) const
125 if (is_equal(*other.bp)) return true;
129 int symbol::degree(symbol const & s) const
131 return compare_same_type(s)==0 ? 1 : 0;
134 int symbol::ldegree(symbol const & s) const
136 return compare_same_type(s)==0 ? 1 : 0;
139 ex symbol::coeff(symbol const & s, int const n) const
141 if (compare_same_type(s)==0) {
142 return n==1 ? exONE() : exZERO();
144 return n==0 ? *this : exZERO();
148 ex symbol::eval(int level) const
150 if (level == -max_recursion_level) {
151 throw(std::runtime_error("max recursion level reached"));
154 if (asexinfop->is_assigned) {
155 setflag(status_flags::evaluated);
157 return (asexinfop->assigned_expression);
159 return (asexinfop->assigned_expression).eval(level);
166 ex symbol::subs(lst const & ls, lst const & lr) const
168 ASSERT(ls.nops()==lr.nops());
170 for (int i=0; i<ls.nops(); i++) {
171 ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
172 is_ex_of_type(ls.op(i),idx));
174 #endif // def DOASSERT
176 for (int i=0; i<ls.nops(); i++) {
177 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
178 if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
186 int symbol::compare_same_type(basic const & other) const
188 ASSERT(is_of_type(other,symbol));
189 const symbol *o = static_cast<const symbol *>(&other);
190 if (serial==o->serial) return 0;
191 return serial < o->serial ? -1 : 1;
194 bool symbol::is_equal_same_type(basic const & other) const
196 ASSERT(is_of_type(other,symbol));
197 const symbol *o = static_cast<const symbol *>(&other);
198 return serial==o->serial;
201 unsigned symbol::return_type(void) const
203 return return_types::commutative;
206 unsigned symbol::return_type_tinfo(void) const
211 unsigned symbol::calchash(void) const
213 // return golden_ratio_hash(tinfo()) ^ serial;
214 hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
215 setflag(status_flags::hash_calculated);
220 // virtual functions which can be overridden by derived classes
226 // non-virtual functions in this class
231 void symbol::assign(ex const & value)
233 asexinfop->is_assigned=1;
234 asexinfop->assigned_expression=value;
235 clearflag(status_flags::evaluated);
238 void symbol::unassign(void)
240 if (asexinfop->is_assigned) {
241 asexinfop->is_assigned=0;
242 asexinfop->assigned_expression=exZERO();
244 setflag(status_flags::evaluated);
249 string & symbol::autoname_prefix(void)
251 static string * s=new string("symbol");
256 // static member variables
261 unsigned symbol::next_serial=0;
263 // string const symbol::autoname_prefix="symbol";
269 const symbol some_symbol;
270 type_info const & typeid_symbol=typeid(some_symbol);
273 // subclass assigned_ex_info
276 /** Default ctor. Defaults to unassigned. */
277 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)