3 * Implementation of GiNaC's symbolic objects. */
6 * GiNaC Copyright (C) 1999-2003 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(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 std::string & initname) : inherited(TINFO_symbol)
76 TeX_name = default_TeX_name();
77 serial = next_serial++;
78 asexinfop = new assigned_ex_info;
79 setflag(status_flags::evaluated | status_flags::expanded);
82 symbol::symbol(const std::string & initname, const std::string & texname) : inherited(TINFO_symbol)
86 serial = next_serial++;
87 asexinfop = new assigned_ex_info;
88 setflag(status_flags::evaluated | status_flags::expanded);
95 /** Construct object from archive_node. */
96 symbol::symbol(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
98 serial = next_serial++;
99 if (!(n.find_string("name", name)))
100 name = autoname_prefix() + ToString(serial);
101 if (!(n.find_string("TeXname", TeX_name)))
102 TeX_name = default_TeX_name();
103 asexinfop = new assigned_ex_info;
104 setflag(status_flags::evaluated);
107 /** Unarchive the object. */
108 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
110 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
112 // If symbol is in sym_lst, return the existing symbol
113 for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
114 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
118 // Otherwise add new symbol to list and return it
123 /** Archive the object. */
124 void symbol::archive(archive_node &n) const
126 inherited::archive(n);
127 n.add_string("name", name);
128 if (TeX_name != default_TeX_name())
129 n.add_string("TeX_name", TeX_name);
133 // functions overriding virtual functions from base classes
138 void symbol::print(const print_context & c, unsigned level) const
140 if (is_a<print_tree>(c)) {
142 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
143 << ", serial=" << serial
144 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
147 } else if (is_a<print_latex>(c)) {
149 } else if (is_a<print_python_repr>(c)) {
150 c.s << class_name() << "('" << name;
151 if (TeX_name != default_TeX_name())
152 c.s << "','" << TeX_name;
158 bool symbol::info(unsigned inf) const
160 if (inf==info_flags::symbol) return true;
161 if (inf==info_flags::polynomial ||
162 inf==info_flags::integer_polynomial ||
163 inf==info_flags::cinteger_polynomial ||
164 inf==info_flags::rational_polynomial ||
165 inf==info_flags::crational_polynomial ||
166 inf==info_flags::rational_function)
169 return inherited::info(inf);
172 ex symbol::eval(int level) const
174 if (level == -max_recursion_level)
175 throw(std::runtime_error("max recursion level reached"));
177 if (asexinfop->is_assigned) {
178 setflag(status_flags::evaluated);
180 return (asexinfop->assigned_expression);
182 return (asexinfop->assigned_expression).eval(level);
190 /** Implementation of ex::diff() for single differentiation of a symbol.
194 ex symbol::derivative(const symbol & s) const
196 if (compare_same_type(s))
202 int symbol::compare_same_type(const basic & other) const
204 GINAC_ASSERT(is_a<symbol>(other));
205 const symbol *o = static_cast<const symbol *>(&other);
206 if (serial==o->serial) return 0;
207 return serial < o->serial ? -1 : 1;
210 bool symbol::is_equal_same_type(const basic & other) const
212 GINAC_ASSERT(is_a<symbol>(other));
213 const symbol *o = static_cast<const symbol *>(&other);
214 return serial==o->serial;
217 unsigned symbol::calchash(void) const
219 hashvalue = golden_ratio_hash(tinfo() ^ serial);
220 setflag(status_flags::hash_calculated);
225 // virtual functions which can be overridden by derived classes
231 // non-virtual functions in this class
236 void symbol::assign(const ex & value)
238 asexinfop->is_assigned = true;
239 asexinfop->assigned_expression = value;
240 clearflag(status_flags::evaluated | status_flags::expanded);
243 void symbol::unassign(void)
245 if (asexinfop->is_assigned) {
246 asexinfop->is_assigned = false;
247 asexinfop->assigned_expression = _ex0;
249 setflag(status_flags::evaluated | status_flags::expanded);
254 /** Symbols not constructed with a string get one assigned using this
255 * prefix and a number. */
256 std::string & symbol::autoname_prefix(void)
258 static std::string *s = new std::string("symbol");
262 /** Return default TeX name for symbol. This recognizes some greek letters. */
263 std::string symbol::default_TeX_name(void) const
265 if (name=="alpha" || name=="beta" || name=="gamma"
266 || name=="delta" || name=="epsilon" || name=="varepsilon"
267 || name=="zeta" || name=="eta" || name=="theta"
268 || name=="vartheta" || name=="iota" || name=="kappa"
269 || name=="lambda" || name=="mu" || name=="nu"
270 || name=="xi" || name=="omicron" || name=="pi"
271 || name=="varpi" || name=="rho" || name=="varrho"
272 || name=="sigma" || name=="varsigma" || name=="tau"
273 || name=="upsilon" || name=="phi" || name=="varphi"
274 || name=="chi" || name=="psi" || name=="omega"
275 || name=="Gamma" || name=="Delta" || name=="Theta"
276 || name=="Lambda" || name=="Xi" || name=="Pi"
277 || name=="Sigma" || name=="Upsilon" || name=="Phi"
278 || name=="Psi" || name=="Omega")
285 // static member variables
290 unsigned symbol::next_serial = 0;
293 // subclass assigned_ex_info
296 /** Default ctor. Defaults to unassigned. */
297 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(false), refcount(1)