1 /** @file relational.cpp
3 * Implementation of relations between expressions */
6 * GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
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.
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.
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
26 #include "relational.h"
27 #include "operators.h"
35 GINAC_IMPLEMENT_REGISTERED_CLASS(relational, basic)
38 // default ctor, dtor, copy ctor, assignment operator and helpers
41 relational::relational() : basic(TINFO_relational) {}
43 void relational::copy(const relational & other)
51 DEFAULT_DESTROY(relational)
59 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational), lh(lhs), rh(rhs), o(oper) {}
65 relational::relational(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
68 if (!(n.find_unsigned("op", opi)))
69 throw (std::runtime_error("unknown relational operator in archive"));
71 n.find_ex("lh", lh, sym_lst);
72 n.find_ex("rh", rh, sym_lst);
75 void relational::archive(archive_node &n) const
77 inherited::archive(n);
80 n.add_unsigned("op", o);
83 DEFAULT_UNARCHIVE(relational)
86 // functions overriding virtual functions from base classes
91 void relational::print(const print_context & c, unsigned level) const
93 if (is_a<print_tree>(c)) {
95 inherited::print(c, level);
99 if (is_a<print_python_repr>(c)) {
100 c.s << class_name() << '(';
106 if (precedence() <= level)
108 lh.print(c, precedence());
126 case greater_or_equal:
130 c.s << "(INVALID RELATIONAL OPERATOR)";
132 if (is_a<print_python_repr>(c))
135 rh.print(c, precedence());
136 if (precedence() <= level)
142 bool relational::info(unsigned inf) const
145 case info_flags::relation:
147 case info_flags::relation_equal:
149 case info_flags::relation_not_equal:
151 case info_flags::relation_less:
153 case info_flags::relation_less_or_equal:
154 return o==less_or_equal;
155 case info_flags::relation_greater:
157 case info_flags::relation_greater_or_equal:
158 return o==greater_or_equal;
163 unsigned relational::nops() const
168 ex & relational::let_op(int i)
173 return i==0 ? lh : rh;
176 ex relational::eval(int level) const
181 if (level == -max_recursion_level)
182 throw(std::runtime_error("max recursion level reached"));
184 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
187 ex relational::simplify_ncmul(const exvector & v) const
189 return lh.simplify_ncmul(v);
194 int relational::compare_same_type(const basic & other) const
196 GINAC_ASSERT(is_exactly_a<relational>(other));
197 const relational &oth = static_cast<const relational &>(other);
198 if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
204 return (o < oth.o) ? -1 : 1;
208 return (o < oth.o) ? -1 : 1;
211 if (oth.o!=greater_or_equal)
212 return (o < oth.o) ? -1 : 1;
216 return (o < oth.o) ? -1 : 1;
218 case greater_or_equal:
219 if (oth.o!=less_or_equal)
220 return (o < oth.o) ? -1 : 1;
223 const int lcmpval = lh.compare(oth.rh);
224 return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
227 bool relational::match_same_type(const basic & other) const
229 GINAC_ASSERT(is_exactly_a<relational>(other));
230 const relational &oth = static_cast<const relational &>(other);
235 unsigned relational::return_type(void) const
237 GINAC_ASSERT(lh.return_type()==rh.return_type());
238 return lh.return_type();
241 unsigned relational::return_type_tinfo(void) const
243 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
244 return lh.return_type_tinfo();
247 unsigned relational::calchash(void) const
249 unsigned v = golden_ratio_hash(tinfo());
250 unsigned lhash = lh.gethash();
251 unsigned rhash = rh.gethash();
269 case greater_or_equal:
277 // store calculated hash value only if object is already evaluated
278 if (flags & status_flags::evaluated) {
279 setflag(status_flags::hash_calculated);
287 // new virtual functions which can be overridden by derived classes
290 /** Left hand side of relational. */
291 ex relational::lhs(void) const
296 /** Right hand side of relational. */
297 ex relational::rhs(void) const
303 // non-virtual functions in this class
306 relational::safe_bool relational::make_safe_bool(bool cond) const
308 return cond? &safe_bool_helper::nonnull : 0;
311 /** Cast the relational into a boolean, mainly for evaluation within an
312 * if-statement. Note that (a<b) == false does not imply (a>=b) == true in
313 * the general symbolic case. A false result means the comparison is either
314 * false or undecidable (except of course for !=, where true means either
315 * unequal or undecidable). */
316 relational::operator relational::safe_bool() const
319 if (!is_exactly_a<numeric>(df))
320 // cannot decide on non-numerical results
321 return o==not_equal ? make_safe_bool(true) : make_safe_bool(false);
325 return make_safe_bool(ex_to<numeric>(df).is_zero());
327 return make_safe_bool(!ex_to<numeric>(df).is_zero());
329 return make_safe_bool(ex_to<numeric>(df)<_num0);
331 return make_safe_bool(ex_to<numeric>(df)<=_num0);
333 return make_safe_bool(ex_to<numeric>(df)>_num0);
334 case greater_or_equal:
335 return make_safe_bool(ex_to<numeric>(df)>=_num0);
337 throw(std::logic_error("invalid relational operator"));