]> www.ginac.de Git - ginac.git/blob - ginac/relational.cpp
when normal() doesn't recur any further because the specified recursion level
[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         if (o<oth.o)
325                 return -1;
326         else
327                 return 1;
328 }
329
330 unsigned relational::return_type(void) const
331 {
332         GINAC_ASSERT(lh.return_type()==rh.return_type());
333         return lh.return_type();
334 }
335    
336 unsigned relational::return_type_tinfo(void) const
337 {
338         GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
339         return lh.return_type_tinfo();
340 }
341
342 //////////
343 // new virtual functions which can be overridden by derived classes
344 //////////
345
346 /** Left hand side of relational. */
347 ex relational::lhs(void) const
348 {
349         return lh;
350 }
351
352 /** Right hand side of relational. */
353 ex relational::rhs(void) const
354 {
355         return rh;    
356 }
357
358 //////////
359 // non-virtual functions in this class
360 //////////
361
362 relational::operator bool() const
363 {
364         // please note that (a<b) == false does not imply (a>=b) == true
365         // a false result means the comparison is either false or undecidable
366         // (except for !=, where true means unequal or undecidable)
367         ex df = lh-rh;
368         if (!is_ex_exactly_of_type(df,numeric))
369                 // cannot decide on non-numerical results
370                 return o==not_equal ? true : false;
371         
372         int cmpval = ex_to_numeric(df).compare(_num0());
373         switch (o) {
374         case equal:
375                 return cmpval==0;
376         case not_equal:
377                 return cmpval!=0;
378         case less:
379                 return cmpval<0;
380         case less_or_equal:
381                 return cmpval<=0;
382         case greater:
383                 return cmpval>0;
384         case greater_or_equal:
385                 return cmpval>=0;
386         default:
387                 throw(std::logic_error("invalid relational operator"));
388         }
389 }
390
391 //////////
392 // static member variables
393 //////////
394
395 // protected
396
397 unsigned relational::precedence=20;
398
399 //////////
400 // global constants
401 //////////
402
403 const relational some_relational;
404 const std::type_info & typeid_relational = typeid(some_relational);
405
406 #ifndef NO_NAMESPACE_GINAC
407 } // namespace GiNaC
408 #endif // ndef NO_NAMESPACE_GINAC