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 debugmsg("symbol default ctor", LOGLEVEL_CONSTRUCT);
44 name = TeX_name = autoname_prefix()+ToString(serial);
45 asexinfop = new assigned_ex_info;
46 setflag(status_flags::evaluated | status_flags::expanded);
49 /** For use by copy ctor and assignment operator. */
50 void symbol::copy(const symbol & other)
52 inherited::copy(other);
54 TeX_name = other.TeX_name;
55 serial = other.serial;
56 asexinfop = other.asexinfop;
57 ++asexinfop->refcount;
60 void symbol::destroy(bool call_parent)
62 if (--asexinfop->refcount == 0)
65 inherited::destroy(call_parent);
74 symbol::symbol(const symbol & other)
76 debugmsg("symbol copy ctor", LOGLEVEL_CONSTRUCT);
80 symbol::symbol(const std::string & initname) : inherited(TINFO_symbol)
82 debugmsg("symbol ctor from string", LOGLEVEL_CONSTRUCT);
84 TeX_name = default_TeX_name();
85 serial = next_serial++;
86 asexinfop = new assigned_ex_info;
87 setflag(status_flags::evaluated | status_flags::expanded);
90 symbol::symbol(const std::string & initname, const std::string & texname) : inherited(TINFO_symbol)
92 debugmsg("symbol ctor from string", LOGLEVEL_CONSTRUCT);
95 serial = next_serial++;
96 asexinfop = new assigned_ex_info;
97 setflag(status_flags::evaluated | status_flags::expanded);
104 /** Construct object from archive_node. */
105 symbol::symbol(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
107 debugmsg("symbol ctor from archive_node", LOGLEVEL_CONSTRUCT);
108 serial = next_serial++;
109 if (!(n.find_string("name", name)))
110 name = autoname_prefix() + ToString(serial);
111 if (!(n.find_string("TeXname", TeX_name)))
112 TeX_name = default_TeX_name();
113 asexinfop = new assigned_ex_info;
114 setflag(status_flags::evaluated);
117 /** Unarchive the object. */
118 ex symbol::unarchive(const archive_node &n, const lst &sym_lst)
120 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
122 // If symbol is in sym_lst, return the existing symbol
123 for (unsigned i=0; i<sym_lst.nops(); i++) {
124 if (is_ex_of_type(sym_lst.op(i), symbol) && (ex_to<symbol>(sym_lst.op(i)).name == ex_to<symbol>(s).name))
125 return sym_lst.op(i);
130 /** Archive the object. */
131 void symbol::archive(archive_node &n) const
133 inherited::archive(n);
134 n.add_string("name", name);
135 if (TeX_name != default_TeX_name())
136 n.add_string("TeX_name", TeX_name);
140 // functions overriding virtual functions from bases classes
145 basic *symbol::duplicate() const
147 debugmsg("symbol duplicate", LOGLEVEL_DUPLICATE);
148 return new symbol(*this);
151 void symbol::print(const print_context & c, unsigned level) const
153 debugmsg("symbol print", LOGLEVEL_PRINT);
155 if (is_of_type(c, print_tree)) {
157 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
158 << ", serial=" << serial
159 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
162 } else if (is_of_type(c, print_latex))
168 bool symbol::info(unsigned inf) const
170 if (inf==info_flags::symbol) return true;
171 if (inf==info_flags::polynomial ||
172 inf==info_flags::integer_polynomial ||
173 inf==info_flags::cinteger_polynomial ||
174 inf==info_flags::rational_polynomial ||
175 inf==info_flags::crational_polynomial ||
176 inf==info_flags::rational_function)
179 return inherited::info(inf);
182 int symbol::degree(const ex & s) const
184 return is_equal(*s.bp) ? 1 : 0;
187 int symbol::ldegree(const ex & s) const
189 return is_equal(*s.bp) ? 1 : 0;
192 ex symbol::coeff(const ex & s, int n) const
195 return n==1 ? _ex1() : _ex0();
197 return n==0 ? *this : _ex0();
200 ex symbol::eval(int level) const
202 if (level == -max_recursion_level)
203 throw(std::runtime_error("max recursion level reached"));
205 if (asexinfop->is_assigned) {
206 setflag(status_flags::evaluated);
208 return (asexinfop->assigned_expression);
210 return (asexinfop->assigned_expression).eval(level);
218 /** Implementation of ex::diff() for single differentiation of a symbol.
222 ex symbol::derivative(const symbol & s) const
224 if (compare_same_type(s))
230 int symbol::compare_same_type(const basic & other) const
232 GINAC_ASSERT(is_of_type(other,symbol));
233 const symbol *o = static_cast<const symbol *>(&other);
234 if (serial==o->serial) return 0;
235 return serial < o->serial ? -1 : 1;
238 bool symbol::is_equal_same_type(const basic & other) const
240 GINAC_ASSERT(is_of_type(other,symbol));
241 const symbol *o = static_cast<const symbol *>(&other);
242 return serial==o->serial;
245 unsigned symbol::return_type(void) const
247 return return_types::commutative;
250 unsigned symbol::return_type_tinfo(void) const
255 unsigned symbol::calchash(void) const
257 // this is where the schoolbook method
258 // (golden_ratio_hash(tinfo()) ^ serial)
259 // is not good enough yet...
260 hashvalue = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
261 setflag(status_flags::hash_calculated);
266 // virtual functions which can be overridden by derived classes
272 // non-virtual functions in this class
277 void symbol::assign(const ex & value)
279 asexinfop->is_assigned = 1;
280 asexinfop->assigned_expression = value;
281 clearflag(status_flags::evaluated | status_flags::expanded);
284 void symbol::unassign(void)
286 if (asexinfop->is_assigned) {
287 asexinfop->is_assigned = 0;
288 asexinfop->assigned_expression = _ex0();
290 setflag(status_flags::evaluated | status_flags::expanded);
295 /** Symbols not constructed with a string get one assigned using this
296 * prefix and a number. */
297 std::string & symbol::autoname_prefix(void)
299 static std::string *s = new std::string("symbol");
303 /** Return default TeX name for symbol. This recognizes some greek letters. */
304 std::string symbol::default_TeX_name(void) const
306 if (name=="alpha" || name=="beta" || name=="gamma"
307 || name=="delta" || name=="epsilon" || name=="varepsilon"
308 || name=="zeta" || name=="eta" || name=="theta"
309 || name=="vartheta" || name=="iota" || name=="kappa"
310 || name=="lambda" || name=="mu" || name=="nu"
311 || name=="xi" || name=="omicron" || name=="pi"
312 || name=="varpi" || name=="rho" || name=="varrho"
313 || name=="sigma" || name=="varsigma" || name=="tau"
314 || name=="upsilon" || name=="phi" || name=="varphi"
315 || name=="chi" || name=="psi" || name=="omega"
316 || name=="Gamma" || name=="Delta" || name=="Theta"
317 || name=="Lambda" || name=="Xi" || name=="Pi"
318 || name=="Sigma" || name=="Upsilon" || name=="Phi"
319 || name=="Psi" || name=="Omega")
326 // static member variables
331 unsigned symbol::next_serial = 0;
334 // subclass assigned_ex_info
337 /** Default ctor. Defaults to unassigned. */
338 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)