]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
- The status_flags::expanded is now used on some occasions.
[ginac.git] / ginac / constant.cpp
1 /** @file constant.cpp
2  *
3  *  Implementation of GiNaC's constant types and some special constants. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <string>
24 #include <stdexcept>
25
26 #include "constant.h"
27 #include "numeric.h"
28 #include "ex.h"
29 #include "archive.h"
30 #include "debugmsg.h"
31
32 #ifndef NO_NAMESPACE_GINAC
33 namespace GiNaC {
34 #endif // ndef NO_NAMESPACE_GINAC
35
36 GINAC_IMPLEMENT_REGISTERED_CLASS(constant, basic)
37
38 //////////
39 // default constructor, destructor, copy constructor assignment operator and helpers
40 //////////
41
42 // public
43
44 constant::constant() :
45     basic(TINFO_constant), name(""), ef(0),
46     number(0), serial(next_serial++)
47 {
48     debugmsg("constant default constructor",LOGLEVEL_CONSTRUCT);
49 }
50
51 constant::~constant()
52 {
53     debugmsg("constant destructor",LOGLEVEL_DESTRUCT);
54     destroy(0);
55 }
56
57 constant::constant(const constant & other)
58 {
59     debugmsg("constant copy constructor",LOGLEVEL_CONSTRUCT);
60     copy(other);
61 }
62
63 // protected
64
65 void constant::copy(const constant & other)
66 {
67     basic::copy(other);
68     name=other.name;
69     serial=other.serial;
70     ef=other.ef;
71     if (other.number != 0) {
72         number = new numeric(*other.number);
73     } else {
74         number = 0;
75     }
76     // fct_assigned=other.fct_assigned;
77 }
78
79 void constant::destroy(bool call_parent)
80 {
81     delete number;
82     if (call_parent)
83         basic::destroy(call_parent);
84 }
85
86 //////////
87 // other constructors
88 //////////
89
90 // public
91
92 constant::constant(const 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++)
96 {
97     debugmsg("constant constructor from string, function",LOGLEVEL_CONSTRUCT);
98 }
99
100 constant::constant(const string & initname, const numeric & initnumber) :
101     basic(TINFO_constant), name(initname), ef(0),
102     number(new numeric(initnumber)), /* fct_assigned(false),*/ serial(next_serial++)
103 {
104     debugmsg("constant constructor from string, numeric",LOGLEVEL_CONSTRUCT);
105 }
106
107 //////////
108 // archiving
109 //////////
110
111 /** Construct object from archive_node. */
112 constant::constant(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
113 {
114     debugmsg("constant constructor from archive_node", LOGLEVEL_CONSTRUCT);
115 }
116
117 /** Unarchive the object. */
118 ex constant::unarchive(const archive_node &n, const lst &sym_lst)
119 {
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)
122     string s;
123     if (n.find_string("name", s)) {
124         if (s == Pi.name)
125             return Pi;
126         else if (s == Catalan.name)
127             return Catalan;
128         else if (s == EulerGamma.name)
129             return EulerGamma;
130         else
131             throw (std::runtime_error("unknown constant '" + s + "' in archive"));
132     } else
133         throw (std::runtime_error("unnamed constant in archive"));
134 }
135
136 /** Archive the object. */
137 void constant::archive(archive_node &n) const
138 {
139     inherited::archive(n);
140     n.add_string("name", name);
141 }
142
143 //////////
144 // functions overriding virtual functions from bases classes
145 //////////
146
147 // public
148
149 basic * constant::duplicate() const
150 {
151     debugmsg("constant duplicate",LOGLEVEL_DUPLICATE);
152     return new constant(*this);
153 }
154
155 void constant::print(ostream & os, unsigned upper_precedence) const
156 {
157     debugmsg("constant print",LOGLEVEL_PRINT);
158     os << name;
159 }
160
161 void constant::printraw(ostream & os) const
162 {
163     debugmsg("constant printraw",LOGLEVEL_PRINT);
164     os << "constant(" << name << ")";
165 }
166
167 void constant::printtree(ostream & os, unsigned indent) const
168 {
169     debugmsg("constant printtree",LOGLEVEL_PRINT);
170     os << string(indent,' ') << name
171        << ", type=" << typeid(*this).name()
172        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
173        << ", flags=" << flags << endl;
174 }
175
176 void constant::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
177 {
178     debugmsg("constant print csrc",LOGLEVEL_PRINT);
179     os << name;
180 }
181
182 ex constant::evalf(int level) const
183 {
184     if (ef!=0) {
185         return ef();
186     } else if (number != 0) {
187         return *number;
188     }
189     return *this;
190 }
191
192 // protected
193
194 /** Implementation of ex::diff() for a constant. It always returns 0.
195  *
196  *  @see ex::diff */
197 ex constant::derivative(const symbol & s) const
198 {
199     return _ex0();
200 }
201
202 int constant::compare_same_type(const basic & other) const
203 {
204     GINAC_ASSERT(is_exactly_of_type(other, constant));
205     // const constant & o=static_cast<constant &>(const_cast<basic &>(other));
206     // return name.compare(o.name);
207     const constant *o = static_cast<const constant *>(&other);
208     if (serial==o->serial) return 0;
209     return serial < o->serial ? -1 : 1;
210 }
211
212 bool constant::is_equal_same_type(const basic & other) const
213 {
214     GINAC_ASSERT(is_exactly_of_type(other, constant));
215     const constant *o = static_cast<const constant *>(&other);
216     return serial==o->serial;
217 }
218
219 //////////
220 // new virtual functions which can be overridden by derived classes
221 //////////
222
223 // none
224
225 //////////
226 // non-virtual functions in this class
227 //////////
228
229 // none
230
231 //////////
232 // static member variables
233 //////////
234
235 unsigned constant::next_serial=0;
236
237 //////////
238 // global constants
239 //////////
240
241 const constant some_constant;
242 const type_info & typeid_constant=typeid(some_constant);
243
244 /**  Pi. (3.14159...)  Diverts straight into CLN for evalf(). */
245 const constant Pi("Pi", PiEvalf);
246 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
247 const constant EulerGamma("EulerGamma", EulerGammaEvalf);
248 /** Euler's constant. (0.57721...)  Sometimes called Euler-Mascheroni constant.
249  *  Diverts straight into CLN for evalf(). */
250 const constant Catalan("Catalan", CatalanEvalf);
251
252 #ifndef NO_NAMESPACE_GINAC
253 } // namespace GiNaC
254 #endif // ndef NO_NAMESPACE_GINAC