]> www.ginac.de Git - ginac.git/blob - ginac/relational.cpp
* As agreed upon at lunch, remove debugmsg since it is hardly of any use.
[ginac.git] / ginac / relational.cpp
1 /** @file relational.cpp
2  *
3  *  Implementation of relations between expressions */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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 "print.h"
28 #include "archive.h"
29 #include "utils.h"
30
31 namespace GiNaC {
32
33 GINAC_IMPLEMENT_REGISTERED_CLASS(relational, basic)
34
35 //////////
36 // default ctor, dtor, copy ctor, assignment operator and helpers
37 //////////
38
39 relational::relational() : basic(TINFO_relational) {}
40
41 void relational::copy(const relational & other)
42 {
43         basic::copy(other);
44         lh=other.lh;
45         rh=other.rh;
46         o=other.o;
47 }
48
49 DEFAULT_DESTROY(relational)
50
51 //////////
52 // other ctors
53 //////////
54
55 // public
56
57 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational), lh(lhs), rh(rhs), o(oper) {}
58
59 //////////
60 // archiving
61 //////////
62
63 relational::relational(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
64 {
65         unsigned int opi;
66         if (!(n.find_unsigned("op", opi)))
67                 throw (std::runtime_error("unknown relational operator in archive"));
68         o = (operators)opi;
69         n.find_ex("lh", lh, sym_lst);
70         n.find_ex("rh", rh, sym_lst);
71 }
72
73 void relational::archive(archive_node &n) const
74 {
75         inherited::archive(n);
76         n.add_ex("lh", lh);
77         n.add_ex("rh", rh);
78         n.add_unsigned("op", o);
79 }
80
81 DEFAULT_UNARCHIVE(relational)
82
83 //////////
84 // functions overriding virtual functions from base classes
85 //////////
86
87 // public
88
89 void relational::print(const print_context & c, unsigned level) const
90 {
91         if (is_a<print_tree>(c)) {
92
93                 inherited::print(c, level);
94
95         } else {
96
97                 if (precedence() <= level)
98                         c.s << "(";
99                 lh.print(c, precedence());
100                 switch (o) {
101                 case equal:
102                         c.s << "==";
103                         break;
104                 case not_equal:
105                         c.s << "!=";
106                         break;
107                 case less:
108                         c.s << "<";
109                         break;
110                 case less_or_equal:
111                         c.s << "<=";
112                         break;
113                 case greater:
114                         c.s << ">";
115                         break;
116                 case greater_or_equal:
117                         c.s << ">=";
118                         break;
119                 default:
120                         c.s << "(INVALID RELATIONAL OPERATOR)";
121                 }
122                 rh.print(c, precedence());
123                 if (precedence() <= level)
124                         c.s << ")";
125         }
126 }
127
128 bool relational::info(unsigned inf) const
129 {
130         switch (inf) {
131         case info_flags::relation:
132                 return 1;
133         case info_flags::relation_equal:
134                 return o==equal;
135         case info_flags::relation_not_equal:
136                 return o==not_equal;
137         case info_flags::relation_less:
138                 return o==less;
139         case info_flags::relation_less_or_equal:
140                 return o==less_or_equal;
141         case info_flags::relation_greater:
142                 return o==greater;
143         case info_flags::relation_greater_or_equal:
144                 return o==greater_or_equal;
145         }
146         return 0;
147 }
148
149 unsigned relational::nops() const
150 {
151         return 2;
152 }
153
154 ex & relational::let_op(int i)
155 {
156         GINAC_ASSERT(i>=0);
157         GINAC_ASSERT(i<2);
158
159         return i==0 ? lh : rh;
160 }
161
162 ex relational::eval(int level) const
163 {
164         if (level==1)
165                 return this->hold();
166         
167         if (level == -max_recursion_level)
168                 throw(std::runtime_error("max recursion level reached"));
169         
170         return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
171 }
172
173 ex relational::simplify_ncmul(const exvector & v) const
174 {
175         return lh.simplify_ncmul(v);
176 }
177
178 // protected
179
180 int relational::compare_same_type(const basic & other) const
181 {
182         GINAC_ASSERT(is_exactly_a<relational>(other));
183         const relational &oth = static_cast<const relational &>(other);
184         
185         if (o == oth.o) {
186                 int cmpval = lh.compare(oth.lh);
187                 if (cmpval)
188                         return cmpval;
189                 else
190                         return rh.compare(oth.rh);
191         }
192         return (o < oth.o) ? -1 : 1;
193 }
194
195 bool relational::match_same_type(const basic & other) const
196 {
197         GINAC_ASSERT(is_exactly_a<relational>(other));
198         const relational &oth = static_cast<const relational &>(other);
199
200         return o == oth.o;
201 }
202
203 unsigned relational::return_type(void) const
204 {
205         GINAC_ASSERT(lh.return_type()==rh.return_type());
206         return lh.return_type();
207 }
208    
209 unsigned relational::return_type_tinfo(void) const
210 {
211         GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
212         return lh.return_type_tinfo();
213 }
214
215 //////////
216 // new virtual functions which can be overridden by derived classes
217 //////////
218
219 /** Left hand side of relational. */
220 ex relational::lhs(void) const
221 {
222         return lh;
223 }
224
225 /** Right hand side of relational. */
226 ex relational::rhs(void) const
227 {
228         return rh;    
229 }
230
231 //////////
232 // non-virtual functions in this class
233 //////////
234
235 /** Cast the relational into a boolean, mainly for evaluation within an 
236  *  if-statement.  Note that (a<b) == false does not imply (a>=b) == true in
237  *  the general symbolic case.  A false result means the comparison is either
238  *  false or undecidable (except of course for !=, where true means either
239  *  unequal or undecidable). */
240 relational::operator bool() const
241 {
242         const ex df = lh-rh;
243         if (!is_ex_exactly_of_type(df,numeric))
244                 // cannot decide on non-numerical results
245                 return o==not_equal ? true : false;
246         
247         switch (o) {
248         case equal:
249                 return ex_to<numeric>(df).is_zero();
250         case not_equal:
251                 return !ex_to<numeric>(df).is_zero();
252         case less:
253                 return ex_to<numeric>(df)<_num0;
254         case less_or_equal:
255                 return ex_to<numeric>(df)<=_num0;
256         case greater:
257                 return ex_to<numeric>(df)>_num0;
258         case greater_or_equal:
259                 return ex_to<numeric>(df)>=_num0;
260         default:
261                 throw(std::logic_error("invalid relational operator"));
262         }
263 }
264
265 } // namespace GiNaC