]> www.ginac.de Git - ginac.git/blob - ginac/relational.cpp
- killed old add::print
[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_GINAC_NAMESPACE
32 namespace GiNaC {
33 #endif // ndef NO_GINAC_NAMESPACE
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(0);
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(1);
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(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(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(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))->
290             setflag(status_flags::dynallocated  |
291                     status_flags::evaluated );
292 }
293
294 ex relational::evalf(int level) const
295 {
296     if (level==1) {
297         return *this;
298     }
299     if (level == -max_recursion_level) {
300         throw(std::runtime_error("max recursion level reached"));
301     }
302     return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
303             setflag(status_flags::dynallocated);
304 }
305
306 ex relational::simplify_ncmul(const exvector & v) const
307 {
308     return lh.simplify_ncmul(v);
309 }
310
311 // protected
312
313 int relational::compare_same_type(const basic & other) const
314 {
315     GINAC_ASSERT(is_exactly_of_type(other, relational));
316     const relational & oth=static_cast<const relational &>(const_cast<basic &>(other));
317     
318     int cmpval;
319     
320     if (o == oth.o) {
321         cmpval=lh.compare(oth.lh);
322         if (cmpval==0) {
323             return rh.compare(oth.rh);
324         } else {
325             return cmpval;
326         }
327     }
328     if (o<oth.o) {
329         return -1;
330     } else {
331         return 1;
332     }
333 }
334
335 unsigned relational::return_type(void) const
336 {
337     GINAC_ASSERT(lh.return_type()==rh.return_type());
338     return lh.return_type();
339 }
340    
341 unsigned relational::return_type_tinfo(void) const
342 {
343     GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
344     return lh.return_type_tinfo();
345 }
346
347 //////////
348 // new virtual functions which can be overridden by derived classes
349 //////////
350
351 // none
352
353 //////////
354 // non-virtual functions in this class
355 //////////
356
357 #include <iostream>
358
359 relational::operator bool() const
360 {
361     // please note that (a<b) == false does not imply (a>=b) == true
362     // a false result means the comparison is either false or undecidable
363     // (except for !=, where true means unequal or undecidable)
364     ex df=lh-rh;
365     if (!is_ex_exactly_of_type(df,numeric)) {
366         return o==not_equal ? true : false; // cannot decide on non-numerical results
367     }
368     int cmpval=ex_to_numeric(df).compare(_num0());
369     switch (o) {
370     case equal:
371         return cmpval==0;
372         break;
373     case not_equal:
374         return cmpval!=0;
375         break;
376     case less:
377         return cmpval<0;
378         break;
379     case less_or_equal:
380         return cmpval<=0;
381         break;
382     case greater:
383         return cmpval>0;
384         break;
385     case greater_or_equal:
386         return cmpval>=0;
387         break;
388     default:
389         throw(std::logic_error("invalid relational operator"));
390     }
391     return 0;
392 }
393
394 //////////
395 // static member variables
396 //////////
397
398 // protected
399
400 unsigned relational::precedence=20;
401
402 //////////
403 // global constants
404 //////////
405
406 const relational some_relational;
407 const type_info & typeid_relational=typeid(some_relational);
408
409 #ifndef NO_GINAC_NAMESPACE
410 } // namespace GiNaC
411 #endif // ndef NO_GINAC_NAMESPACE