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"
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, 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 size_t relational::nops() const
168 ex relational::op(size_t i) const
172 return i==0 ? lh : rh;
175 ex relational::map(map_function & f) const
177 return (new relational(f(lh), f(rh), o))->setflag(status_flags::dynallocated);
180 ex relational::eval(int level) const
185 if (level == -max_recursion_level)
186 throw(std::runtime_error("max recursion level reached"));
188 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
191 ex relational::subs(const lst & ls, const lst & lr, unsigned options) const
193 const ex & subsed_lh = lh.subs(ls, lr, options);
194 const ex & subsed_rh = rh.subs(ls, lr, options);
196 if (!are_ex_trivially_equal(lh, subsed_lh) || !are_ex_trivially_equal(rh, subsed_rh))
197 return relational(subsed_lh, subsed_rh, o).basic::subs(ls, lr, options);
199 return basic::subs(ls, lr, options);
202 ex relational::eval_ncmul(const exvector & v) const
204 return lh.eval_ncmul(v);
209 int relational::compare_same_type(const basic & other) const
211 GINAC_ASSERT(is_exactly_a<relational>(other));
212 const relational &oth = static_cast<const relational &>(other);
213 if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
219 return (o < oth.o) ? -1 : 1;
223 return (o < oth.o) ? -1 : 1;
226 if (oth.o!=greater_or_equal)
227 return (o < oth.o) ? -1 : 1;
231 return (o < oth.o) ? -1 : 1;
233 case greater_or_equal:
234 if (oth.o!=less_or_equal)
235 return (o < oth.o) ? -1 : 1;
238 const int lcmpval = lh.compare(oth.rh);
239 return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
242 bool relational::match_same_type(const basic & other) const
244 GINAC_ASSERT(is_exactly_a<relational>(other));
245 const relational &oth = static_cast<const relational &>(other);
250 unsigned relational::return_type(void) const
252 GINAC_ASSERT(lh.return_type()==rh.return_type());
253 return lh.return_type();
256 unsigned relational::return_type_tinfo(void) const
258 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
259 return lh.return_type_tinfo();
262 unsigned relational::calchash(void) const
264 unsigned v = golden_ratio_hash(tinfo());
265 unsigned lhash = lh.gethash();
266 unsigned rhash = rh.gethash();
284 case greater_or_equal:
292 // store calculated hash value only if object is already evaluated
293 if (flags & status_flags::evaluated) {
294 setflag(status_flags::hash_calculated);
302 // new virtual functions which can be overridden by derived classes
305 /** Left hand side of relational. */
306 ex relational::lhs(void) const
311 /** Right hand side of relational. */
312 ex relational::rhs(void) const
318 // non-virtual functions in this class
321 relational::safe_bool relational::make_safe_bool(bool cond) const
323 return cond? &safe_bool_helper::nonnull : 0;
326 /** Cast the relational into a boolean, mainly for evaluation within an
327 * if-statement. Note that (a<b) == false does not imply (a>=b) == true in
328 * the general symbolic case. A false result means the comparison is either
329 * false or undecidable (except of course for !=, where true means either
330 * unequal or undecidable). */
331 relational::operator relational::safe_bool() const
334 if (!is_exactly_a<numeric>(df))
335 // cannot decide on non-numerical results
336 return o==not_equal ? make_safe_bool(true) : make_safe_bool(false);
340 return make_safe_bool(ex_to<numeric>(df).is_zero());
342 return make_safe_bool(!ex_to<numeric>(df).is_zero());
344 return make_safe_bool(ex_to<numeric>(df)<_num0);
346 return make_safe_bool(ex_to<numeric>(df)<=_num0);
348 return make_safe_bool(ex_to<numeric>(df)>_num0);
349 case greater_or_equal:
350 return make_safe_bool(ex_to<numeric>(df)>=_num0);
352 throw(std::logic_error("invalid relational operator"));