]> www.ginac.de Git - ginac.git/blob - ginac/constant.cpp
- added spec file
[ginac.git] / ginac / constant.cpp
1 /** @file constant.cpp
2  *
3  *  Implementation of GiNaC's constant types and some special constants.
4  *
5  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <string>
23 #include <stdexcept>
24
25 #include "constant.h"
26 #include "numeric.h"
27 #include "ex.h"
28
29 //////////
30 // default constructor, destructor, copy constructor assignment operator and helpers
31 //////////
32
33 // public
34
35 constant::~constant()
36 {
37     debugmsg("constant destructor",LOGLEVEL_DESTRUCT);
38     destroy(0);
39 }
40
41 constant::constant(constant const & other)
42 {
43     debugmsg("constant copy constructor",LOGLEVEL_CONSTRUCT);
44     copy(other);
45 }
46
47 // protected
48
49 void constant::copy(constant const & other)
50 {
51     basic::copy(other);
52     name=other.name;
53     serial=other.serial;
54     ef=other.ef;
55     if (other.number != 0) {
56         number = new numeric(*other.number);
57     } else {
58         number = 0;
59     }
60     fct_assigned=other.fct_assigned;
61 }
62
63 void constant::destroy(bool call_parent)
64 {
65     delete number;
66     if (call_parent)
67         basic::destroy(call_parent);
68 }
69
70 //////////
71 // other constructors
72 //////////
73
74 // public
75
76 constant::constant(string const & initname, ex (*efun)()) :
77     basic(TINFO_constant), name(initname), ef(efun),
78     number(0), fct_assigned(true), serial(next_serial++)
79 {
80     debugmsg("constant constructor from string, function",LOGLEVEL_CONSTRUCT);
81 }
82
83 constant::constant(string const & initname, numeric const & initnumber) :
84     basic(TINFO_constant), name(initname), ef(0),
85     number(new numeric(initnumber)), fct_assigned(false), serial(next_serial++)
86 {
87     debugmsg("constant constructor from string, numeric",LOGLEVEL_CONSTRUCT);
88 }
89
90 //////////
91 // functions overriding virtual functions from bases classes
92 //////////
93
94 // public
95
96 basic * constant::duplicate() const
97 {
98     debugmsg("constant duplicate",LOGLEVEL_DUPLICATE);
99     return new constant(*this);
100 }
101
102 ex constant::evalf(int level) const
103 {
104     if (fct_assigned) {
105         return ef();
106     } else if (number != 0) {
107         return *number;
108     }
109     return *this;
110 }
111
112 // protected
113
114 int constant::compare_same_type(basic const & other) const
115 {
116     ASSERT(is_exactly_of_type(other, constant));
117     // constant const & o=static_cast<constant &>(const_cast<basic &>(other));
118     // return name.compare(o.name);
119     const constant *o = static_cast<const constant *>(&other);
120     if (serial==o->serial) return 0;
121     return serial < o->serial ? -1 : 1;
122 }
123
124 bool constant::is_equal_same_type(basic const & other) const
125 {
126     ASSERT(is_exactly_of_type(other, constant));
127     const constant *o = static_cast<const constant *>(&other);
128     return serial==o->serial;
129 }
130
131 //////////
132 // new virtual functions which can be overridden by derived classes
133 //////////
134
135 // none
136
137 //////////
138 // non-virtual functions in this class
139 //////////
140
141 // none
142
143 //////////
144 // static member variables
145 //////////
146
147 unsigned constant::next_serial=0;
148
149 //////////
150 // global constants
151 //////////
152
153 const constant some_constant("",0);
154 type_info const & typeid_constant=typeid(some_constant);
155
156 /**  Pi. (3.14159...)  Diverts straight into CLN for evalf(). */
157 const constant Pi("Pi", PiEvalf);
158 /** Catalan's constant. (0.91597...)  Diverts straight into CLN for evalf(). */
159 const constant EulerGamma("EulerGamma", EulerGammaEvalf);
160 /** Euler's constant. (0.57721...)  Sometimes called Euler-Mascheroni constant.
161  *  Diverts straight into CLN for evalf(). */
162 const constant Catalan("Catalan", CatalanEvalf);