1 /** @file relational.cpp
3 * Implementation of relations between expressions */
6 * GiNaC Copyright (C) 1999-2003 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"
34 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(relational, basic,
35 print_func<print_context>(&relational::do_print).
36 print_func<print_tree>(&basic::do_print_tree).
37 print_func<print_python_repr>(&relational::do_print_python_repr))
40 // default constructor
43 relational::relational() : basic(TINFO_relational) {}
51 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational), lh(lhs), rh(rhs), o(oper) {}
57 relational::relational(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
60 if (!(n.find_unsigned("op", opi)))
61 throw (std::runtime_error("unknown relational operator in archive"));
63 n.find_ex("lh", lh, sym_lst);
64 n.find_ex("rh", rh, sym_lst);
67 void relational::archive(archive_node &n) const
69 inherited::archive(n);
72 n.add_unsigned("op", o);
75 DEFAULT_UNARCHIVE(relational)
78 // functions overriding virtual functions from base classes
83 static void print_operator(const print_context & c, relational::operators o)
86 case relational::equal:
89 case relational::not_equal:
92 case relational::less:
95 case relational::less_or_equal:
98 case relational::greater:
101 case relational::greater_or_equal:
105 c.s << "(INVALID RELATIONAL OPERATOR)";
110 void relational::do_print(const print_context & c, unsigned level) const
112 if (precedence() <= level)
114 lh.print(c, precedence());
115 print_operator(c, o);
116 rh.print(c, precedence());
117 if (precedence() <= level)
121 void relational::do_print_python_repr(const print_python_repr & c, unsigned level) const
123 c.s << class_name() << '(';
128 print_operator(c, o);
132 bool relational::info(unsigned inf) const
135 case info_flags::relation:
137 case info_flags::relation_equal:
139 case info_flags::relation_not_equal:
141 case info_flags::relation_less:
143 case info_flags::relation_less_or_equal:
144 return o==less_or_equal;
145 case info_flags::relation_greater:
147 case info_flags::relation_greater_or_equal:
148 return o==greater_or_equal;
153 size_t relational::nops() const
158 ex relational::op(size_t i) const
162 return i==0 ? lh : rh;
165 ex relational::map(map_function & f) const
167 return (new relational(f(lh), f(rh), o))->setflag(status_flags::dynallocated);
170 ex relational::eval(int level) const
175 if (level == -max_recursion_level)
176 throw(std::runtime_error("max recursion level reached"));
178 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
181 ex relational::subs(const exmap & m, unsigned options) const
183 const ex & subsed_lh = lh.subs(m, options);
184 const ex & subsed_rh = rh.subs(m, options);
186 if (!are_ex_trivially_equal(lh, subsed_lh) || !are_ex_trivially_equal(rh, subsed_rh))
187 return relational(subsed_lh, subsed_rh, o).subs_one_level(m, options);
189 return subs_one_level(m, options);
192 ex relational::eval_ncmul(const exvector & v) const
194 return lh.eval_ncmul(v);
199 int relational::compare_same_type(const basic & other) const
201 GINAC_ASSERT(is_exactly_a<relational>(other));
202 const relational &oth = static_cast<const relational &>(other);
203 if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
209 return (o < oth.o) ? -1 : 1;
213 return (o < oth.o) ? -1 : 1;
216 if (oth.o!=greater_or_equal)
217 return (o < oth.o) ? -1 : 1;
221 return (o < oth.o) ? -1 : 1;
223 case greater_or_equal:
224 if (oth.o!=less_or_equal)
225 return (o < oth.o) ? -1 : 1;
228 const int lcmpval = lh.compare(oth.rh);
229 return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
232 bool relational::match_same_type(const basic & other) const
234 GINAC_ASSERT(is_exactly_a<relational>(other));
235 const relational &oth = static_cast<const relational &>(other);
240 unsigned relational::return_type() const
242 GINAC_ASSERT(lh.return_type()==rh.return_type());
243 return lh.return_type();
246 unsigned relational::return_type_tinfo() const
248 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
249 return lh.return_type_tinfo();
252 unsigned relational::calchash() const
254 unsigned v = golden_ratio_hash(tinfo());
255 unsigned lhash = lh.gethash();
256 unsigned rhash = rh.gethash();
274 case greater_or_equal:
282 // store calculated hash value only if object is already evaluated
283 if (flags & status_flags::evaluated) {
284 setflag(status_flags::hash_calculated);
292 // new virtual functions which can be overridden by derived classes
295 /** Left hand side of relational. */
296 ex relational::lhs() const
301 /** Right hand side of relational. */
302 ex relational::rhs() const
308 // non-virtual functions in this class
311 relational::safe_bool relational::make_safe_bool(bool cond) const
313 return cond? &safe_bool_helper::nonnull : 0;
316 /** Cast the relational into a boolean, mainly for evaluation within an
317 * if-statement. Note that (a<b) == false does not imply (a>=b) == true in
318 * the general symbolic case. A false result means the comparison is either
319 * false or undecidable (except of course for !=, where true means either
320 * unequal or undecidable). */
321 relational::operator relational::safe_bool() const
324 if (!is_exactly_a<numeric>(df))
325 // cannot decide on non-numerical results
326 return o==not_equal ? make_safe_bool(true) : make_safe_bool(false);
330 return make_safe_bool(ex_to<numeric>(df).is_zero());
332 return make_safe_bool(!ex_to<numeric>(df).is_zero());
334 return make_safe_bool(ex_to<numeric>(df)<_num0);
336 return make_safe_bool(ex_to<numeric>(df)<=_num0);
338 return make_safe_bool(ex_to<numeric>(df)>_num0);
339 case greater_or_equal:
340 return make_safe_bool(ex_to<numeric>(df)>=_num0);
342 throw(std::logic_error("invalid relational operator"));