]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
* ginac/registrar.h: dtor is inlined now.
[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 "idx.h"
29 #include "archive.h"
30 #include "debugmsg.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         debugmsg("symbol default ctor", LOGLEVEL_CONSTRUCT);
44         name = autoname_prefix()+ToString(serial);
45         asexinfop = new assigned_ex_info;
46         setflag(status_flags::evaluated | status_flags::expanded);
47 }
48
49 /** For use by copy ctor and assignment operator. */
50 void symbol::copy(const symbol & other)
51 {
52         inherited::copy(other);
53         name = other.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         debugmsg("symbol copy ctor", LOGLEVEL_CONSTRUCT);
76         copy(other);
77 }
78
79 symbol::symbol(const std::string & initname) : inherited(TINFO_symbol)
80 {
81         debugmsg("symbol ctor from string", LOGLEVEL_CONSTRUCT);
82         name = initname;
83         serial = next_serial++;
84         asexinfop = new assigned_ex_info;
85         setflag(status_flags::evaluated | status_flags::expanded);
86 }
87
88 //////////
89 // archiving
90 //////////
91
92 /** Construct object from archive_node. */
93 symbol::symbol(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
94 {
95         debugmsg("symbol ctor from archive_node", LOGLEVEL_CONSTRUCT);
96         serial = next_serial++;
97         if (!(n.find_string("name", name)))
98                 name = autoname_prefix() + ToString(serial);
99         asexinfop = new assigned_ex_info;
100         setflag(status_flags::evaluated);
101 }
102
103 /** Unarchive the object. */
104 ex symbol::unarchive(const archive_node &n, const lst &sym_lst)
105 {
106         ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
107         
108         // If symbol is in sym_lst, return the existing symbol
109         for (unsigned i=0; i<sym_lst.nops(); i++) {
110                 if (is_ex_of_type(sym_lst.op(i), symbol) && (ex_to_symbol(sym_lst.op(i)).name == ex_to_symbol(s).name))
111                         return sym_lst.op(i);
112         }
113         return s;
114 }
115
116 /** Archive the object. */
117 void symbol::archive(archive_node &n) const
118 {
119         inherited::archive(n);
120         n.add_string("name", name);
121 }
122
123 //////////
124 // functions overriding virtual functions from bases classes
125 //////////
126
127 // public
128
129 basic *symbol::duplicate() const
130 {
131         debugmsg("symbol duplicate", LOGLEVEL_DUPLICATE);
132         return new symbol(*this);
133 }
134
135 void symbol::print(std::ostream & os, unsigned upper_precedence) const
136 {
137         debugmsg("symbol print",LOGLEVEL_PRINT);
138         os << name;
139 }
140
141 void symbol::printraw(std::ostream & os) const
142 {
143         debugmsg("symbol printraw",LOGLEVEL_PRINT);
144         os << "symbol(" << "name=" << name << ",serial=" << serial
145            << ",hash=" << hashvalue << ",flags=" << flags << ")";
146 }
147
148 void symbol::printtree(std::ostream & os, unsigned indent) const
149 {
150         debugmsg("symbol printtree",LOGLEVEL_PRINT);
151         os << std::string(indent,' ') << name << " (symbol): "
152            << "serial=" << serial
153            << ", hash=" << hashvalue
154            << " (0x" << std::hex << hashvalue << std::dec << ")"
155            << ", flags=" << flags << std::endl;
156 }
157
158 void symbol::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
159 {
160         debugmsg("symbol print csrc", LOGLEVEL_PRINT);
161         os << name;
162 }
163
164 bool symbol::info(unsigned inf) const
165 {
166         if (inf==info_flags::symbol) return true;
167         if (inf==info_flags::polynomial ||
168             inf==info_flags::integer_polynomial ||
169             inf==info_flags::cinteger_polynomial ||
170             inf==info_flags::rational_polynomial ||
171             inf==info_flags::crational_polynomial ||
172             inf==info_flags::rational_function)
173                 return true;
174         else
175                 return inherited::info(inf);
176 }
177
178 ex symbol::expand(unsigned options) const
179 {
180         return this->hold();
181 }
182
183 bool symbol::has(const ex & other) const
184 {
185         if (this->is_equal(*other.bp))
186                 return true;
187         else
188                 return false;
189 }
190
191 int symbol::degree(const symbol & s) const
192 {
193         return compare_same_type(s)==0 ? 1 : 0;
194 }
195
196 int symbol::ldegree(const symbol & s) const
197 {
198         return compare_same_type(s)==0 ? 1 : 0;
199 }
200
201 ex symbol::coeff(const symbol & s, int n) const
202 {
203         if (compare_same_type(s)==0)
204                 return n==1 ? _ex1() : _ex0();
205         else
206                 return n==0 ? *this : _ex0();
207 }
208
209 ex symbol::eval(int level) const
210 {
211         if (level == -max_recursion_level)
212                 throw(std::runtime_error("max recursion level reached"));
213         
214         if (asexinfop->is_assigned) {
215                 setflag(status_flags::evaluated);
216                 if (level==1)
217                         return (asexinfop->assigned_expression);
218                 else
219                         return (asexinfop->assigned_expression).eval(level);
220         } else {
221                 return this->hold();
222         }
223 }
224
225 ex symbol::subs(const lst & ls, const lst & lr) const
226 {
227         GINAC_ASSERT(ls.nops()==lr.nops());
228 #ifdef DO_GINAC_ASSERT
229         for (unsigned i=0; i<ls.nops(); i++)
230                 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
231                              is_ex_of_type(ls.op(i),idx));
232 #endif // def DO_GINAC_ASSERT
233
234         for (unsigned i=0; i<ls.nops(); i++) {
235                 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
236                         if (compare_same_type(ex_to_symbol(ls.op(i)))==0)
237                                 return lr.op(i);
238                 }
239         }
240         return *this;
241 }
242
243 // protected
244
245 /** Implementation of ex::diff() for single differentiation of a symbol.
246  *  It returns 1 or 0.
247  *
248  *  @see ex::diff */
249 ex symbol::derivative(const symbol & s) const
250 {
251         if (compare_same_type(s))
252                 return _ex0();
253         else
254                 return _ex1();
255 }
256
257 int symbol::compare_same_type(const basic & other) const
258 {
259         GINAC_ASSERT(is_of_type(other,symbol));
260         const symbol *o = static_cast<const symbol *>(&other);
261         if (serial==o->serial) return 0;
262         return serial < o->serial ? -1 : 1;
263 }
264
265 bool symbol::is_equal_same_type(const basic & other) const
266 {
267         GINAC_ASSERT(is_of_type(other,symbol));
268         const symbol *o = static_cast<const symbol *>(&other);
269         return serial==o->serial;
270 }
271
272 unsigned symbol::return_type(void) const
273 {
274         return return_types::commutative;
275 }
276    
277 unsigned symbol::return_type_tinfo(void) const
278 {
279         return tinfo_key;
280 }
281
282 unsigned symbol::calchash(void) const
283 {
284         // this is where the schoolbook method
285         // (golden_ratio_hash(tinfo()) ^ serial)
286         // is not good enough yet...
287         hashvalue = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
288         setflag(status_flags::hash_calculated);
289         return hashvalue;
290 }
291
292 //////////
293 // virtual functions which can be overridden by derived classes
294 //////////
295
296 // none
297
298 //////////
299 // non-virtual functions in this class
300 //////////
301
302 // public
303
304 void symbol::assign(const ex & value)
305 {
306         asexinfop->is_assigned = 1;
307         asexinfop->assigned_expression = value;
308         clearflag(status_flags::evaluated | status_flags::expanded);
309 }
310
311 void symbol::unassign(void)
312 {
313         if (asexinfop->is_assigned) {
314                 asexinfop->is_assigned = 0;
315                 asexinfop->assigned_expression = _ex0();
316         }
317         setflag(status_flags::evaluated | status_flags::expanded);
318 }
319
320 // private
321
322 /** Symbols not constructed with a string get one assigned using this
323  *  prefix and a number. */
324 std::string & symbol::autoname_prefix(void)
325 {
326         static std::string *s = new std::string("symbol");
327         return *s;
328 }
329
330 //////////
331 // static member variables
332 //////////
333
334 // private
335
336 unsigned symbol::next_serial = 0;
337
338 //////////
339 // subclass assigned_ex_info
340 //////////
341
342 /** Default ctor.  Defaults to unassigned. */
343 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)
344 {
345 }
346
347 } // namespace GiNaC