3 * Implementation of GiNaC's constant types and some special constants. */
6 * GiNaC Copyright (C) 1999-2021 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
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))
43 // default constructor
48 constant::constant() : ef(nullptr), serial(next_serial++), domain(domain::complex)
50 setflag(status_flags::evaluated | status_flags::expanded);
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)
63 TeX_name = "\\mathrm{" + name + "}";
66 setflag(status_flags::evaluated | status_flags::expanded);
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)
73 TeX_name = "\\mathrm{" + name + "}";
76 setflag(status_flags::evaluated | status_flags::expanded);
83 void constant::read_archive(const archive_node &n, lst &sym_lst)
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)
88 if (n.find_string("name", s)) {
91 else if (s == Catalan.name)
93 else if (s == Euler.name)
96 throw (std::runtime_error("unknown constant '" + s + "' in archive"));
98 throw (std::runtime_error("unnamed constant in archive"));
100 GINAC_BIND_UNARCHIVER(constant);
102 void constant::archive(archive_node &n) const
104 inherited::archive(n);
105 n.add_string("name", name);
109 // functions overriding virtual functions from base classes
114 void constant::do_print(const print_context & c, unsigned level) const
119 void constant::do_print_tree(const print_tree & c, unsigned level) const
121 c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
122 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
126 void constant::do_print_latex(const print_latex & c, unsigned level) const
131 void constant::do_print_python_repr(const print_python_repr & c, unsigned level) const
133 c.s << class_name() << "('" << name << "'";
134 if (TeX_name != "\\mathrm{" + name + "}")
135 c.s << ",TeX_name='" << TeX_name << "'";
139 bool constant::info(unsigned inf) const
141 if (inf == info_flags::polynomial)
143 if (inf == info_flags::real)
144 return domain==domain::real || domain==domain::positive ;
145 if (inf==info_flags::positive || inf==info_flags::nonnegative)
146 return domain == domain::positive;
148 return inherited::info(inf);
151 ex constant::evalf() const
156 return number.evalf();
161 bool constant::is_polynomial(const ex & var) const
166 ex constant::conjugate() const
168 if ( domain==domain::real || domain==domain::positive )
170 return conjugate_function(*this).hold();
173 ex constant::real_part() const
175 if ( domain==domain::real || domain==domain::positive )
177 return real_part_function(*this).hold();
180 ex constant::imag_part() const
182 if ( domain==domain::real || domain==domain::positive )
184 return imag_part_function(*this).hold();
189 /** Implementation of ex::diff() for a constant always returns 0.
192 ex constant::derivative(const symbol & s) const
197 int constant::compare_same_type(const basic & other) const
199 GINAC_ASSERT(is_exactly_a<constant>(other));
200 const constant &o = static_cast<const constant &>(other);
202 if (serial == o.serial)
205 return serial < o.serial ? -1 : 1;
208 bool constant::is_equal_same_type(const basic & other) const
210 GINAC_ASSERT(is_exactly_a<constant>(other));
211 const constant &o = static_cast<const constant &>(other);
213 return serial == o.serial;
216 unsigned constant::calchash() const
218 const void* typeid_this = (const void*)typeid(*this).name();
219 hashvalue = golden_ratio_hash((uintptr_t)typeid_this ^ serial);
221 setflag(status_flags::hash_calculated);
227 // new virtual functions which can be overridden by derived classes
233 // non-virtual functions in this class
239 // static member variables
242 unsigned constant::next_serial = 0;
248 /** Pi. (3.14159...) Diverts straight into CLN for evalf(). */
249 const constant Pi("Pi", PiEvalf, "\\pi", domain::positive);
251 /** Euler's constant. (0.57721...) Sometimes called Euler-Mascheroni constant.
252 * Diverts straight into CLN for evalf(). */
253 const constant Euler("Euler", EulerEvalf, "\\gamma_E", domain::positive);
255 /** Catalan's constant. (0.91597...) Diverts straight into CLN for evalf(). */
256 const constant Catalan("Catalan", CatalanEvalf, "G", domain::positive);