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