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