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
32 #ifndef NO_NAMESPACE_GINAC
34 #endif // ndef NO_NAMESPACE_GINAC
36 GINAC_IMPLEMENT_REGISTERED_CLASS(constant, basic)
39 // default constructor, destructor, copy constructor assignment operator and helpers
44 constant::constant() : basic(TINFO_constant), name(""), ef(0), number(0), serial(next_serial++)
46 debugmsg("constant default constructor",LOGLEVEL_CONSTRUCT);
51 void constant::copy(const constant & other)
57 if (other.number != 0) {
58 number = new numeric(*other.number);
62 // fct_assigned=other.fct_assigned;
65 void constant::destroy(bool call_parent)
69 basic::destroy(call_parent);
78 constant::constant(const std::string & initname, evalffunctype efun)
79 : basic(TINFO_constant), name(initname), ef(efun), number(0), serial(next_serial++)
81 debugmsg("constant constructor from string, function",LOGLEVEL_CONSTRUCT);
84 constant::constant(const std::string & initname, const numeric & initnumber)
85 : basic(TINFO_constant), name(initname), ef(0), number(new numeric(initnumber)), serial(next_serial++)
87 debugmsg("constant constructor from string, numeric",LOGLEVEL_CONSTRUCT);
94 /** Construct object from archive_node. */
95 constant::constant(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
97 debugmsg("constant constructor from archive_node", LOGLEVEL_CONSTRUCT);
100 /** Unarchive the object. */
101 ex constant::unarchive(const archive_node &n, const lst &sym_lst)
103 // Find constant by name (!! this is bad: 'twould be better if there
104 // was a list of all global constants that we could search)
106 if (n.find_string("name", s)) {
109 else if (s == Catalan.name)
111 else if (s == Euler.name)
114 throw (std::runtime_error("unknown constant '" + s + "' in archive"));
116 throw (std::runtime_error("unnamed constant in archive"));
119 /** Archive the object. */
120 void constant::archive(archive_node &n) const
122 inherited::archive(n);
123 n.add_string("name", name);
127 // functions overriding virtual functions from bases classes
132 basic * constant::duplicate() const
134 debugmsg("constant duplicate",LOGLEVEL_DUPLICATE);
135 return new constant(*this);
138 void constant::print(std::ostream & os, unsigned upper_precedence) const
140 debugmsg("constant print",LOGLEVEL_PRINT);
144 void constant::printraw(std::ostream & os) const
146 debugmsg("constant printraw",LOGLEVEL_PRINT);
147 os << "constant(" << name << ")";
150 void constant::printtree(std::ostream & os, unsigned indent) const
152 debugmsg("constant printtree",LOGLEVEL_PRINT);
153 os << std::string(indent,' ') << name
154 << ", type=" << class_name()
155 << ", hash=" << hashvalue
156 << " (0x" << std::hex << hashvalue << std::dec << ")"
157 << ", flags=" << flags << std::endl;
160 void constant::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
162 debugmsg("constant print csrc",LOGLEVEL_PRINT);
166 ex constant::evalf(int level) const
170 } else if (number != 0) {
178 /** Implementation of ex::diff() for a constant. It always returns 0.
181 ex constant::derivative(const symbol & s) const
186 int constant::compare_same_type(const basic & other) const
188 GINAC_ASSERT(is_exactly_of_type(other, constant));
189 // const constant & o=static_cast<constant &>(const_cast<basic &>(other));
190 // return name.compare(o.name);
191 const constant *o = static_cast<const constant *>(&other);
192 if (serial==o->serial) return 0;
193 return serial < o->serial ? -1 : 1;
196 bool constant::is_equal_same_type(const basic & other) const
198 GINAC_ASSERT(is_exactly_of_type(other, constant));
199 const constant *o = static_cast<const constant *>(&other);
200 return serial==o->serial;
204 // new virtual functions which can be overridden by derived classes
210 // non-virtual functions in this class
216 // static member variables
219 unsigned constant::next_serial=0;
225 /** Pi. (3.14159...) Diverts straight into CLN for evalf(). */
226 const constant Pi("Pi", PiEvalf);
228 /** Euler's constant. (0.57721...) Sometimes called Euler-Mascheroni constant.
229 * Diverts straight into CLN for evalf(). */
230 const constant Euler("Euler", EulerEvalf);
232 /** Catalan's constant. (0.91597...) Diverts straight into CLN for evalf(). */
233 const constant Catalan("Catalan", CatalanEvalf);
235 #ifndef NO_NAMESPACE_GINAC
237 #endif // ndef NO_NAMESPACE_GINAC