3 * Implementation of GiNaC's symbolic objects. */
12 // default constructor, destructor, copy constructor assignment operator and helpers
15 symbol::symbol() : basic(TINFO_SYMBOL)
17 debugmsg("symbol default constructor",LOGLEVEL_CONSTRUCT);
19 name=autoname_prefix()+ToString(serial);
20 asexinfop=new assigned_ex_info;
21 setflag(status_flags::evaluated);
26 debugmsg("symbol destructor",LOGLEVEL_DESTRUCT);
30 symbol::symbol(symbol const & other)
32 debugmsg("symbol copy constructor",LOGLEVEL_CONSTRUCT);
36 void symbol::copy(symbol const & other)
41 asexinfop=other.asexinfop;
42 ++asexinfop->refcount;
45 void symbol::destroy(bool call_parent)
47 if (--asexinfop->refcount == 0) {
51 basic::destroy(call_parent);
55 // how should the following be interpreted?
59 // probably as: x=ex(y);
67 symbol::symbol(string const & initname) : basic(TINFO_SYMBOL)
69 debugmsg("symbol constructor from string",LOGLEVEL_CONSTRUCT);
72 asexinfop=new assigned_ex_info;
73 setflag(status_flags::evaluated);
77 // functions overriding virtual functions from bases classes
82 basic * symbol::duplicate() const
84 debugmsg("symbol duplicate",LOGLEVEL_DUPLICATE);
85 return new symbol(*this);
88 bool symbol::info(unsigned inf) const
90 if (inf==info_flags::symbol) return true;
91 if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial || inf==info_flags::rational_function) {
94 return basic::info(inf);
98 ex symbol::expand(unsigned options) const
103 bool symbol::has(ex const & other) const
105 if (is_equal(*other.bp)) return true;
109 int symbol::degree(symbol const & s) const
111 return compare_same_type(s)==0 ? 1 : 0;
114 int symbol::ldegree(symbol const & s) const
116 return compare_same_type(s)==0 ? 1 : 0;
119 ex symbol::coeff(symbol const & s, int const n) const
121 if (compare_same_type(s)==0) {
122 return n==1 ? exONE() : exZERO();
124 return n==0 ? *this : exZERO();
128 ex symbol::eval(int level) const
130 if (level == -max_recursion_level) {
131 throw(std::runtime_error("max recursion level reached"));
134 if (asexinfop->is_assigned) {
135 setflag(status_flags::evaluated);
137 return (asexinfop->assigned_expression);
139 return (asexinfop->assigned_expression).eval(level);
146 ex symbol::subs(lst const & ls, lst const & lr) const
148 ASSERT(ls.nops()==lr.nops());
150 for (int i=0; i<ls.nops(); i++) {
151 ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
152 is_ex_of_type(ls.op(i),idx));
154 #endif // def DOASSERT
156 for (int i=0; i<ls.nops(); i++) {
157 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
158 if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
166 int symbol::compare_same_type(basic const & other) const
168 ASSERT(is_of_type(other,symbol));
169 const symbol *o = static_cast<const symbol *>(&other);
170 if (serial==o->serial) return 0;
171 return serial < o->serial ? -1 : 1;
174 bool symbol::is_equal_same_type(basic const & other) const
176 ASSERT(is_of_type(other,symbol));
177 const symbol *o = static_cast<const symbol *>(&other);
178 return serial==o->serial;
181 unsigned symbol::return_type(void) const
183 return return_types::commutative;
186 unsigned symbol::return_type_tinfo(void) const
191 unsigned symbol::calchash(void) const
193 // return golden_ratio_hash(tinfo()) ^ serial;
194 hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
195 setflag(status_flags::hash_calculated);
200 // virtual functions which can be overridden by derived classes
206 // non-virtual functions in this class
211 void symbol::assign(ex const & value)
213 asexinfop->is_assigned=1;
214 asexinfop->assigned_expression=value;
215 clearflag(status_flags::evaluated);
218 void symbol::unassign(void)
220 if (asexinfop->is_assigned) {
221 asexinfop->is_assigned=0;
222 asexinfop->assigned_expression=exZERO();
224 setflag(status_flags::evaluated);
229 string & symbol::autoname_prefix(void)
231 static string * s=new string("symbol");
236 // static member variables
241 unsigned symbol::next_serial=0;
243 // string const symbol::autoname_prefix="symbol";
249 const symbol some_symbol;
250 type_info const & typeid_symbol=typeid(some_symbol);
253 // subclass assigned_ex_info
256 /** Default ctor. Defaults to unassigned. */
257 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)