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"
32 // default constructor, destructor, copy constructor assignment operator and helpers
37 relational::relational() : basic(TINFO_relational)
39 debugmsg("relational default constructor",LOGLEVEL_CONSTRUCT);
42 relational::~relational()
44 debugmsg("relational destructor",LOGLEVEL_DESTRUCT);
48 relational::relational(relational const & other)
50 debugmsg("relational copy constructor",LOGLEVEL_CONSTRUCT);
54 relational const & relational::operator=(relational const & other)
56 debugmsg("relational operator=",LOGLEVEL_ASSIGNMENT);
66 void relational::copy(relational const & other)
74 void relational::destroy(bool call_parent)
76 if (call_parent) basic::destroy(call_parent);
85 relational::relational(ex const & lhs, ex const & rhs, operators oper) : basic(TINFO_relational)
87 debugmsg("relational constructor ex,ex,operator",LOGLEVEL_CONSTRUCT);
94 // functions overriding virtual functions from bases classes
99 basic * relational::duplicate() const
101 debugmsg("relational duplicate",LOGLEVEL_DUPLICATE);
102 return new relational(*this);
105 bool relational::info(unsigned inf) const
108 case info_flags::relation:
110 case info_flags::relation_equal:
112 case info_flags::relation_not_equal:
114 case info_flags::relation_less:
116 case info_flags::relation_less_or_equal:
117 return o==less_or_equal;
118 case info_flags::relation_greater:
120 case info_flags::relation_greater_or_equal:
121 return o==greater_or_equal;
126 int relational::nops() const
131 ex & relational::let_op(int const i)
136 return i==0 ? lh : rh;
139 ex relational::eval(int level) const
144 if (level == -max_recursion_level) {
145 throw(std::runtime_error("max recursion level reached"));
147 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
148 setflag(status_flags::dynallocated |
149 status_flags::evaluated );
152 ex relational::evalf(int level) const
157 if (level == -max_recursion_level) {
158 throw(std::runtime_error("max recursion level reached"));
160 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
161 setflag(status_flags::dynallocated);
164 ex relational::simplify_ncmul(exvector const & v) const
166 return lh.simplify_ncmul(v);
171 int relational::compare_same_type(basic const & other) const
173 ASSERT(is_exactly_of_type(other, relational));
174 relational const & oth=static_cast<relational const &>(const_cast<basic &>(other));
179 cmpval=lh.compare(oth.lh);
181 return rh.compare(oth.rh);
193 unsigned relational::return_type(void) const
195 ASSERT(lh.return_type()==rh.return_type());
196 return lh.return_type();
199 unsigned relational::return_type_tinfo(void) const
201 ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
202 return lh.return_type_tinfo();
206 // new virtual functions which can be overridden by derived classes
212 // non-virtual functions in this class
217 relational::operator bool() const
219 // please note that (a<b) == false does not imply (a>=b) == true
220 // a false result means the comparison is either false or undecidable
221 // (except for !=, where true means unequal or undecidable)
223 if (!is_ex_exactly_of_type(df,numeric)) {
224 return o==not_equal ? true : false; // cannot decide on non-numerical results
226 int cmpval=ex_to_numeric(df).compare(numZERO());
243 case greater_or_equal:
247 throw(std::logic_error("invalid relational operator"));
253 // static member variables
258 unsigned relational::precedence=20;
264 const relational some_relational;
265 type_info const & typeid_relational=typeid(some_relational);