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