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"
34 GINAC_IMPLEMENT_REGISTERED_CLASS(relational, basic)
37 // default ctor, dtor, copy ctor assignment operator and helpers
40 relational::relational() : basic(TINFO_relational)
42 debugmsg("relational default ctor",LOGLEVEL_CONSTRUCT);
45 void relational::copy(const relational & other)
53 DEFAULT_DESTROY(relational)
61 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational)
63 debugmsg("relational ctor ex,ex,operator",LOGLEVEL_CONSTRUCT);
73 relational::relational(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
75 debugmsg("relational ctor from archive_node", LOGLEVEL_CONSTRUCT);
77 if (!(n.find_unsigned("op", opi)))
78 throw (std::runtime_error("unknown relational operator in archive"));
80 n.find_ex("lh", lh, sym_lst);
81 n.find_ex("rh", rh, sym_lst);
84 void relational::archive(archive_node &n) const
86 inherited::archive(n);
89 n.add_unsigned("op", o);
92 DEFAULT_UNARCHIVE(relational)
95 // functions overriding virtual functions from bases classes
100 void relational::print(const print_context & c, unsigned level) const
102 debugmsg("relational print",LOGLEVEL_PRINT);
104 if (is_of_type(c, print_tree)) {
106 inherited::print(c, level);
110 if (precedence() <= level)
112 lh.print(c, precedence());
129 case greater_or_equal:
133 c.s << "(INVALID RELATIONAL OPERATOR)";
135 rh.print(c, precedence());
136 if (precedence() <= level)
141 bool relational::info(unsigned inf) const
144 case info_flags::relation:
146 case info_flags::relation_equal:
148 case info_flags::relation_not_equal:
150 case info_flags::relation_less:
152 case info_flags::relation_less_or_equal:
153 return o==less_or_equal;
154 case info_flags::relation_greater:
156 case info_flags::relation_greater_or_equal:
157 return o==greater_or_equal;
162 unsigned relational::nops() const
167 ex & relational::let_op(int i)
172 return i==0 ? lh : rh;
175 ex relational::eval(int level) const
180 if (level == -max_recursion_level)
181 throw(std::runtime_error("max recursion level reached"));
183 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
186 ex relational::evalf(int level) const
191 if (level==-max_recursion_level)
192 throw(std::runtime_error("max recursion level reached"));
194 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated);
197 ex relational::simplify_ncmul(const exvector & v) const
199 return lh.simplify_ncmul(v);
204 int relational::compare_same_type(const basic & other) const
206 GINAC_ASSERT(is_exactly_of_type(other, relational));
207 const relational & oth=static_cast<const relational &>(const_cast<basic &>(other));
212 cmpval = lh.compare(oth.lh);
214 return rh.compare(oth.rh);
224 unsigned relational::return_type(void) const
226 GINAC_ASSERT(lh.return_type()==rh.return_type());
227 return lh.return_type();
230 unsigned relational::return_type_tinfo(void) const
232 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
233 return lh.return_type_tinfo();
237 // new virtual functions which can be overridden by derived classes
240 /** Left hand side of relational. */
241 ex relational::lhs(void) const
246 /** Right hand side of relational. */
247 ex relational::rhs(void) const
253 // non-virtual functions in this class
256 relational::operator bool() const
258 // please note that (a<b) == false does not imply (a>=b) == true
259 // a false result means the comparison is either false or undecidable
260 // (except for !=, where true means unequal or undecidable)
262 if (!is_ex_exactly_of_type(df,numeric))
263 // cannot decide on non-numerical results
264 return o==not_equal ? true : false;
266 int cmpval = ex_to_numeric(df).compare(_num0());
278 case greater_or_equal:
281 throw(std::logic_error("invalid relational operator"));