1 /** @file relational.cpp
3 * Implementation of relations between expressions
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "relational.h"
28 // default constructor, destructor, copy constructor assignment operator and helpers
33 relational::relational() : basic(TINFO_relational)
35 debugmsg("relational default constructor",LOGLEVEL_CONSTRUCT);
38 relational::~relational()
40 debugmsg("relational destructor",LOGLEVEL_DESTRUCT);
44 relational::relational(relational const & other)
46 debugmsg("relational copy constructor",LOGLEVEL_CONSTRUCT);
50 relational const & relational::operator=(relational const & other)
52 debugmsg("relational operator=",LOGLEVEL_ASSIGNMENT);
62 void relational::copy(relational const & other)
70 void relational::destroy(bool call_parent)
72 if (call_parent) basic::destroy(call_parent);
81 relational::relational(ex const & lhs, ex const & rhs, operators oper) : basic(TINFO_relational)
83 debugmsg("relational constructor ex,ex,operator",LOGLEVEL_CONSTRUCT);
90 // functions overriding virtual functions from bases classes
95 basic * relational::duplicate() const
97 debugmsg("relational duplicate",LOGLEVEL_DUPLICATE);
98 return new relational(*this);
101 bool relational::info(unsigned inf) const
104 case info_flags::relation:
106 case info_flags::relation_equal:
108 case info_flags::relation_not_equal:
110 case info_flags::relation_less:
112 case info_flags::relation_less_or_equal:
113 return o==less_or_equal;
114 case info_flags::relation_greater:
116 case info_flags::relation_greater_or_equal:
117 return o==greater_or_equal;
122 int relational::nops() const
127 ex & relational::let_op(int const i)
132 return i==0 ? lh : rh;
135 ex relational::eval(int level) const
140 if (level == -max_recursion_level) {
141 throw(std::runtime_error("max recursion level reached"));
143 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
144 setflag(status_flags::dynallocated |
145 status_flags::evaluated );
148 ex relational::evalf(int level) const
153 if (level == -max_recursion_level) {
154 throw(std::runtime_error("max recursion level reached"));
156 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
157 setflag(status_flags::dynallocated);
160 ex relational::simplify_ncmul(exvector const & v) const
162 return lh.simplify_ncmul(v);
167 int relational::compare_same_type(basic const & other) const
169 ASSERT(is_exactly_of_type(other, relational));
170 relational const & oth=static_cast<relational const &>(const_cast<basic &>(other));
175 cmpval=lh.compare(oth.lh);
177 return rh.compare(oth.rh);
189 unsigned relational::return_type(void) const
191 ASSERT(lh.return_type()==rh.return_type());
192 return lh.return_type();
195 unsigned relational::return_type_tinfo(void) const
197 ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
198 return lh.return_type_tinfo();
202 // new virtual functions which can be overridden by derived classes
208 // non-virtual functions in this class
213 relational::operator bool() const
215 // please note that (a<b) == false does not imply (a>=b) == true
216 // a false result means the comparison is either false or undecidable
217 // (except for !=, where true means unequal or undecidable)
219 if (!is_ex_exactly_of_type(df,numeric)) {
220 return o==not_equal ? true : false; // cannot decide on non-numerical results
222 int cmpval=ex_to_numeric(df).compare(numZERO());
239 case greater_or_equal:
243 throw(std::logic_error("invalid relational operator"));
249 // static member variables
254 unsigned relational::precedence=20;
260 const relational some_relational;
261 type_info const & typeid_relational=typeid(some_relational);