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