]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
d6c07409a32d1ed581b6c0cbed444c6161ba9c9b
[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 "utils.h"
32
33 namespace GiNaC {
34
35 GINAC_IMPLEMENT_REGISTERED_CLASS(constant, basic)
36
37 //////////
38 // default ctor, dtor, copy ctor, assignment operator and helpers
39 //////////
40
41 // public
42
43 constant::constant() : basic(TINFO_constant), ef(0), number(0), serial(next_serial++) {}
44
45 // protected
46
47 /** For use by copy ctor and assignment operator. */
48 void constant::copy(const constant & other)
49 {
50         inherited::copy(other);
51         name = other.name;
52         TeX_name = other.TeX_name;
53         serial = other.serial;
54         ef = other.ef;
55         if (other.number != 0)
56                 number = new numeric(*other.number);
57         else
58                 number = 0;
59 }
60
61 void constant::destroy(bool call_parent)
62 {
63         delete number;
64         if (call_parent)
65                 inherited::destroy(call_parent);
66 }
67
68 //////////
69 // other ctors
70 //////////
71
72 // public
73
74 constant::constant(const std::string & initname, evalffunctype efun, const std::string & texname)
75   : basic(TINFO_constant), name(initname), ef(efun), number(0), serial(next_serial++)
76 {
77         if (texname.empty())
78                 TeX_name = "\\mbox{" + name + "}";
79         else
80                 TeX_name = texname;
81         setflag(status_flags::evaluated | status_flags::expanded);
82 }
83
84 constant::constant(const std::string & initname, const numeric & initnumber, const std::string & texname)
85   : basic(TINFO_constant), name(initname), ef(0), number(new numeric(initnumber)), serial(next_serial++)
86 {
87         if (texname.empty())
88                 TeX_name = "\\mbox{" + name + "}";
89         else
90                 TeX_name = texname;
91         setflag(status_flags::evaluated | status_flags::expanded);
92 }
93
94 //////////
95 // archiving
96 //////////
97
98 constant::constant(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) {}
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 base classes
126 //////////
127
128 // public
129
130 void constant::print(const print_context & c, unsigned level) const
131 {
132         if (is_a<print_tree>(c)) {
133                 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
134                     << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
135                     << std::endl;
136         } else if (is_a<print_latex>(c))
137                 c.s << TeX_name;
138         else
139                 c.s << name;
140 }
141
142 int constant::degree(const ex & s) const
143 {
144         return is_equal(ex_to<basic>(s)) ? 1 : 0;
145 }
146
147 int constant::ldegree(const ex & s) const
148 {
149         return is_equal(ex_to<basic>(s)) ? 1 : 0;
150 }
151
152 ex constant::coeff(const ex & s, int n) const
153 {
154         if (is_equal(ex_to<basic>(s)))
155                 return n==1 ? _ex1 : _ex0;
156         else
157                 return n==0 ? *this : _ex0;
158 }
159
160 ex constant::evalf(int level) const
161 {
162         if (ef!=0) {
163                 return ef();
164         } else if (number != 0) {
165                 return number->evalf();
166         }
167         return *this;
168 }
169
170 // protected
171
172 /** Implementation of ex::diff() for a constant always returns 0.
173  *
174  *  @see ex::diff */
175 ex constant::derivative(const symbol & s) const
176 {
177         return _ex0;
178 }
179
180 int constant::compare_same_type(const basic & other) const
181 {
182         GINAC_ASSERT(is_exactly_a<constant>(other));
183         const constant &o = static_cast<const constant &>(other);
184
185         if (serial == o.serial)
186                 return 0;
187         else
188                 return serial < o.serial ? -1 : 1;
189 }
190
191 bool constant::is_equal_same_type(const basic & other) const
192 {
193         GINAC_ASSERT(is_exactly_a<constant>(other));
194         const constant &o = static_cast<const constant &>(other);
195
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, "\\pi");
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, "\\gamma_E");
238
239 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
240 const constant Catalan("Catalan", CatalanEvalf, "G");
241
242 } // namespace GiNaC