]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
Added posibility to make map_functions out of members.
[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-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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 "archive.h"
31 #include "utils.h"
32 #include "inifcns.h"
33
34 namespace GiNaC {
35
36 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(constant, basic,
37   print_func<print_context>(&constant::do_print).
38   print_func<print_latex>(&constant::do_print_latex).
39   print_func<print_tree>(&constant::do_print_tree).
40   print_func<print_python_repr>(&constant::do_print_python_repr))
41
42 //////////
43 // default constructor
44 //////////
45
46 // public
47
48 constant::constant() : basic(&constant::tinfo_static), ef(0), serial(next_serial++), domain(domain::complex)
49 {
50         setflag(status_flags::evaluated | status_flags::expanded);
51 }
52
53 //////////
54 // other constructors
55 //////////
56
57 // public
58
59 constant::constant(const std::string & initname, evalffunctype efun, const std::string & texname, unsigned dm)
60   : basic(&constant::tinfo_static), name(initname), ef(efun), serial(next_serial++), domain(dm)
61 {
62         if (texname.empty())
63                 TeX_name = "\\mbox{" + name + "}";
64         else
65                 TeX_name = texname;
66         setflag(status_flags::evaluated | status_flags::expanded);
67 }
68
69 constant::constant(const std::string & initname, const numeric & initnumber, const std::string & texname, unsigned dm)
70   : basic(&constant::tinfo_static), name(initname), ef(0), number(initnumber), serial(next_serial++), domain(dm)
71 {
72         if (texname.empty())
73                 TeX_name = "\\mbox{" + name + "}";
74         else
75                 TeX_name = texname;
76         setflag(status_flags::evaluated | status_flags::expanded);
77 }
78
79 //////////
80 // archiving
81 //////////
82
83 constant::constant(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {}
84
85 ex constant::unarchive(const archive_node &n, lst &sym_lst)
86 {
87         // Find constant by name (!! this is bad: 'twould be better if there
88         // was a list of all global constants that we could search)
89         std::string s;
90         if (n.find_string("name", s)) {
91                 if (s == Pi.name)
92                         return Pi;
93                 else if (s == Catalan.name)
94                         return Catalan;
95                 else if (s == Euler.name)
96                         return Euler;
97                 else
98                         throw (std::runtime_error("unknown constant '" + s + "' in archive"));
99         } else
100                 throw (std::runtime_error("unnamed constant in archive"));
101 }
102
103 void constant::archive(archive_node &n) const
104 {
105         inherited::archive(n);
106         n.add_string("name", name);
107 }
108
109 //////////
110 // functions overriding virtual functions from base classes
111 //////////
112
113 // public
114
115 void constant::do_print(const print_context & c, unsigned level) const
116 {
117         c.s << name;
118 }
119
120 void constant::do_print_tree(const print_tree & c, unsigned level) const
121 {
122         c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
123             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
124             << std::endl;
125 }
126
127 void constant::do_print_latex(const print_latex & c, unsigned level) const
128 {
129         c.s << TeX_name;
130 }
131
132 void constant::do_print_python_repr(const print_python_repr & c, unsigned level) const
133 {
134         c.s << class_name() << "('" << name << "'";
135         if (TeX_name != "\\mbox{" + name + "}")
136                 c.s << ",TeX_name='" << TeX_name << "'";
137         c.s << ')';
138 }
139
140 bool constant::info(unsigned inf) const
141 {
142         if (inf == info_flags::polynomial)
143                 return true;
144         if (inf == info_flags::real)
145                 return domain == domain::real;
146         else
147                 return inherited::info(inf);
148 }
149
150 ex constant::evalf(int level) const
151 {
152         if (ef!=0) {
153                 return ef();
154         } else {
155                 return number.evalf();
156         }
157         return *this;
158 }
159
160 bool constant::is_polynomial(const ex & var) const
161 {
162         return true;
163 }
164
165 ex constant::conjugate() const
166 {
167         if ( domain == domain::real )
168                 return *this;
169         return conjugate_function(*this).hold();
170 }
171
172 ex constant::real_part() const
173 {
174         if ( domain == domain::real )
175                 return *this;
176         return real_part_function(*this).hold();
177 }
178
179 ex constant::imag_part() const
180 {
181         if ( domain == domain::real )
182                 return 0;
183         return imag_part_function(*this).hold();
184 }
185
186 // protected
187
188 /** Implementation of ex::diff() for a constant always returns 0.
189  *
190  *  @see ex::diff */
191 ex constant::derivative(const symbol & s) const
192 {
193         return _ex0;
194 }
195
196 int constant::compare_same_type(const basic & other) const
197 {
198         GINAC_ASSERT(is_exactly_a<constant>(other));
199         const constant &o = static_cast<const constant &>(other);
200
201         if (serial == o.serial)
202                 return 0;
203         else
204                 return serial < o.serial ? -1 : 1;
205 }
206
207 bool constant::is_equal_same_type(const basic & other) const
208 {
209         GINAC_ASSERT(is_exactly_a<constant>(other));
210         const constant &o = static_cast<const constant &>(other);
211
212         return serial == o.serial;
213 }
214
215 unsigned constant::calchash() const
216 {
217         hashvalue = golden_ratio_hash((p_int)tinfo() ^ serial);
218
219         setflag(status_flags::hash_calculated);
220
221         return hashvalue;
222 }
223
224 //////////
225 // new virtual functions which can be overridden by derived classes
226 //////////
227
228 // none
229
230 //////////
231 // non-virtual functions in this class
232 //////////
233
234 // none
235
236 //////////
237 // static member variables
238 //////////
239
240 unsigned constant::next_serial = 0;
241
242 //////////
243 // global constants
244 //////////
245
246 /**  Pi. (3.14159...)  Diverts straight into CLN for evalf(). */
247 const constant Pi("Pi", PiEvalf, "\\pi", domain::real);
248
249 /** Euler's constant. (0.57721...)  Sometimes called Euler-Mascheroni constant.
250  *  Diverts straight into CLN for evalf(). */
251 const constant Euler("Euler", EulerEvalf, "\\gamma_E", domain::real);
252
253 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
254 const constant Catalan("Catalan", CatalanEvalf, "G", domain::real);
255
256 } // namespace GiNaC