]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
some changes to allow GiNaC to cooperate with cint:
[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 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 "debugmsg.h"
30
31 #ifndef NO_GINAC_NAMESPACE
32 namespace GiNaC {
33 #endif // ndef NO_GINAC_NAMESPACE
34
35 //////////
36 // default constructor, destructor, copy constructor assignment operator and helpers
37 //////////
38
39 // public
40
41 constant::constant() :
42     basic(TINFO_constant), name(""), ef(0),
43     number(0), serial(next_serial++)
44 {
45     debugmsg("constant default constructor",LOGLEVEL_CONSTRUCT);
46 }
47
48 constant::~constant()
49 {
50     debugmsg("constant destructor",LOGLEVEL_DESTRUCT);
51     destroy(0);
52 }
53
54 constant::constant(constant const & other)
55 {
56     debugmsg("constant copy constructor",LOGLEVEL_CONSTRUCT);
57     copy(other);
58 }
59
60 // protected
61
62 void constant::copy(constant const & other)
63 {
64     basic::copy(other);
65     name=other.name;
66     serial=other.serial;
67     ef=other.ef;
68     if (other.number != 0) {
69         number = new numeric(*other.number);
70     } else {
71         number = 0;
72     }
73     // fct_assigned=other.fct_assigned;
74 }
75
76 void constant::destroy(bool call_parent)
77 {
78     delete number;
79     if (call_parent)
80         basic::destroy(call_parent);
81 }
82
83 //////////
84 // other constructors
85 //////////
86
87 // public
88
89 constant::constant(string const & initname, evalffunctype efun) :
90     basic(TINFO_constant), name(initname), ef(efun),
91     // number(0), fct_assigned(true), serial(next_serial++)
92     number(0), serial(next_serial++)
93 {
94     debugmsg("constant constructor from string, function",LOGLEVEL_CONSTRUCT);
95 }
96
97 constant::constant(string const & initname, numeric const & initnumber) :
98     basic(TINFO_constant), name(initname), ef(0),
99     number(new numeric(initnumber)), /* fct_assigned(false),*/ serial(next_serial++)
100 {
101     debugmsg("constant constructor from string, numeric",LOGLEVEL_CONSTRUCT);
102 }
103
104 //////////
105 // functions overriding virtual functions from bases classes
106 //////////
107
108 // public
109
110 basic * constant::duplicate() const
111 {
112     debugmsg("constant duplicate",LOGLEVEL_DUPLICATE);
113     return new constant(*this);
114 }
115
116 void constant::print(ostream & os, unsigned upper_precedence) const
117 {
118     debugmsg("constant print",LOGLEVEL_PRINT);
119     os << name;
120 }
121
122 void constant::printraw(ostream & os) const
123 {
124     debugmsg("constant printraw",LOGLEVEL_PRINT);
125     os << "constant(" << name << ")";
126 }
127
128 void constant::printtree(ostream & os, unsigned indent) const
129 {
130     debugmsg("constant printtree",LOGLEVEL_PRINT);
131     os << string(indent,' ') << name
132        << ", type=" << typeid(*this).name()
133        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
134        << ", flags=" << flags << endl;
135 }
136
137 void constant::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
138 {
139     debugmsg("constant print csrc",LOGLEVEL_PRINT);
140     os << name;
141 }
142
143 ex constant::evalf(int level) const
144 {
145     if (ef!=0) {
146         return ef();
147     } else if (number != 0) {
148         return *number;
149     }
150     return *this;
151 }
152
153 // protected
154
155 int constant::compare_same_type(basic const & other) const
156 {
157     GINAC_ASSERT(is_exactly_of_type(other, constant));
158     // constant const & o=static_cast<constant &>(const_cast<basic &>(other));
159     // return name.compare(o.name);
160     const constant *o = static_cast<const constant *>(&other);
161     if (serial==o->serial) return 0;
162     return serial < o->serial ? -1 : 1;
163 }
164
165 bool constant::is_equal_same_type(basic const & other) const
166 {
167     GINAC_ASSERT(is_exactly_of_type(other, constant));
168     const constant *o = static_cast<const constant *>(&other);
169     return serial==o->serial;
170 }
171
172 //////////
173 // new virtual functions which can be overridden by derived classes
174 //////////
175
176 // none
177
178 //////////
179 // non-virtual functions in this class
180 //////////
181
182 // none
183
184 //////////
185 // static member variables
186 //////////
187
188 unsigned constant::next_serial=0;
189
190 //////////
191 // global constants
192 //////////
193
194 const constant some_constant;
195 type_info const & typeid_constant=typeid(some_constant);
196
197 /**  Pi. (3.14159...)  Diverts straight into CLN for evalf(). */
198 const constant Pi("Pi", PiEvalf);
199 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
200 const constant EulerGamma("EulerGamma", EulerGammaEvalf);
201 /** Euler's constant. (0.57721...)  Sometimes called Euler-Mascheroni constant.
202  *  Diverts straight into CLN for evalf(). */
203 const constant Catalan("Catalan", CatalanEvalf);
204
205 #ifndef NO_GINAC_NAMESPACE
206 } // namespace GiNaC
207 #endif // ndef NO_GINAC_NAMESPACE