]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
[DOC] Update the tutorial for GiNaC::function resolution.
[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-2025 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 "constant.h"
24 #include "numeric.h"
25 #include "ex.h"
26 #include "archive.h"
27 #include "utils.h"
28 #include "inifcns.h"
29
30 #include <stdexcept>
31 #include <string>
32
33 namespace GiNaC {
34
35 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(constant, basic,
36   print_func<print_context>(&constant::do_print).
37   print_func<print_latex>(&constant::do_print_latex).
38   print_func<print_tree>(&constant::do_print_tree).
39   print_func<print_python_repr>(&constant::do_print_python_repr))
40
41 //////////
42 // default constructor
43 //////////
44
45 // public
46
47 constant::constant() : ef(nullptr), serial(next_serial++), domain(domain::complex)
48 {
49         setflag(status_flags::evaluated | status_flags::expanded);
50 }
51
52 //////////
53 // other constructors
54 //////////
55
56 // public
57
58 constant::constant(const std::string & initname, evalffunctype efun, const std::string & texname, unsigned dm)
59   : name(initname), ef(efun), serial(next_serial++), domain(dm)
60 {
61         if (texname.empty())
62                 TeX_name = "\\mathrm{" + name + "}";
63         else
64                 TeX_name = texname;
65         setflag(status_flags::evaluated | status_flags::expanded);
66 }
67
68 constant::constant(const std::string & initname, const numeric & initnumber, const std::string & texname, unsigned dm)
69   : name(initname), ef(nullptr), number(initnumber), serial(next_serial++), domain(dm)
70 {
71         if (texname.empty())
72                 TeX_name = "\\mathrm{" + name + "}";
73         else
74                 TeX_name = texname;
75         setflag(status_flags::evaluated | status_flags::expanded);
76 }
77
78 //////////
79 // archiving
80 //////////
81
82 void constant::read_archive(const archive_node &n, lst &sym_lst)
83 {
84         // Find constant by name (!! this is bad: 'twould be better if there
85         // was a list of all global constants that we could search)
86         std::string s;
87         if (n.find_string("name", s)) {
88                 if (s == Pi.name)
89                         *this = Pi;
90                 else if (s == Catalan.name)
91                         *this = Catalan;
92                 else if (s == Euler.name)
93                         *this = Euler;
94                 else
95                         throw (std::runtime_error("unknown constant '" + s + "' in archive"));
96         } else
97                 throw (std::runtime_error("unnamed constant in archive"));
98 }
99 GINAC_BIND_UNARCHIVER(constant);
100
101 void constant::archive(archive_node &n) const
102 {
103         inherited::archive(n);
104         n.add_string("name", name);
105 }
106
107 //////////
108 // functions overriding virtual functions from base classes
109 //////////
110
111 // public
112
113 void constant::do_print(const print_context & c, unsigned level) const
114 {
115         c.s << name;
116 }
117
118 void constant::do_print_tree(const print_tree & c, unsigned level) const
119 {
120         c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
121             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
122             << std::endl;
123 }
124
125 void constant::do_print_latex(const print_latex & c, unsigned level) const
126 {
127         c.s << TeX_name;
128 }
129
130 void constant::do_print_python_repr(const print_python_repr & c, unsigned level) const
131 {
132         c.s << class_name() << "('" << name << "'";
133         if (TeX_name != "\\mathrm{" + name + "}")
134                 c.s << ",TeX_name='" << TeX_name << "'";
135         c.s << ')';
136 }
137
138 bool constant::info(unsigned inf) const
139 {
140         if (inf == info_flags::polynomial)
141                 return true;
142         if (inf == info_flags::real)
143                 return domain==domain::real || domain==domain::positive ;
144         if (inf==info_flags::positive || inf==info_flags::nonnegative)
145                 return domain == domain::positive;
146         else
147                 return inherited::info(inf);
148 }
149
150 ex constant::evalf() const
151 {
152         if (ef!=nullptr) {
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 || domain==domain::positive )
168                 return *this;
169         return conjugate_function(*this).hold();
170 }
171
172 ex constant::real_part() const
173 {
174         if ( domain==domain::real || domain==domain::positive )
175                 return *this;
176         return real_part_function(*this).hold();
177 }
178
179 ex constant::imag_part() const
180 {
181         if ( domain==domain::real || domain==domain::positive )
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         const void* typeid_this = (const void*)typeid(*this).name();
218         hashvalue = golden_ratio_hash((uintptr_t)typeid_this ^ serial);
219
220         setflag(status_flags::hash_calculated);
221
222         return hashvalue;
223 }
224
225 //////////
226 // new virtual functions which can be overridden by derived classes
227 //////////
228
229 // none
230
231 //////////
232 // non-virtual functions in this class
233 //////////
234
235 // none
236
237 //////////
238 // static member variables
239 //////////
240
241 unsigned constant::next_serial = 0;
242
243 //////////
244 // global constants
245 //////////
246
247 /**  Pi. (3.14159...)  Diverts straight into CLN for evalf(). */
248 const constant Pi("Pi", PiEvalf, "\\pi", domain::positive);
249
250 /** Euler's constant. (0.57721...)  Sometimes called Euler-Mascheroni constant.
251  *  Diverts straight into CLN for evalf(). */
252 const constant Euler("Euler", EulerEvalf, "\\gamma_E", domain::positive);
253
254 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
255 const constant Catalan("Catalan", CatalanEvalf, "G", domain::positive);
256
257 } // namespace GiNaC