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
34 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symbol, basic,
35 print_func<print_context>(&symbol::do_print).
36 print_func<print_latex>(&symbol::do_print_latex).
37 print_func<print_tree>(&symbol::do_print_tree).
38 print_func<print_python_repr>(&symbol::do_print_python_repr))
41 // default constructor
45 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(autoname_prefix() + ToString(serial)), TeX_name(name)
47 setflag(status_flags::evaluated | status_flags::expanded);
56 symbol::symbol(const std::string & initname)
57 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(default_TeX_name())
59 setflag(status_flags::evaluated | status_flags::expanded);
62 symbol::symbol(const std::string & initname, const std::string & texname)
63 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname)
65 setflag(status_flags::evaluated | status_flags::expanded);
72 /** Construct object from archive_node. */
73 symbol::symbol(const archive_node &n, lst &sym_lst)
74 : inherited(n, sym_lst), asexinfop(new assigned_ex_info), serial(next_serial++)
76 if (!(n.find_string("name", name)))
77 name = autoname_prefix() + ToString(serial);
78 if (!(n.find_string("TeXname", TeX_name)))
79 TeX_name = default_TeX_name();
80 setflag(status_flags::evaluated);
83 /** Unarchive the object. */
84 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
86 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
88 // If symbol is in sym_lst, return the existing symbol
89 for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
90 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
94 // Otherwise add new symbol to list and return it
99 /** Archive the object. */
100 void symbol::archive(archive_node &n) const
102 inherited::archive(n);
103 n.add_string("name", name);
104 if (TeX_name != default_TeX_name())
105 n.add_string("TeX_name", TeX_name);
109 // functions overriding virtual functions from base classes
114 void symbol::do_print(const print_context & c, unsigned level) const
119 void symbol::do_print_latex(const print_latex & c, unsigned level) const
124 void symbol::do_print_tree(const print_tree & c, unsigned level) const
126 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
127 << ", serial=" << serial
128 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
132 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
134 c.s << class_name() << "('" << name;
135 if (TeX_name != default_TeX_name())
136 c.s << "','" << TeX_name;
140 bool symbol::info(unsigned inf) const
142 if (inf==info_flags::symbol) return true;
143 if (inf==info_flags::polynomial ||
144 inf==info_flags::integer_polynomial ||
145 inf==info_flags::cinteger_polynomial ||
146 inf==info_flags::rational_polynomial ||
147 inf==info_flags::crational_polynomial ||
148 inf==info_flags::rational_function)
151 return inherited::info(inf);
154 ex symbol::eval(int level) const
156 if (level == -max_recursion_level)
157 throw(std::runtime_error("max recursion level reached"));
159 if (asexinfop->is_assigned) {
160 setflag(status_flags::evaluated);
162 return (asexinfop->assigned_expression);
164 return (asexinfop->assigned_expression).eval(level);
172 /** Implementation of ex::diff() for single differentiation of a symbol.
176 ex symbol::derivative(const symbol & s) const
178 if (compare_same_type(s))
184 int symbol::compare_same_type(const basic & other) const
186 GINAC_ASSERT(is_a<symbol>(other));
187 const symbol *o = static_cast<const symbol *>(&other);
188 if (serial==o->serial) return 0;
189 return serial < o->serial ? -1 : 1;
192 bool symbol::is_equal_same_type(const basic & other) const
194 GINAC_ASSERT(is_a<symbol>(other));
195 const symbol *o = static_cast<const symbol *>(&other);
196 return serial==o->serial;
199 unsigned symbol::calchash() const
201 hashvalue = golden_ratio_hash(tinfo() ^ serial);
202 setflag(status_flags::hash_calculated);
207 // virtual functions which can be overridden by derived classes
213 // non-virtual functions in this class
218 void symbol::assign(const ex & value)
220 asexinfop->is_assigned = true;
221 asexinfop->assigned_expression = value;
222 clearflag(status_flags::evaluated | status_flags::expanded);
225 void symbol::unassign()
227 if (asexinfop->is_assigned) {
228 asexinfop->is_assigned = false;
229 asexinfop->assigned_expression = _ex0;
231 setflag(status_flags::evaluated | status_flags::expanded);
236 /** Symbols not constructed with a string get one assigned using this
237 * prefix and a number. */
238 std::string & symbol::autoname_prefix()
240 static std::string *s = new std::string("symbol");
244 /** Return default TeX name for symbol. This recognizes some greek letters. */
245 std::string symbol::default_TeX_name() const
247 if (name=="alpha" || name=="beta" || name=="gamma"
248 || name=="delta" || name=="epsilon" || name=="varepsilon"
249 || name=="zeta" || name=="eta" || name=="theta"
250 || name=="vartheta" || name=="iota" || name=="kappa"
251 || name=="lambda" || name=="mu" || name=="nu"
252 || name=="xi" || name=="omicron" || name=="pi"
253 || name=="varpi" || name=="rho" || name=="varrho"
254 || name=="sigma" || name=="varsigma" || name=="tau"
255 || name=="upsilon" || name=="phi" || name=="varphi"
256 || name=="chi" || name=="psi" || name=="omega"
257 || name=="Gamma" || name=="Delta" || name=="Theta"
258 || name=="Lambda" || name=="Xi" || name=="Pi"
259 || name=="Sigma" || name=="Upsilon" || name=="Phi"
260 || name=="Psi" || name=="Omega")
267 // static member variables
272 unsigned symbol::next_serial = 0;
275 // subclass assigned_ex_info
278 /** Default ctor. Defaults to unassigned. */
279 symbol::assigned_ex_info::assigned_ex_info() : is_assigned(false)