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 basic * relational::duplicate() const
116 debugmsg("relational duplicate",LOGLEVEL_DUPLICATE);
117 return new relational(*this);
120 void relational::print(std::ostream & os, unsigned upper_precedence) const
122 debugmsg("relational print",LOGLEVEL_PRINT);
123 if (precedence<=upper_precedence) os << "(";
124 lh.print(os,precedence);
141 case greater_or_equal:
145 os << "(INVALID RELATIONAL OPERATOR)";
147 rh.print(os,precedence);
148 if (precedence<=upper_precedence) os << ")";
151 void relational::printraw(std::ostream & os) const
153 debugmsg("relational printraw",LOGLEVEL_PRINT);
175 case greater_or_equal:
179 os << "(INVALID RELATIONAL OPERATOR)";
184 void relational::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
186 debugmsg("relational print csrc", LOGLEVEL_PRINT);
187 if (precedence<=upper_precedence)
190 // Print left-hand expression
191 lh.bp->printcsrc(os, type, precedence);
193 // Print relational operator
210 case greater_or_equal:
214 os << "(INVALID RELATIONAL OPERATOR)";
218 // Print right-hand operator
219 rh.bp->printcsrc(os, type, precedence);
221 if (precedence <= upper_precedence)
225 bool relational::info(unsigned inf) const
228 case info_flags::relation:
230 case info_flags::relation_equal:
232 case info_flags::relation_not_equal:
234 case info_flags::relation_less:
236 case info_flags::relation_less_or_equal:
237 return o==less_or_equal;
238 case info_flags::relation_greater:
240 case info_flags::relation_greater_or_equal:
241 return o==greater_or_equal;
246 unsigned relational::nops() const
251 ex & relational::let_op(int i)
256 return i==0 ? lh : rh;
259 ex relational::eval(int level) const
264 if (level == -max_recursion_level) {
265 throw(std::runtime_error("max recursion level reached"));
267 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
270 ex relational::evalf(int level) const
275 if (level==-max_recursion_level)
276 throw(std::runtime_error("max recursion level reached"));
278 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated);
281 ex relational::simplify_ncmul(const exvector & v) const
283 return lh.simplify_ncmul(v);
288 int relational::compare_same_type(const basic & other) const
290 GINAC_ASSERT(is_exactly_of_type(other, relational));
291 const relational & oth=static_cast<const relational &>(const_cast<basic &>(other));
296 cmpval = lh.compare(oth.lh);
298 return rh.compare(oth.rh);
308 unsigned relational::return_type(void) const
310 GINAC_ASSERT(lh.return_type()==rh.return_type());
311 return lh.return_type();
314 unsigned relational::return_type_tinfo(void) const
316 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
317 return lh.return_type_tinfo();
321 // new virtual functions which can be overridden by derived classes
324 /** Left hand side of relational. */
325 ex relational::lhs(void) const
330 /** Right hand side of relational. */
331 ex relational::rhs(void) const
337 // non-virtual functions in this class
340 relational::operator bool() const
342 // please note that (a<b) == false does not imply (a>=b) == true
343 // a false result means the comparison is either false or undecidable
344 // (except for !=, where true means unequal or undecidable)
346 if (!is_ex_exactly_of_type(df,numeric))
347 // cannot decide on non-numerical results
348 return o==not_equal ? true : false;
350 int cmpval = ex_to_numeric(df).compare(_num0());
362 case greater_or_equal:
365 throw(std::logic_error("invalid relational operator"));
370 // static member variables
375 unsigned relational::precedence=20;
377 #ifndef NO_NAMESPACE_GINAC
379 #endif // ndef NO_NAMESPACE_GINAC