]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
series expansion behaviour fixed.
[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-2003 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 #include <iostream>
26
27 #include "constant.h"
28 #include "numeric.h"
29 #include "ex.h"
30 #include "print.h"
31 #include "archive.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         setflag(status_flags::evaluated | status_flags::expanded);
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         if (texname.empty())
82                 TeX_name = "\\mbox{" + name + "}";
83         else
84                 TeX_name = texname;
85         setflag(status_flags::evaluated | status_flags::expanded);
86 }
87
88 constant::constant(const std::string & initname, const numeric & initnumber, const std::string & texname)
89   : basic(TINFO_constant), name(initname), ef(0), number(new numeric(initnumber)), serial(next_serial++)
90 {
91         if (texname.empty())
92                 TeX_name = "\\mbox{" + name + "}";
93         else
94                 TeX_name = texname;
95         setflag(status_flags::evaluated | status_flags::expanded);
96 }
97
98 //////////
99 // archiving
100 //////////
101
102 constant::constant(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {}
103
104 ex constant::unarchive(const archive_node &n, lst &sym_lst)
105 {
106         // Find constant by name (!! this is bad: 'twould be better if there
107         // was a list of all global constants that we could search)
108         std::string s;
109         if (n.find_string("name", s)) {
110                 if (s == Pi.name)
111                         return Pi;
112                 else if (s == Catalan.name)
113                         return Catalan;
114                 else if (s == Euler.name)
115                         return Euler;
116                 else
117                         throw (std::runtime_error("unknown constant '" + s + "' in archive"));
118         } else
119                 throw (std::runtime_error("unnamed constant in archive"));
120 }
121
122 void constant::archive(archive_node &n) const
123 {
124         inherited::archive(n);
125         n.add_string("name", name);
126 }
127
128 //////////
129 // functions overriding virtual functions from base classes
130 //////////
131
132 // public
133
134 void constant::print(const print_context & c, unsigned level) const
135 {
136         if (is_a<print_tree>(c)) {
137                 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
138                     << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
139                     << std::endl;
140         } else if (is_a<print_latex>(c)) {
141                 c.s << TeX_name;
142         } else if (is_a<print_python_repr>(c)) {
143                 c.s << class_name() << "('" << name << "'";
144                 if (TeX_name != "\\mbox{" + name + "}")
145                         c.s << ",TeX_name='" << TeX_name << "'";
146                 c.s << ')';
147         } else
148                 c.s << name;
149 }
150
151 ex constant::evalf(int level) const
152 {
153         if (ef!=0) {
154                 return ef();
155         } else if (number != 0) {
156                 return number->evalf();
157         }
158         return *this;
159 }
160
161 // protected
162
163 /** Implementation of ex::diff() for a constant always returns 0.
164  *
165  *  @see ex::diff */
166 ex constant::derivative(const symbol & s) const
167 {
168         return _ex0;
169 }
170
171 int constant::compare_same_type(const basic & other) const
172 {
173         GINAC_ASSERT(is_exactly_a<constant>(other));
174         const constant &o = static_cast<const constant &>(other);
175
176         if (serial == o.serial)
177                 return 0;
178         else
179                 return serial < o.serial ? -1 : 1;
180 }
181
182 bool constant::is_equal_same_type(const basic & other) const
183 {
184         GINAC_ASSERT(is_exactly_a<constant>(other));
185         const constant &o = static_cast<const constant &>(other);
186
187         return serial == o.serial;
188 }
189
190 unsigned constant::calchash(void) const
191 {
192         hashvalue = golden_ratio_hash(tinfo() ^ serial);
193
194         setflag(status_flags::hash_calculated);
195
196         return hashvalue;
197 }
198
199 //////////
200 // new virtual functions which can be overridden by derived classes
201 //////////
202
203 // none
204
205 //////////
206 // non-virtual functions in this class
207 //////////
208
209 // none
210
211 //////////
212 // static member variables
213 //////////
214
215 unsigned constant::next_serial = 0;
216
217 //////////
218 // global constants
219 //////////
220
221 /**  Pi. (3.14159...)  Diverts straight into CLN for evalf(). */
222 const constant Pi("Pi", PiEvalf, "\\pi");
223
224 /** Euler's constant. (0.57721...)  Sometimes called Euler-Mascheroni constant.
225  *  Diverts straight into CLN for evalf(). */
226 const constant Euler("Euler", EulerEvalf, "\\gamma_E");
227
228 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
229 const constant Catalan("Catalan", CatalanEvalf, "G");
230
231 } // namespace GiNaC