]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
- added the beta function to GiNaC
[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 {
43     debugmsg("constant destructor",LOGLEVEL_DESTRUCT);
44     destroy(0);
45 }
46
47 constant::constant(constant const & other)
48 {
49     debugmsg("constant copy constructor",LOGLEVEL_CONSTRUCT);
50     copy(other);
51 }
52
53 // protected
54
55 void constant::copy(constant const & other)
56 {
57     basic::copy(other);
58     name=other.name;
59     serial=other.serial;
60     ef=other.ef;
61     if (other.number != 0) {
62         number = new numeric(*other.number);
63     } else {
64         number = 0;
65     }
66     fct_assigned=other.fct_assigned;
67 }
68
69 void constant::destroy(bool call_parent)
70 {
71     delete number;
72     if (call_parent)
73         basic::destroy(call_parent);
74 }
75
76 //////////
77 // other constructors
78 //////////
79
80 // public
81
82 constant::constant(string const & initname, ex (*efun)()) :
83     basic(TINFO_constant), name(initname), ef(efun),
84     number(0), fct_assigned(true), serial(next_serial++)
85 {
86     debugmsg("constant constructor from string, function",LOGLEVEL_CONSTRUCT);
87 }
88
89 constant::constant(string const & initname, numeric const & initnumber) :
90     basic(TINFO_constant), name(initname), ef(0),
91     number(new numeric(initnumber)), fct_assigned(false), serial(next_serial++)
92 {
93     debugmsg("constant constructor from string, numeric",LOGLEVEL_CONSTRUCT);
94 }
95
96 //////////
97 // functions overriding virtual functions from bases classes
98 //////////
99
100 // public
101
102 basic * constant::duplicate() const
103 {
104     debugmsg("constant duplicate",LOGLEVEL_DUPLICATE);
105     return new constant(*this);
106 }
107
108 ex constant::evalf(int level) const
109 {
110     if (fct_assigned) {
111         return ef();
112     } else if (number != 0) {
113         return *number;
114     }
115     return *this;
116 }
117
118 // protected
119
120 int constant::compare_same_type(basic const & other) const
121 {
122     GINAC_ASSERT(is_exactly_of_type(other, constant));
123     // constant const & o=static_cast<constant &>(const_cast<basic &>(other));
124     // return name.compare(o.name);
125     const constant *o = static_cast<const constant *>(&other);
126     if (serial==o->serial) return 0;
127     return serial < o->serial ? -1 : 1;
128 }
129
130 bool constant::is_equal_same_type(basic const & other) const
131 {
132     GINAC_ASSERT(is_exactly_of_type(other, constant));
133     const constant *o = static_cast<const constant *>(&other);
134     return serial==o->serial;
135 }
136
137 //////////
138 // new virtual functions which can be overridden by derived classes
139 //////////
140
141 // none
142
143 //////////
144 // non-virtual functions in this class
145 //////////
146
147 // none
148
149 //////////
150 // static member variables
151 //////////
152
153 unsigned constant::next_serial=0;
154
155 //////////
156 // global constants
157 //////////
158
159 const constant some_constant("",0);
160 type_info const & typeid_constant=typeid(some_constant);
161
162 /**  Pi. (3.14159...)  Diverts straight into CLN for evalf(). */
163 const constant Pi("Pi", PiEvalf);
164 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
165 const constant EulerGamma("EulerGamma", EulerGammaEvalf);
166 /** Euler's constant. (0.57721...)  Sometimes called Euler-Mascheroni constant.
167  *  Diverts straight into CLN for evalf(). */
168 const constant Catalan("Catalan", CatalanEvalf);
169
170 #ifndef NO_GINAC_NAMESPACE
171 } // namespace GiNaC
172 #endif // ndef NO_GINAC_NAMESPACE