3 * Implementation of GiNaC's constant types and some special constants. */
6 * GiNaC Copyright (C) 1999-2025 Johannes Gutenberg University Mainz, Germany
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.
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.
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
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))
42 // default constructor
47 constant::constant() : ef(nullptr), serial(next_serial++), domain(domain::complex)
49 setflag(status_flags::evaluated | status_flags::expanded);
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)
62 TeX_name = "\\mathrm{" + name + "}";
65 setflag(status_flags::evaluated | status_flags::expanded);
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)
72 TeX_name = "\\mathrm{" + name + "}";
75 setflag(status_flags::evaluated | status_flags::expanded);
82 void constant::read_archive(const archive_node &n, lst &sym_lst)
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)
87 if (n.find_string("name", s)) {
90 else if (s == Catalan.name)
92 else if (s == Euler.name)
95 throw (std::runtime_error("unknown constant '" + s + "' in archive"));
97 throw (std::runtime_error("unnamed constant in archive"));
99 GINAC_BIND_UNARCHIVER(constant);
101 void constant::archive(archive_node &n) const
103 inherited::archive(n);
104 n.add_string("name", name);
108 // functions overriding virtual functions from base classes
113 void constant::do_print(const print_context & c, unsigned level) const
118 void constant::do_print_tree(const print_tree & c, unsigned level) const
120 c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
121 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
125 void constant::do_print_latex(const print_latex & c, unsigned level) const
130 void constant::do_print_python_repr(const print_python_repr & c, unsigned level) const
132 c.s << class_name() << "('" << name << "'";
133 if (TeX_name != "\\mathrm{" + name + "}")
134 c.s << ",TeX_name='" << TeX_name << "'";
138 bool constant::info(unsigned inf) const
140 if (inf == info_flags::polynomial)
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;
147 return inherited::info(inf);
150 ex constant::evalf() const
155 return number.evalf();
160 bool constant::is_polynomial(const ex & var) const
165 ex constant::conjugate() const
167 if ( domain==domain::real || domain==domain::positive )
169 return conjugate_function(*this).hold();
172 ex constant::real_part() const
174 if ( domain==domain::real || domain==domain::positive )
176 return real_part_function(*this).hold();
179 ex constant::imag_part() const
181 if ( domain==domain::real || domain==domain::positive )
183 return imag_part_function(*this).hold();
188 /** Implementation of ex::diff() for a constant always returns 0.
191 ex constant::derivative(const symbol & s) const
196 int constant::compare_same_type(const basic & other) const
198 GINAC_ASSERT(is_exactly_a<constant>(other));
199 const constant &o = static_cast<const constant &>(other);
201 if (serial == o.serial)
204 return serial < o.serial ? -1 : 1;
207 bool constant::is_equal_same_type(const basic & other) const
209 GINAC_ASSERT(is_exactly_a<constant>(other));
210 const constant &o = static_cast<const constant &>(other);
212 return serial == o.serial;
215 unsigned constant::calchash() const
217 const void* typeid_this = (const void*)typeid(*this).name();
218 hashvalue = golden_ratio_hash((uintptr_t)typeid_this ^ serial);
220 setflag(status_flags::hash_calculated);
226 // new virtual functions which can be overridden by derived classes
232 // non-virtual functions in this class
238 // static member variables
241 unsigned constant::next_serial = 0;
247 /** Pi. (3.14159...) Diverts straight into CLN for evalf(). */
248 const constant Pi("Pi", PiEvalf, "\\pi", domain::positive);
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);
254 /** Catalan's constant. (0.91597...) Diverts straight into CLN for evalf(). */
255 const constant Catalan("Catalan", CatalanEvalf, "G", domain::positive);