3 * Implementation of GiNaC's constant types and some special constants. */
6 * GiNaC Copyright (C) 1999-2001 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 debugmsg("constant default ctor",LOGLEVEL_CONSTRUCT);
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++)
81 debugmsg("constant ctor from string, function",LOGLEVEL_CONSTRUCT);
83 TeX_name = "\\mbox{" + name + "}";
86 setflag(status_flags::evaluated);
89 constant::constant(const std::string & initname, const numeric & initnumber, const std::string & texname)
90 : basic(TINFO_constant), name(initname), ef(0), number(new numeric(initnumber)), serial(next_serial++)
92 debugmsg("constant ctor from string, numeric",LOGLEVEL_CONSTRUCT);
94 TeX_name = "\\mbox{" + name + "}";
97 setflag(status_flags::evaluated);
104 constant::constant(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
106 debugmsg("constant ctor from archive_node", LOGLEVEL_CONSTRUCT);
109 ex constant::unarchive(const archive_node &n, const lst &sym_lst)
111 // Find constant by name (!! this is bad: 'twould be better if there
112 // was a list of all global constants that we could search)
114 if (n.find_string("name", s)) {
117 else if (s == Catalan.name)
119 else if (s == Euler.name)
122 throw (std::runtime_error("unknown constant '" + s + "' in archive"));
124 throw (std::runtime_error("unnamed constant in archive"));
127 void constant::archive(archive_node &n) const
129 inherited::archive(n);
130 n.add_string("name", name);
134 // functions overriding virtual functions from bases classes
139 void constant::print(const print_context & c, unsigned level) const
141 debugmsg("constant print", LOGLEVEL_PRINT);
143 if (is_of_type(c, print_tree)) {
144 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
145 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
147 } else if (is_of_type(c, print_latex))
153 int constant::degree(const ex & s) const
155 return is_equal(*s.bp) ? 1 : 0;
158 int constant::ldegree(const ex & s) const
160 return is_equal(*s.bp) ? 1 : 0;
163 ex constant::coeff(const ex & s, int n) const
166 return n==1 ? _ex1() : _ex0();
168 return n==0 ? *this : _ex0();
171 ex constant::evalf(int level) const
175 } else if (number != 0) {
176 return number->evalf();
183 /** Implementation of ex::diff() for a constant. It always returns 0.
186 ex constant::derivative(const symbol & s) const
191 int constant::compare_same_type(const basic & other) const
193 GINAC_ASSERT(is_exactly_of_type(other, constant));
194 // const constant & o=static_cast<constant &>(const_cast<basic &>(other));
195 // return name.compare(o.name);
196 const constant *o = static_cast<const constant *>(&other);
197 if (serial==o->serial) return 0;
198 return serial < o->serial ? -1 : 1;
201 bool constant::is_equal_same_type(const basic & other) const
203 GINAC_ASSERT(is_exactly_of_type(other, constant));
204 const constant *o = static_cast<const constant *>(&other);
205 return serial==o->serial;
208 unsigned constant::calchash(void) const
210 hashvalue = golden_ratio_hash(tinfo() ^ serial);
211 // mask out numeric hashes:
212 hashvalue &= 0x7FFFFFFFU;
214 setflag(status_flags::hash_calculated);
220 // new virtual functions which can be overridden by derived classes
226 // non-virtual functions in this class
232 // static member variables
235 unsigned constant::next_serial = 0;
241 /** Pi. (3.14159...) Diverts straight into CLN for evalf(). */
242 const constant Pi("Pi", PiEvalf, "\\pi");
244 /** Euler's constant. (0.57721...) Sometimes called Euler-Mascheroni constant.
245 * Diverts straight into CLN for evalf(). */
246 const constant Euler("Euler", EulerEvalf, "\\gamma_E");
248 /** Catalan's constant. (0.91597...) Diverts straight into CLN for evalf(). */
249 const constant Catalan("Catalan", CatalanEvalf, "G");