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