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