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