* Add Pearu's patch canonicalizing class relational, augmented by missing
[ginac.git] / ginac / relational.cpp
1 /** @file relational.cpp
2  *
3  *  Implementation of relations between expressions */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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 <iostream>
24 #include <stdexcept>
25
26 #include "relational.h"
27 #include "numeric.h"
28 #include "print.h"
29 #include "archive.h"
30 #include "utils.h"
31
32 namespace GiNaC {
33
34 GINAC_IMPLEMENT_REGISTERED_CLASS(relational, basic)
35
36 //////////
37 // default ctor, dtor, copy ctor, assignment operator and helpers
38 //////////
39
40 relational::relational() : basic(TINFO_relational) {}
41
42 void relational::copy(const relational & other)
43 {
44         basic::copy(other);
45         lh=other.lh;
46         rh=other.rh;
47         o=other.o;
48 }
49
50 DEFAULT_DESTROY(relational)
51
52 //////////
53 // other ctors
54 //////////
55
56 // public
57
58 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational), lh(lhs), rh(rhs), o(oper) {}
59
60 //////////
61 // archiving
62 //////////
63
64 relational::relational(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
65 {
66         unsigned int opi;
67         if (!(n.find_unsigned("op", opi)))
68                 throw (std::runtime_error("unknown relational operator in archive"));
69         o = (operators)opi;
70         n.find_ex("lh", lh, sym_lst);
71         n.find_ex("rh", rh, sym_lst);
72 }
73
74 void relational::archive(archive_node &n) const
75 {
76         inherited::archive(n);
77         n.add_ex("lh", lh);
78         n.add_ex("rh", rh);
79         n.add_unsigned("op", o);
80 }
81
82 DEFAULT_UNARCHIVE(relational)
83
84 //////////
85 // functions overriding virtual functions from base classes
86 //////////
87
88 // public
89
90 void relational::print(const print_context & c, unsigned level) const
91 {
92         if (is_a<print_tree>(c)) {
93
94                 inherited::print(c, level);
95
96         } else {
97
98                 if (precedence() <= level)
99                         c.s << "(";
100                 lh.print(c, precedence());
101                 switch (o) {
102                         case equal:
103                                 c.s << "==";
104                                 break;
105                         case not_equal:
106                                 c.s << "!=";
107                                 break;
108                         case less:
109                                 c.s << "<";
110                                 break;
111                         case less_or_equal:
112                                 c.s << "<=";
113                                 break;
114                         case greater:
115                                 c.s << ">";
116                                 break;
117                         case greater_or_equal:
118                                 c.s << ">=";
119                                 break;
120                         default:
121                                 c.s << "(INVALID RELATIONAL OPERATOR)";
122                 }
123                 rh.print(c, precedence());
124                 if (precedence() <= level)
125                         c.s << ")";
126         }
127 }
128
129 bool relational::info(unsigned inf) const
130 {
131         switch (inf) {
132                 case info_flags::relation:
133                         return 1;
134                 case info_flags::relation_equal:
135                         return o==equal;
136                 case info_flags::relation_not_equal:
137                         return o==not_equal;
138                 case info_flags::relation_less:
139                         return o==less;
140                 case info_flags::relation_less_or_equal:
141                         return o==less_or_equal;
142                 case info_flags::relation_greater:
143                         return o==greater;
144                 case info_flags::relation_greater_or_equal:
145                         return o==greater_or_equal;
146         }
147         return 0;
148 }
149
150 unsigned relational::nops() const
151 {
152         return 2;
153 }
154
155 ex & relational::let_op(int i)
156 {
157         GINAC_ASSERT(i>=0);
158         GINAC_ASSERT(i<2);
159
160         return i==0 ? lh : rh;
161 }
162
163 ex relational::eval(int level) const
164 {
165         if (level==1)
166                 return this->hold();
167         
168         if (level == -max_recursion_level)
169                 throw(std::runtime_error("max recursion level reached"));
170         
171         return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
172 }
173
174 ex relational::simplify_ncmul(const exvector & v) const
175 {
176         return lh.simplify_ncmul(v);
177 }
178
179 // protected
180
181 int relational::compare_same_type(const basic & other) const
182 {
183         GINAC_ASSERT(is_exactly_a<relational>(other));
184         const relational &oth = static_cast<const relational &>(other);
185         if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
186                 return 0;
187         switch (o) {
188                 case equal:
189                 case not_equal:
190                         if (oth.o!=o)
191                                 return (o < oth.o) ? -1 : 1;
192                         break;
193                 case less:
194                         if (oth.o!=greater)
195                                 return (o < oth.o) ? -1 : 1;
196                         break;
197                 case less_or_equal:
198                         if (oth.o!=greater_or_equal)
199                                 return (o < oth.o) ? -1 : 1;
200                         break;
201                 case greater:
202                         if (oth.o!=less)
203                                 return (o < oth.o) ? -1 : 1;
204                         break;
205                 case greater_or_equal:
206                         if (oth.o!=less_or_equal)
207                                 return (o < oth.o) ? -1 : 1;
208                         break;
209         }
210         const int lcmpval = lh.compare(oth.rh);
211         return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
212 }
213
214 bool relational::match_same_type(const basic & other) const
215 {
216         GINAC_ASSERT(is_exactly_a<relational>(other));
217         const relational &oth = static_cast<const relational &>(other);
218
219         return o == oth.o;
220 }
221
222 unsigned relational::return_type(void) const
223 {
224         GINAC_ASSERT(lh.return_type()==rh.return_type());
225         return lh.return_type();
226 }
227    
228 unsigned relational::return_type_tinfo(void) const
229 {
230         GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
231         return lh.return_type_tinfo();
232 }
233
234 unsigned relational::calchash(void) const
235 {
236         unsigned v = golden_ratio_hash(tinfo());
237         unsigned lhash = lh.gethash();
238         unsigned rhash = rh.gethash();
239
240         v = rotate_left_31(v);
241         switch(o) {
242                 case equal:
243                 case not_equal:
244                         if (lhash>rhash) {
245                                 v ^= lhash;
246                                 lhash = rhash;
247                         } else {
248                                 v ^= rhash;
249                         }
250                         break;
251                 case less:
252                 case less_or_equal:
253                         v ^= rhash;
254                         break;
255                 case greater:
256                 case greater_or_equal:
257                         v ^= lhash;
258                         lhash = rhash;
259                         break;
260         }
261         v = rotate_left_31(v);
262         v ^= lhash;
263
264         // mask out numeric hashes:
265         v &= 0x7FFFFFFFU;
266
267         // store calculated hash value only if object is already evaluated
268         if (flags & status_flags::evaluated) {
269                 setflag(status_flags::hash_calculated);
270                 hashvalue = v;
271         }
272
273         return v;
274 }
275
276 bool relational::is_equal_same_type(const basic & other) const
277 {
278         GINAC_ASSERT(is_a<relational>(other));
279         const relational &oth = static_cast<const relational &>(other);
280         if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
281                 return true;
282         switch (o) {
283                 case equal:
284                 case not_equal:
285                         if (oth.o!=o)
286                                 return false;
287                         break;
288                 case less:
289                         if (oth.o!=greater)
290                                 return false;
291                         break;
292                 case less_or_equal:
293                         if (oth.o!=greater_or_equal)
294                                 return false;
295                         break;
296                 case greater:
297                         if (oth.o!=less)
298                                 return false;
299                         break;
300                 case greater_or_equal:
301                         if (oth.o!=less_or_equal)
302                                 return false;
303                         break;
304         }
305         return lh.is_equal(oth.rh) && rh.is_equal(oth.lh);
306 }
307
308
309 //////////
310 // new virtual functions which can be overridden by derived classes
311 //////////
312
313 /** Left hand side of relational. */
314 ex relational::lhs(void) const
315 {
316         return lh;
317 }
318
319 /** Right hand side of relational. */
320 ex relational::rhs(void) const
321 {
322         return rh;    
323 }
324
325 //////////
326 // non-virtual functions in this class
327 //////////
328
329 /** Cast the relational into a boolean, mainly for evaluation within an 
330  *  if-statement.  Note that (a<b) == false does not imply (a>=b) == true in
331  *  the general symbolic case.  A false result means the comparison is either
332  *  false or undecidable (except of course for !=, where true means either
333  *  unequal or undecidable). */
334 relational::operator bool() const
335 {
336         const ex df = lh-rh;
337         if (!is_ex_exactly_of_type(df,numeric))
338                 // cannot decide on non-numerical results
339                 return o==not_equal ? true : false;
340         
341         switch (o) {
342                 case equal:
343                         return ex_to<numeric>(df).is_zero();
344                 case not_equal:
345                         return !ex_to<numeric>(df).is_zero();
346                 case less:
347                         return ex_to<numeric>(df)<_num0;
348                 case less_or_equal:
349                         return ex_to<numeric>(df)<=_num0;
350                 case greater:
351                         return ex_to<numeric>(df)>_num0;
352                 case greater_or_equal:
353                         return ex_to<numeric>(df)>=_num0;
354                 default:
355                         throw(std::logic_error("invalid relational operator"));
356         }
357 }
358
359 } // namespace GiNaC