]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
fderivative wasn't printed correctly
[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-2003 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 #include <iostream>
26
27 #include "constant.h"
28 #include "numeric.h"
29 #include "ex.h"
30 #include "archive.h"
31 #include "utils.h"
32
33 namespace GiNaC {
34
35 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(constant, basic,
36   print_func<print_context>(&constant::do_print).
37   print_func<print_latex>(&constant::do_print_latex).
38   print_func<print_tree>(&constant::do_print_tree).
39   print_func<print_python_repr>(&constant::do_print_python_repr))
40
41 //////////
42 // default constructor
43 //////////
44
45 // public
46
47 constant::constant() : basic(TINFO_constant), ef(0), serial(next_serial++)
48 {
49         setflag(status_flags::evaluated | status_flags::expanded);
50 }
51
52 //////////
53 // other constructors
54 //////////
55
56 // public
57
58 constant::constant(const std::string & initname, evalffunctype efun, const std::string & texname)
59   : basic(TINFO_constant), name(initname), ef(efun), serial(next_serial++)
60 {
61         if (texname.empty())
62                 TeX_name = "\\mbox{" + name + "}";
63         else
64                 TeX_name = texname;
65         setflag(status_flags::evaluated | status_flags::expanded);
66 }
67
68 constant::constant(const std::string & initname, const numeric & initnumber, const std::string & texname)
69   : basic(TINFO_constant), name(initname), ef(0), number(initnumber), serial(next_serial++)
70 {
71         if (texname.empty())
72                 TeX_name = "\\mbox{" + name + "}";
73         else
74                 TeX_name = texname;
75         setflag(status_flags::evaluated | status_flags::expanded);
76 }
77
78 //////////
79 // archiving
80 //////////
81
82 constant::constant(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {}
83
84 ex constant::unarchive(const archive_node &n, lst &sym_lst)
85 {
86         // Find constant by name (!! this is bad: 'twould be better if there
87         // was a list of all global constants that we could search)
88         std::string s;
89         if (n.find_string("name", s)) {
90                 if (s == Pi.name)
91                         return Pi;
92                 else if (s == Catalan.name)
93                         return Catalan;
94                 else if (s == Euler.name)
95                         return Euler;
96                 else
97                         throw (std::runtime_error("unknown constant '" + s + "' in archive"));
98         } else
99                 throw (std::runtime_error("unnamed constant in archive"));
100 }
101
102 void constant::archive(archive_node &n) const
103 {
104         inherited::archive(n);
105         n.add_string("name", name);
106 }
107
108 //////////
109 // functions overriding virtual functions from base classes
110 //////////
111
112 // public
113
114 void constant::do_print(const print_context & c, unsigned level) const
115 {
116         c.s << name;
117 }
118
119 void constant::do_print_tree(const print_tree & c, unsigned level) const
120 {
121         c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
122             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
123             << std::endl;
124 }
125
126 void constant::do_print_latex(const print_latex & c, unsigned level) const
127 {
128         c.s << TeX_name;
129 }
130
131 void constant::do_print_python_repr(const print_python_repr & c, unsigned level) const
132 {
133         c.s << class_name() << "('" << name << "'";
134         if (TeX_name != "\\mbox{" + name + "}")
135                 c.s << ",TeX_name='" << TeX_name << "'";
136         c.s << ')';
137 }
138
139 ex constant::evalf(int level) const
140 {
141         if (ef!=0) {
142                 return ef();
143         } else {
144                 return number.evalf();
145         }
146         return *this;
147 }
148
149 // protected
150
151 /** Implementation of ex::diff() for a constant always returns 0.
152  *
153  *  @see ex::diff */
154 ex constant::derivative(const symbol & s) const
155 {
156         return _ex0;
157 }
158
159 int constant::compare_same_type(const basic & other) const
160 {
161         GINAC_ASSERT(is_exactly_a<constant>(other));
162         const constant &o = static_cast<const constant &>(other);
163
164         if (serial == o.serial)
165                 return 0;
166         else
167                 return serial < o.serial ? -1 : 1;
168 }
169
170 bool constant::is_equal_same_type(const basic & other) const
171 {
172         GINAC_ASSERT(is_exactly_a<constant>(other));
173         const constant &o = static_cast<const constant &>(other);
174
175         return serial == o.serial;
176 }
177
178 unsigned constant::calchash() const
179 {
180         hashvalue = golden_ratio_hash(tinfo() ^ serial);
181
182         setflag(status_flags::hash_calculated);
183
184         return hashvalue;
185 }
186
187 //////////
188 // new virtual functions which can be overridden by derived classes
189 //////////
190
191 // none
192
193 //////////
194 // non-virtual functions in this class
195 //////////
196
197 // none
198
199 //////////
200 // static member variables
201 //////////
202
203 unsigned constant::next_serial = 0;
204
205 //////////
206 // global constants
207 //////////
208
209 /**  Pi. (3.14159...)  Diverts straight into CLN for evalf(). */
210 const constant Pi("Pi", PiEvalf, "\\pi");
211
212 /** Euler's constant. (0.57721...)  Sometimes called Euler-Mascheroni constant.
213  *  Diverts straight into CLN for evalf(). */
214 const constant Euler("Euler", EulerEvalf, "\\gamma_E");
215
216 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
217 const constant Catalan("Catalan", CatalanEvalf, "G");
218
219 } // namespace GiNaC