]> www.ginac.de Git - ginac.git/blob - ginac/relational.cpp
Fixed 50 percent of the bugs on Alex' list. More, soon...
[ginac.git] / ginac / relational.cpp
1 /** @file relational.cpp
2  *
3  *  Implementation of relations between expressions
4  *
5  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdexcept>
23
24 #include "relational.h"
25 #include "numeric.h"
26
27 //////////
28 // default constructor, destructor, copy constructor assignment operator and helpers
29 //////////
30
31 // public
32
33 relational::relational() : basic(TINFO_relational)
34 {
35     debugmsg("relational default constructor",LOGLEVEL_CONSTRUCT);
36 }
37
38 relational::~relational()
39 {
40     debugmsg("relational destructor",LOGLEVEL_DESTRUCT);
41     destroy(0);
42 }
43
44 relational::relational(relational const & other)
45 {
46     debugmsg("relational copy constructor",LOGLEVEL_CONSTRUCT);
47     copy(other);
48 }
49
50 relational const & relational::operator=(relational const & other)
51 {
52     debugmsg("relational operator=",LOGLEVEL_ASSIGNMENT);
53     if (this != &other) {
54         destroy(1);
55         copy(other);
56     }
57     return *this;
58 }
59
60 // protected
61
62 void relational::copy(relational const & other)
63 {
64     basic::copy(other);
65     lh=other.lh;
66     rh=other.rh;
67     o=other.o;
68 }
69
70 void relational::destroy(bool call_parent)
71 {
72     if (call_parent) basic::destroy(call_parent);
73 }
74
75 //////////
76 // other constructors
77 //////////
78
79 // public
80
81 relational::relational(ex const & lhs, ex const & rhs, operators oper) : basic(TINFO_relational)
82 {
83     debugmsg("relational constructor ex,ex,operator",LOGLEVEL_CONSTRUCT);
84     lh=lhs;
85     rh=rhs;
86     o=oper;
87 }
88
89 //////////
90 // functions overriding virtual functions from bases classes
91 //////////
92
93 // public
94
95 basic * relational::duplicate() const
96 {
97     debugmsg("relational duplicate",LOGLEVEL_DUPLICATE);
98     return new relational(*this);
99 }
100
101 bool relational::info(unsigned inf) const
102 {
103     switch (inf) {
104     case info_flags::relation:
105         return 1;
106     case info_flags::relation_equal:
107         return o==equal;
108     case info_flags::relation_not_equal:
109         return o==not_equal;
110     case info_flags::relation_less:
111         return o==less;
112     case info_flags::relation_less_or_equal:
113         return o==less_or_equal;
114     case info_flags::relation_greater:
115         return o==greater;
116     case info_flags::relation_greater_or_equal:
117         return o==greater_or_equal;
118     }
119     return 0;
120 }
121
122 int relational::nops() const
123 {
124     return 2;
125 }
126
127 ex & relational::let_op(int const i)
128 {
129     ASSERT(i>=0);
130     ASSERT(i<2);
131
132     return i==0 ? lh : rh;
133 }
134     
135 ex relational::eval(int level) const
136 {
137     if (level==1) {
138         return this->hold();
139     }
140     if (level == -max_recursion_level) {
141         throw(std::runtime_error("max recursion level reached"));
142     }
143     return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
144             setflag(status_flags::dynallocated  |
145                     status_flags::evaluated );
146 }
147
148 ex relational::evalf(int level) const
149 {
150     if (level==1) {
151         return *this;
152     }
153     if (level == -max_recursion_level) {
154         throw(std::runtime_error("max recursion level reached"));
155     }
156     return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
157             setflag(status_flags::dynallocated);
158 }
159
160 ex relational::simplify_ncmul(exvector const & v) const
161 {
162     return lh.simplify_ncmul(v);
163 }
164
165 // protected
166
167 int relational::compare_same_type(basic const & other) const
168 {
169     ASSERT(is_exactly_of_type(other, relational));
170     relational const & oth=static_cast<relational const &>(const_cast<basic &>(other));
171     
172     int cmpval;
173     
174     if (o == oth.o) {
175         cmpval=lh.compare(oth.lh);
176         if (cmpval==0) {
177             return rh.compare(oth.rh);
178         } else {
179             return cmpval;
180         }
181     }
182     if (o<oth.o) {
183         return -1;
184     } else {
185         return 1;
186     }
187 }
188
189 unsigned relational::return_type(void) const
190 {
191     ASSERT(lh.return_type()==rh.return_type());
192     return lh.return_type();
193 }
194    
195 unsigned relational::return_type_tinfo(void) const
196 {
197     ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
198     return lh.return_type_tinfo();
199 }
200
201 //////////
202 // new virtual functions which can be overridden by derived classes
203 //////////
204
205 // none
206
207 //////////
208 // non-virtual functions in this class
209 //////////
210
211 #include <iostream>
212
213 relational::operator bool() const
214 {
215     // please note that (a<b) == false does not imply (a>=b) == true
216     // a false result means the comparison is either false or undecidable
217     // (except for !=, where true means unequal or undecidable)
218     ex df=lh-rh;
219     if (!is_ex_exactly_of_type(df,numeric)) {
220         return o==not_equal ? true : false; // cannot decide on non-numerical results
221     }
222     int cmpval=ex_to_numeric(df).compare(numZERO());
223     switch (o) {
224     case equal:
225         return cmpval==0;
226         break;
227     case not_equal:
228         return cmpval!=0;
229         break;
230     case less:
231         return cmpval<0;
232         break;
233     case less_or_equal:
234         return cmpval<=0;
235         break;
236     case greater:
237         return cmpval>0;
238         break;
239     case greater_or_equal:
240         return cmpval>=0;
241         break;
242     default:
243         throw(std::logic_error("invalid relational operator"));
244     }
245     return 0;
246 }
247
248 //////////
249 // static member variables
250 //////////
251
252 // protected
253
254 unsigned relational::precedence=20;
255
256 //////////
257 // global constants
258 //////////
259
260 const relational some_relational;
261 type_info const & typeid_relational=typeid(some_relational);
262