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