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
41 relational::relational() : basic(TINFO_relational)
43 debugmsg("relational default ctor",LOGLEVEL_CONSTRUCT);
48 void relational::copy(const relational & other)
56 void relational::destroy(bool call_parent)
58 if (call_parent) basic::destroy(call_parent);
67 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational)
69 debugmsg("relational ctor ex,ex,operator",LOGLEVEL_CONSTRUCT);
79 /** Construct object from archive_node. */
80 relational::relational(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
82 debugmsg("relational ctor from archive_node", LOGLEVEL_CONSTRUCT);
84 if (!(n.find_unsigned("op", opi)))
85 throw (std::runtime_error("unknown relational operator in archive"));
87 n.find_ex("lh", lh, sym_lst);
88 n.find_ex("rh", rh, sym_lst);
91 /** Unarchive the object. */
92 ex relational::unarchive(const archive_node &n, const lst &sym_lst)
94 return (new relational(n, sym_lst))->setflag(status_flags::dynallocated);
97 /** Archive the object. */
98 void relational::archive(archive_node &n) const
100 inherited::archive(n);
103 n.add_unsigned("op", o);
107 // functions overriding virtual functions from bases classes
112 void relational::print(std::ostream & os, unsigned upper_precedence) const
114 debugmsg("relational print",LOGLEVEL_PRINT);
115 if (precedence<=upper_precedence) os << "(";
116 lh.print(os,precedence);
133 case greater_or_equal:
137 os << "(INVALID RELATIONAL OPERATOR)";
139 rh.print(os,precedence);
140 if (precedence<=upper_precedence) os << ")";
143 void relational::printraw(std::ostream & os) const
145 debugmsg("relational printraw",LOGLEVEL_PRINT);
167 case greater_or_equal:
171 os << "(INVALID RELATIONAL OPERATOR)";
176 void relational::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
178 debugmsg("relational print csrc", LOGLEVEL_PRINT);
179 if (precedence<=upper_precedence)
182 // Print left-hand expression
183 lh.bp->printcsrc(os, type, precedence);
185 // Print relational operator
202 case greater_or_equal:
206 os << "(INVALID RELATIONAL OPERATOR)";
210 // Print right-hand operator
211 rh.bp->printcsrc(os, type, precedence);
213 if (precedence <= upper_precedence)
217 bool relational::info(unsigned inf) const
220 case info_flags::relation:
222 case info_flags::relation_equal:
224 case info_flags::relation_not_equal:
226 case info_flags::relation_less:
228 case info_flags::relation_less_or_equal:
229 return o==less_or_equal;
230 case info_flags::relation_greater:
232 case info_flags::relation_greater_or_equal:
233 return o==greater_or_equal;
238 unsigned relational::nops() const
243 ex & relational::let_op(int i)
248 return i==0 ? lh : rh;
251 ex relational::eval(int level) const
256 if (level == -max_recursion_level)
257 throw(std::runtime_error("max recursion level reached"));
259 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
262 ex relational::evalf(int level) const
267 if (level==-max_recursion_level)
268 throw(std::runtime_error("max recursion level reached"));
270 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated);
273 ex relational::simplify_ncmul(const exvector & v) const
275 return lh.simplify_ncmul(v);
280 int relational::compare_same_type(const basic & other) const
282 GINAC_ASSERT(is_exactly_of_type(other, relational));
283 const relational & oth=static_cast<const relational &>(const_cast<basic &>(other));
288 cmpval = lh.compare(oth.lh);
290 return rh.compare(oth.rh);
300 unsigned relational::return_type(void) const
302 GINAC_ASSERT(lh.return_type()==rh.return_type());
303 return lh.return_type();
306 unsigned relational::return_type_tinfo(void) const
308 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
309 return lh.return_type_tinfo();
313 // new virtual functions which can be overridden by derived classes
316 /** Left hand side of relational. */
317 ex relational::lhs(void) const
322 /** Right hand side of relational. */
323 ex relational::rhs(void) const
329 // non-virtual functions in this class
332 relational::operator bool() const
334 // please note that (a<b) == false does not imply (a>=b) == true
335 // a false result means the comparison is either false or undecidable
336 // (except for !=, where true means unequal or undecidable)
338 if (!is_ex_exactly_of_type(df,numeric))
339 // cannot decide on non-numerical results
340 return o==not_equal ? true : false;
342 int cmpval = ex_to_numeric(df).compare(_num0());
354 case greater_or_equal:
357 throw(std::logic_error("invalid relational operator"));
362 // static member variables
367 unsigned relational::precedence = 20;