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
29 // default constructor, destructor, copy constructor assignment operator and helpers
32 symbol::symbol() : basic(TINFO_SYMBOL)
34 debugmsg("symbol default constructor",LOGLEVEL_CONSTRUCT);
36 name=autoname_prefix()+ToString(serial);
37 asexinfop=new assigned_ex_info;
38 setflag(status_flags::evaluated);
43 debugmsg("symbol destructor",LOGLEVEL_DESTRUCT);
47 symbol::symbol(symbol const & other)
49 debugmsg("symbol copy constructor",LOGLEVEL_CONSTRUCT);
53 void symbol::copy(symbol const & other)
58 asexinfop=other.asexinfop;
59 ++asexinfop->refcount;
62 void symbol::destroy(bool call_parent)
64 if (--asexinfop->refcount == 0) {
68 basic::destroy(call_parent);
72 // how should the following be interpreted?
76 // probably as: x=ex(y);
84 symbol::symbol(string const & initname) : basic(TINFO_SYMBOL)
86 debugmsg("symbol constructor from string",LOGLEVEL_CONSTRUCT);
89 asexinfop=new assigned_ex_info;
90 setflag(status_flags::evaluated);
94 // functions overriding virtual functions from bases classes
99 basic * symbol::duplicate() const
101 debugmsg("symbol duplicate",LOGLEVEL_DUPLICATE);
102 return new symbol(*this);
105 bool symbol::info(unsigned inf) const
107 if (inf==info_flags::symbol) return true;
108 if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial || inf==info_flags::rational_function) {
111 return basic::info(inf);
115 ex symbol::expand(unsigned options) const
120 bool symbol::has(ex const & other) const
122 if (is_equal(*other.bp)) return true;
126 int symbol::degree(symbol const & s) const
128 return compare_same_type(s)==0 ? 1 : 0;
131 int symbol::ldegree(symbol const & s) const
133 return compare_same_type(s)==0 ? 1 : 0;
136 ex symbol::coeff(symbol const & s, int const n) const
138 if (compare_same_type(s)==0) {
139 return n==1 ? exONE() : exZERO();
141 return n==0 ? *this : exZERO();
145 ex symbol::eval(int level) const
147 if (level == -max_recursion_level) {
148 throw(std::runtime_error("max recursion level reached"));
151 if (asexinfop->is_assigned) {
152 setflag(status_flags::evaluated);
154 return (asexinfop->assigned_expression);
156 return (asexinfop->assigned_expression).eval(level);
163 ex symbol::subs(lst const & ls, lst const & lr) const
165 ASSERT(ls.nops()==lr.nops());
167 for (int i=0; i<ls.nops(); i++) {
168 ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
169 is_ex_of_type(ls.op(i),idx));
171 #endif // def DOASSERT
173 for (int i=0; i<ls.nops(); i++) {
174 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
175 if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
183 int symbol::compare_same_type(basic const & other) const
185 ASSERT(is_of_type(other,symbol));
186 const symbol *o = static_cast<const symbol *>(&other);
187 if (serial==o->serial) return 0;
188 return serial < o->serial ? -1 : 1;
191 bool symbol::is_equal_same_type(basic const & other) const
193 ASSERT(is_of_type(other,symbol));
194 const symbol *o = static_cast<const symbol *>(&other);
195 return serial==o->serial;
198 unsigned symbol::return_type(void) const
200 return return_types::commutative;
203 unsigned symbol::return_type_tinfo(void) const
208 unsigned symbol::calchash(void) const
210 // return golden_ratio_hash(tinfo()) ^ serial;
211 hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
212 setflag(status_flags::hash_calculated);
217 // virtual functions which can be overridden by derived classes
223 // non-virtual functions in this class
228 void symbol::assign(ex const & value)
230 asexinfop->is_assigned=1;
231 asexinfop->assigned_expression=value;
232 clearflag(status_flags::evaluated);
235 void symbol::unassign(void)
237 if (asexinfop->is_assigned) {
238 asexinfop->is_assigned=0;
239 asexinfop->assigned_expression=exZERO();
241 setflag(status_flags::evaluated);
246 string & symbol::autoname_prefix(void)
248 static string * s=new string("symbol");
253 // static member variables
258 unsigned symbol::next_serial=0;
260 // string const symbol::autoname_prefix="symbol";
266 const symbol some_symbol;
267 type_info const & typeid_symbol=typeid(some_symbol);
270 // subclass assigned_ex_info
273 /** Default ctor. Defaults to unassigned. */
274 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)