5628f25ca67d04d06ec66cc039171ec09edfb28c
[ginac.git] / ginac / relational.cpp
1 /** @file relational.cpp
2  *
3  *  Implementation of relations between expressions */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 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 <stdexcept>
24
25 #include "relational.h"
26 #include "numeric.h"
27 #include "archive.h"
28 #include "utils.h"
29 #include "debugmsg.h"
30
31 #ifndef NO_NAMESPACE_GINAC
32 namespace GiNaC {
33 #endif // ndef NO_NAMESPACE_GINAC
34
35 GINAC_IMPLEMENT_REGISTERED_CLASS(relational, basic)
36
37 //////////
38 // default constructor, destructor, copy constructor assignment operator and helpers
39 //////////
40
41 // public
42
43 relational::relational() : basic(TINFO_relational)
44 {
45         debugmsg("relational default constructor",LOGLEVEL_CONSTRUCT);
46 }
47
48 relational::~relational()
49 {
50         debugmsg("relational destructor",LOGLEVEL_DESTRUCT);
51         destroy(false);
52 }
53
54 relational::relational(const relational & other)
55 {
56         debugmsg("relational copy constructor",LOGLEVEL_CONSTRUCT);
57         copy(other);
58 }
59
60 const relational & relational::operator=(const relational & other)
61 {
62         debugmsg("relational operator=",LOGLEVEL_ASSIGNMENT);
63         if (this != &other) {
64                 destroy(true);
65                 copy(other);
66         }
67         return *this;
68 }
69
70 // protected
71
72 void relational::copy(const relational & other)
73 {
74         basic::copy(other);
75         lh=other.lh;
76         rh=other.rh;
77         o=other.o;
78 }
79
80 void relational::destroy(bool call_parent)
81 {
82         if (call_parent) basic::destroy(call_parent);
83 }
84
85 //////////
86 // other constructors
87 //////////
88
89 // public
90
91 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational)
92 {
93         debugmsg("relational constructor ex,ex,operator",LOGLEVEL_CONSTRUCT);
94         lh=lhs;
95         rh=rhs;
96         o=oper;
97 }
98
99 //////////
100 // archiving
101 //////////
102
103 /** Construct object from archive_node. */
104 relational::relational(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
105 {
106         debugmsg("relational constructor from archive_node", LOGLEVEL_CONSTRUCT);
107         unsigned int opi;
108         if (!(n.find_unsigned("op", opi)))
109                 throw (std::runtime_error("unknown relational operator in archive"));
110         o = (operators)opi;
111         n.find_ex("lh", lh, sym_lst);
112         n.find_ex("rh", rh, sym_lst);
113 }
114
115 /** Unarchive the object. */
116 ex relational::unarchive(const archive_node &n, const lst &sym_lst)
117 {
118         return (new relational(n, sym_lst))->setflag(status_flags::dynallocated);
119 }
120
121 /** Archive the object. */
122 void relational::archive(archive_node &n) const
123 {
124         inherited::archive(n);
125         n.add_ex("lh", lh);
126         n.add_ex("rh", rh);
127         n.add_unsigned("op", o);
128 }
129
130 //////////
131 // functions overriding virtual functions from bases classes
132 //////////
133
134 // public
135
136 basic * relational::duplicate() const
137 {
138         debugmsg("relational duplicate",LOGLEVEL_DUPLICATE);
139         return new relational(*this);
140 }
141
142 void relational::print(std::ostream & os, unsigned upper_precedence) const
143 {
144         debugmsg("relational print",LOGLEVEL_PRINT);
145         if (precedence<=upper_precedence) os << "(";
146         lh.print(os,precedence);
147         switch (o) {
148         case equal:
149                 os << "==";
150                 break;
151         case not_equal:
152                 os << "!=";
153                 break;
154         case less:
155                 os << "<";
156                 break;
157         case less_or_equal:
158                 os << "<=";
159                 break;
160         case greater:
161                 os << ">";
162                 break;
163         case greater_or_equal:
164                 os << ">=";
165                 break;
166         default:
167                 os << "(INVALID RELATIONAL OPERATOR)";
168         }
169         rh.print(os,precedence);
170         if (precedence<=upper_precedence) os << ")";
171 }
172
173 void relational::printraw(std::ostream & os) const
174 {
175         debugmsg("relational printraw",LOGLEVEL_PRINT);
176         os << "RELATIONAL(";
177         lh.printraw(os);
178         os << ",";
179         rh.printraw(os);
180         os << ",";
181         switch (o) {
182         case equal:
183                 os << "==";
184                 break;
185         case not_equal:
186                 os << "!=";
187                 break;
188         case less:
189                 os << "<";
190                 break;
191         case less_or_equal:
192                 os << "<=";
193                 break;
194         case greater:
195                 os << ">";
196                 break;
197         case greater_or_equal:
198                 os << ">=";
199                 break;
200         default:
201                 os << "(INVALID RELATIONAL OPERATOR)";
202         }
203         os << ")";
204 }
205
206 void relational::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
207 {
208         debugmsg("relational print csrc", LOGLEVEL_PRINT);
209         if (precedence<=upper_precedence)
210                 os << "(";
211
212         // Print left-hand expression
213         lh.bp->printcsrc(os, type, precedence);
214
215         // Print relational operator
216         switch (o) {
217                 case equal:
218                         os << "==";
219                         break;
220                 case not_equal:
221                         os << "!=";
222                         break;
223                 case less:
224                         os << "<";
225                         break;
226                 case less_or_equal:
227                         os << "<=";
228                         break;
229                 case greater:
230                         os << ">";
231                         break;
232                 case greater_or_equal:
233                         os << ">=";
234                         break;
235                 default:
236                         os << "(INVALID RELATIONAL OPERATOR)";
237                         break;
238         }
239
240         // Print right-hand operator
241         rh.bp->printcsrc(os, type, precedence);
242
243         if (precedence <= upper_precedence)
244                 os << ")";
245 }
246
247 bool relational::info(unsigned inf) const
248 {
249         switch (inf) {
250         case info_flags::relation:
251                 return 1;
252         case info_flags::relation_equal:
253                 return o==equal;
254         case info_flags::relation_not_equal:
255                 return o==not_equal;
256         case info_flags::relation_less:
257                 return o==less;
258         case info_flags::relation_less_or_equal:
259                 return o==less_or_equal;
260         case info_flags::relation_greater:
261                 return o==greater;
262         case info_flags::relation_greater_or_equal:
263                 return o==greater_or_equal;
264         }
265         return 0;
266 }
267
268 unsigned relational::nops() const
269 {
270         return 2;
271 }
272
273 ex & relational::let_op(int i)
274 {
275         GINAC_ASSERT(i>=0);
276         GINAC_ASSERT(i<2);
277
278         return i==0 ? lh : rh;
279 }
280         
281 ex relational::eval(int level) const
282 {
283         if (level==1) {
284                 return this->hold();
285         }
286         if (level == -max_recursion_level) {
287                 throw(std::runtime_error("max recursion level reached"));
288         }
289         return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
290 }
291
292 ex relational::evalf(int level) const
293 {
294         if (level==1) {
295                 return *this;
296         }
297         if (level == -max_recursion_level) {
298                 throw(std::runtime_error("max recursion level reached"));
299         }
300         return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated);
301 }
302
303 ex relational::simplify_ncmul(const exvector & v) const
304 {
305         return lh.simplify_ncmul(v);
306 }
307
308 // protected
309
310 int relational::compare_same_type(const basic & other) const
311 {
312         GINAC_ASSERT(is_exactly_of_type(other, relational));
313         const relational & oth=static_cast<const relational &>(const_cast<basic &>(other));
314         
315         int cmpval;
316         
317         if (o == oth.o) {
318                 cmpval=lh.compare(oth.lh);
319                 if (cmpval==0) {
320                         return rh.compare(oth.rh);
321                 } else {
322                         return cmpval;
323                 }
324         }
325         if (o<oth.o) {
326                 return -1;
327         } else {
328                 return 1;
329         }
330 }
331
332 unsigned relational::return_type(void) const
333 {
334         GINAC_ASSERT(lh.return_type()==rh.return_type());
335         return lh.return_type();
336 }
337    
338 unsigned relational::return_type_tinfo(void) const
339 {
340         GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
341         return lh.return_type_tinfo();
342 }
343
344 //////////
345 // new virtual functions which can be overridden by derived classes
346 //////////
347
348 /** Left hand side of relational. */
349 ex relational::lhs(void) const
350 {
351         return lh;
352 }
353
354 /** Right hand side of relational. */
355 ex relational::rhs(void) const
356 {
357         return rh;    
358 }
359
360 //////////
361 // non-virtual functions in this class
362 //////////
363
364 relational::operator bool() const
365 {
366         // please note that (a<b) == false does not imply (a>=b) == true
367         // a false result means the comparison is either false or undecidable
368         // (except for !=, where true means unequal or undecidable)
369         ex df=lh-rh;
370         if (!is_ex_exactly_of_type(df,numeric)) {
371                 return o==not_equal ? true : false; // cannot decide on non-numerical results
372         }
373         int cmpval=ex_to_numeric(df).compare(_num0());
374         switch (o) {
375         case equal:
376                 return cmpval==0;
377                 break;
378         case not_equal:
379                 return cmpval!=0;
380                 break;
381         case less:
382                 return cmpval<0;
383                 break;
384         case less_or_equal:
385                 return cmpval<=0;
386                 break;
387         case greater:
388                 return cmpval>0;
389                 break;
390         case greater_or_equal:
391                 return cmpval>=0;
392                 break;
393         default:
394                 throw(std::logic_error("invalid relational operator"));
395         }
396         return 0;
397 }
398
399 //////////
400 // static member variables
401 //////////
402
403 // protected
404
405 unsigned relational::precedence=20;
406
407 //////////
408 // global constants
409 //////////
410
411 const relational some_relational;
412 const type_info & typeid_relational=typeid(some_relational);
413
414 #ifndef NO_NAMESPACE_GINAC
415 } // namespace GiNaC
416 #endif // ndef NO_NAMESPACE_GINAC