1 /** @file relational.cpp
3 * Implementation of relations between expressions */
6 * GiNaC Copyright (C) 1999-2007 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() : basic(&relational::tinfo_static) {}
51 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(&relational::tinfo_static), 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 const ex &mapped_lh = f(lh);
168 const ex &mapped_rh = f(rh);
170 if (!are_ex_trivially_equal(lh, mapped_lh)
171 || !are_ex_trivially_equal(rh, mapped_rh))
172 return (new relational(mapped_lh, mapped_rh, o))->setflag(status_flags::dynallocated);
177 ex relational::eval(int level) const
182 if (level == -max_recursion_level)
183 throw(std::runtime_error("max recursion level reached"));
185 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
188 ex relational::subs(const exmap & m, unsigned options) const
190 const ex & subsed_lh = lh.subs(m, options);
191 const ex & subsed_rh = rh.subs(m, options);
193 if (!are_ex_trivially_equal(lh, subsed_lh) || !are_ex_trivially_equal(rh, subsed_rh))
194 return relational(subsed_lh, subsed_rh, o).subs_one_level(m, options);
196 return subs_one_level(m, options);
199 ex relational::eval_ncmul(const exvector & v) const
201 return lh.eval_ncmul(v);
206 int relational::compare_same_type(const basic & other) const
208 GINAC_ASSERT(is_exactly_a<relational>(other));
209 const relational &oth = static_cast<const relational &>(other);
210 if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
216 return (o < oth.o) ? -1 : 1;
220 return (o < oth.o) ? -1 : 1;
223 if (oth.o!=greater_or_equal)
224 return (o < oth.o) ? -1 : 1;
228 return (o < oth.o) ? -1 : 1;
230 case greater_or_equal:
231 if (oth.o!=less_or_equal)
232 return (o < oth.o) ? -1 : 1;
235 const int lcmpval = lh.compare(oth.rh);
236 return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
239 bool relational::match_same_type(const basic & other) const
241 GINAC_ASSERT(is_exactly_a<relational>(other));
242 const relational &oth = static_cast<const relational &>(other);
247 unsigned relational::return_type() const
249 GINAC_ASSERT(lh.return_type()==rh.return_type());
250 return lh.return_type();
253 tinfo_t relational::return_type_tinfo() const
255 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
256 return lh.return_type_tinfo();
259 unsigned relational::calchash() const
261 unsigned v = golden_ratio_hash((p_int)tinfo());
262 unsigned lhash = lh.gethash();
263 unsigned rhash = rh.gethash();
281 case greater_or_equal:
289 // store calculated hash value only if object is already evaluated
290 if (flags & status_flags::evaluated) {
291 setflag(status_flags::hash_calculated);
299 // new virtual functions which can be overridden by derived classes
302 /** Left hand side of relational. */
303 ex relational::lhs() const
308 /** Right hand side of relational. */
309 ex relational::rhs() const
315 // non-virtual functions in this class
318 relational::safe_bool relational::make_safe_bool(bool cond) const
320 return cond? &safe_bool_helper::nonnull : 0;
323 /** Cast the relational into a boolean, mainly for evaluation within an
324 * if-statement. Note that (a<b) == false does not imply (a>=b) == true in
325 * the general symbolic case. A false result means the comparison is either
326 * false or undecidable (except of course for !=, where true means either
327 * unequal or undecidable). */
328 relational::operator relational::safe_bool() const
331 if (!is_exactly_a<numeric>(df))
332 // cannot decide on non-numerical results
333 return o==not_equal ? make_safe_bool(true) : make_safe_bool(false);
337 return make_safe_bool(ex_to<numeric>(df).is_zero());
339 return make_safe_bool(!ex_to<numeric>(df).is_zero());
341 return make_safe_bool(ex_to<numeric>(df)<(*_num0_p));
343 return make_safe_bool(ex_to<numeric>(df)<=(*_num0_p));
345 return make_safe_bool(ex_to<numeric>(df)>(*_num0_p));
346 case greater_or_equal:
347 return make_safe_bool(ex_to<numeric>(df)>=(*_num0_p));
349 throw(std::logic_error("invalid relational operator"));