subs() and normal() use maps instead of lists, resulting in a huge performance
[ginac.git] / ginac / relational.cpp
1 /** @file relational.cpp
2  *
3  *  Implementation of relations between expressions */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2003 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 "print.h"
30 #include "archive.h"
31 #include "utils.h"
32
33 namespace GiNaC {
34
35 GINAC_IMPLEMENT_REGISTERED_CLASS(relational, basic)
36
37 //////////
38 // default constructor
39 //////////
40
41 relational::relational() : basic(TINFO_relational) {}
42
43 //////////
44 // other constructors
45 //////////
46
47 // public
48
49 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational), lh(lhs), rh(rhs), o(oper) {}
50
51 //////////
52 // archiving
53 //////////
54
55 relational::relational(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
56 {
57         unsigned int opi;
58         if (!(n.find_unsigned("op", opi)))
59                 throw (std::runtime_error("unknown relational operator in archive"));
60         o = (operators)opi;
61         n.find_ex("lh", lh, sym_lst);
62         n.find_ex("rh", rh, sym_lst);
63 }
64
65 void relational::archive(archive_node &n) const
66 {
67         inherited::archive(n);
68         n.add_ex("lh", lh);
69         n.add_ex("rh", rh);
70         n.add_unsigned("op", o);
71 }
72
73 DEFAULT_UNARCHIVE(relational)
74
75 //////////
76 // functions overriding virtual functions from base classes
77 //////////
78
79 // public
80
81 void relational::print(const print_context & c, unsigned level) const
82 {
83         if (is_a<print_tree>(c)) {
84
85                 inherited::print(c, level);
86
87         } else {
88
89                 if (is_a<print_python_repr>(c)) {
90                         c.s << class_name() << '(';
91                         lh.print(c);
92                         c.s << ',';
93                         rh.print(c);
94                         c.s << ",'";
95                 } else {
96                         if (precedence() <= level)
97                                 c.s << "(";
98                         lh.print(c, precedence());
99                 }
100                 switch (o) {
101                         case equal:
102                                 c.s << "==";
103                                 break;
104                         case not_equal:
105                                 c.s << "!=";
106                                 break;
107                         case less:
108                                 c.s << "<";
109                                 break;
110                         case less_or_equal:
111                                 c.s << "<=";
112                                 break;
113                         case greater:
114                                 c.s << ">";
115                                 break;
116                         case greater_or_equal:
117                                 c.s << ">=";
118                                 break;
119                         default:
120                                 c.s << "(INVALID RELATIONAL OPERATOR)";
121                 }
122                 if (is_a<print_python_repr>(c))
123                         c.s << "')";
124                 else {
125                         rh.print(c, precedence());
126                         if (precedence() <= level)
127                                 c.s << ")";
128                 }
129         }
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