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