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