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