]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
- changed behaviour of numeric::is_rational() and added numeric::is_cinteger()
[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, ex (*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 ex constant::evalf(int level) const
117 {
118     if (ef!=0) {
119         return ef();
120     } else if (number != 0) {
121         return *number;
122     }
123     return *this;
124 }
125
126 // protected
127
128 int constant::compare_same_type(basic const & other) const
129 {
130     GINAC_ASSERT(is_exactly_of_type(other, constant));
131     // constant const & o=static_cast<constant &>(const_cast<basic &>(other));
132     // return name.compare(o.name);
133     const constant *o = static_cast<const constant *>(&other);
134     if (serial==o->serial) return 0;
135     return serial < o->serial ? -1 : 1;
136 }
137
138 bool constant::is_equal_same_type(basic const & other) const
139 {
140     GINAC_ASSERT(is_exactly_of_type(other, constant));
141     const constant *o = static_cast<const constant *>(&other);
142     return serial==o->serial;
143 }
144
145 //////////
146 // new virtual functions which can be overridden by derived classes
147 //////////
148
149 // none
150
151 //////////
152 // non-virtual functions in this class
153 //////////
154
155 // none
156
157 //////////
158 // static member variables
159 //////////
160
161 unsigned constant::next_serial=0;
162
163 //////////
164 // global constants
165 //////////
166
167 const constant some_constant;
168 type_info const & typeid_constant=typeid(some_constant);
169
170 /**  Pi. (3.14159...)  Diverts straight into CLN for evalf(). */
171 const constant Pi("Pi", PiEvalf);
172 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
173 const constant EulerGamma("EulerGamma", EulerGammaEvalf);
174 /** Euler's constant. (0.57721...)  Sometimes called Euler-Mascheroni constant.
175  *  Diverts straight into CLN for evalf(). */
176 const constant Catalan("Catalan", CatalanEvalf);
177
178 #ifndef NO_GINAC_NAMESPACE
179 } // namespace GiNaC
180 #endif // ndef NO_GINAC_NAMESPACE