3 * Implementation of GiNaC's symbolic objects. */
6 * GiNaC Copyright (C) 1999-2001 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 GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(symbol, basic)
38 // default ctor, dtor, copy ctor, assignment operator and helpers
41 symbol::symbol() : inherited(TINFO_symbol), serial(next_serial++)
43 name = TeX_name = autoname_prefix()+ToString(serial);
44 asexinfop = new assigned_ex_info;
45 setflag(status_flags::evaluated | status_flags::expanded);
48 /** For use by copy ctor and assignment operator. */
49 void symbol::copy(const symbol & other)
51 inherited::copy(other);
53 TeX_name = other.TeX_name;
54 serial = other.serial;
55 asexinfop = other.asexinfop;
56 ++asexinfop->refcount;
59 void symbol::destroy(bool call_parent)
61 if (--asexinfop->refcount == 0)
64 inherited::destroy(call_parent);
73 symbol::symbol(const symbol & other)
78 symbol::symbol(const std::string & initname) : inherited(TINFO_symbol)
81 TeX_name = default_TeX_name();
82 serial = next_serial++;
83 asexinfop = new assigned_ex_info;
84 setflag(status_flags::evaluated | status_flags::expanded);
87 symbol::symbol(const std::string & initname, const std::string & texname) : inherited(TINFO_symbol)
91 serial = next_serial++;
92 asexinfop = new assigned_ex_info;
93 setflag(status_flags::evaluated | status_flags::expanded);
100 /** Construct object from archive_node. */
101 symbol::symbol(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
103 serial = next_serial++;
104 if (!(n.find_string("name", name)))
105 name = autoname_prefix() + ToString(serial);
106 if (!(n.find_string("TeXname", TeX_name)))
107 TeX_name = default_TeX_name();
108 asexinfop = new assigned_ex_info;
109 setflag(status_flags::evaluated);
112 /** Unarchive the object. */
113 ex symbol::unarchive(const archive_node &n, const lst &sym_lst)
115 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
117 // If symbol is in sym_lst, return the existing symbol
118 for (unsigned i=0; i<sym_lst.nops(); i++) {
119 if (is_ex_of_type(sym_lst.op(i), symbol) && (ex_to<symbol>(sym_lst.op(i)).name == ex_to<symbol>(s).name))
120 return sym_lst.op(i);
125 /** Archive the object. */
126 void symbol::archive(archive_node &n) const
128 inherited::archive(n);
129 n.add_string("name", name);
130 if (TeX_name != default_TeX_name())
131 n.add_string("TeX_name", TeX_name);
135 // functions overriding virtual functions from base classes
140 basic *symbol::duplicate() const
142 return new symbol(*this);
145 void symbol::print(const print_context & c, unsigned level) const
147 if (is_a<print_tree>(c)) {
149 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
150 << ", serial=" << serial
151 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
154 } else if (is_a<print_latex>(c))
160 bool symbol::info(unsigned inf) const
162 if (inf==info_flags::symbol) return true;
163 if (inf==info_flags::polynomial ||
164 inf==info_flags::integer_polynomial ||
165 inf==info_flags::cinteger_polynomial ||
166 inf==info_flags::rational_polynomial ||
167 inf==info_flags::crational_polynomial ||
168 inf==info_flags::rational_function)
171 return inherited::info(inf);
174 int symbol::degree(const ex & s) const
176 return is_equal(ex_to<basic>(s)) ? 1 : 0;
179 int symbol::ldegree(const ex & s) const
181 return is_equal(ex_to<basic>(s)) ? 1 : 0;
184 ex symbol::coeff(const ex & s, int n) const
186 if (is_equal(ex_to<basic>(s)))
187 return n==1 ? _ex1 : _ex0;
189 return n==0 ? *this : _ex0;
192 ex symbol::eval(int level) const
194 if (level == -max_recursion_level)
195 throw(std::runtime_error("max recursion level reached"));
197 if (asexinfop->is_assigned) {
198 setflag(status_flags::evaluated);
200 return (asexinfop->assigned_expression);
202 return (asexinfop->assigned_expression).eval(level);
210 /** Implementation of ex::diff() for single differentiation of a symbol.
214 ex symbol::derivative(const symbol & s) const
216 if (compare_same_type(s))
222 int symbol::compare_same_type(const basic & other) const
224 GINAC_ASSERT(is_a<symbol>(other));
225 const symbol *o = static_cast<const symbol *>(&other);
226 if (serial==o->serial) return 0;
227 return serial < o->serial ? -1 : 1;
230 bool symbol::is_equal_same_type(const basic & other) const
232 GINAC_ASSERT(is_a<symbol>(other));
233 const symbol *o = static_cast<const symbol *>(&other);
234 return serial==o->serial;
237 unsigned symbol::calchash(void) const
239 // this is where the schoolbook method
240 // (golden_ratio_hash(tinfo()) ^ serial)
241 // is not good enough yet...
242 hashvalue = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
243 setflag(status_flags::hash_calculated);
248 // virtual functions which can be overridden by derived classes
254 // non-virtual functions in this class
259 void symbol::assign(const ex & value)
261 asexinfop->is_assigned = 1;
262 asexinfop->assigned_expression = value;
263 clearflag(status_flags::evaluated | status_flags::expanded);
266 void symbol::unassign(void)
268 if (asexinfop->is_assigned) {
269 asexinfop->is_assigned = 0;
270 asexinfop->assigned_expression = _ex0;
272 setflag(status_flags::evaluated | status_flags::expanded);
277 /** Symbols not constructed with a string get one assigned using this
278 * prefix and a number. */
279 std::string & symbol::autoname_prefix(void)
281 static std::string *s = new std::string("symbol");
285 /** Return default TeX name for symbol. This recognizes some greek letters. */
286 std::string symbol::default_TeX_name(void) const
288 if (name=="alpha" || name=="beta" || name=="gamma"
289 || name=="delta" || name=="epsilon" || name=="varepsilon"
290 || name=="zeta" || name=="eta" || name=="theta"
291 || name=="vartheta" || name=="iota" || name=="kappa"
292 || name=="lambda" || name=="mu" || name=="nu"
293 || name=="xi" || name=="omicron" || name=="pi"
294 || name=="varpi" || name=="rho" || name=="varrho"
295 || name=="sigma" || name=="varsigma" || name=="tau"
296 || name=="upsilon" || name=="phi" || name=="varphi"
297 || name=="chi" || name=="psi" || name=="omega"
298 || name=="Gamma" || name=="Delta" || name=="Theta"
299 || name=="Lambda" || name=="Xi" || name=="Pi"
300 || name=="Sigma" || name=="Upsilon" || name=="Phi"
301 || name=="Psi" || name=="Omega")
308 // static member variables
313 unsigned symbol::next_serial = 0;
316 // subclass assigned_ex_info
319 /** Default ctor. Defaults to unassigned. */
320 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)