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"
29 #ifndef NO_GINAC_NAMESPACE
31 #endif // ndef NO_GINAC_NAMESPACE
34 // default constructor, destructor, copy constructor assignment operator and helpers
39 relational::relational() : basic(TINFO_relational)
41 debugmsg("relational default constructor",LOGLEVEL_CONSTRUCT);
44 relational::~relational()
46 debugmsg("relational destructor",LOGLEVEL_DESTRUCT);
50 relational::relational(relational const & other)
52 debugmsg("relational copy constructor",LOGLEVEL_CONSTRUCT);
56 relational const & relational::operator=(relational const & other)
58 debugmsg("relational operator=",LOGLEVEL_ASSIGNMENT);
68 void relational::copy(relational const & other)
76 void relational::destroy(bool call_parent)
78 if (call_parent) basic::destroy(call_parent);
87 relational::relational(ex const & lhs, ex const & rhs, operators oper) : basic(TINFO_relational)
89 debugmsg("relational constructor ex,ex,operator",LOGLEVEL_CONSTRUCT);
96 // functions overriding virtual functions from bases classes
101 basic * relational::duplicate() const
103 debugmsg("relational duplicate",LOGLEVEL_DUPLICATE);
104 return new relational(*this);
107 void relational::print(ostream & os, unsigned upper_precedence) const
109 debugmsg("relational print",LOGLEVEL_PRINT);
110 if (precedence<=upper_precedence) os << "(";
111 lh.print(os,precedence);
128 case greater_or_equal:
132 os << "(INVALID RELATIONAL OPERATOR)";
134 rh.print(os,precedence);
135 if (precedence<=upper_precedence) os << ")";
138 void relational::printraw(ostream & os) const
140 debugmsg("relational printraw",LOGLEVEL_PRINT);
162 case greater_or_equal:
166 os << "(INVALID RELATIONAL OPERATOR)";
171 void relational::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
173 debugmsg("relational print csrc", LOGLEVEL_PRINT);
174 if (precedence<=upper_precedence)
177 // Print left-hand expression
178 lh.bp->printcsrc(os, type, precedence);
180 // Print relational operator
197 case greater_or_equal:
201 os << "(INVALID RELATIONAL OPERATOR)";
205 // Print right-hand operator
206 rh.bp->printcsrc(os, type, precedence);
208 if (precedence <= upper_precedence)
212 bool relational::info(unsigned inf) const
215 case info_flags::relation:
217 case info_flags::relation_equal:
219 case info_flags::relation_not_equal:
221 case info_flags::relation_less:
223 case info_flags::relation_less_or_equal:
224 return o==less_or_equal;
225 case info_flags::relation_greater:
227 case info_flags::relation_greater_or_equal:
228 return o==greater_or_equal;
233 int relational::nops() const
238 ex & relational::let_op(int const i)
243 return i==0 ? lh : rh;
246 ex relational::eval(int level) const
251 if (level == -max_recursion_level) {
252 throw(std::runtime_error("max recursion level reached"));
254 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
255 setflag(status_flags::dynallocated |
256 status_flags::evaluated );
259 ex relational::evalf(int level) const
264 if (level == -max_recursion_level) {
265 throw(std::runtime_error("max recursion level reached"));
267 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
268 setflag(status_flags::dynallocated);
271 ex relational::simplify_ncmul(exvector const & v) const
273 return lh.simplify_ncmul(v);
278 int relational::compare_same_type(basic const & other) const
280 GINAC_ASSERT(is_exactly_of_type(other, relational));
281 relational const & oth=static_cast<relational const &>(const_cast<basic &>(other));
286 cmpval=lh.compare(oth.lh);
288 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
319 // non-virtual functions in this class
324 relational::operator bool() const
326 // please note that (a<b) == false does not imply (a>=b) == true
327 // a false result means the comparison is either false or undecidable
328 // (except for !=, where true means unequal or undecidable)
330 if (!is_ex_exactly_of_type(df,numeric)) {
331 return o==not_equal ? true : false; // cannot decide on non-numerical results
333 int cmpval=ex_to_numeric(df).compare(numZERO());
350 case greater_or_equal:
354 throw(std::logic_error("invalid relational operator"));
360 // static member variables
365 unsigned relational::precedence=20;
371 const relational some_relational;
372 type_info const & typeid_relational=typeid(some_relational);
374 #ifndef NO_GINAC_NAMESPACE
376 #endif // ndef NO_GINAC_NAMESPACE