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