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