]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
symbols can be made noncommutative (see symbol constructors); some feedback
[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), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol)
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()), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol)
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), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol)
64 {
65         setflag(status_flags::evaluated | status_flags::expanded);
66 }
67
68 symbol::symbol(const std::string & initname, unsigned rt, unsigned rtt)
69  : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(default_TeX_name()), ret_type(rt), ret_type_tinfo(rtt)
70 {
71         setflag(status_flags::evaluated | status_flags::expanded);
72 }
73
74 symbol::symbol(const std::string & initname, const std::string & texname, unsigned rt, unsigned rtt)
75  : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname), ret_type(rt), ret_type_tinfo(rtt)
76 {
77         setflag(status_flags::evaluated | status_flags::expanded);
78 }
79
80 //////////
81 // archiving
82 //////////
83
84 /** Construct object from archive_node. */
85 symbol::symbol(const archive_node &n, lst &sym_lst)
86  : inherited(n, sym_lst), asexinfop(new assigned_ex_info), serial(next_serial++), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol)
87 {
88         if (!(n.find_string("name", name)))
89                 name = autoname_prefix() + ToString(serial);
90         if (!(n.find_string("TeXname", TeX_name)))
91                 TeX_name = default_TeX_name();
92         if (!n.find_unsigned("return_type", ret_type))
93                 ret_type = return_types::commutative;
94         if (!n.find_unsigned("return_type_tinfo", ret_type_tinfo))
95                 ret_type_tinfo = TINFO_symbol;
96         setflag(status_flags::evaluated | status_flags::expanded);
97 }
98
99 /** Unarchive the object. */
100 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
101 {
102         ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
103
104         // If symbol is in sym_lst, return the existing symbol
105         for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
106                 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
107                         return *it;
108         }
109
110         // Otherwise add new symbol to list and return it
111         sym_lst.append(s);
112         return s;
113 }
114
115 /** Archive the object. */
116 void symbol::archive(archive_node &n) const
117 {
118         inherited::archive(n);
119         n.add_string("name", name);
120         if (TeX_name != default_TeX_name())
121                 n.add_string("TeX_name", TeX_name);
122         if (ret_type != return_types::commutative)
123                 n.add_unsigned("return_type", ret_type);
124         if (ret_type_tinfo != TINFO_symbol)
125                 n.add_unsigned("return_type_tinfo", ret_type_tinfo);
126 }
127
128 //////////
129 // functions overriding virtual functions from base classes
130 //////////
131
132 // public
133
134 void symbol::do_print(const print_context & c, unsigned level) const
135 {
136         c.s << name;
137 }
138
139 void symbol::do_print_latex(const print_latex & c, unsigned level) const
140 {
141         c.s << TeX_name;
142 }
143
144 void symbol::do_print_tree(const print_tree & c, unsigned level) const
145 {
146         c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
147             << ", serial=" << serial
148             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
149             << std::endl;
150 }
151
152 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
153 {
154         c.s << class_name() << "('" << name;
155         if (TeX_name != default_TeX_name())
156                 c.s << "','" << TeX_name;
157         c.s << "')";
158 }
159
160 bool symbol::info(unsigned inf) const
161 {
162         if (inf==info_flags::symbol) return true;
163         if (inf==info_flags::polynomial ||
164             inf==info_flags::integer_polynomial ||
165             inf==info_flags::cinteger_polynomial ||
166             inf==info_flags::rational_polynomial ||
167             inf==info_flags::crational_polynomial ||
168             inf==info_flags::rational_function)
169                 return true;
170         else
171                 return inherited::info(inf);
172 }
173
174 ex symbol::eval(int level) const
175 {
176         if (level == -max_recursion_level)
177                 throw(std::runtime_error("max recursion level reached"));
178         
179         if (asexinfop->is_assigned) {
180                 setflag(status_flags::evaluated);
181                 if (level==1)
182                         return (asexinfop->assigned_expression);
183                 else
184                         return (asexinfop->assigned_expression).eval(level);
185         } else {
186                 return this->hold();
187         }
188 }
189
190 // protected
191
192 /** Implementation of ex::diff() for single differentiation of a symbol.
193  *  It returns 1 or 0.
194  *
195  *  @see ex::diff */
196 ex symbol::derivative(const symbol & s) const
197 {
198         if (compare_same_type(s))
199                 return _ex0;
200         else
201                 return _ex1;
202 }
203
204 int symbol::compare_same_type(const basic & other) const
205 {
206         GINAC_ASSERT(is_a<symbol>(other));
207         const symbol *o = static_cast<const symbol *>(&other);
208         if (serial==o->serial) return 0;
209         return serial < o->serial ? -1 : 1;
210 }
211
212 bool symbol::is_equal_same_type(const basic & other) const
213 {
214         GINAC_ASSERT(is_a<symbol>(other));
215         const symbol *o = static_cast<const symbol *>(&other);
216         return serial==o->serial;
217 }
218
219 unsigned symbol::calchash() const
220 {
221         hashvalue = golden_ratio_hash(tinfo() ^ serial);
222         setflag(status_flags::hash_calculated);
223         return hashvalue;
224 }
225
226 //////////
227 // virtual functions which can be overridden by derived classes
228 //////////
229
230 // none
231
232 //////////
233 // non-virtual functions in this class
234 //////////
235
236 // public
237
238 void symbol::assign(const ex & value)
239 {
240         asexinfop->is_assigned = true;
241         asexinfop->assigned_expression = value;
242         clearflag(status_flags::evaluated | status_flags::expanded);
243 }
244
245 void symbol::unassign()
246 {
247         if (asexinfop->is_assigned) {
248                 asexinfop->is_assigned = false;
249                 asexinfop->assigned_expression = _ex0;
250         }
251         setflag(status_flags::evaluated | status_flags::expanded);
252 }
253
254 // private
255
256 /** Symbols not constructed with a string get one assigned using this
257  *  prefix and a number. */
258 std::string & symbol::autoname_prefix()
259 {
260         static std::string *s = new std::string("symbol");
261         return *s;
262 }
263
264 /** Return default TeX name for symbol. This recognizes some greek letters. */
265 std::string symbol::default_TeX_name() const
266 {
267         if (name=="alpha"        || name=="beta"         || name=="gamma"
268          || name=="delta"        || name=="epsilon"      || name=="varepsilon"
269          || name=="zeta"         || name=="eta"          || name=="theta"
270          || name=="vartheta"     || name=="iota"         || name=="kappa"
271          || name=="lambda"       || name=="mu"           || name=="nu"
272          || name=="xi"           || name=="omicron"      || name=="pi"
273          || name=="varpi"        || name=="rho"          || name=="varrho"
274          || name=="sigma"        || name=="varsigma"     || name=="tau"
275          || name=="upsilon"      || name=="phi"          || name=="varphi"
276          || name=="chi"          || name=="psi"          || name=="omega"
277          || name=="Gamma"        || name=="Delta"        || name=="Theta"
278          || name=="Lambda"       || name=="Xi"           || name=="Pi"
279          || name=="Sigma"        || name=="Upsilon"      || name=="Phi"
280          || name=="Psi"          || name=="Omega")
281                 return "\\" + name;
282         else
283                 return name;
284 }
285
286 //////////
287 // static member variables
288 //////////
289
290 // private
291
292 unsigned symbol::next_serial = 0;
293
294 //////////
295 // subclass assigned_ex_info
296 //////////
297
298 /** Default ctor.  Defaults to unassigned. */
299 symbol::assigned_ex_info::assigned_ex_info() : is_assigned(false)
300 {
301 }
302
303 } // namespace GiNaC