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