]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
symbol: remove return_type_tinfo() and return_type() (shrink symbol by 8 bytes)
[ginac.git] / ginac / symbol.cpp
1 /** @file symbol.cpp
2  *
3  *  Implementation of GiNaC's symbolic objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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 #include "inifcns.h"
32
33 namespace GiNaC {
34
35 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symbol, basic,
36   print_func<print_context>(&symbol::do_print).
37   print_func<print_latex>(&symbol::do_print_latex).
38   print_func<print_tree>(&symbol::do_print_tree).
39   print_func<print_python_repr>(&symbol::do_print_python_repr))
40
41 //////////
42 // default constructor
43 //////////
44
45 // symbol
46
47 symbol::symbol()
48  :  serial(next_serial++), name(autoname_prefix() + ToString(serial)), TeX_name(name), domain(domain::complex)
49 {
50         setflag(status_flags::evaluated | status_flags::expanded);
51 }
52
53 // realsymbol
54
55 realsymbol::realsymbol()
56 {
57         domain = domain::real;
58 }
59
60 // possymbol
61
62 possymbol::possymbol()
63 {
64         domain = domain::positive;
65 }
66
67 //////////
68 // other constructors
69 //////////
70
71 // public
72
73 // symbol
74
75 symbol::symbol(const std::string & initname, unsigned domain) :
76         serial(next_serial++), name(initname), TeX_name(default_TeX_name()),
77         domain(domain)
78 {
79         setflag(status_flags::evaluated | status_flags::expanded);
80 }
81
82 symbol::symbol(const std::string & initname, const std::string & texname, unsigned domain) :
83         serial(next_serial++), name(initname), TeX_name(texname), domain(domain)
84 {
85         setflag(status_flags::evaluated | status_flags::expanded);
86 }
87
88 // realsymbol
89         
90 realsymbol::realsymbol(const std::string & initname, unsigned domain)
91  : symbol(initname, domain) { }
92
93 realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned domain)
94  : symbol(initname, texname, domain) { }
95
96 // possymbol
97         
98 possymbol::possymbol(const std::string & initname, unsigned domain)
99  : symbol(initname, domain) { }
100
101 possymbol::possymbol(const std::string & initname, const std::string & texname, unsigned domain)
102  : symbol(initname, texname, domain) { }
103
104 //////////
105 // archiving
106 //////////
107
108 /** Construct object from archive_node. */
109 symbol::symbol(const archive_node &n, lst &sym_lst)
110  : inherited(n, sym_lst), serial(next_serial++)
111 {
112         if (!n.find_string("name", name))
113                 name = autoname_prefix() + ToString(serial);
114         if (!n.find_string("TeXname", TeX_name))
115                 TeX_name = default_TeX_name();
116         if (!n.find_unsigned("domain", domain))
117                 domain = domain::complex;
118         setflag(status_flags::evaluated | status_flags::expanded);
119 }
120
121 /** Unarchive the object. */
122 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
123 {
124         ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
125
126         // If symbol is in sym_lst, return the existing symbol
127         for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
128                 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
129                         return *it;
130         }
131
132         // Otherwise add new symbol to list and return it
133         sym_lst.append(s);
134         return s;
135 }
136
137 /** Archive the object. */
138 void symbol::archive(archive_node &n) const
139 {
140         inherited::archive(n);
141         n.add_string("name", name);
142         if (TeX_name != default_TeX_name())
143                 n.add_string("TeX_name", TeX_name);
144         if (domain != domain::complex)
145                 n.add_unsigned("domain", domain);
146 }
147
148 //////////
149 // functions overriding virtual functions from base classes
150 //////////
151
152 // public
153
154 void symbol::do_print(const print_context & c, unsigned level) const
155 {
156         c.s << name;
157 }
158
159 void symbol::do_print_latex(const print_latex & c, unsigned level) const
160 {
161         c.s << TeX_name;
162 }
163
164 void symbol::do_print_tree(const print_tree & c, unsigned level) const
165 {
166         c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
167             << ", serial=" << serial
168             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
169             << ", domain=" << domain
170             << std::endl;
171 }
172
173 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
174 {
175         c.s << class_name() << "('" << name;
176         if (TeX_name != default_TeX_name())
177                 c.s << "','" << TeX_name;
178         c.s << "')";
179 }
180
181 bool symbol::info(unsigned inf) const
182 {
183         switch (inf) {
184                 case info_flags::symbol:
185                 case info_flags::polynomial:
186                 case info_flags::integer_polynomial: 
187                 case info_flags::cinteger_polynomial: 
188                 case info_flags::rational_polynomial: 
189                 case info_flags::crational_polynomial: 
190                 case info_flags::rational_function: 
191                 case info_flags::expanded:
192                         return true;
193                 case info_flags::real:
194                         return domain == domain::real || domain == domain::positive;
195                 case info_flags::positive:
196                 case info_flags::nonnegative:
197                         return domain == domain::positive;
198                 case info_flags::has_indices:
199                         return false;
200         }
201         return inherited::info(inf);
202 }
203
204 ex symbol::conjugate() const
205 {
206         if (this->domain == domain::complex) {
207                 return conjugate_function(*this).hold();
208         } else {
209                 return *this;
210         }
211 }
212
213 ex symbol::real_part() const
214 {
215         if (domain==domain::real || domain==domain::positive)
216                 return *this;
217         return real_part_function(*this).hold();
218 }
219
220 ex symbol::imag_part() const
221 {
222         if (domain==domain::real || domain==domain::positive)
223                 return 0;
224         return imag_part_function(*this).hold();
225 }
226
227 bool symbol::is_polynomial(const ex & var) const
228 {
229         return true;
230 }
231
232 // protected
233
234 /** Implementation of ex::diff() for single differentiation of a symbol.
235  *  It returns 1 or 0.
236  *
237  *  @see ex::diff */
238 ex symbol::derivative(const symbol & s) const
239 {
240         if (compare_same_type(s))
241                 return _ex0;
242         else
243                 return _ex1;
244 }
245
246 int symbol::compare_same_type(const basic & other) const
247 {
248         GINAC_ASSERT(is_a<symbol>(other));
249         const symbol *o = static_cast<const symbol *>(&other);
250         if (serial==o->serial) return 0;
251         return serial < o->serial ? -1 : 1;
252 }
253
254 bool symbol::is_equal_same_type(const basic & other) const
255 {
256         GINAC_ASSERT(is_a<symbol>(other));
257         const symbol *o = static_cast<const symbol *>(&other);
258         return serial==o->serial;
259 }
260
261 unsigned symbol::calchash() const
262 {
263         const void* this_tinfo = (const void*)typeid(*this).name();
264         hashvalue = golden_ratio_hash((p_int)this_tinfo ^ serial);
265         setflag(status_flags::hash_calculated);
266         return hashvalue;
267 }
268
269 //////////
270 // virtual functions which can be overridden by derived classes
271 //////////
272
273 // none
274
275 //////////
276 // non-virtual functions in this class
277 //////////
278
279 // private
280
281 /** Symbols not constructed with a string get one assigned using this
282  *  prefix and a number. */
283 std::string& symbol::autoname_prefix()
284 {
285         static std::string s("symbol");
286         return s;
287 }
288
289 /** Return default TeX name for symbol. This recognizes some greek letters. */
290 std::string symbol::default_TeX_name() const
291 {
292         if (name=="alpha"        || name=="beta"         || name=="gamma"
293          || name=="delta"        || name=="epsilon"      || name=="varepsilon"
294          || name=="zeta"         || name=="eta"          || name=="theta"
295          || name=="vartheta"     || name=="iota"         || name=="kappa"
296          || name=="lambda"       || name=="mu"           || name=="nu"
297          || name=="xi"           || name=="omicron"      || name=="pi"
298          || name=="varpi"        || name=="rho"          || name=="varrho"
299          || name=="sigma"        || name=="varsigma"     || name=="tau"
300          || name=="upsilon"      || name=="phi"          || name=="varphi"
301          || name=="chi"          || name=="psi"          || name=="omega"
302          || name=="Gamma"        || name=="Delta"        || name=="Theta"
303          || name=="Lambda"       || name=="Xi"           || name=="Pi"
304          || name=="Sigma"        || name=="Upsilon"      || name=="Phi"
305          || name=="Psi"          || name=="Omega")
306                 return "\\" + name;
307         else
308                 return name;
309 }
310
311 //////////
312 // static member variables
313 //////////
314
315 // private
316
317 unsigned symbol::next_serial = 0;
318
319 } // namespace GiNaC