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