1 /** @file relational.cpp
3 * Implementation of relations between expressions */
6 * GiNaC Copyright (C) 1999-2001 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 (precedence() <= level)
100 lh.print(c, precedence());
117 case greater_or_equal:
121 c.s << "(INVALID RELATIONAL OPERATOR)";
123 rh.print(c, precedence());
124 if (precedence() <= level)
129 bool relational::info(unsigned inf) const
132 case info_flags::relation:
134 case info_flags::relation_equal:
136 case info_flags::relation_not_equal:
138 case info_flags::relation_less:
140 case info_flags::relation_less_or_equal:
141 return o==less_or_equal;
142 case info_flags::relation_greater:
144 case info_flags::relation_greater_or_equal:
145 return o==greater_or_equal;
150 unsigned relational::nops() const
155 ex & relational::let_op(int i)
160 return i==0 ? lh : rh;
163 ex relational::eval(int level) const
168 if (level == -max_recursion_level)
169 throw(std::runtime_error("max recursion level reached"));
171 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
174 ex relational::simplify_ncmul(const exvector & v) const
176 return lh.simplify_ncmul(v);
181 int relational::compare_same_type(const basic & other) const
183 GINAC_ASSERT(is_exactly_a<relational>(other));
184 const relational &oth = static_cast<const relational &>(other);
185 if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
191 return (o < oth.o) ? -1 : 1;
195 return (o < oth.o) ? -1 : 1;
198 if (oth.o!=greater_or_equal)
199 return (o < oth.o) ? -1 : 1;
203 return (o < oth.o) ? -1 : 1;
205 case greater_or_equal:
206 if (oth.o!=less_or_equal)
207 return (o < oth.o) ? -1 : 1;
210 const int lcmpval = lh.compare(oth.rh);
211 return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
214 bool relational::match_same_type(const basic & other) const
216 GINAC_ASSERT(is_exactly_a<relational>(other));
217 const relational &oth = static_cast<const relational &>(other);
222 unsigned relational::return_type(void) const
224 GINAC_ASSERT(lh.return_type()==rh.return_type());
225 return lh.return_type();
228 unsigned relational::return_type_tinfo(void) const
230 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
231 return lh.return_type_tinfo();
234 unsigned relational::calchash(void) const
236 unsigned v = golden_ratio_hash(tinfo());
237 unsigned lhash = lh.gethash();
238 unsigned rhash = rh.gethash();
240 v = rotate_left_31(v);
256 case greater_or_equal:
261 v = rotate_left_31(v);
264 // mask out numeric hashes:
267 // store calculated hash value only if object is already evaluated
268 if (flags & status_flags::evaluated) {
269 setflag(status_flags::hash_calculated);
276 bool relational::is_equal_same_type(const basic & other) const
278 GINAC_ASSERT(is_a<relational>(other));
279 const relational &oth = static_cast<const relational &>(other);
280 if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh))
293 if (oth.o!=greater_or_equal)
300 case greater_or_equal:
301 if (oth.o!=less_or_equal)
305 return lh.is_equal(oth.rh) && rh.is_equal(oth.lh);
310 // new virtual functions which can be overridden by derived classes
313 /** Left hand side of relational. */
314 ex relational::lhs(void) const
319 /** Right hand side of relational. */
320 ex relational::rhs(void) const
326 // non-virtual functions in this class
329 /** Cast the relational into a boolean, mainly for evaluation within an
330 * if-statement. Note that (a<b) == false does not imply (a>=b) == true in
331 * the general symbolic case. A false result means the comparison is either
332 * false or undecidable (except of course for !=, where true means either
333 * unequal or undecidable). */
334 relational::operator bool() const
337 if (!is_ex_exactly_of_type(df,numeric))
338 // cannot decide on non-numerical results
339 return o==not_equal ? true : false;
343 return ex_to<numeric>(df).is_zero();
345 return !ex_to<numeric>(df).is_zero();
347 return ex_to<numeric>(df)<_num0;
349 return ex_to<numeric>(df)<=_num0;
351 return ex_to<numeric>(df)>_num0;
352 case greater_or_equal:
353 return ex_to<numeric>(df)>=_num0;
355 throw(std::logic_error("invalid relational operator"));