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