]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
0286b0e9cf2fdcc821b51fe30b8b0a8f42e4dd07
[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-2001 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() : basic(TINFO_constant), name(""), ef(0), number(0), serial(next_serial++)
45 {
46         debugmsg("constant default constructor",LOGLEVEL_CONSTRUCT);
47 }
48
49 constant::~constant()
50 {
51         debugmsg("constant destructor",LOGLEVEL_DESTRUCT);
52         destroy(0);
53 }
54
55 constant::constant(const constant & other)
56 {
57         debugmsg("constant copy constructor",LOGLEVEL_CONSTRUCT);
58         copy(other);
59 }
60
61 // protected
62
63 void constant::copy(const constant & other)
64 {
65         basic::copy(other);
66         name=other.name;
67         serial=other.serial;
68         ef=other.ef;
69         if (other.number != 0) {
70                 number = new numeric(*other.number);
71         } else {
72                 number = 0;
73         }
74         // fct_assigned=other.fct_assigned;
75 }
76
77 void constant::destroy(bool call_parent)
78 {
79         delete number;
80         if (call_parent)
81                 basic::destroy(call_parent);
82 }
83
84 //////////
85 // other constructors
86 //////////
87
88 // public
89
90 constant::constant(const std::string & initname, evalffunctype efun)
91   : basic(TINFO_constant), name(initname), ef(efun), number(0), serial(next_serial++)
92 {
93         debugmsg("constant constructor from string, function",LOGLEVEL_CONSTRUCT);
94 }
95
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++)
98 {
99         debugmsg("constant constructor from string, numeric",LOGLEVEL_CONSTRUCT);
100 }
101
102 //////////
103 // archiving
104 //////////
105
106 /** Construct object from archive_node. */
107 constant::constant(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
108 {
109         debugmsg("constant constructor from archive_node", LOGLEVEL_CONSTRUCT);
110 }
111
112 /** Unarchive the object. */
113 ex constant::unarchive(const archive_node &n, const lst &sym_lst)
114 {
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)
117         std::string s;
118         if (n.find_string("name", s)) {
119                 if (s == Pi.name)
120                         return Pi;
121                 else if (s == Catalan.name)
122                         return Catalan;
123                 else if (s == Euler.name)
124                         return Euler;
125                 else
126                         throw (std::runtime_error("unknown constant '" + s + "' in archive"));
127         } else
128                 throw (std::runtime_error("unnamed constant in archive"));
129 }
130
131 /** Archive the object. */
132 void constant::archive(archive_node &n) const
133 {
134         inherited::archive(n);
135         n.add_string("name", name);
136 }
137
138 //////////
139 // functions overriding virtual functions from bases classes
140 //////////
141
142 // public
143
144 basic * constant::duplicate() const
145 {
146         debugmsg("constant duplicate",LOGLEVEL_DUPLICATE);
147         return new constant(*this);
148 }
149
150 void constant::print(std::ostream & os, unsigned upper_precedence) const
151 {
152         debugmsg("constant print",LOGLEVEL_PRINT);
153         os << name;
154 }
155
156 void constant::printraw(std::ostream & os) const
157 {
158         debugmsg("constant printraw",LOGLEVEL_PRINT);
159         os << "constant(" << name << ")";
160 }
161
162 void constant::printtree(std::ostream & os, unsigned indent) const
163 {
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;
170 }
171
172 void constant::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
173 {
174         debugmsg("constant print csrc",LOGLEVEL_PRINT);
175         os << name;
176 }
177
178 ex constant::evalf(int level) const
179 {
180         if (ef!=0) {
181                 return ef();
182         } else if (number != 0) {
183                 return *number;
184         }
185         return *this;
186 }
187
188 // protected
189
190 /** Implementation of ex::diff() for a constant. It always returns 0.
191  *
192  *  @see ex::diff */
193 ex constant::derivative(const symbol & s) const
194 {
195         return _ex0();
196 }
197
198 int constant::compare_same_type(const basic & other) const
199 {
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;
206 }
207
208 bool constant::is_equal_same_type(const basic & other) const
209 {
210         GINAC_ASSERT(is_exactly_of_type(other, constant));
211         const constant *o = static_cast<const constant *>(&other);
212         return serial==o->serial;
213 }
214
215 //////////
216 // new virtual functions which can be overridden by derived classes
217 //////////
218
219 // none
220
221 //////////
222 // non-virtual functions in this class
223 //////////
224
225 // none
226
227 //////////
228 // static member variables
229 //////////
230
231 unsigned constant::next_serial=0;
232
233 //////////
234 // global constants
235 //////////
236
237 const constant some_constant;
238 const std::type_info & typeid_constant = typeid(some_constant);
239
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);
247
248 #ifndef NO_NAMESPACE_GINAC
249 } // namespace GiNaC
250 #endif // ndef NO_NAMESPACE_GINAC