3 * Implementation of GiNaC's constant types and some special constants. */
6 * GiNaC Copyright (C) 1999-2004 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
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() : basic(TINFO_constant), ef(0), serial(next_serial++)
49 setflag(status_flags::evaluated | status_flags::expanded);
58 constant::constant(const std::string & initname, evalffunctype efun, const std::string & texname)
59 : basic(TINFO_constant), name(initname), ef(efun), serial(next_serial++)
62 TeX_name = "\\mbox{" + name + "}";
65 setflag(status_flags::evaluated | status_flags::expanded);
68 constant::constant(const std::string & initname, const numeric & initnumber, const std::string & texname)
69 : basic(TINFO_constant), name(initname), ef(0), number(initnumber), serial(next_serial++)
72 TeX_name = "\\mbox{" + name + "}";
75 setflag(status_flags::evaluated | status_flags::expanded);
82 constant::constant(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {}
84 ex constant::unarchive(const archive_node &n, lst &sym_lst)
86 // Find constant by name (!! this is bad: 'twould be better if there
87 // was a list of all global constants that we could search)
89 if (n.find_string("name", s)) {
92 else if (s == Catalan.name)
94 else if (s == Euler.name)
97 throw (std::runtime_error("unknown constant '" + s + "' in archive"));
99 throw (std::runtime_error("unnamed constant in archive"));
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 != "\\mbox{" + name + "}")
135 c.s << ",TeX_name='" << TeX_name << "'";
139 ex constant::evalf(int level) const
144 return number.evalf();
151 /** Implementation of ex::diff() for a constant always returns 0.
154 ex constant::derivative(const symbol & s) const
159 int constant::compare_same_type(const basic & other) const
161 GINAC_ASSERT(is_exactly_a<constant>(other));
162 const constant &o = static_cast<const constant &>(other);
164 if (serial == o.serial)
167 return serial < o.serial ? -1 : 1;
170 bool constant::is_equal_same_type(const basic & other) const
172 GINAC_ASSERT(is_exactly_a<constant>(other));
173 const constant &o = static_cast<const constant &>(other);
175 return serial == o.serial;
178 unsigned constant::calchash() const
180 hashvalue = golden_ratio_hash(tinfo() ^ serial);
182 setflag(status_flags::hash_calculated);
188 // new virtual functions which can be overridden by derived classes
194 // non-virtual functions in this class
200 // static member variables
203 unsigned constant::next_serial = 0;
209 /** Pi. (3.14159...) Diverts straight into CLN for evalf(). */
210 const constant Pi("Pi", PiEvalf, "\\pi");
212 /** Euler's constant. (0.57721...) Sometimes called Euler-Mascheroni constant.
213 * Diverts straight into CLN for evalf(). */
214 const constant Euler("Euler", EulerEvalf, "\\gamma_E");
216 /** Catalan's constant. (0.91597...) Diverts straight into CLN for evalf(). */
217 const constant Catalan("Catalan", CatalanEvalf, "G");