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"
31 #ifndef NO_NAMESPACE_GINAC
33 #endif // ndef NO_NAMESPACE_GINAC
35 GINAC_IMPLEMENT_REGISTERED_CLASS(relational, basic)
38 // default constructor, destructor, copy constructor assignment operator and helpers
43 relational::relational() : basic(TINFO_relational)
45 debugmsg("relational default constructor",LOGLEVEL_CONSTRUCT);
50 void relational::copy(const relational & other)
58 void relational::destroy(bool call_parent)
60 if (call_parent) basic::destroy(call_parent);
69 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational)
71 debugmsg("relational constructor ex,ex,operator",LOGLEVEL_CONSTRUCT);
81 /** Construct object from archive_node. */
82 relational::relational(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
84 debugmsg("relational constructor from archive_node", LOGLEVEL_CONSTRUCT);
86 if (!(n.find_unsigned("op", opi)))
87 throw (std::runtime_error("unknown relational operator in archive"));
89 n.find_ex("lh", lh, sym_lst);
90 n.find_ex("rh", rh, sym_lst);
93 /** Unarchive the object. */
94 ex relational::unarchive(const archive_node &n, const lst &sym_lst)
96 return (new relational(n, sym_lst))->setflag(status_flags::dynallocated);
99 /** Archive the object. */
100 void relational::archive(archive_node &n) const
102 inherited::archive(n);
105 n.add_unsigned("op", o);
109 // functions overriding virtual functions from bases classes
114 void relational::print(std::ostream & os, unsigned upper_precedence) const
116 debugmsg("relational print",LOGLEVEL_PRINT);
117 if (precedence<=upper_precedence) os << "(";
118 lh.print(os,precedence);
135 case greater_or_equal:
139 os << "(INVALID RELATIONAL OPERATOR)";
141 rh.print(os,precedence);
142 if (precedence<=upper_precedence) os << ")";
145 void relational::printraw(std::ostream & os) const
147 debugmsg("relational printraw",LOGLEVEL_PRINT);
169 case greater_or_equal:
173 os << "(INVALID RELATIONAL OPERATOR)";
178 void relational::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
180 debugmsg("relational print csrc", LOGLEVEL_PRINT);
181 if (precedence<=upper_precedence)
184 // Print left-hand expression
185 lh.bp->printcsrc(os, type, precedence);
187 // Print relational operator
204 case greater_or_equal:
208 os << "(INVALID RELATIONAL OPERATOR)";
212 // Print right-hand operator
213 rh.bp->printcsrc(os, type, precedence);
215 if (precedence <= upper_precedence)
219 bool relational::info(unsigned inf) const
222 case info_flags::relation:
224 case info_flags::relation_equal:
226 case info_flags::relation_not_equal:
228 case info_flags::relation_less:
230 case info_flags::relation_less_or_equal:
231 return o==less_or_equal;
232 case info_flags::relation_greater:
234 case info_flags::relation_greater_or_equal:
235 return o==greater_or_equal;
240 unsigned relational::nops() const
245 ex & relational::let_op(int i)
250 return i==0 ? lh : rh;
253 ex relational::eval(int level) const
258 if (level == -max_recursion_level) {
259 throw(std::runtime_error("max recursion level reached"));
261 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
264 ex relational::evalf(int level) const
269 if (level==-max_recursion_level)
270 throw(std::runtime_error("max recursion level reached"));
272 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated);
275 ex relational::simplify_ncmul(const exvector & v) const
277 return lh.simplify_ncmul(v);
282 int relational::compare_same_type(const basic & other) const
284 GINAC_ASSERT(is_exactly_of_type(other, relational));
285 const relational & oth=static_cast<const relational &>(const_cast<basic &>(other));
290 cmpval = lh.compare(oth.lh);
292 return rh.compare(oth.rh);
302 unsigned relational::return_type(void) const
304 GINAC_ASSERT(lh.return_type()==rh.return_type());
305 return lh.return_type();
308 unsigned relational::return_type_tinfo(void) const
310 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
311 return lh.return_type_tinfo();
315 // new virtual functions which can be overridden by derived classes
318 /** Left hand side of relational. */
319 ex relational::lhs(void) const
324 /** Right hand side of relational. */
325 ex relational::rhs(void) const
331 // non-virtual functions in this class
334 relational::operator bool() const
336 // please note that (a<b) == false does not imply (a>=b) == true
337 // a false result means the comparison is either false or undecidable
338 // (except for !=, where true means unequal or undecidable)
340 if (!is_ex_exactly_of_type(df,numeric))
341 // cannot decide on non-numerical results
342 return o==not_equal ? true : false;
344 int cmpval = ex_to_numeric(df).compare(_num0());
356 case greater_or_equal:
359 throw(std::logic_error("invalid relational operator"));
364 // static member variables
369 unsigned relational::precedence=20;
371 #ifndef NO_NAMESPACE_GINAC
373 #endif // ndef NO_NAMESPACE_GINAC