]> www.ginac.de Git - ginac.git/blob - ginac/relational.cpp
Make it possible to print exmaps and exvectors.
[ginac.git] / ginac / relational.cpp
1 /** @file relational.cpp
2  *
3  *  Implementation of relations between expressions */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2006 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 <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(&relational::tinfo_static) {}
44
45 //////////
46 // other constructors
47 //////////
48
49 // public
50
51 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(&relational::tinfo_static), 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         const ex &mapped_lh = f(lh);
168         const ex &mapped_rh = f(rh);
169
170         if (!are_ex_trivially_equal(lh, mapped_lh)
171          || !are_ex_trivially_equal(rh, mapped_rh))
172                 return (new relational(mapped_lh, mapped_rh, o))->setflag(status_flags::dynallocated);
173         else
174                 return *this;
175 }
176
177 ex relational::eval(int level) const
178 {
179         if (level==1)
180                 return this->hold();
181         
182         if (level == -max_recursion_level)
183                 throw(std::runtime_error("max recursion level reached"));
184         
185         return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
186 }
187
188 ex relational::subs(const exmap & m, unsigned options) const
189 {
190         const ex & subsed_lh = lh.subs(m, options);
191         const ex & subsed_rh = rh.subs(m, options);
192
193         if (!are_ex_trivially_equal(lh, subsed_lh) || !are_ex_trivially_equal(rh, subsed_rh))
194                 return relational(subsed_lh, subsed_rh, o).subs_one_level(m, options);
195         else
196                 return subs_one_level(m, options);
197 }
198
199 ex relational::eval_ncmul(const exvector & v) const
200 {
201         return lh.eval_ncmul(v);
202 }
203
204 // protected
205
206 int relational::compare_same_type(const basic & other) const
207 {
208         GINAC_ASSERT(is_exactly_a<relational>(other));
209         const relational &oth = static_cast<const relational &>(other);
210         if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
211                 return 0;
212         switch (o) {
213                 case equal:
214                 case not_equal:
215                         if (oth.o!=o)
216                                 return (o < oth.o) ? -1 : 1;
217                         break;
218                 case less:
219                         if (oth.o!=greater)
220                                 return (o < oth.o) ? -1 : 1;
221                         break;
222                 case less_or_equal:
223                         if (oth.o!=greater_or_equal)
224                                 return (o < oth.o) ? -1 : 1;
225                         break;
226                 case greater:
227                         if (oth.o!=less)
228                                 return (o < oth.o) ? -1 : 1;
229                         break;
230                 case greater_or_equal:
231                         if (oth.o!=less_or_equal)
232                                 return (o < oth.o) ? -1 : 1;
233                         break;
234         }
235         const int lcmpval = lh.compare(oth.rh);
236         return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
237 }
238
239 bool relational::match_same_type(const basic & other) const
240 {
241         GINAC_ASSERT(is_exactly_a<relational>(other));
242         const relational &oth = static_cast<const relational &>(other);
243
244         return o == oth.o;
245 }
246
247 unsigned relational::return_type() const
248 {
249         GINAC_ASSERT(lh.return_type()==rh.return_type());
250         return lh.return_type();
251 }
252    
253 tinfo_t relational::return_type_tinfo() const
254 {
255         GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
256         return lh.return_type_tinfo();
257 }
258
259 unsigned relational::calchash() const
260 {
261         unsigned v = golden_ratio_hash((p_int)tinfo());
262         unsigned lhash = lh.gethash();
263         unsigned rhash = rh.gethash();
264
265         v = rotate_left(v);
266         switch(o) {
267                 case equal:
268                 case not_equal:
269                         if (lhash>rhash) {
270                                 v ^= lhash;
271                                 lhash = rhash;
272                         } else {
273                                 v ^= rhash;
274                         }
275                         break;
276                 case less:
277                 case less_or_equal:
278                         v ^= rhash;
279                         break;
280                 case greater:
281                 case greater_or_equal:
282                         v ^= lhash;
283                         lhash = rhash;
284                         break;
285         }
286         v = rotate_left(v);
287         v ^= lhash;
288
289         // store calculated hash value only if object is already evaluated
290         if (flags & status_flags::evaluated) {
291                 setflag(status_flags::hash_calculated);
292                 hashvalue = v;
293         }
294
295         return v;
296 }
297
298 //////////
299 // new virtual functions which can be overridden by derived classes
300 //////////
301
302 /** Left hand side of relational. */
303 ex relational::lhs() const
304 {
305         return lh;
306 }
307
308 /** Right hand side of relational. */
309 ex relational::rhs() const
310 {
311         return rh;    
312 }
313
314 //////////
315 // non-virtual functions in this class
316 //////////
317
318 relational::safe_bool relational::make_safe_bool(bool cond) const
319 {
320         return cond? &safe_bool_helper::nonnull : 0;
321 }
322
323 /** Cast the relational into a boolean, mainly for evaluation within an
324  *  if-statement.  Note that (a<b) == false does not imply (a>=b) == true in
325  *  the general symbolic case.  A false result means the comparison is either
326  *  false or undecidable (except of course for !=, where true means either
327  *  unequal or undecidable). */
328 relational::operator relational::safe_bool() const
329 {
330         const ex df = lh-rh;
331         if (!is_exactly_a<numeric>(df))
332                 // cannot decide on non-numerical results
333                 return o==not_equal ? make_safe_bool(true) : make_safe_bool(false);
334
335         switch (o) {
336                 case equal:
337                         return make_safe_bool(ex_to<numeric>(df).is_zero());
338                 case not_equal:
339                         return make_safe_bool(!ex_to<numeric>(df).is_zero());
340                 case less:
341                         return make_safe_bool(ex_to<numeric>(df)<(*_num0_p));
342                 case less_or_equal:
343                         return make_safe_bool(ex_to<numeric>(df)<=(*_num0_p));
344                 case greater:
345                         return make_safe_bool(ex_to<numeric>(df)>(*_num0_p));
346                 case greater_or_equal:
347                         return make_safe_bool(ex_to<numeric>(df)>=(*_num0_p));
348                 default:
349                         throw(std::logic_error("invalid relational operator"));
350         }
351 }
352
353 } // namespace GiNaC