]> www.ginac.de Git - ginac.git/blob - ginac/relational.cpp
76652e980198a65a483632ce07b5614d55f9ff42
[ginac.git] / ginac / relational.cpp
1 /** @file relational.cpp
2  *
3  *  Implementation of relations between expressions */
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 <stdexcept>
24
25 #include "relational.h"
26 #include "numeric.h"
27 #include "debugmsg.h"
28
29 namespace GiNaC {
30
31 //////////
32 // default constructor, destructor, copy constructor assignment operator and helpers
33 //////////
34
35 // public
36
37 relational::relational() : basic(TINFO_relational)
38 {
39     debugmsg("relational default constructor",LOGLEVEL_CONSTRUCT);
40 }
41
42 relational::~relational()
43 {
44     debugmsg("relational destructor",LOGLEVEL_DESTRUCT);
45     destroy(0);
46 }
47
48 relational::relational(relational const & other)
49 {
50     debugmsg("relational copy constructor",LOGLEVEL_CONSTRUCT);
51     copy(other);
52 }
53
54 relational const & relational::operator=(relational const & other)
55 {
56     debugmsg("relational operator=",LOGLEVEL_ASSIGNMENT);
57     if (this != &other) {
58         destroy(1);
59         copy(other);
60     }
61     return *this;
62 }
63
64 // protected
65
66 void relational::copy(relational const & other)
67 {
68     basic::copy(other);
69     lh=other.lh;
70     rh=other.rh;
71     o=other.o;
72 }
73
74 void relational::destroy(bool call_parent)
75 {
76     if (call_parent) basic::destroy(call_parent);
77 }
78
79 //////////
80 // other constructors
81 //////////
82
83 // public
84
85 relational::relational(ex const & lhs, ex const & rhs, operators oper) : basic(TINFO_relational)
86 {
87     debugmsg("relational constructor ex,ex,operator",LOGLEVEL_CONSTRUCT);
88     lh=lhs;
89     rh=rhs;
90     o=oper;
91 }
92
93 //////////
94 // functions overriding virtual functions from bases classes
95 //////////
96
97 // public
98
99 basic * relational::duplicate() const
100 {
101     debugmsg("relational duplicate",LOGLEVEL_DUPLICATE);
102     return new relational(*this);
103 }
104
105 bool relational::info(unsigned inf) const
106 {
107     switch (inf) {
108     case info_flags::relation:
109         return 1;
110     case info_flags::relation_equal:
111         return o==equal;
112     case info_flags::relation_not_equal:
113         return o==not_equal;
114     case info_flags::relation_less:
115         return o==less;
116     case info_flags::relation_less_or_equal:
117         return o==less_or_equal;
118     case info_flags::relation_greater:
119         return o==greater;
120     case info_flags::relation_greater_or_equal:
121         return o==greater_or_equal;
122     }
123     return 0;
124 }
125
126 int relational::nops() const
127 {
128     return 2;
129 }
130
131 ex & relational::let_op(int const i)
132 {
133     GINAC_ASSERT(i>=0);
134     GINAC_ASSERT(i<2);
135
136     return i==0 ? lh : rh;
137 }
138     
139 ex relational::eval(int level) const
140 {
141     if (level==1) {
142         return this->hold();
143     }
144     if (level == -max_recursion_level) {
145         throw(std::runtime_error("max recursion level reached"));
146     }
147     return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
148             setflag(status_flags::dynallocated  |
149                     status_flags::evaluated );
150 }
151
152 ex relational::evalf(int level) const
153 {
154     if (level==1) {
155         return *this;
156     }
157     if (level == -max_recursion_level) {
158         throw(std::runtime_error("max recursion level reached"));
159     }
160     return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
161             setflag(status_flags::dynallocated);
162 }
163
164 ex relational::simplify_ncmul(exvector const & v) const
165 {
166     return lh.simplify_ncmul(v);
167 }
168
169 // protected
170
171 int relational::compare_same_type(basic const & other) const
172 {
173     GINAC_ASSERT(is_exactly_of_type(other, relational));
174     relational const & oth=static_cast<relational const &>(const_cast<basic &>(other));
175     
176     int cmpval;
177     
178     if (o == oth.o) {
179         cmpval=lh.compare(oth.lh);
180         if (cmpval==0) {
181             return rh.compare(oth.rh);
182         } else {
183             return cmpval;
184         }
185     }
186     if (o<oth.o) {
187         return -1;
188     } else {
189         return 1;
190     }
191 }
192
193 unsigned relational::return_type(void) const
194 {
195     GINAC_ASSERT(lh.return_type()==rh.return_type());
196     return lh.return_type();
197 }
198    
199 unsigned relational::return_type_tinfo(void) const
200 {
201     GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
202     return lh.return_type_tinfo();
203 }
204
205 //////////
206 // new virtual functions which can be overridden by derived classes
207 //////////
208
209 // none
210
211 //////////
212 // non-virtual functions in this class
213 //////////
214
215 #include <iostream>
216
217 relational::operator bool() const
218 {
219     // please note that (a<b) == false does not imply (a>=b) == true
220     // a false result means the comparison is either false or undecidable
221     // (except for !=, where true means unequal or undecidable)
222     ex df=lh-rh;
223     if (!is_ex_exactly_of_type(df,numeric)) {
224         return o==not_equal ? true : false; // cannot decide on non-numerical results
225     }
226     int cmpval=ex_to_numeric(df).compare(numZERO());
227     switch (o) {
228     case equal:
229         return cmpval==0;
230         break;
231     case not_equal:
232         return cmpval!=0;
233         break;
234     case less:
235         return cmpval<0;
236         break;
237     case less_or_equal:
238         return cmpval<=0;
239         break;
240     case greater:
241         return cmpval>0;
242         break;
243     case greater_or_equal:
244         return cmpval>=0;
245         break;
246     default:
247         throw(std::logic_error("invalid relational operator"));
248     }
249     return 0;
250 }
251
252 //////////
253 // static member variables
254 //////////
255
256 // protected
257
258 unsigned relational::precedence=20;
259
260 //////////
261 // global constants
262 //////////
263
264 const relational some_relational;
265 type_info const & typeid_relational=typeid(some_relational);
266
267 } // namespace GiNaC