3 * Implementation of GiNaC's constant types and some special constants. */
6 * GiNaC Copyright (C) 1999-2000 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() :
45 basic(TINFO_constant), name(""), ef(0),
46 number(0), serial(next_serial++)
48 debugmsg("constant default constructor",LOGLEVEL_CONSTRUCT);
53 debugmsg("constant destructor",LOGLEVEL_DESTRUCT);
57 constant::constant(const constant & other)
59 debugmsg("constant copy constructor",LOGLEVEL_CONSTRUCT);
65 void constant::copy(const constant & other)
71 if (other.number != 0) {
72 number = new numeric(*other.number);
76 // fct_assigned=other.fct_assigned;
79 void constant::destroy(bool call_parent)
83 basic::destroy(call_parent);
92 constant::constant(const std::string & initname, evalffunctype efun) :
93 basic(TINFO_constant), name(initname), ef(efun),
94 // number(0), fct_assigned(true), serial(next_serial++)
95 number(0), serial(next_serial++)
97 debugmsg("constant constructor from string, function",LOGLEVEL_CONSTRUCT);
100 constant::constant(const std::string & initname, const numeric & initnumber) :
101 basic(TINFO_constant), name(initname), ef(0),
102 number(new numeric(initnumber)), /* fct_assigned(false),*/ serial(next_serial++)
104 debugmsg("constant constructor from string, numeric",LOGLEVEL_CONSTRUCT);
111 /** Construct object from archive_node. */
112 constant::constant(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
114 debugmsg("constant constructor from archive_node", LOGLEVEL_CONSTRUCT);
117 /** Unarchive the object. */
118 ex constant::unarchive(const archive_node &n, const lst &sym_lst)
120 // Find constant by name (!! this is bad: 'twould be better if there
121 // was a list of all global constants that we could search)
123 if (n.find_string("name", s)) {
126 else if (s == Catalan.name)
128 else if (s == Euler.name)
131 throw (std::runtime_error("unknown constant '" + s + "' in archive"));
133 throw (std::runtime_error("unnamed constant in archive"));
136 /** Archive the object. */
137 void constant::archive(archive_node &n) const
139 inherited::archive(n);
140 n.add_string("name", name);
144 // functions overriding virtual functions from bases classes
149 basic * constant::duplicate() const
151 debugmsg("constant duplicate",LOGLEVEL_DUPLICATE);
152 return new constant(*this);
155 void constant::print(std::ostream & os, unsigned upper_precedence) const
157 debugmsg("constant print",LOGLEVEL_PRINT);
161 void constant::printraw(std::ostream & os) const
163 debugmsg("constant printraw",LOGLEVEL_PRINT);
164 os << "constant(" << name << ")";
167 void constant::printtree(std::ostream & os, unsigned indent) const
169 debugmsg("constant printtree",LOGLEVEL_PRINT);
170 os << std::string(indent,' ') << name
171 << ", type=" << typeid(*this).name()
172 << ", hash=" << hashvalue
173 << " (0x" << std::hex << hashvalue << std::dec << ")"
174 << ", flags=" << flags << std::endl;
177 void constant::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
179 debugmsg("constant print csrc",LOGLEVEL_PRINT);
183 ex constant::evalf(int level) const
187 } else if (number != 0) {
195 /** Implementation of ex::diff() for a constant. It always returns 0.
198 ex constant::derivative(const symbol & s) const
203 int constant::compare_same_type(const basic & other) const
205 GINAC_ASSERT(is_exactly_of_type(other, constant));
206 // const constant & o=static_cast<constant &>(const_cast<basic &>(other));
207 // return name.compare(o.name);
208 const constant *o = static_cast<const constant *>(&other);
209 if (serial==o->serial) return 0;
210 return serial < o->serial ? -1 : 1;
213 bool constant::is_equal_same_type(const basic & other) const
215 GINAC_ASSERT(is_exactly_of_type(other, constant));
216 const constant *o = static_cast<const constant *>(&other);
217 return serial==o->serial;
221 // new virtual functions which can be overridden by derived classes
227 // non-virtual functions in this class
233 // static member variables
236 unsigned constant::next_serial=0;
242 const constant some_constant;
243 const type_info & typeid_constant=typeid(some_constant);
245 /** Pi. (3.14159...) Diverts straight into CLN for evalf(). */
246 const constant Pi("Pi", PiEvalf);
247 /** Euler's constant. (0.57721...) Sometimes called Euler-Mascheroni constant.
248 * Diverts straight into CLN for evalf(). */
249 const constant Euler("Euler", EulerEvalf);
250 /** Catalan's constant. (0.91597...) Diverts straight into CLN for evalf(). */
251 const constant Catalan("Catalan", CatalanEvalf);
253 #ifndef NO_NAMESPACE_GINAC
255 #endif // ndef NO_NAMESPACE_GINAC