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