GiNaC 1.8.10
constant.cpp
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2026 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include "constant.h"
23#include "numeric.h"
24#include "ex.h"
25#include "archive.h"
26#include "utils.h"
27#include "inifcns.h"
28
29#include <stdexcept>
30#include <string>
31
32namespace GiNaC {
33
36 print_func<print_latex>(&constant::do_print_latex).
37 print_func<print_tree>(&constant::do_print_tree).
38 print_func<print_python_repr>(&constant::do_print_python_repr))
39
40
41// default constructor
43
44// public
45
46constant::constant() : ef(nullptr), serial(next_serial++), domain(domain::complex)
47{
49}
50
52// other constructors
54
55// public
56
57constant::constant(const std::string & initname, evalffunctype efun, const std::string & texname, unsigned dm)
58 : name(initname), ef(efun), serial(next_serial++), domain(dm)
59{
60 if (texname.empty())
61 TeX_name = "\\mathrm{" + name + "}";
62 else
63 TeX_name = texname;
65}
66
67constant::constant(const std::string & initname, const numeric & initnumber, const std::string & texname, unsigned dm)
68 : name(initname), ef(nullptr), number(initnumber), serial(next_serial++), domain(dm)
69{
70 if (texname.empty())
71 TeX_name = "\\mathrm{" + name + "}";
72 else
73 TeX_name = texname;
75}
76
78// archiving
80
82{
83 // Find constant by name (!! this is bad: 'twould be better if there
84 // was a list of all global constants that we could search)
85 std::string s;
86 if (n.find_string("name", s)) {
87 if (s == Pi.name)
88 *this = Pi;
89 else if (s == Catalan.name)
90 *this = Catalan;
91 else if (s == Euler.name)
92 *this = Euler;
93 else
94 throw (std::runtime_error("unknown constant '" + s + "' in archive"));
95 } else
96 throw (std::runtime_error("unnamed constant in archive"));
97}
99
101{
102 inherited::archive(n);
103 n.add_string("name", name);
104}
105
107// functions overriding virtual functions from base classes
109
110// public
111
112void constant::do_print(const print_context & c, unsigned level) const
113{
114 c.s << name;
115}
116
117void constant::do_print_tree(const print_tree & c, unsigned level) const
118{
119 c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
120 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
121 << std::endl;
122}
123
124void constant::do_print_latex(const print_latex & c, unsigned level) const
125{
126 c.s << TeX_name;
127}
128
129void constant::do_print_python_repr(const print_python_repr & c, unsigned level) const
130{
131 c.s << class_name() << "('" << name << "'";
132 if (TeX_name != "\\mathrm{" + name + "}")
133 c.s << ",TeX_name='" << TeX_name << "'";
134 c.s << ')';
135}
136
137bool constant::info(unsigned inf) const
138{
139 if (inf == info_flags::polynomial)
140 return true;
141 if (inf == info_flags::real)
144 return domain == domain::positive;
145 else
146 return inherited::info(inf);
147}
148
150{
151 if (ef!=nullptr) {
152 return ef();
153 } else {
154 return number.evalf();
155 }
156 return *this;
157}
158
159bool constant::is_polynomial(const ex & var) const
160{
161 return true;
162}
163
165{
167 return *this;
168 return conjugate_function(*this).hold();
169}
170
172{
174 return *this;
175 return real_part_function(*this).hold();
176}
177
179{
181 return 0;
182 return imag_part_function(*this).hold();
183}
184
185// protected
186
191{
192 return _ex0;
193}
194
195int constant::compare_same_type(const basic & other) const
196{
197 GINAC_ASSERT(is_exactly_a<constant>(other));
198 const constant &o = static_cast<const constant &>(other);
199
200 if (serial == o.serial)
201 return 0;
202 else
203 return serial < o.serial ? -1 : 1;
204}
205
206bool constant::is_equal_same_type(const basic & other) const
207{
208 GINAC_ASSERT(is_exactly_a<constant>(other));
209 const constant &o = static_cast<const constant &>(other);
210
211 return serial == o.serial;
212}
213
214unsigned constant::calchash() const
215{
216 const void* typeid_this = (const void*)typeid(*this).name();
217 hashvalue = golden_ratio_hash((uintptr_t)typeid_this ^ serial);
218
220
221 return hashvalue;
222}
223
225// new virtual functions which can be overridden by derived classes
227
228// none
229
231// non-virtual functions in this class
233
234// none
235
237// static member variables
239
240unsigned constant::next_serial = 0;
241
243// global constants
245
247const constant Pi("Pi", PiEvalf, "\\pi", domain::positive);
248
251const constant Euler("Euler", EulerEvalf, "\\gamma_E", domain::positive);
252
254const constant Catalan("Catalan", CatalanEvalf, "G", domain::positive);
255
256} // namespace GiNaC
Archiving of GiNaC expressions.
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition assertion.h:32
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition archive.h:48
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition basic.h:104
const basic & setflag(unsigned f) const
Set some status_flags.
Definition basic.h:287
unsigned hashvalue
hash value
Definition basic.h:302
unsigned flags
of type status_flags
Definition basic.h:301
virtual int compare_same_type(const basic &other) const
Returns order relation between two objects of same type.
Definition basic.cpp:718
This class holds constants, symbols with specific numerical value.
Definition constant.h:40
evalffunctype ef
Definition constant.h:76
ex number
numerical value this constant evalf()s to
Definition constant.h:77
ex evalf() const override
Evaluate object numerically.
Definition constant.cpp:149
std::string name
printname of this constant
Definition constant.h:74
void read_archive(const archive_node &n, lst &syms) override
Load (deserialize) the object from an archive node.
Definition constant.cpp:81
unsigned serial
unique serial number for comparison
Definition constant.h:78
void do_print_latex(const print_latex &c, unsigned level) const
Definition constant.cpp:124
bool is_equal_same_type(const basic &other) const override
Returns true if two objects of same type are equal.
Definition constant.cpp:206
void archive(archive_node &n) const override
Save (serialize) the object into archive node.
Definition constant.cpp:100
void do_print(const print_context &c, unsigned level) const
Definition constant.cpp:112
std::string TeX_name
LaTeX name.
Definition constant.h:75
bool info(unsigned inf) const override
Information about the object.
Definition constant.cpp:137
ex real_part() const override
Definition constant.cpp:171
static unsigned next_serial
Definition constant.h:79
ex derivative(const symbol &s) const override
Implementation of ex::diff() for a constant always returns 0.
Definition constant.cpp:190
void do_print_python_repr(const print_python_repr &c, unsigned level) const
Definition constant.cpp:129
void do_print_tree(const print_tree &c, unsigned level) const
Definition constant.cpp:117
unsigned calchash() const override
Compute the hash value of an object and if it makes sense to store it in the objects status_flags,...
Definition constant.cpp:214
constant(const std::string &initname, evalffunctype efun=nullptr, const std::string &texname=std::string(), unsigned domain=domain::complex)
Definition constant.cpp:57
bool is_polynomial(const ex &var) const override
Check whether this is a polynomial in the given variables.
Definition constant.cpp:159
ex conjugate() const override
Definition constant.cpp:164
ex imag_part() const override
Definition constant.cpp:178
Wrapper template for making GiNaC classes out of STL containers.
Definition container.h:72
Domain of an object.
Definition flags.h:65
Lightweight wrapper for GiNaC's symbolic objects.
Definition ex.h:72
ex evalf() const
Definition ex.h:121
This class is a wrapper around CLN-numbers within the GiNaC class hierarchy.
Definition numeric.h:81
Base class for print_contexts.
Definition print.h:101
Context for latex-parsable output.
Definition print.h:121
Context for python-parsable output.
Definition print.h:137
Context for tree-like output for debugging.
Definition print.h:145
@ expanded
.expand(0) has already done its job (other expand() options ignore this flag)
Definition flags.h:203
@ evaluated
.eval() has already done its job
Definition flags.h:202
@ hash_calculated
.calchash() has already done its job
Definition flags.h:204
Basic CAS symbol.
Definition symbol.h:38
Interface to GiNaC's constant types and some special constants.
Interface to GiNaC's light-weight expression handles.
size_t n
Definition factor.cpp:1431
size_t c
Definition factor.cpp:756
Interface to GiNaC's initially known functions.
Definition add.cpp:35
unsigned golden_ratio_hash(uintptr_t n)
Truncated multiplication with golden ratio, for computing hash values.
Definition utils.h:67
const constant Euler("Euler", EulerEvalf, "\\gamma_E", domain::positive)
Euler's constant.
Definition constant.h:86
ex EulerEvalf()
Floating point evaluation of Euler's constant gamma.
Definition numeric.cpp:2505
ex PiEvalf()
Floating point evaluation of Archimedes' constant Pi.
Definition numeric.cpp:2498
const constant Pi("Pi", PiEvalf, "\\pi", domain::positive)
Pi.
Definition constant.h:84
print_func< print_context >(&varidx::do_print). print_func< print_latex >(&varidx
Definition idx.cpp:43
const constant Catalan("Catalan", CatalanEvalf, "G", domain::positive)
Catalan's constant.
Definition constant.h:85
ex CatalanEvalf()
Floating point evaluation of Catalan's constant.
Definition numeric.cpp:2512
GINAC_IMPLEMENT_REGISTERED_CLASS_OPT_T(lst, basic, print_func< print_context >(&lst::do_print). print_func< print_tree >(&lst::do_print_tree)) template<> bool lst GINAC_BIND_UNARCHIVER(lst)
Specialization of container::info() for lst.
Definition lst.cpp:41
const ex _ex0
Definition utils.cpp:368
Makes the interface to the underlying bignum package available.
#define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options)
Macro for inclusion in the implementation of each registered class.
Definition registrar.h:183
Interface to several small and furry utilities needed within GiNaC but not of any interest to the use...

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.