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