1 /** @file relational.cpp
3 * Implementation of relations between expressions */
6 * GiNaC Copyright (C) 1999-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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>(&relational::do_print_tree).
37 print_func<print_python_repr>(&relational::do_print_python_repr))
40 // default constructor
43 relational::relational() { }
51 relational::relational(const ex & lhs, const ex & rhs, operators oper) :
52 lh(lhs), rh(rhs), o(oper) { }
58 relational::relational(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
61 if (!(n.find_unsigned("op", opi)))
62 throw (std::runtime_error("unknown relational operator in archive"));
64 n.find_ex("lh", lh, sym_lst);
65 n.find_ex("rh", rh, sym_lst);
68 void relational::archive(archive_node &n) const
70 inherited::archive(n);
73 n.add_unsigned("op", o);
76 DEFAULT_UNARCHIVE(relational)
79 // functions overriding virtual functions from base classes
84 static void print_operator(const print_context & c, relational::operators o)
87 case relational::equal:
90 case relational::not_equal:
93 case relational::less:
96 case relational::less_or_equal:
99 case relational::greater:
102 case relational::greater_or_equal:
106 c.s << "(INVALID RELATIONAL OPERATOR)";
111 void relational::do_print(const print_context & c, unsigned level) const
113 if (precedence() <= level)
115 lh.print(c, precedence());
116 print_operator(c, o);
117 rh.print(c, precedence());
118 if (precedence() <= level)
122 void relational::do_print_python_repr(const print_python_repr & c, unsigned level) const
124 c.s << class_name() << '(';
129 print_operator(c, o);
133 bool relational::info(unsigned inf) const
136 case info_flags::relation:
138 case info_flags::relation_equal:
140 case info_flags::relation_not_equal:
142 case info_flags::relation_less:
144 case info_flags::relation_less_or_equal:
145 return o==less_or_equal;
146 case info_flags::relation_greater:
148 case info_flags::relation_greater_or_equal:
149 return o==greater_or_equal;
154 size_t relational::nops() const
159 ex relational::op(size_t i) const
163 return i==0 ? lh : rh;
166 ex relational::map(map_function & f) const
168 const ex &mapped_lh = f(lh);
169 const ex &mapped_rh = f(rh);
171 if (!are_ex_trivially_equal(lh, mapped_lh)
172 || !are_ex_trivially_equal(rh, mapped_rh))
173 return (new relational(mapped_lh, mapped_rh, o))->setflag(status_flags::dynallocated);
178 ex relational::eval(int level) const
183 if (level == -max_recursion_level)
184 throw(std::runtime_error("max recursion level reached"));
186 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
189 ex relational::subs(const exmap & m, unsigned options) const
191 const ex & subsed_lh = lh.subs(m, options);
192 const ex & subsed_rh = rh.subs(m, options);
194 if (!are_ex_trivially_equal(lh, subsed_lh) || !are_ex_trivially_equal(rh, subsed_rh))
195 return relational(subsed_lh, subsed_rh, o).subs_one_level(m, options);
197 return subs_one_level(m, options);
200 ex relational::eval_ncmul(const exvector & v) const
202 return lh.eval_ncmul(v);
207 int relational::compare_same_type(const basic & other) const
209 GINAC_ASSERT(is_exactly_a<relational>(other));
210 const relational &oth = static_cast<const relational &>(other);
211 if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
217 return (o < oth.o) ? -1 : 1;
221 return (o < oth.o) ? -1 : 1;
224 if (oth.o!=greater_or_equal)
225 return (o < oth.o) ? -1 : 1;
229 return (o < oth.o) ? -1 : 1;
231 case greater_or_equal:
232 if (oth.o!=less_or_equal)
233 return (o < oth.o) ? -1 : 1;
236 const int lcmpval = lh.compare(oth.rh);
237 return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
240 bool relational::match_same_type(const basic & other) const
242 GINAC_ASSERT(is_exactly_a<relational>(other));
243 const relational &oth = static_cast<const relational &>(other);
248 unsigned relational::return_type() const
250 GINAC_ASSERT(lh.return_type()==rh.return_type());
251 return lh.return_type();
254 return_type_t relational::return_type_tinfo() const
256 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
257 return lh.return_type_tinfo();
260 unsigned relational::calchash() const
262 const void* this_tinfo = (const void*)typeid(*this).name();
263 unsigned v = golden_ratio_hash((p_int)this_tinfo);
264 unsigned lhash = lh.gethash();
265 unsigned rhash = rh.gethash();
283 case greater_or_equal:
291 // store calculated hash value only if object is already evaluated
292 if (flags & status_flags::evaluated) {
293 setflag(status_flags::hash_calculated);
301 // new virtual functions which can be overridden by derived classes
304 /** Left hand side of relational. */
305 ex relational::lhs() const
310 /** Right hand side of relational. */
311 ex relational::rhs() const
317 // non-virtual functions in this class
320 relational::safe_bool relational::make_safe_bool(bool cond) const
322 return cond? &safe_bool_helper::nonnull : 0;
325 /** Cast the relational into a boolean, mainly for evaluation within an
326 * if-statement. Note that (a<b) == false does not imply (a>=b) == true in
327 * the general symbolic case. A false result means the comparison is either
328 * false or undecidable (except of course for !=, where true means either
329 * unequal or undecidable). */
330 relational::operator relational::safe_bool() const
333 if (!is_exactly_a<numeric>(df))
334 // cannot decide on non-numerical results
335 return o==not_equal ? make_safe_bool(true) : make_safe_bool(false);
339 return make_safe_bool(ex_to<numeric>(df).is_zero());
341 return make_safe_bool(!ex_to<numeric>(df).is_zero());
343 return make_safe_bool(ex_to<numeric>(df)<(*_num0_p));
345 return make_safe_bool(ex_to<numeric>(df)<=(*_num0_p));
347 return make_safe_bool(ex_to<numeric>(df)>(*_num0_p));
348 case greater_or_equal:
349 return make_safe_bool(ex_to<numeric>(df)>=(*_num0_p));
351 throw(std::logic_error("invalid relational operator"));