1 /** @file relational.cpp
3 * Implementation of relations between expressions */
6 * GiNaC Copyright (C) 1999-2001 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"
33 GINAC_IMPLEMENT_REGISTERED_CLASS(relational, basic)
36 // default ctor, dtor, copy ctor, assignment operator and helpers
39 relational::relational() : basic(TINFO_relational) {}
41 void relational::copy(const relational & other)
49 DEFAULT_DESTROY(relational)
57 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational), lh(lhs), rh(rhs), o(oper) {}
63 relational::relational(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
66 if (!(n.find_unsigned("op", opi)))
67 throw (std::runtime_error("unknown relational operator in archive"));
69 n.find_ex("lh", lh, sym_lst);
70 n.find_ex("rh", rh, sym_lst);
73 void relational::archive(archive_node &n) const
75 inherited::archive(n);
78 n.add_unsigned("op", o);
81 DEFAULT_UNARCHIVE(relational)
84 // functions overriding virtual functions from base classes
89 void relational::print(const print_context & c, unsigned level) const
91 if (is_a<print_tree>(c)) {
93 inherited::print(c, level);
97 if (precedence() <= level)
99 lh.print(c, precedence());
116 case greater_or_equal:
120 c.s << "(INVALID RELATIONAL OPERATOR)";
122 rh.print(c, precedence());
123 if (precedence() <= level)
128 bool relational::info(unsigned inf) const
131 case info_flags::relation:
133 case info_flags::relation_equal:
135 case info_flags::relation_not_equal:
137 case info_flags::relation_less:
139 case info_flags::relation_less_or_equal:
140 return o==less_or_equal;
141 case info_flags::relation_greater:
143 case info_flags::relation_greater_or_equal:
144 return o==greater_or_equal;
149 unsigned relational::nops() const
154 ex & relational::let_op(int i)
159 return i==0 ? lh : rh;
162 ex relational::eval(int level) const
167 if (level == -max_recursion_level)
168 throw(std::runtime_error("max recursion level reached"));
170 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
173 ex relational::simplify_ncmul(const exvector & v) const
175 return lh.simplify_ncmul(v);
180 int relational::compare_same_type(const basic & other) const
182 GINAC_ASSERT(is_exactly_a<relational>(other));
183 const relational &oth = static_cast<const relational &>(other);
186 int cmpval = lh.compare(oth.lh);
190 return rh.compare(oth.rh);
192 return (o < oth.o) ? -1 : 1;
195 bool relational::match_same_type(const basic & other) const
197 GINAC_ASSERT(is_exactly_a<relational>(other));
198 const relational &oth = static_cast<const relational &>(other);
203 unsigned relational::return_type(void) const
205 GINAC_ASSERT(lh.return_type()==rh.return_type());
206 return lh.return_type();
209 unsigned relational::return_type_tinfo(void) const
211 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
212 return lh.return_type_tinfo();
216 // new virtual functions which can be overridden by derived classes
219 /** Left hand side of relational. */
220 ex relational::lhs(void) const
225 /** Right hand side of relational. */
226 ex relational::rhs(void) const
232 // non-virtual functions in this class
235 /** Cast the relational into a boolean, mainly for evaluation within an
236 * if-statement. Note that (a<b) == false does not imply (a>=b) == true in
237 * the general symbolic case. A false result means the comparison is either
238 * false or undecidable (except of course for !=, where true means either
239 * unequal or undecidable). */
240 relational::operator bool() const
243 if (!is_ex_exactly_of_type(df,numeric))
244 // cannot decide on non-numerical results
245 return o==not_equal ? true : false;
249 return ex_to<numeric>(df).is_zero();
251 return !ex_to<numeric>(df).is_zero();
253 return ex_to<numeric>(df)<_num0;
255 return ex_to<numeric>(df)<=_num0;
257 return ex_to<numeric>(df)>_num0;
258 case greater_or_equal:
259 return ex_to<numeric>(df)>=_num0;
261 throw(std::logic_error("invalid relational operator"));