]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
added exhashmap<> as a replacement for map<> that uses hashing
[ginac.git] / ginac / symbol.cpp
1 /** @file symbol.cpp
2  *
3  *  Implementation of GiNaC's symbolic objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <string>
24 #include <stdexcept>
25
26 #include "symbol.h"
27 #include "lst.h"
28 #include "archive.h"
29 #include "tostring.h"
30 #include "utils.h"
31
32 namespace GiNaC {
33
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))
39
40 //////////
41 // default constructor
42 //////////
43
44 symbol::symbol()
45  : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(autoname_prefix() + ToString(serial)), TeX_name(name)
46 {
47         setflag(status_flags::evaluated | status_flags::expanded);
48 }
49
50 //////////
51 // other constructors
52 //////////
53
54 // public
55
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())
58 {
59         setflag(status_flags::evaluated | status_flags::expanded);
60 }
61
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)
64 {
65         setflag(status_flags::evaluated | status_flags::expanded);
66 }
67
68 //////////
69 // archiving
70 //////////
71
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++)
75 {
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);
81 }
82
83 /** Unarchive the object. */
84 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
85 {
86         ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
87
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))
91                         return *it;
92         }
93
94         // Otherwise add new symbol to list and return it
95         sym_lst.append(s);
96         return s;
97 }
98
99 /** Archive the object. */
100 void symbol::archive(archive_node &n) const
101 {
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);
106 }
107
108 //////////
109 // functions overriding virtual functions from base classes
110 //////////
111
112 // public
113
114 void symbol::do_print(const print_context & c, unsigned level) const
115 {
116         c.s << name;
117 }
118
119 void symbol::do_print_latex(const print_latex & c, unsigned level) const
120 {
121         c.s << TeX_name;
122 }
123
124 void symbol::do_print_tree(const print_tree & c, unsigned level) const
125 {
126         c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
127             << ", serial=" << serial
128             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
129             << std::endl;
130 }
131
132 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
133 {
134         c.s << class_name() << "('" << name;
135         if (TeX_name != default_TeX_name())
136                 c.s << "','" << TeX_name;
137         c.s << "')";
138 }
139
140 bool symbol::info(unsigned inf) const
141 {
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)
149                 return true;
150         else
151                 return inherited::info(inf);
152 }
153
154 ex symbol::eval(int level) const
155 {
156         if (level == -max_recursion_level)
157                 throw(std::runtime_error("max recursion level reached"));
158         
159         if (asexinfop->is_assigned) {
160                 setflag(status_flags::evaluated);
161                 if (level==1)
162                         return (asexinfop->assigned_expression);
163                 else
164                         return (asexinfop->assigned_expression).eval(level);
165         } else {
166                 return this->hold();
167         }
168 }
169
170 // protected
171
172 /** Implementation of ex::diff() for single differentiation of a symbol.
173  *  It returns 1 or 0.
174  *
175  *  @see ex::diff */
176 ex symbol::derivative(const symbol & s) const
177 {
178         if (compare_same_type(s))
179                 return _ex0;
180         else
181                 return _ex1;
182 }
183
184 int symbol::compare_same_type(const basic & other) const
185 {
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;
190 }
191
192 bool symbol::is_equal_same_type(const basic & other) const
193 {
194         GINAC_ASSERT(is_a<symbol>(other));
195         const symbol *o = static_cast<const symbol *>(&other);
196         return serial==o->serial;
197 }
198
199 unsigned symbol::calchash() const
200 {
201         hashvalue = golden_ratio_hash(tinfo() ^ serial);
202         setflag(status_flags::hash_calculated);
203         return hashvalue;
204 }
205
206 //////////
207 // virtual functions which can be overridden by derived classes
208 //////////
209
210 // none
211
212 //////////
213 // non-virtual functions in this class
214 //////////
215
216 // public
217
218 void symbol::assign(const ex & value)
219 {
220         asexinfop->is_assigned = true;
221         asexinfop->assigned_expression = value;
222         clearflag(status_flags::evaluated | status_flags::expanded);
223 }
224
225 void symbol::unassign()
226 {
227         if (asexinfop->is_assigned) {
228                 asexinfop->is_assigned = false;
229                 asexinfop->assigned_expression = _ex0;
230         }
231         setflag(status_flags::evaluated | status_flags::expanded);
232 }
233
234 // private
235
236 /** Symbols not constructed with a string get one assigned using this
237  *  prefix and a number. */
238 std::string & symbol::autoname_prefix()
239 {
240         static std::string *s = new std::string("symbol");
241         return *s;
242 }
243
244 /** Return default TeX name for symbol. This recognizes some greek letters. */
245 std::string symbol::default_TeX_name() const
246 {
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")
261                 return "\\" + name;
262         else
263                 return name;
264 }
265
266 //////////
267 // static member variables
268 //////////
269
270 // private
271
272 unsigned symbol::next_serial = 0;
273
274 //////////
275 // subclass assigned_ex_info
276 //////////
277
278 /** Default ctor.  Defaults to unassigned. */
279 symbol::assigned_ex_info::assigned_ex_info() : is_assigned(false)
280 {
281 }
282
283 } // namespace GiNaC