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
25 #include "relational.h"
33 GINAC_IMPLEMENT_REGISTERED_CLASS(relational, basic)
36 // default ctor, dtor, copy ctor assignment operator and helpers
39 relational::relational() : basic(TINFO_relational)
41 debugmsg("relational default ctor",LOGLEVEL_CONSTRUCT);
44 void relational::copy(const relational & other)
52 DEFAULT_DESTROY(relational)
60 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational)
62 debugmsg("relational ctor ex,ex,operator",LOGLEVEL_CONSTRUCT);
72 relational::relational(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
74 debugmsg("relational ctor from archive_node", LOGLEVEL_CONSTRUCT);
76 if (!(n.find_unsigned("op", opi)))
77 throw (std::runtime_error("unknown relational operator in archive"));
79 n.find_ex("lh", lh, sym_lst);
80 n.find_ex("rh", rh, sym_lst);
83 void relational::archive(archive_node &n) const
85 inherited::archive(n);
88 n.add_unsigned("op", o);
91 DEFAULT_UNARCHIVE(relational)
94 // functions overriding virtual functions from bases classes
99 void relational::print(std::ostream & os, unsigned upper_precedence) const
101 debugmsg("relational print",LOGLEVEL_PRINT);
102 if (precedence<=upper_precedence) os << "(";
103 lh.print(os,precedence);
120 case greater_or_equal:
124 os << "(INVALID RELATIONAL OPERATOR)";
126 rh.print(os,precedence);
127 if (precedence<=upper_precedence) os << ")";
130 void relational::printraw(std::ostream & os) const
132 debugmsg("relational printraw",LOGLEVEL_PRINT);
133 os << class_name() << "(";
154 case greater_or_equal:
158 os << "(INVALID RELATIONAL OPERATOR)";
163 void relational::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
165 debugmsg("relational print csrc", LOGLEVEL_PRINT);
166 if (precedence<=upper_precedence)
169 // Print left-hand expression
170 lh.bp->printcsrc(os, type, precedence);
172 // Print relational operator
189 case greater_or_equal:
193 os << "(INVALID RELATIONAL OPERATOR)";
197 // Print right-hand operator
198 rh.bp->printcsrc(os, type, precedence);
200 if (precedence <= upper_precedence)
204 bool relational::info(unsigned inf) const
207 case info_flags::relation:
209 case info_flags::relation_equal:
211 case info_flags::relation_not_equal:
213 case info_flags::relation_less:
215 case info_flags::relation_less_or_equal:
216 return o==less_or_equal;
217 case info_flags::relation_greater:
219 case info_flags::relation_greater_or_equal:
220 return o==greater_or_equal;
225 unsigned relational::nops() const
230 ex & relational::let_op(int i)
235 return i==0 ? lh : rh;
238 ex relational::eval(int level) const
243 if (level == -max_recursion_level)
244 throw(std::runtime_error("max recursion level reached"));
246 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
249 ex relational::evalf(int level) const
254 if (level==-max_recursion_level)
255 throw(std::runtime_error("max recursion level reached"));
257 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated);
260 ex relational::simplify_ncmul(const exvector & v) const
262 return lh.simplify_ncmul(v);
267 int relational::compare_same_type(const basic & other) const
269 GINAC_ASSERT(is_exactly_of_type(other, relational));
270 const relational & oth=static_cast<const relational &>(const_cast<basic &>(other));
275 cmpval = lh.compare(oth.lh);
277 return rh.compare(oth.rh);
287 unsigned relational::return_type(void) const
289 GINAC_ASSERT(lh.return_type()==rh.return_type());
290 return lh.return_type();
293 unsigned relational::return_type_tinfo(void) const
295 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
296 return lh.return_type_tinfo();
300 // new virtual functions which can be overridden by derived classes
303 /** Left hand side of relational. */
304 ex relational::lhs(void) const
309 /** Right hand side of relational. */
310 ex relational::rhs(void) const
316 // non-virtual functions in this class
319 relational::operator bool() const
321 // please note that (a<b) == false does not imply (a>=b) == true
322 // a false result means the comparison is either false or undecidable
323 // (except for !=, where true means unequal or undecidable)
325 if (!is_ex_exactly_of_type(df,numeric))
326 // cannot decide on non-numerical results
327 return o==not_equal ? true : false;
329 int cmpval = ex_to_numeric(df).compare(_num0());
341 case greater_or_equal:
344 throw(std::logic_error("invalid relational operator"));
349 // static member variables
354 unsigned relational::precedence = 20;