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