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
27 // default constructor, destructor, copy constructor assignment operator and helpers
32 relational::relational() : basic(TINFO_RELATIONAL)
34 debugmsg("relational default constructor",LOGLEVEL_CONSTRUCT);
37 relational::~relational()
39 debugmsg("relational destructor",LOGLEVEL_DESTRUCT);
43 relational::relational(relational const & other)
45 debugmsg("relational copy constructor",LOGLEVEL_CONSTRUCT);
49 relational const & relational::operator=(relational const & other)
51 debugmsg("relational operator=",LOGLEVEL_ASSIGNMENT);
61 void relational::copy(relational const & other)
69 void relational::destroy(bool call_parent)
71 if (call_parent) basic::destroy(call_parent);
80 relational::relational(ex const & lhs, ex const & rhs, operators oper) : basic(TINFO_RELATIONAL)
82 debugmsg("relational constructor ex,ex,operator",LOGLEVEL_CONSTRUCT);
89 // functions overriding virtual functions from bases classes
94 basic * relational::duplicate() const
96 debugmsg("relational duplicate",LOGLEVEL_DUPLICATE);
97 return new relational(*this);
100 bool relational::info(unsigned inf) const
103 case info_flags::relation:
105 case info_flags::relation_equal:
107 case info_flags::relation_not_equal:
109 case info_flags::relation_less:
111 case info_flags::relation_less_or_equal:
112 return o==less_or_equal;
113 case info_flags::relation_greater:
115 case info_flags::relation_greater_or_equal:
116 return o==greater_or_equal;
121 int relational::nops() const
126 ex & relational::let_op(int const i)
131 return i==0 ? lh : rh;
134 ex relational::eval(int level) const
139 if (level == -max_recursion_level) {
140 throw(std::runtime_error("max recursion level reached"));
142 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
143 setflag(status_flags::dynallocated |
144 status_flags::evaluated );
147 ex relational::evalf(int level) const
152 if (level == -max_recursion_level) {
153 throw(std::runtime_error("max recursion level reached"));
155 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
156 setflag(status_flags::dynallocated);
159 ex relational::simplify_ncmul(exvector const & v) const
161 return lh.simplify_ncmul(v);
166 int relational::compare_same_type(basic const & other) const
168 ASSERT(is_exactly_of_type(other, relational));
169 relational const & oth=static_cast<relational const &>(const_cast<basic &>(other));
174 cmpval=lh.compare(oth.lh);
176 return rh.compare(oth.rh);
188 unsigned relational::return_type(void) const
190 ASSERT(lh.return_type()==rh.return_type());
191 return lh.return_type();
194 unsigned relational::return_type_tinfo(void) const
196 ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
197 return lh.return_type_tinfo();
201 // new virtual functions which can be overridden by derived classes
207 // non-virtual functions in this class
212 relational::operator bool() const
214 // please note that (a<b) == false does not imply (a>=b) == true
215 // a false result means the comparison is either false or undecidable
216 // (except for !=, where true means unequal or undecidable)
218 if (!is_ex_exactly_of_type(df,numeric)) {
219 return o==not_equal ? true : false; // cannot decide on non-numerical results
221 int cmpval=ex_to_numeric(df).compare(numZERO());
238 case greater_or_equal:
242 throw(std::logic_error("invalid relational operator"));
248 // static member variables
253 unsigned relational::precedence=20;
259 const relational some_relational;
260 type_info const & typeid_relational=typeid(some_relational);