]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
- Fixed an assertion-thinko in matrix::fraction_free_elimination().
[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 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++)
96 {
97     debugmsg("constant constructor from string, function",LOGLEVEL_CONSTRUCT);
98 }
99
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++)
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     std::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 == Euler.name)
129             return Euler;
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(std::ostream & os, unsigned upper_precedence) const
156 {
157     debugmsg("constant print",LOGLEVEL_PRINT);
158     os << name;
159 }
160
161 void constant::printraw(std::ostream & os) const
162 {
163     debugmsg("constant printraw",LOGLEVEL_PRINT);
164     os << "constant(" << name << ")";
165 }
166
167 void constant::printtree(std::ostream & os, unsigned indent) const
168 {
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;
175 }
176
177 void constant::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
178 {
179     debugmsg("constant print csrc",LOGLEVEL_PRINT);
180     os << name;
181 }
182
183 ex constant::evalf(int level) const
184 {
185     if (ef!=0) {
186         return ef();
187     } else if (number != 0) {
188         return *number;
189     }
190     return *this;
191 }
192
193 // protected
194
195 /** Implementation of ex::diff() for a constant. It always returns 0.
196  *
197  *  @see ex::diff */
198 ex constant::derivative(const symbol & s) const
199 {
200     return _ex0();
201 }
202
203 int constant::compare_same_type(const basic & other) const
204 {
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;
211 }
212
213 bool constant::is_equal_same_type(const basic & other) const
214 {
215     GINAC_ASSERT(is_exactly_of_type(other, constant));
216     const constant *o = static_cast<const constant *>(&other);
217     return serial==o->serial;
218 }
219
220 //////////
221 // new virtual functions which can be overridden by derived classes
222 //////////
223
224 // none
225
226 //////////
227 // non-virtual functions in this class
228 //////////
229
230 // none
231
232 //////////
233 // static member variables
234 //////////
235
236 unsigned constant::next_serial=0;
237
238 //////////
239 // global constants
240 //////////
241
242 const constant some_constant;
243 const type_info & typeid_constant=typeid(some_constant);
244
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);
252
253 #ifndef NO_NAMESPACE_GINAC
254 } // namespace GiNaC
255 #endif // ndef NO_NAMESPACE_GINAC