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

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