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
35 // default constructor, destructor, copy constructor assignment operator and helpers
38 symbol::symbol() : basic(TINFO_symbol)
40 debugmsg("symbol default constructor",LOGLEVEL_CONSTRUCT);
42 name=autoname_prefix()+ToString(serial);
43 asexinfop=new assigned_ex_info;
44 setflag(status_flags::evaluated);
49 debugmsg("symbol destructor",LOGLEVEL_DESTRUCT);
53 symbol::symbol(symbol const & other)
55 debugmsg("symbol copy constructor",LOGLEVEL_CONSTRUCT);
59 void symbol::copy(symbol const & other)
64 asexinfop=other.asexinfop;
65 ++asexinfop->refcount;
68 void symbol::destroy(bool call_parent)
70 if (--asexinfop->refcount == 0) {
74 basic::destroy(call_parent);
78 // how should the following be interpreted?
82 // probably as: x=ex(y);
90 symbol::symbol(string const & initname) : basic(TINFO_symbol)
92 debugmsg("symbol constructor from string",LOGLEVEL_CONSTRUCT);
95 asexinfop=new assigned_ex_info;
96 setflag(status_flags::evaluated);
100 // functions overriding virtual functions from bases classes
105 basic * symbol::duplicate() const
107 debugmsg("symbol duplicate",LOGLEVEL_DUPLICATE);
108 return new symbol(*this);
111 bool symbol::info(unsigned inf) const
113 if (inf==info_flags::symbol) return true;
114 if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial || inf==info_flags::rational_function) {
117 return basic::info(inf);
121 ex symbol::expand(unsigned options) const
126 bool symbol::has(ex const & other) const
128 if (is_equal(*other.bp)) return true;
132 int symbol::degree(symbol const & s) const
134 return compare_same_type(s)==0 ? 1 : 0;
137 int symbol::ldegree(symbol const & s) const
139 return compare_same_type(s)==0 ? 1 : 0;
142 ex symbol::coeff(symbol const & s, int const n) const
144 if (compare_same_type(s)==0) {
145 return n==1 ? exONE() : exZERO();
147 return n==0 ? *this : exZERO();
151 ex symbol::eval(int level) const
153 if (level == -max_recursion_level) {
154 throw(std::runtime_error("max recursion level reached"));
157 if (asexinfop->is_assigned) {
158 setflag(status_flags::evaluated);
160 return (asexinfop->assigned_expression);
162 return (asexinfop->assigned_expression).eval(level);
169 ex symbol::subs(lst const & ls, lst const & lr) const
171 GINAC_ASSERT(ls.nops()==lr.nops());
172 #ifdef DO_GINAC_ASSERT
173 for (int i=0; i<ls.nops(); i++) {
174 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
175 is_ex_of_type(ls.op(i),idx));
177 #endif // def DO_GINAC_ASSERT
179 for (int i=0; i<ls.nops(); i++) {
180 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
181 if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
189 int symbol::compare_same_type(basic const & other) const
191 GINAC_ASSERT(is_of_type(other,symbol));
192 const symbol *o = static_cast<const symbol *>(&other);
193 if (serial==o->serial) return 0;
194 return serial < o->serial ? -1 : 1;
197 bool symbol::is_equal_same_type(basic const & other) const
199 GINAC_ASSERT(is_of_type(other,symbol));
200 const symbol *o = static_cast<const symbol *>(&other);
201 return serial==o->serial;
204 unsigned symbol::return_type(void) const
206 return return_types::commutative;
209 unsigned symbol::return_type_tinfo(void) const
214 unsigned symbol::calchash(void) const
216 // return golden_ratio_hash(tinfo()) ^ serial;
217 hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
218 setflag(status_flags::hash_calculated);
223 // virtual functions which can be overridden by derived classes
229 // non-virtual functions in this class
234 void symbol::assign(ex const & value)
236 asexinfop->is_assigned=1;
237 asexinfop->assigned_expression=value;
238 clearflag(status_flags::evaluated);
241 void symbol::unassign(void)
243 if (asexinfop->is_assigned) {
244 asexinfop->is_assigned=0;
245 asexinfop->assigned_expression=exZERO();
247 setflag(status_flags::evaluated);
252 string & symbol::autoname_prefix(void)
254 static string * s=new string("symbol");
259 // static member variables
264 unsigned symbol::next_serial=0;
266 // string const symbol::autoname_prefix="symbol";
272 const symbol some_symbol;
273 type_info const & typeid_symbol=typeid(some_symbol);
276 // subclass assigned_ex_info
279 /** Default ctor. Defaults to unassigned. */
280 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)