3 * Implementation of GiNaC's constant types and some special constants. */
6 * GiNaC Copyright (C) 1999-2008 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(0), 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 = "\\mbox{" + 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(0), number(initnumber), serial(next_serial++), domain(dm)
73 TeX_name = "\\mbox{" + name + "}";
76 setflag(status_flags::evaluated | status_flags::expanded);
83 constant::constant(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {}
85 ex constant::unarchive(const archive_node &n, lst &sym_lst)
87 // Find constant by name (!! this is bad: 'twould be better if there
88 // was a list of all global constants that we could search)
90 if (n.find_string("name", s)) {
93 else if (s == Catalan.name)
95 else if (s == Euler.name)
98 throw (std::runtime_error("unknown constant '" + s + "' in archive"));
100 throw (std::runtime_error("unnamed constant in archive"));
103 void constant::archive(archive_node &n) const
105 inherited::archive(n);
106 n.add_string("name", name);
110 // functions overriding virtual functions from base classes
115 void constant::do_print(const print_context & c, unsigned level) const
120 void constant::do_print_tree(const print_tree & c, unsigned level) const
122 c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
123 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
127 void constant::do_print_latex(const print_latex & c, unsigned level) const
132 void constant::do_print_python_repr(const print_python_repr & c, unsigned level) const
134 c.s << class_name() << "('" << name << "'";
135 if (TeX_name != "\\mbox{" + name + "}")
136 c.s << ",TeX_name='" << TeX_name << "'";
140 bool constant::info(unsigned inf) const
142 if (inf == info_flags::polynomial)
144 if (inf == info_flags::real)
145 return domain==domain::real || domain==domain::positive ;
146 if (inf==info_flags::positive || inf==info_flags::nonnegative)
147 return domain == domain::positive;
149 return inherited::info(inf);
152 ex constant::evalf(int level) const
157 return number.evalf();
162 bool constant::is_polynomial(const ex & var) const
167 ex constant::conjugate() const
169 if ( domain==domain::real || domain==domain::positive )
171 return conjugate_function(*this).hold();
174 ex constant::real_part() const
176 if ( domain==domain::real || domain==domain::positive )
178 return real_part_function(*this).hold();
181 ex constant::imag_part() const
183 if ( domain==domain::real || domain==domain::positive )
185 return imag_part_function(*this).hold();
190 /** Implementation of ex::diff() for a constant always returns 0.
193 ex constant::derivative(const symbol & s) const
198 int constant::compare_same_type(const basic & other) const
200 GINAC_ASSERT(is_exactly_a<constant>(other));
201 const constant &o = static_cast<const constant &>(other);
203 if (serial == o.serial)
206 return serial < o.serial ? -1 : 1;
209 bool constant::is_equal_same_type(const basic & other) const
211 GINAC_ASSERT(is_exactly_a<constant>(other));
212 const constant &o = static_cast<const constant &>(other);
214 return serial == o.serial;
217 unsigned constant::calchash() const
219 const void* typeid_this = (const void*)typeid(*this).name();
220 hashvalue = golden_ratio_hash((p_int)typeid_this ^ serial);
222 setflag(status_flags::hash_calculated);
228 // new virtual functions which can be overridden by derived classes
234 // non-virtual functions in this class
240 // static member variables
243 unsigned constant::next_serial = 0;
249 /** Pi. (3.14159...) Diverts straight into CLN for evalf(). */
250 const constant Pi("Pi", PiEvalf, "\\pi", domain::positive);
252 /** Euler's constant. (0.57721...) Sometimes called Euler-Mascheroni constant.
253 * Diverts straight into CLN for evalf(). */
254 const constant Euler("Euler", EulerEvalf, "\\gamma_E", domain::positive);
256 /** Catalan's constant. (0.91597...) Diverts straight into CLN for evalf(). */
257 const constant Catalan("Catalan", CatalanEvalf, "G", domain::positive);