1 /** @file relational.cpp
3 * Implementation of relations between expressions */
6 * GiNaC Copyright (C) 1999 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"
30 #ifndef NO_GINAC_NAMESPACE
32 #endif // ndef NO_GINAC_NAMESPACE
35 // default constructor, destructor, copy constructor assignment operator and helpers
40 relational::relational() : basic(TINFO_relational)
42 debugmsg("relational default constructor",LOGLEVEL_CONSTRUCT);
45 relational::~relational()
47 debugmsg("relational destructor",LOGLEVEL_DESTRUCT);
51 relational::relational(relational const & other)
53 debugmsg("relational copy constructor",LOGLEVEL_CONSTRUCT);
57 relational const & relational::operator=(relational const & other)
59 debugmsg("relational operator=",LOGLEVEL_ASSIGNMENT);
69 void relational::copy(relational const & other)
77 void relational::destroy(bool call_parent)
79 if (call_parent) basic::destroy(call_parent);
88 relational::relational(ex const & lhs, ex const & rhs, operators oper) : basic(TINFO_relational)
90 debugmsg("relational constructor ex,ex,operator",LOGLEVEL_CONSTRUCT);
97 // functions overriding virtual functions from bases classes
102 basic * relational::duplicate() const
104 debugmsg("relational duplicate",LOGLEVEL_DUPLICATE);
105 return new relational(*this);
108 void relational::print(ostream & os, unsigned upper_precedence) const
110 debugmsg("relational print",LOGLEVEL_PRINT);
111 if (precedence<=upper_precedence) os << "(";
112 lh.print(os,precedence);
129 case greater_or_equal:
133 os << "(INVALID RELATIONAL OPERATOR)";
135 rh.print(os,precedence);
136 if (precedence<=upper_precedence) os << ")";
139 void relational::printraw(ostream & os) const
141 debugmsg("relational printraw",LOGLEVEL_PRINT);
163 case greater_or_equal:
167 os << "(INVALID RELATIONAL OPERATOR)";
172 void relational::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
174 debugmsg("relational print csrc", LOGLEVEL_PRINT);
175 if (precedence<=upper_precedence)
178 // Print left-hand expression
179 lh.bp->printcsrc(os, type, precedence);
181 // Print relational operator
198 case greater_or_equal:
202 os << "(INVALID RELATIONAL OPERATOR)";
206 // Print right-hand operator
207 rh.bp->printcsrc(os, type, precedence);
209 if (precedence <= upper_precedence)
213 bool relational::info(unsigned inf) const
216 case info_flags::relation:
218 case info_flags::relation_equal:
220 case info_flags::relation_not_equal:
222 case info_flags::relation_less:
224 case info_flags::relation_less_or_equal:
225 return o==less_or_equal;
226 case info_flags::relation_greater:
228 case info_flags::relation_greater_or_equal:
229 return o==greater_or_equal;
234 int relational::nops() const
239 ex & relational::let_op(int const i)
244 return i==0 ? lh : rh;
247 ex relational::eval(int level) const
252 if (level == -max_recursion_level) {
253 throw(std::runtime_error("max recursion level reached"));
255 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
256 setflag(status_flags::dynallocated |
257 status_flags::evaluated );
260 ex relational::evalf(int level) const
265 if (level == -max_recursion_level) {
266 throw(std::runtime_error("max recursion level reached"));
268 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
269 setflag(status_flags::dynallocated);
272 ex relational::simplify_ncmul(exvector const & v) const
274 return lh.simplify_ncmul(v);
279 int relational::compare_same_type(basic const & other) const
281 GINAC_ASSERT(is_exactly_of_type(other, relational));
282 relational const & oth=static_cast<relational const &>(const_cast<basic &>(other));
287 cmpval=lh.compare(oth.lh);
289 return rh.compare(oth.rh);
301 unsigned relational::return_type(void) const
303 GINAC_ASSERT(lh.return_type()==rh.return_type());
304 return lh.return_type();
307 unsigned relational::return_type_tinfo(void) const
309 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
310 return lh.return_type_tinfo();
314 // new virtual functions which can be overridden by derived classes
320 // non-virtual functions in this class
325 relational::operator bool() const
327 // please note that (a<b) == false does not imply (a>=b) == true
328 // a false result means the comparison is either false or undecidable
329 // (except for !=, where true means unequal or undecidable)
331 if (!is_ex_exactly_of_type(df,numeric)) {
332 return o==not_equal ? true : false; // cannot decide on non-numerical results
334 int cmpval=ex_to_numeric(df).compare(_num0());
351 case greater_or_equal:
355 throw(std::logic_error("invalid relational operator"));
361 // static member variables
366 unsigned relational::precedence=20;
372 const relational some_relational;
373 type_info const & typeid_relational=typeid(some_relational);
375 #ifndef NO_GINAC_NAMESPACE
377 #endif // ndef NO_GINAC_NAMESPACE