1 /** @file relational.cpp
3 * Implementation of relations between expressions */
6 * GiNaC Copyright (C) 1999-2002 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"
34 GINAC_IMPLEMENT_REGISTERED_CLASS(relational, basic)
37 // default ctor, dtor, copy ctor, assignment operator and helpers
40 relational::relational() : basic(TINFO_relational) {}
42 void relational::copy(const relational & other)
50 DEFAULT_DESTROY(relational)
58 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational), lh(lhs), rh(rhs), o(oper) {}
64 relational::relational(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
67 if (!(n.find_unsigned("op", opi)))
68 throw (std::runtime_error("unknown relational operator in archive"));
70 n.find_ex("lh", lh, sym_lst);
71 n.find_ex("rh", rh, sym_lst);
74 void relational::archive(archive_node &n) const
76 inherited::archive(n);
79 n.add_unsigned("op", o);
82 DEFAULT_UNARCHIVE(relational)
85 // functions overriding virtual functions from base classes
90 void relational::print(const print_context & c, unsigned level) const
92 if (is_a<print_tree>(c)) {
94 inherited::print(c, level);
98 if (is_a<print_python_repr>(c)) {
99 c.s << class_name() << '(';
105 if (precedence() <= level)
107 lh.print(c, precedence());
125 case greater_or_equal:
129 c.s << "(INVALID RELATIONAL OPERATOR)";
131 if (is_a<print_python_repr>(c))
134 rh.print(c, precedence());
135 if (precedence() <= level)
141 bool relational::info(unsigned inf) const
144 case info_flags::relation:
146 case info_flags::relation_equal:
148 case info_flags::relation_not_equal:
150 case info_flags::relation_less:
152 case info_flags::relation_less_or_equal:
153 return o==less_or_equal;
154 case info_flags::relation_greater:
156 case info_flags::relation_greater_or_equal:
157 return o==greater_or_equal;
162 unsigned relational::nops() const
167 ex & relational::let_op(int i)
172 return i==0 ? lh : rh;
175 ex relational::eval(int level) const
180 if (level == -max_recursion_level)
181 throw(std::runtime_error("max recursion level reached"));
183 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
186 ex relational::simplify_ncmul(const exvector & v) const
188 return lh.simplify_ncmul(v);
193 int relational::compare_same_type(const basic & other) const
195 GINAC_ASSERT(is_exactly_a<relational>(other));
196 const relational &oth = static_cast<const relational &>(other);
197 if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
203 return (o < oth.o) ? -1 : 1;
207 return (o < oth.o) ? -1 : 1;
210 if (oth.o!=greater_or_equal)
211 return (o < oth.o) ? -1 : 1;
215 return (o < oth.o) ? -1 : 1;
217 case greater_or_equal:
218 if (oth.o!=less_or_equal)
219 return (o < oth.o) ? -1 : 1;
222 const int lcmpval = lh.compare(oth.rh);
223 return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
226 bool relational::match_same_type(const basic & other) const
228 GINAC_ASSERT(is_exactly_a<relational>(other));
229 const relational &oth = static_cast<const relational &>(other);
234 unsigned relational::return_type(void) const
236 GINAC_ASSERT(lh.return_type()==rh.return_type());
237 return lh.return_type();
240 unsigned relational::return_type_tinfo(void) const
242 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
243 return lh.return_type_tinfo();
246 unsigned relational::calchash(void) const
248 unsigned v = golden_ratio_hash(tinfo());
249 unsigned lhash = lh.gethash();
250 unsigned rhash = rh.gethash();
252 v = rotate_left_31(v);
268 case greater_or_equal:
273 v = rotate_left_31(v);
276 // mask out numeric hashes:
279 // store calculated hash value only if object is already evaluated
280 if (flags & status_flags::evaluated) {
281 setflag(status_flags::hash_calculated);
289 // new virtual functions which can be overridden by derived classes
292 /** Left hand side of relational. */
293 ex relational::lhs(void) const
298 /** Right hand side of relational. */
299 ex relational::rhs(void) const
305 // non-virtual functions in this class
308 relational::safe_bool relational::make_safe_bool(bool cond) const
310 return cond? &safe_bool_helper::nonnull : 0;
313 /** Cast the relational into a boolean, mainly for evaluation within an
314 * if-statement. Note that (a<b) == false does not imply (a>=b) == true in
315 * the general symbolic case. A false result means the comparison is either
316 * false or undecidable (except of course for !=, where true means either
317 * unequal or undecidable). */
318 relational::operator relational::safe_bool() const
321 if (!is_ex_exactly_of_type(df,numeric))
322 // cannot decide on non-numerical results
323 return o==not_equal ? make_safe_bool(true) : make_safe_bool(false);
327 return make_safe_bool(ex_to<numeric>(df).is_zero());
329 return make_safe_bool(!ex_to<numeric>(df).is_zero());
331 return make_safe_bool(ex_to<numeric>(df)<_num0);
333 return make_safe_bool(ex_to<numeric>(df)<=_num0);
335 return make_safe_bool(ex_to<numeric>(df)>_num0);
336 case greater_or_equal:
337 return make_safe_bool(ex_to<numeric>(df)>=_num0);
339 throw(std::logic_error("invalid relational operator"));