]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
- Duh, we had some style discussion again...
[ginac.git] / ginac / symbol.cpp
1 /** @file symbol.cpp
2  *
3  *  Implementation of GiNaC's symbolic objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 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 #ifndef NO_NAMESPACE_GINAC
34 namespace GiNaC {
35 #endif // ndef NO_NAMESPACE_GINAC
36
37 GINAC_IMPLEMENT_REGISTERED_CLASS(symbol, basic)
38
39 //////////
40 // default constructor, destructor, copy constructor assignment operator and helpers
41 //////////
42
43 symbol::symbol() : inherited(TINFO_symbol)
44 {
45         debugmsg("symbol default constructor", LOGLEVEL_CONSTRUCT);
46         serial = next_serial++;
47         name = autoname_prefix()+ToString(serial);
48         asexinfop = new assigned_ex_info;
49         setflag(status_flags::evaluated);
50 }
51
52 symbol::~symbol()
53 {
54         debugmsg("symbol destructor", LOGLEVEL_DESTRUCT);
55         destroy(0);
56 }
57
58 symbol::symbol(const symbol & other)
59 {
60         debugmsg("symbol copy constructor", LOGLEVEL_CONSTRUCT);
61         copy(other);
62 }
63
64 void symbol::copy(const symbol & other)
65 {
66         inherited::copy(other);
67         name = other.name;
68         serial = other.serial;
69         asexinfop = other.asexinfop;
70         ++asexinfop->refcount;
71 }
72
73 void symbol::destroy(bool call_parent)
74 {
75         if (--asexinfop->refcount == 0) {
76                 delete asexinfop;
77         }
78         if (call_parent) {
79                 inherited::destroy(call_parent);
80         }
81 }
82
83 // how should the following be interpreted?
84 // symbol x;
85 // symbol y;
86 // x=y;
87 // probably as: x=ex(y);
88
89 //////////
90 // other constructors
91 //////////
92
93 // public
94
95 symbol::symbol(const std::string & initname) : inherited(TINFO_symbol)
96 {
97         debugmsg("symbol constructor from string", LOGLEVEL_CONSTRUCT);
98         name = initname;
99         serial = next_serial++;
100         asexinfop = new assigned_ex_info;
101         setflag(status_flags::evaluated);
102 }
103
104 //////////
105 // archiving
106 //////////
107
108 /** Construct object from archive_node. */
109 symbol::symbol(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
110 {
111         debugmsg("symbol constructor from archive_node", LOGLEVEL_CONSTRUCT);
112         serial = next_serial++;
113         if (!(n.find_string("name", name)))
114                 name = autoname_prefix() + ToString(serial);
115         asexinfop = new assigned_ex_info;
116         setflag(status_flags::evaluated);
117 }
118
119 /** Unarchive the object. */
120 ex symbol::unarchive(const archive_node &n, const lst &sym_lst)
121 {
122         ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
123
124         // If symbol is in sym_lst, return the existing symbol
125         for (unsigned i=0; i<sym_lst.nops(); i++) {
126                 if (is_ex_of_type(sym_lst.op(i), symbol) && (ex_to_symbol(sym_lst.op(i)).name == ex_to_symbol(s).name))
127                         return sym_lst.op(i);
128         }
129         return s;
130 }
131
132 /** Archive the object. */
133 void symbol::archive(archive_node &n) const
134 {
135         inherited::archive(n);
136         n.add_string("name", name);
137 }
138
139 //////////
140 // functions overriding virtual functions from bases classes
141 //////////
142
143 // public
144
145 basic *symbol::duplicate() const
146 {
147         debugmsg("symbol duplicate", LOGLEVEL_DUPLICATE);
148         return new symbol(*this);
149 }
150
151 void symbol::print(std::ostream & os, unsigned upper_precedence) const
152 {
153         debugmsg("symbol print",LOGLEVEL_PRINT);
154         os << name;
155 }
156
157 void symbol::printraw(std::ostream & os) const
158 {
159         debugmsg("symbol printraw",LOGLEVEL_PRINT);
160         os << "symbol(" << "name=" << name << ",serial=" << serial
161            << ",hash=" << hashvalue << ",flags=" << flags << ")";
162 }
163
164 void symbol::printtree(std::ostream & os, unsigned indent) const
165 {
166         debugmsg("symbol printtree",LOGLEVEL_PRINT);
167         os << std::string(indent,' ') << name << " (symbol): "
168            << "serial=" << serial
169            << ", hash=" << hashvalue
170            << " (0x" << std::hex << hashvalue << std::dec << ")"
171            << ", flags=" << flags << std::endl;
172 }
173
174 void symbol::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
175 {
176         debugmsg("symbol print csrc", LOGLEVEL_PRINT);
177         os << name;
178 }
179
180 bool symbol::info(unsigned inf) const
181 {
182         if (inf==info_flags::symbol) return true;
183         if (inf==info_flags::polynomial ||
184             inf==info_flags::integer_polynomial ||
185             inf==info_flags::cinteger_polynomial ||
186             inf==info_flags::rational_polynomial ||
187             inf==info_flags::crational_polynomial ||
188             inf==info_flags::rational_function) {
189                 return true;
190         } else {
191                 return inherited::info(inf);
192         }
193 }
194
195 ex symbol::expand(unsigned options) const
196 {
197         return this->hold();
198 }
199
200 bool symbol::has(const ex & other) const
201 {
202         if (is_equal(*other.bp)) return true;
203         return false;
204 }
205
206 int symbol::degree(const symbol & s) const
207 {
208         return compare_same_type(s)==0 ? 1 : 0;
209 }
210
211 int symbol::ldegree(const symbol & s) const
212 {
213         return compare_same_type(s)==0 ? 1 : 0;
214 }
215
216 ex symbol::coeff(const symbol & s, int n) const
217 {
218         if (compare_same_type(s)==0) {
219                 return n==1 ? _ex1() : _ex0();
220         } else {
221                 return n==0 ? *this : _ex0();
222         }
223 }
224
225 ex symbol::eval(int level) const
226 {
227         if (level == -max_recursion_level) {
228                 throw(std::runtime_error("max recursion level reached"));
229         }
230         
231         if (asexinfop->is_assigned) {
232                 setflag(status_flags::evaluated);
233                 if (level==1) {
234                         return (asexinfop->assigned_expression);
235                 } else {
236                         return (asexinfop->assigned_expression).eval(level);
237                 }
238         } else {
239                 return this->hold();
240         }
241 }
242
243 ex symbol::subs(const lst & ls, const lst & lr) const
244 {
245         GINAC_ASSERT(ls.nops()==lr.nops());
246 #ifdef DO_GINAC_ASSERT
247         for (unsigned i=0; i<ls.nops(); i++) {
248                 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
249                            is_ex_of_type(ls.op(i),idx));
250         }
251 #endif // def DO_GINAC_ASSERT
252
253         for (unsigned i=0; i<ls.nops(); i++) {
254                 if (is_ex_exactly_of_type(ls.op(i),symbol)) {
255                         if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
256                 }
257         }
258         return *this;
259 }
260
261 // protected
262
263 /** Implementation of ex::diff() for single differentiation of a symbol.
264  *  It returns 1 or 0.
265  *
266  *  @see ex::diff */
267 ex symbol::derivative(const symbol & s) const
268 {
269         if (compare_same_type(s)) {
270                 return _ex0();
271         } else {
272                 return _ex1();
273         }
274 }
275
276 int symbol::compare_same_type(const basic & other) const
277 {
278         GINAC_ASSERT(is_of_type(other,symbol));
279         const symbol *o = static_cast<const symbol *>(&other);
280         if (serial==o->serial) return 0;
281         return serial < o->serial ? -1 : 1;
282 }
283
284 bool symbol::is_equal_same_type(const basic & other) const
285 {
286         GINAC_ASSERT(is_of_type(other,symbol));
287         const symbol *o = static_cast<const symbol *>(&other);
288         return serial==o->serial;
289 }
290
291 unsigned symbol::return_type(void) const
292 {
293         return return_types::commutative;
294 }
295    
296 unsigned symbol::return_type_tinfo(void) const
297 {
298         return tinfo_key;
299 }
300
301 unsigned symbol::calchash(void) const
302 {
303         // return golden_ratio_hash(tinfo()) ^ serial;
304         hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
305         setflag(status_flags::hash_calculated);
306         return hashvalue;
307 }
308
309 //////////
310 // virtual functions which can be overridden by derived classes
311 //////////
312
313 // none
314
315 //////////
316 // non-virtual functions in this class
317 //////////
318
319 // public
320
321 void symbol::assign(const ex & value)
322 {
323         asexinfop->is_assigned=1;
324         asexinfop->assigned_expression=value;
325         clearflag(status_flags::evaluated);
326 }
327
328 void symbol::unassign(void)
329 {
330         if (asexinfop->is_assigned) {
331                 asexinfop->is_assigned=0;
332                 asexinfop->assigned_expression=_ex0();
333         }
334         setflag(status_flags::evaluated);
335 }
336
337 // private
338
339 std::string & symbol::autoname_prefix(void)
340 {
341         static std::string * s=new std::string("symbol");
342         return *s;
343 }
344
345 //////////
346 // static member variables
347 //////////
348
349 // private
350
351 unsigned symbol::next_serial=0;
352
353 // std::string const symbol::autoname_prefix="symbol";
354
355 //////////
356 // global constants
357 //////////
358
359 const symbol some_symbol;
360 const std::type_info & typeid_symbol = typeid(some_symbol);
361
362 //////////
363 // subclass assigned_ex_info
364 //////////
365
366 /** Default ctor.  Defaults to unassigned. */
367 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)
368 {
369 }
370
371 #ifndef NO_NAMESPACE_GINAC
372 } // namespace GiNaC
373 #endif // ndef NO_NAMESPACE_GINAC