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 debugmsg("constant destructor",LOGLEVEL_DESTRUCT);
55 constant::constant(const constant & other)
57 debugmsg("constant copy constructor",LOGLEVEL_CONSTRUCT);
63 void constant::copy(const constant & other)
69 if (other.number != 0) {
70 number = new numeric(*other.number);
74 // fct_assigned=other.fct_assigned;
77 void constant::destroy(bool call_parent)
81 basic::destroy(call_parent);
90 constant::constant(const std::string & initname, evalffunctype efun)
91 : basic(TINFO_constant), name(initname), ef(efun), number(0), serial(next_serial++)
93 debugmsg("constant constructor from string, function",LOGLEVEL_CONSTRUCT);
96 constant::constant(const std::string & initname, const numeric & initnumber)
97 : basic(TINFO_constant), name(initname), ef(0), number(new numeric(initnumber)), serial(next_serial++)
99 debugmsg("constant constructor from string, numeric",LOGLEVEL_CONSTRUCT);
106 /** Construct object from archive_node. */
107 constant::constant(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
109 debugmsg("constant constructor from archive_node", LOGLEVEL_CONSTRUCT);
112 /** Unarchive the object. */
113 ex constant::unarchive(const archive_node &n, const lst &sym_lst)
115 // Find constant by name (!! this is bad: 'twould be better if there
116 // was a list of all global constants that we could search)
118 if (n.find_string("name", s)) {
121 else if (s == Catalan.name)
123 else if (s == Euler.name)
126 throw (std::runtime_error("unknown constant '" + s + "' in archive"));
128 throw (std::runtime_error("unnamed constant in archive"));
131 /** Archive the object. */
132 void constant::archive(archive_node &n) const
134 inherited::archive(n);
135 n.add_string("name", name);
139 // functions overriding virtual functions from bases classes
144 basic * constant::duplicate() const
146 debugmsg("constant duplicate",LOGLEVEL_DUPLICATE);
147 return new constant(*this);
150 void constant::print(std::ostream & os, unsigned upper_precedence) const
152 debugmsg("constant print",LOGLEVEL_PRINT);
156 void constant::printraw(std::ostream & os) const
158 debugmsg("constant printraw",LOGLEVEL_PRINT);
159 os << "constant(" << name << ")";
162 void constant::printtree(std::ostream & os, unsigned indent) const
164 debugmsg("constant printtree",LOGLEVEL_PRINT);
165 os << std::string(indent,' ') << name
166 << ", type=" << class_name()
167 << ", hash=" << hashvalue
168 << " (0x" << std::hex << hashvalue << std::dec << ")"
169 << ", flags=" << flags << std::endl;
172 void constant::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
174 debugmsg("constant print csrc",LOGLEVEL_PRINT);
178 ex constant::evalf(int level) const
182 } else if (number != 0) {
190 /** Implementation of ex::diff() for a constant. It always returns 0.
193 ex constant::derivative(const symbol & s) const
198 int constant::compare_same_type(const basic & other) const
200 GINAC_ASSERT(is_exactly_of_type(other, constant));
201 // const constant & o=static_cast<constant &>(const_cast<basic &>(other));
202 // return name.compare(o.name);
203 const constant *o = static_cast<const constant *>(&other);
204 if (serial==o->serial) return 0;
205 return serial < o->serial ? -1 : 1;
208 bool constant::is_equal_same_type(const basic & other) const
210 GINAC_ASSERT(is_exactly_of_type(other, constant));
211 const constant *o = static_cast<const constant *>(&other);
212 return serial==o->serial;
216 // new virtual functions which can be overridden by derived classes
222 // non-virtual functions in this class
228 // static member variables
231 unsigned constant::next_serial=0;
237 const constant some_constant;
238 const std::type_info & typeid_constant = typeid(some_constant);
240 /** Pi. (3.14159...) Diverts straight into CLN for evalf(). */
241 const constant Pi("Pi", PiEvalf);
242 /** Euler's constant. (0.57721...) Sometimes called Euler-Mascheroni constant.
243 * Diverts straight into CLN for evalf(). */
244 const constant Euler("Euler", EulerEvalf);
245 /** Catalan's constant. (0.91597...) Diverts straight into CLN for evalf(). */
246 const constant Catalan("Catalan", CatalanEvalf);
248 #ifndef NO_NAMESPACE_GINAC
250 #endif // ndef NO_NAMESPACE_GINAC