]> www.ginac.de Git - ginac.git/blob - ginac/relational.cpp
Implement modular multivariate GCD (based on chinese remaindering algorithm).
[ginac.git] / ginac / relational.cpp
1 /** @file relational.cpp
2  *
3  *  Implementation of relations between expressions */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2008 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() { }
44
45 //////////
46 // other constructors
47 //////////
48
49 // public
50
51 relational::relational(const ex & lhs, const ex & rhs, operators oper) :
52         lh(lhs), rh(rhs), o(oper) { }
53
54 //////////
55 // archiving
56 //////////
57
58 void relational::read_archive(const archive_node& n, lst& sym_lst)
59 {
60         inherited::read_archive(n, sym_lst);
61         unsigned int opi;
62         if (!(n.find_unsigned("op", opi)))
63                 throw (std::runtime_error("unknown relational operator in archive"));
64         o = (operators)opi;
65         n.find_ex("lh", lh, sym_lst);
66         n.find_ex("rh", rh, sym_lst);
67 }
68 GINAC_BIND_UNARCHIVER(relational);
69
70 void relational::archive(archive_node &n) const
71 {
72         inherited::archive(n);
73         n.add_ex("lh", lh);
74         n.add_ex("rh", rh);
75         n.add_unsigned("op", o);
76 }
77
78 //////////
79 // functions overriding virtual functions from base classes
80 //////////
81
82 // public
83
84 static void print_operator(const print_context & c, relational::operators o)
85 {
86         switch (o) {
87         case relational::equal:
88                 c.s << "==";
89                 break;
90         case relational::not_equal:
91                 c.s << "!=";
92                 break;
93         case relational::less:
94                 c.s << "<";
95                 break;
96         case relational::less_or_equal:
97                 c.s << "<=";
98                 break;
99         case relational::greater:
100                 c.s << ">";
101                 break;
102         case relational::greater_or_equal:
103                 c.s << ">=";
104                 break;
105         default:
106                 c.s << "(INVALID RELATIONAL OPERATOR)";
107                 break;
108         }
109 }
110
111 void relational::do_print(const print_context & c, unsigned level) const
112 {
113         if (precedence() <= level)
114                 c.s << "(";
115         lh.print(c, precedence());
116         print_operator(c, o);
117         rh.print(c, precedence());
118         if (precedence() <= level)
119                 c.s << ")";
120 }
121
122 void relational::do_print_python_repr(const print_python_repr & c, unsigned level) const
123 {
124         c.s << class_name() << '(';
125         lh.print(c);
126         c.s << ',';
127         rh.print(c);
128         c.s << ",'";
129         print_operator(c, o);
130         c.s << "')";
131 }
132
133 bool relational::info(unsigned inf) const
134 {
135         switch (inf) {
136                 case info_flags::relation:
137                         return 1;
138                 case info_flags::relation_equal:
139                         return o==equal;
140                 case info_flags::relation_not_equal:
141                         return o==not_equal;
142                 case info_flags::relation_less:
143                         return o==less;
144                 case info_flags::relation_less_or_equal:
145                         return o==less_or_equal;
146                 case info_flags::relation_greater:
147                         return o==greater;
148                 case info_flags::relation_greater_or_equal:
149                         return o==greater_or_equal;
150         }
151         return 0;
152 }
153
154 size_t relational::nops() const
155 {
156         return 2;
157 }
158
159 ex relational::op(size_t i) const
160 {
161         GINAC_ASSERT(i<2);
162
163         return i==0 ? lh : rh;
164 }
165
166 ex relational::map(map_function & f) const
167 {
168         const ex &mapped_lh = f(lh);
169         const ex &mapped_rh = f(rh);
170
171         if (!are_ex_trivially_equal(lh, mapped_lh)
172          || !are_ex_trivially_equal(rh, mapped_rh))
173                 return (new relational(mapped_lh, mapped_rh, o))->setflag(status_flags::dynallocated);
174         else
175                 return *this;
176 }
177
178 ex relational::eval(int level) const
179 {
180         if (level==1)
181                 return this->hold();
182         
183         if (level == -max_recursion_level)
184                 throw(std::runtime_error("max recursion level reached"));
185         
186         return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
187 }
188
189 ex relational::subs(const exmap & m, unsigned options) const
190 {
191         const ex & subsed_lh = lh.subs(m, options);
192         const ex & subsed_rh = rh.subs(m, options);
193
194         if (!are_ex_trivially_equal(lh, subsed_lh) || !are_ex_trivially_equal(rh, subsed_rh))
195                 return relational(subsed_lh, subsed_rh, o).subs_one_level(m, options);
196         else
197                 return subs_one_level(m, options);
198 }
199
200 ex relational::eval_ncmul(const exvector & v) const
201 {
202         return lh.eval_ncmul(v);
203 }
204
205 // protected
206
207 int relational::compare_same_type(const basic & other) const
208 {
209         GINAC_ASSERT(is_exactly_a<relational>(other));
210         const relational &oth = static_cast<const relational &>(other);
211         if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
212                 return 0;
213         switch (o) {
214                 case equal:
215                 case not_equal:
216                         if (oth.o!=o)
217                                 return (o < oth.o) ? -1 : 1;
218                         break;
219                 case less:
220                         if (oth.o!=greater)
221                                 return (o < oth.o) ? -1 : 1;
222                         break;
223                 case less_or_equal:
224                         if (oth.o!=greater_or_equal)
225                                 return (o < oth.o) ? -1 : 1;
226                         break;
227                 case greater:
228                         if (oth.o!=less)
229                                 return (o < oth.o) ? -1 : 1;
230                         break;
231                 case greater_or_equal:
232                         if (oth.o!=less_or_equal)
233                                 return (o < oth.o) ? -1 : 1;
234                         break;
235         }
236         const int lcmpval = lh.compare(oth.rh);
237         return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
238 }
239
240 bool relational::match_same_type(const basic & other) const
241 {
242         GINAC_ASSERT(is_exactly_a<relational>(other));
243         const relational &oth = static_cast<const relational &>(other);
244
245         return o == oth.o;
246 }
247
248 unsigned relational::return_type() const
249 {
250         GINAC_ASSERT(lh.return_type()==rh.return_type());
251         return lh.return_type();
252 }
253    
254 return_type_t relational::return_type_tinfo() const
255 {
256         GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
257         return lh.return_type_tinfo();
258 }
259
260 unsigned relational::calchash() const
261 {
262         const void* this_tinfo = (const void*)typeid(*this).name();
263         unsigned v = golden_ratio_hash((p_int)this_tinfo);
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