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 ctor, dtor, copy ctor, assignment operator and helpers
44 constant::constant() : basic(TINFO_constant), ef(0), number(0), serial(next_serial++)
46 setflag(status_flags::evaluated | status_flags::expanded);
51 /** For use by copy ctor and assignment operator. */
52 void constant::copy(const constant & other)
54 inherited::copy(other);
56 TeX_name = other.TeX_name;
57 serial = other.serial;
59 if (other.number != 0)
60 number = new numeric(*other.number);
65 void constant::destroy(bool call_parent)
69 inherited::destroy(call_parent);
78 constant::constant(const std::string & initname, evalffunctype efun, const std::string & texname)
79 : basic(TINFO_constant), name(initname), ef(efun), number(0), serial(next_serial++)
82 TeX_name = "\\mbox{" + name + "}";
85 setflag(status_flags::evaluated | status_flags::expanded);
88 constant::constant(const std::string & initname, const numeric & initnumber, const std::string & texname)
89 : basic(TINFO_constant), name(initname), ef(0), number(new numeric(initnumber)), serial(next_serial++)
92 TeX_name = "\\mbox{" + name + "}";
95 setflag(status_flags::evaluated | status_flags::expanded);
102 constant::constant(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {}
104 ex constant::unarchive(const archive_node &n, lst &sym_lst)
106 // Find constant by name (!! this is bad: 'twould be better if there
107 // was a list of all global constants that we could search)
109 if (n.find_string("name", s)) {
112 else if (s == Catalan.name)
114 else if (s == Euler.name)
117 throw (std::runtime_error("unknown constant '" + s + "' in archive"));
119 throw (std::runtime_error("unnamed constant in archive"));
122 void constant::archive(archive_node &n) const
124 inherited::archive(n);
125 n.add_string("name", name);
129 // functions overriding virtual functions from base classes
134 void constant::print(const print_context & c, unsigned level) const
136 if (is_a<print_tree>(c)) {
137 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
138 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
140 } else if (is_a<print_latex>(c)) {
142 } else if (is_a<print_python_repr>(c)) {
143 c.s << class_name() << "('" << name << "'";
144 if (TeX_name != "\\mbox{" + name + "}")
145 c.s << ",TeX_name='" << TeX_name << "'";
151 ex constant::evalf(int level) const
155 } else if (number != 0) {
156 return number->evalf();
163 /** Implementation of ex::diff() for a constant always returns 0.
166 ex constant::derivative(const symbol & s) const
171 int constant::compare_same_type(const basic & other) const
173 GINAC_ASSERT(is_exactly_a<constant>(other));
174 const constant &o = static_cast<const constant &>(other);
176 if (serial == o.serial)
179 return serial < o.serial ? -1 : 1;
182 bool constant::is_equal_same_type(const basic & other) const
184 GINAC_ASSERT(is_exactly_a<constant>(other));
185 const constant &o = static_cast<const constant &>(other);
187 return serial == o.serial;
190 unsigned constant::calchash(void) const
192 hashvalue = golden_ratio_hash(tinfo() ^ serial);
194 setflag(status_flags::hash_calculated);
200 // new virtual functions which can be overridden by derived classes
206 // non-virtual functions in this class
212 // static member variables
215 unsigned constant::next_serial = 0;
221 /** Pi. (3.14159...) Diverts straight into CLN for evalf(). */
222 const constant Pi("Pi", PiEvalf, "\\pi");
224 /** Euler's constant. (0.57721...) Sometimes called Euler-Mascheroni constant.
225 * Diverts straight into CLN for evalf(). */
226 const constant Euler("Euler", EulerEvalf, "\\gamma_E");
228 /** Catalan's constant. (0.91597...) Diverts straight into CLN for evalf(). */
229 const constant Catalan("Catalan", CatalanEvalf, "G");