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