1 /** @file relational.cpp
3 * Implementation of relations between expressions */
6 * GiNaC Copyright (C) 1999-2000 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);
48 relational::~relational()
50 debugmsg("relational destructor",LOGLEVEL_DESTRUCT);
54 relational::relational(const relational & other)
56 debugmsg("relational copy constructor",LOGLEVEL_CONSTRUCT);
60 const relational & relational::operator=(const relational & other)
62 debugmsg("relational operator=",LOGLEVEL_ASSIGNMENT);
72 void relational::copy(const relational & other)
80 void relational::destroy(bool call_parent)
82 if (call_parent) basic::destroy(call_parent);
91 relational::relational(const ex & lhs, const ex & rhs, operators oper) : basic(TINFO_relational)
93 debugmsg("relational constructor ex,ex,operator",LOGLEVEL_CONSTRUCT);
103 /** Construct object from archive_node. */
104 relational::relational(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
106 debugmsg("relational constructor from archive_node", LOGLEVEL_CONSTRUCT);
108 if (!(n.find_unsigned("op", opi)))
109 throw (std::runtime_error("unknown relational operator in archive"));
111 n.find_ex("lh", lh, sym_lst);
112 n.find_ex("rh", rh, sym_lst);
115 /** Unarchive the object. */
116 ex relational::unarchive(const archive_node &n, const lst &sym_lst)
118 return (new relational(n, sym_lst))->setflag(status_flags::dynallocated);
121 /** Archive the object. */
122 void relational::archive(archive_node &n) const
124 inherited::archive(n);
127 n.add_unsigned("op", o);
131 // functions overriding virtual functions from bases classes
136 basic * relational::duplicate() const
138 debugmsg("relational duplicate",LOGLEVEL_DUPLICATE);
139 return new relational(*this);
142 void relational::print(std::ostream & os, unsigned upper_precedence) const
144 debugmsg("relational print",LOGLEVEL_PRINT);
145 if (precedence<=upper_precedence) os << "(";
146 lh.print(os,precedence);
163 case greater_or_equal:
167 os << "(INVALID RELATIONAL OPERATOR)";
169 rh.print(os,precedence);
170 if (precedence<=upper_precedence) os << ")";
173 void relational::printraw(std::ostream & os) const
175 debugmsg("relational printraw",LOGLEVEL_PRINT);
197 case greater_or_equal:
201 os << "(INVALID RELATIONAL OPERATOR)";
206 void relational::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
208 debugmsg("relational print csrc", LOGLEVEL_PRINT);
209 if (precedence<=upper_precedence)
212 // Print left-hand expression
213 lh.bp->printcsrc(os, type, precedence);
215 // Print relational operator
232 case greater_or_equal:
236 os << "(INVALID RELATIONAL OPERATOR)";
240 // Print right-hand operator
241 rh.bp->printcsrc(os, type, precedence);
243 if (precedence <= upper_precedence)
247 bool relational::info(unsigned inf) const
250 case info_flags::relation:
252 case info_flags::relation_equal:
254 case info_flags::relation_not_equal:
256 case info_flags::relation_less:
258 case info_flags::relation_less_or_equal:
259 return o==less_or_equal;
260 case info_flags::relation_greater:
262 case info_flags::relation_greater_or_equal:
263 return o==greater_or_equal;
268 unsigned relational::nops() const
273 ex & relational::let_op(int i)
278 return i==0 ? lh : rh;
281 ex relational::eval(int level) const
286 if (level == -max_recursion_level) {
287 throw(std::runtime_error("max recursion level reached"));
289 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated | status_flags::evaluated);
292 ex relational::evalf(int level) const
297 if (level==-max_recursion_level)
298 throw(std::runtime_error("max recursion level reached"));
300 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->setflag(status_flags::dynallocated);
303 ex relational::simplify_ncmul(const exvector & v) const
305 return lh.simplify_ncmul(v);
310 int relational::compare_same_type(const basic & other) const
312 GINAC_ASSERT(is_exactly_of_type(other, relational));
313 const relational & oth=static_cast<const relational &>(const_cast<basic &>(other));
318 cmpval = lh.compare(oth.lh);
320 return rh.compare(oth.rh);
330 unsigned relational::return_type(void) const
332 GINAC_ASSERT(lh.return_type()==rh.return_type());
333 return lh.return_type();
336 unsigned relational::return_type_tinfo(void) const
338 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
339 return lh.return_type_tinfo();
343 // new virtual functions which can be overridden by derived classes
346 /** Left hand side of relational. */
347 ex relational::lhs(void) const
352 /** Right hand side of relational. */
353 ex relational::rhs(void) const
359 // non-virtual functions in this class
362 relational::operator bool() const
364 // please note that (a<b) == false does not imply (a>=b) == true
365 // a false result means the comparison is either false or undecidable
366 // (except for !=, where true means unequal or undecidable)
368 if (!is_ex_exactly_of_type(df,numeric))
369 // cannot decide on non-numerical results
370 return o==not_equal ? true : false;
372 int cmpval = ex_to_numeric(df).compare(_num0());
384 case greater_or_equal:
387 throw(std::logic_error("invalid relational operator"));
392 // static member variables
397 unsigned relational::precedence=20;
403 const relational some_relational;
404 const std::type_info & typeid_relational = typeid(some_relational);
406 #ifndef NO_NAMESPACE_GINAC
408 #endif // ndef NO_NAMESPACE_GINAC