]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
7afc35beb749b404a6b078ceb2051cccf0ce836b
[ginac.git] / ginac / constant.cpp
1 /** @file constant.cpp
2  *
3  *  Implementation of GiNaC's constant types and some special constants. */
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 "constant.h"
27 #include "numeric.h"
28 #include "ex.h"
29 #include "print.h"
30 #include "archive.h"
31 #include "debugmsg.h"
32 #include "utils.h"
33
34 namespace GiNaC {
35
36 GINAC_IMPLEMENT_REGISTERED_CLASS(constant, basic)
37
38 //////////
39 // default ctor, dtor, copy ctor assignment operator and helpers
40 //////////
41
42 // public
43
44 constant::constant() : basic(TINFO_constant), name(""), ef(0), number(0), serial(next_serial++)
45 {
46         debugmsg("constant default ctor",LOGLEVEL_CONSTRUCT);
47 }
48
49 // protected
50
51 /** For use by copy ctor and assignment operator. */
52 void constant::copy(const constant & other)
53 {
54         inherited::copy(other);
55         name = other.name;
56         serial = other.serial;
57         ef = other.ef;
58         if (other.number != 0)
59                 number = new numeric(*other.number);
60         else
61                 number = 0;
62 }
63
64 void constant::destroy(bool call_parent)
65 {
66         delete number;
67         if (call_parent)
68                 inherited::destroy(call_parent);
69 }
70
71 //////////
72 // other ctors
73 //////////
74
75 // public
76
77 constant::constant(const std::string & initname, evalffunctype efun)
78   : basic(TINFO_constant), name(initname), ef(efun), number(0), serial(next_serial++)
79 {
80         debugmsg("constant ctor from string, function",LOGLEVEL_CONSTRUCT);
81         setflag(status_flags::evaluated);
82 }
83
84 constant::constant(const std::string & initname, const numeric & initnumber)
85   : basic(TINFO_constant), name(initname), ef(0), number(new numeric(initnumber)), serial(next_serial++)
86 {
87         debugmsg("constant ctor from string, numeric",LOGLEVEL_CONSTRUCT);
88         setflag(status_flags::evaluated);
89 }
90
91 //////////
92 // archiving
93 //////////
94
95 constant::constant(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
96 {
97         debugmsg("constant ctor from archive_node", LOGLEVEL_CONSTRUCT);
98 }
99
100 ex constant::unarchive(const archive_node &n, const lst &sym_lst)
101 {
102         // Find constant by name (!! this is bad: 'twould be better if there
103         // was a list of all global constants that we could search)
104         std::string s;
105         if (n.find_string("name", s)) {
106                 if (s == Pi.name)
107                         return Pi;
108                 else if (s == Catalan.name)
109                         return Catalan;
110                 else if (s == Euler.name)
111                         return Euler;
112                 else
113                         throw (std::runtime_error("unknown constant '" + s + "' in archive"));
114         } else
115                 throw (std::runtime_error("unnamed constant in archive"));
116 }
117
118 void constant::archive(archive_node &n) const
119 {
120         inherited::archive(n);
121         n.add_string("name", name);
122 }
123
124 //////////
125 // functions overriding virtual functions from bases classes
126 //////////
127
128 // public
129
130 void constant::print(const print_context & c, unsigned level) const
131 {
132         debugmsg("constant print", LOGLEVEL_PRINT);
133
134         if (is_of_type(c, print_tree)) {
135
136                 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
137                     << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
138                     << std::endl;
139
140         } else
141                 c.s << name;
142 }
143
144 int constant::degree(const ex & s) const
145 {
146         return is_equal(*s.bp) ? 1 : 0;
147 }
148
149 int constant::ldegree(const ex & s) const
150 {
151         return is_equal(*s.bp) ? 1 : 0;
152 }
153
154 ex constant::coeff(const ex & s, int n) const
155 {
156         if (is_equal(*s.bp))
157                 return n==1 ? _ex1() : _ex0();
158         else
159                 return n==0 ? *this : _ex0();
160 }
161
162 ex constant::evalf(int level) const
163 {
164         if (ef!=0) {
165                 return ef();
166         } else if (number != 0) {
167                 return number->evalf();
168         }
169         return *this;
170 }
171
172 // protected
173
174 /** Implementation of ex::diff() for a constant. It always returns 0.
175  *
176  *  @see ex::diff */
177 ex constant::derivative(const symbol & s) const
178 {
179         return _ex0();
180 }
181
182 int constant::compare_same_type(const basic & other) const
183 {
184         GINAC_ASSERT(is_exactly_of_type(other, constant));
185         // const constant & o=static_cast<constant &>(const_cast<basic &>(other));
186         // return name.compare(o.name);
187         const constant *o = static_cast<const constant *>(&other);
188         if (serial==o->serial) return 0;
189         return serial < o->serial ? -1 : 1;
190 }
191
192 bool constant::is_equal_same_type(const basic & other) const
193 {
194         GINAC_ASSERT(is_exactly_of_type(other, constant));
195         const constant *o = static_cast<const constant *>(&other);
196         return serial==o->serial;
197 }
198
199 unsigned constant::calchash(void) const
200 {
201         hashvalue = golden_ratio_hash(tinfo() ^ serial);
202         // mask out numeric hashes:
203         hashvalue &= 0x7FFFFFFFU;
204         
205         setflag(status_flags::hash_calculated);
206         
207         return hashvalue;
208 }
209
210 //////////
211 // new virtual functions which can be overridden by derived classes
212 //////////
213
214 // none
215
216 //////////
217 // non-virtual functions in this class
218 //////////
219
220 // none
221
222 //////////
223 // static member variables
224 //////////
225
226 unsigned constant::next_serial = 0;
227
228 //////////
229 // global constants
230 //////////
231
232 /**  Pi. (3.14159...)  Diverts straight into CLN for evalf(). */
233 const constant Pi("Pi", PiEvalf);
234
235 /** Euler's constant. (0.57721...)  Sometimes called Euler-Mascheroni constant.
236  *  Diverts straight into CLN for evalf(). */
237 const constant Euler("Euler", EulerEvalf);
238
239 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
240 const constant Catalan("Catalan", CatalanEvalf);
241
242 } // namespace GiNaC