3 * Implementation of GiNaC's constant types and some special constants. */
6 * GiNaC Copyright (C) 1999-2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 GINAC_IMPLEMENT_REGISTERED_CLASS(constant, basic)
39 // default constructor
44 constant::constant() : basic(TINFO_constant), ef(0), serial(next_serial++)
46 setflag(status_flags::evaluated | status_flags::expanded);
55 constant::constant(const std::string & initname, evalffunctype efun, const std::string & texname)
56 : basic(TINFO_constant), name(initname), ef(efun), serial(next_serial++)
59 TeX_name = "\\mbox{" + name + "}";
62 setflag(status_flags::evaluated | status_flags::expanded);
65 constant::constant(const std::string & initname, const numeric & initnumber, const std::string & texname)
66 : basic(TINFO_constant), name(initname), ef(0), number(initnumber), serial(next_serial++)
69 TeX_name = "\\mbox{" + name + "}";
72 setflag(status_flags::evaluated | status_flags::expanded);
79 constant::constant(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {}
81 ex constant::unarchive(const archive_node &n, lst &sym_lst)
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)
86 if (n.find_string("name", s)) {
89 else if (s == Catalan.name)
91 else if (s == Euler.name)
94 throw (std::runtime_error("unknown constant '" + s + "' in archive"));
96 throw (std::runtime_error("unnamed constant in archive"));
99 void constant::archive(archive_node &n) const
101 inherited::archive(n);
102 n.add_string("name", name);
106 // functions overriding virtual functions from base classes
111 void constant::print(const print_context & c, unsigned level) const
113 if (is_a<print_tree>(c)) {
114 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
115 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
117 } else if (is_a<print_latex>(c)) {
119 } else if (is_a<print_python_repr>(c)) {
120 c.s << class_name() << "('" << name << "'";
121 if (TeX_name != "\\mbox{" + name + "}")
122 c.s << ",TeX_name='" << TeX_name << "'";
128 ex constant::evalf(int level) const
133 return number.evalf();
140 /** Implementation of ex::diff() for a constant always returns 0.
143 ex constant::derivative(const symbol & s) const
148 int constant::compare_same_type(const basic & other) const
150 GINAC_ASSERT(is_exactly_a<constant>(other));
151 const constant &o = static_cast<const constant &>(other);
153 if (serial == o.serial)
156 return serial < o.serial ? -1 : 1;
159 bool constant::is_equal_same_type(const basic & other) const
161 GINAC_ASSERT(is_exactly_a<constant>(other));
162 const constant &o = static_cast<const constant &>(other);
164 return serial == o.serial;
167 unsigned constant::calchash() const
169 hashvalue = golden_ratio_hash(tinfo() ^ serial);
171 setflag(status_flags::hash_calculated);
177 // new virtual functions which can be overridden by derived classes
183 // non-virtual functions in this class
189 // static member variables
192 unsigned constant::next_serial = 0;
198 /** Pi. (3.14159...) Diverts straight into CLN for evalf(). */
199 const constant Pi("Pi", PiEvalf, "\\pi");
201 /** Euler's constant. (0.57721...) Sometimes called Euler-Mascheroni constant.
202 * Diverts straight into CLN for evalf(). */
203 const constant Euler("Euler", EulerEvalf, "\\gamma_E");
205 /** Catalan's constant. (0.91597...) Diverts straight into CLN for evalf(). */
206 const constant Catalan("Catalan", CatalanEvalf, "G");