1 /** @file relational.cpp
3 * Implementation of relations between expressions */
6 * GiNaC Copyright (C) 1999-2018 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
23 #include "relational.h"
24 #include "operators.h"
28 #include "hash_seed.h"
35 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(relational, basic,
36 print_func<print_context>(&relational::do_print).
37 print_func<print_tree>(&relational::do_print_tree).
38 print_func<print_python_repr>(&relational::do_print_python_repr))
41 // default constructor
44 relational::relational() { }
52 relational::relational(const ex & lhs, const ex & rhs, operators oper) :
53 lh(lhs), rh(rhs), o(oper) { }
59 void relational::read_archive(const archive_node& n, lst& sym_lst)
61 inherited::read_archive(n, sym_lst);
63 if (!(n.find_unsigned("op", opi)))
64 throw (std::runtime_error("unknown relational operator in archive"));
66 n.find_ex("lh", lh, sym_lst);
67 n.find_ex("rh", rh, sym_lst);
69 GINAC_BIND_UNARCHIVER(relational);
71 void relational::archive(archive_node &n) const
73 inherited::archive(n);
76 n.add_unsigned("op", o);
80 // functions overriding virtual functions from base classes
85 static void print_operator(const print_context & c, relational::operators o)
88 case relational::equal:
91 case relational::not_equal:
94 case relational::less:
97 case relational::less_or_equal:
100 case relational::greater:
103 case relational::greater_or_equal:
107 c.s << "(INVALID RELATIONAL OPERATOR)";
112 void relational::do_print(const print_context & c, unsigned level) const
114 if (precedence() <= level)
116 lh.print(c, precedence());
117 print_operator(c, o);
118 rh.print(c, precedence());
119 if (precedence() <= level)
123 void relational::do_print_python_repr(const print_python_repr & c, unsigned level) const
125 c.s << class_name() << '(';
130 print_operator(c, o);
134 bool relational::info(unsigned inf) const
137 case info_flags::relation:
139 case info_flags::relation_equal:
141 case info_flags::relation_not_equal:
143 case info_flags::relation_less:
145 case info_flags::relation_less_or_equal:
146 return o==less_or_equal;
147 case info_flags::relation_greater:
149 case info_flags::relation_greater_or_equal:
150 return o==greater_or_equal;
155 size_t relational::nops() const
160 ex relational::op(size_t i) const
164 return i==0 ? lh : rh;
167 ex relational::map(map_function & f) const
169 const ex &mapped_lh = f(lh);
170 const ex &mapped_rh = f(rh);
172 if (!are_ex_trivially_equal(lh, mapped_lh)
173 || !are_ex_trivially_equal(rh, mapped_rh))
174 return dynallocate<relational>(mapped_lh, mapped_rh, o);
179 ex relational::subs(const exmap & m, unsigned options) const
181 const ex & subsed_lh = lh.subs(m, options);
182 const ex & subsed_rh = rh.subs(m, options);
184 if (!are_ex_trivially_equal(lh, subsed_lh) || !are_ex_trivially_equal(rh, subsed_rh))
185 return relational(subsed_lh, subsed_rh, o).subs_one_level(m, options);
187 return subs_one_level(m, options);
190 ex relational::eval_ncmul(const exvector & v) const
192 return lh.eval_ncmul(v);
197 int relational::compare_same_type(const basic & other) const
199 GINAC_ASSERT(is_exactly_a<relational>(other));
200 const relational &oth = static_cast<const relational &>(other);
201 if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
207 return (o < oth.o) ? -1 : 1;
211 return (o < oth.o) ? -1 : 1;
214 if (oth.o!=greater_or_equal)
215 return (o < oth.o) ? -1 : 1;
219 return (o < oth.o) ? -1 : 1;
221 case greater_or_equal:
222 if (oth.o!=less_or_equal)
223 return (o < oth.o) ? -1 : 1;
226 const int lcmpval = lh.compare(oth.rh);
227 return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
230 bool relational::match_same_type(const basic & other) const
232 GINAC_ASSERT(is_exactly_a<relational>(other));
233 const relational &oth = static_cast<const relational &>(other);
238 unsigned relational::return_type() const
240 GINAC_ASSERT(lh.return_type()==rh.return_type());
241 return lh.return_type();
244 return_type_t relational::return_type_tinfo() const
246 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
247 return lh.return_type_tinfo();
250 unsigned relational::calchash() const
252 unsigned v = make_hash_seed(typeid(*this));
253 unsigned lhash = lh.gethash();
254 unsigned rhash = rh.gethash();
272 case greater_or_equal:
280 // store calculated hash value only if object is already evaluated
281 if (flags & status_flags::evaluated) {
282 setflag(status_flags::hash_calculated);
290 // new virtual functions which can be overridden by derived classes
296 // non-virtual functions in this class
299 relational::safe_bool relational::make_safe_bool(bool cond) const
301 return cond? &safe_bool_helper::nonnull : nullptr;
304 /** Cast the relational into a Boolean, mainly for evaluation within an
305 * if-statement. Note that (a<b) == false does not imply (a>=b) == true in
306 * the general symbolic case. A false result means the comparison is either
307 * false or undecidable (except of course for !=, where true means either
308 * unequal or undecidable). */
309 relational::operator relational::safe_bool() const
312 if (!is_exactly_a<numeric>(df))
313 // cannot decide on non-numerical results
314 return o==not_equal ? make_safe_bool(true) : make_safe_bool(false);
318 return make_safe_bool(ex_to<numeric>(df).is_zero());
320 return make_safe_bool(!ex_to<numeric>(df).is_zero());
322 return make_safe_bool(ex_to<numeric>(df)<(*_num0_p));
324 return make_safe_bool(ex_to<numeric>(df)<=(*_num0_p));
326 return make_safe_bool(ex_to<numeric>(df)>(*_num0_p));
327 case greater_or_equal:
328 return make_safe_bool(ex_to<numeric>(df)>=(*_num0_p));
330 throw(std::logic_error("invalid relational operator"));