]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
pseries::expand(): do not generate zero terms.
[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 // protected
50
51 void constant::copy(const constant & other)
52 {
53         basic::copy(other);
54         name=other.name;
55         serial=other.serial;
56         ef=other.ef;
57         if (other.number != 0) {
58                 number = new numeric(*other.number);
59         } else {
60                 number = 0;
61         }
62 }
63
64 void constant::destroy(bool call_parent)
65 {
66         delete number;
67         if (call_parent)
68                 basic::destroy(call_parent);
69 }
70
71 //////////
72 // other constructors
73 //////////
74
75 // public
76
77 constant::constant(const std::string & initname, evalffunctype efun)
78   : basic(TINFO_constant), name(initname), ef(efun), number(0), serial(next_serial++)
79 {
80         debugmsg("constant constructor from string, function",LOGLEVEL_CONSTRUCT);
81 }
82
83 constant::constant(const std::string & initname, const numeric & initnumber)
84   : basic(TINFO_constant), name(initname), ef(0), number(new numeric(initnumber)), serial(next_serial++)
85 {
86         debugmsg("constant constructor from string, numeric",LOGLEVEL_CONSTRUCT);
87 }
88
89 //////////
90 // archiving
91 //////////
92
93 /** Construct object from archive_node. */
94 constant::constant(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
95 {
96         debugmsg("constant constructor from archive_node", LOGLEVEL_CONSTRUCT);
97 }
98
99 /** Unarchive the object. */
100 ex constant::unarchive(const archive_node &n, const lst &sym_lst)
101 {
102         // Find constant by name (!! this is bad: 'twould be better if there
103         // was a list of all global constants that we could search)
104         std::string s;
105         if (n.find_string("name", s)) {
106                 if (s == Pi.name)
107                         return Pi;
108                 else if (s == Catalan.name)
109                         return Catalan;
110                 else if (s == Euler.name)
111                         return Euler;
112                 else
113                         throw (std::runtime_error("unknown constant '" + s + "' in archive"));
114         } else
115                 throw (std::runtime_error("unnamed constant in archive"));
116 }
117
118 /** Archive the object. */
119 void constant::archive(archive_node &n) const
120 {
121         inherited::archive(n);
122         n.add_string("name", name);
123 }
124
125 //////////
126 // functions overriding virtual functions from bases classes
127 //////////
128
129 // public
130
131 void constant::print(std::ostream & os, unsigned upper_precedence) const
132 {
133         debugmsg("constant print",LOGLEVEL_PRINT);
134         os << name;
135 }
136
137 void constant::printraw(std::ostream & os) const
138 {
139         debugmsg("constant printraw",LOGLEVEL_PRINT);
140         os << "constant(" << name << ")";
141 }
142
143 void constant::printtree(std::ostream & os, unsigned indent) const
144 {
145         debugmsg("constant printtree",LOGLEVEL_PRINT);
146         os << std::string(indent,' ') << name
147            << ", type=" << class_name()
148            << ", hash=" << hashvalue
149            << " (0x" << std::hex << hashvalue << std::dec << ")"
150            << ", flags=" << flags << std::endl;
151 }
152
153 void constant::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
154 {
155         debugmsg("constant print csrc",LOGLEVEL_PRINT);
156         os << name;
157 }
158
159 ex constant::evalf(int level) const
160 {
161         if (ef!=0) {
162                 return ef();
163         } else if (number != 0) {
164                 return *number;
165         }
166         return *this;
167 }
168
169 // protected
170
171 /** Implementation of ex::diff() for a constant. It always returns 0.
172  *
173  *  @see ex::diff */
174 ex constant::derivative(const symbol & s) const
175 {
176         return _ex0();
177 }
178
179 int constant::compare_same_type(const basic & other) const
180 {
181         GINAC_ASSERT(is_exactly_of_type(other, constant));
182         // const constant & o=static_cast<constant &>(const_cast<basic &>(other));
183         // return name.compare(o.name);
184         const constant *o = static_cast<const constant *>(&other);
185         if (serial==o->serial) return 0;
186         return serial < o->serial ? -1 : 1;
187 }
188
189 bool constant::is_equal_same_type(const basic & other) const
190 {
191         GINAC_ASSERT(is_exactly_of_type(other, constant));
192         const constant *o = static_cast<const constant *>(&other);
193         return serial==o->serial;
194 }
195
196 //////////
197 // new virtual functions which can be overridden by derived classes
198 //////////
199
200 // none
201
202 //////////
203 // non-virtual functions in this class
204 //////////
205
206 // none
207
208 //////////
209 // static member variables
210 //////////
211
212 unsigned constant::next_serial=0;
213
214 //////////
215 // global constants
216 //////////
217
218 /**  Pi. (3.14159...)  Diverts straight into CLN for evalf(). */
219 const constant Pi("Pi", PiEvalf);
220
221 /** Euler's constant. (0.57721...)  Sometimes called Euler-Mascheroni constant.
222  *  Diverts straight into CLN for evalf(). */
223 const constant Euler("Euler", EulerEvalf);
224
225 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
226 const constant Catalan("Catalan", CatalanEvalf);
227
228 #ifndef NO_NAMESPACE_GINAC
229 } // namespace GiNaC
230 #endif // ndef NO_NAMESPACE_GINAC