]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
- files which are generated by perl scripts are made before compilation
[ginac.git] / ginac / symbol.cpp
1 /** @file symbol.cpp
2  *
3  *  Implementation of GiNaC's symbolic objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999 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 "utils.h"
29 #include "idx.h"
30 #include "debugmsg.h"
31
32 namespace GiNaC {
33
34 //////////
35 // default constructor, destructor, copy constructor assignment operator and helpers
36 //////////
37
38 symbol::symbol() : basic(TINFO_symbol)
39 {
40     debugmsg("symbol default constructor",LOGLEVEL_CONSTRUCT);
41     serial=next_serial++;
42     name=autoname_prefix()+ToString(serial);
43     asexinfop=new assigned_ex_info;
44     setflag(status_flags::evaluated);
45 }
46
47 symbol::~symbol()
48 {
49     debugmsg("symbol destructor",LOGLEVEL_DESTRUCT);
50     destroy(0);
51 }
52
53 symbol::symbol(symbol const & other)
54 {
55     debugmsg("symbol copy constructor",LOGLEVEL_CONSTRUCT);
56     copy(other);
57 }
58
59 void symbol::copy(symbol const & other)
60 {
61     basic::copy(other);
62     name=other.name;
63     serial=other.serial;
64     asexinfop=other.asexinfop;
65     ++asexinfop->refcount;
66 }
67
68 void symbol::destroy(bool call_parent)
69 {
70     if (--asexinfop->refcount == 0) {
71         delete asexinfop;
72     }
73     if (call_parent) {
74         basic::destroy(call_parent);
75     }
76 }
77
78 // how should the following be interpreted?
79 // symbol x;
80 // symbol y;
81 // x=y;
82 // probably as: x=ex(y);
83
84 //////////
85 // other constructors
86 //////////
87
88 // public
89
90 symbol::symbol(string const & initname) : basic(TINFO_symbol)
91 {
92     debugmsg("symbol constructor from string",LOGLEVEL_CONSTRUCT);
93     name=initname;
94     serial=next_serial++;
95     asexinfop=new assigned_ex_info;
96     setflag(status_flags::evaluated);
97 }
98
99 //////////
100 // functions overriding virtual functions from bases classes
101 //////////
102
103 // public
104
105 basic * symbol::duplicate() const
106 {
107     debugmsg("symbol duplicate",LOGLEVEL_DUPLICATE);
108     return new symbol(*this);
109 }
110
111 bool symbol::info(unsigned inf) const
112 {
113     if (inf==info_flags::symbol) return true;
114     if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial || inf==info_flags::rational_function) {
115         return true;
116     } else {
117         return basic::info(inf);
118     }
119 }
120
121 ex symbol::expand(unsigned options) const
122 {
123     return this->hold();
124 }
125
126 bool symbol::has(ex const & other) const
127 {
128     if (is_equal(*other.bp)) return true;
129     return false;
130 }
131
132 int symbol::degree(symbol const & s) const
133 {
134     return compare_same_type(s)==0 ? 1 : 0;
135 }
136
137 int symbol::ldegree(symbol const & s) const
138 {
139     return compare_same_type(s)==0 ? 1 : 0;
140 }
141
142 ex symbol::coeff(symbol const & s, int const n) const
143 {
144     if (compare_same_type(s)==0) {
145         return n==1 ? exONE() : exZERO();
146     } else {
147         return n==0 ? *this : exZERO();
148     }
149 }
150
151 ex symbol::eval(int level) const
152 {
153     if (level == -max_recursion_level) {
154         throw(std::runtime_error("max recursion level reached"));
155     }
156     
157     if (asexinfop->is_assigned) {
158         setflag(status_flags::evaluated);
159         if (level==1) {
160             return (asexinfop->assigned_expression);
161         } else {
162             return (asexinfop->assigned_expression).eval(level);
163         }
164     } else {
165         return this->hold();
166     }
167 }
168
169 ex symbol::subs(lst const & ls, lst const & lr) const
170 {
171     GINAC_ASSERT(ls.nops()==lr.nops());
172 #ifdef DO_GINAC_ASSERT
173     for (int i=0; i<ls.nops(); i++) {
174         GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
175                is_ex_of_type(ls.op(i),idx));
176     }
177 #endif // def DO_GINAC_ASSERT
178
179     for (int i=0; i<ls.nops(); i++) {
180         if (is_ex_exactly_of_type(ls.op(i),symbol)) {
181             if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
182         }
183     }
184     return *this;
185 }
186
187 // protected
188
189 int symbol::compare_same_type(basic const & other) const
190 {
191     GINAC_ASSERT(is_of_type(other,symbol));
192     const symbol *o = static_cast<const symbol *>(&other);
193     if (serial==o->serial) return 0;
194     return serial < o->serial ? -1 : 1;
195 }
196
197 bool symbol::is_equal_same_type(basic const & other) const
198 {
199     GINAC_ASSERT(is_of_type(other,symbol));
200     const symbol *o = static_cast<const symbol *>(&other);
201     return serial==o->serial;
202 }
203
204 unsigned symbol::return_type(void) const
205 {
206     return return_types::commutative;
207 }
208    
209 unsigned symbol::return_type_tinfo(void) const
210 {
211     return tinfo_key;
212 }
213
214 unsigned symbol::calchash(void) const
215 {
216     // return golden_ratio_hash(tinfo()) ^ serial;
217     hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
218     setflag(status_flags::hash_calculated);
219     return hashvalue;
220 }
221
222 //////////
223 // virtual functions which can be overridden by derived classes
224 //////////
225
226 // none
227
228 //////////
229 // non-virtual functions in this class
230 //////////
231
232 // public
233
234 void symbol::assign(ex const & value)
235 {
236     asexinfop->is_assigned=1;
237     asexinfop->assigned_expression=value;
238     clearflag(status_flags::evaluated);
239 }
240
241 void symbol::unassign(void)
242 {
243     if (asexinfop->is_assigned) {
244         asexinfop->is_assigned=0;
245         asexinfop->assigned_expression=exZERO();
246     }
247     setflag(status_flags::evaluated);
248 }
249
250 // private
251
252 string & symbol::autoname_prefix(void)
253 {
254     static string * s=new string("symbol");
255     return *s;
256 }
257
258 //////////
259 // static member variables
260 //////////
261
262 // private
263
264 unsigned symbol::next_serial=0;
265
266 // string const symbol::autoname_prefix="symbol";
267
268 //////////
269 // global constants
270 //////////
271
272 const symbol some_symbol;
273 type_info const & typeid_symbol=typeid(some_symbol);
274
275 //////////
276 // subclass assigned_ex_info
277 //////////
278
279 /** Default ctor.  Defaults to unassigned. */
280 symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)
281 {
282 }
283
284 } // namespace GiNaC