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