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(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(ostream & os) const
175 debugmsg("relational printraw",LOGLEVEL_PRINT);
197 case greater_or_equal:
201 os << "(INVALID RELATIONAL OPERATOR)";
206 void relational::printcsrc(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))->
290 setflag(status_flags::dynallocated |
291 status_flags::evaluated );
294 ex relational::evalf(int level) const
299 if (level == -max_recursion_level) {
300 throw(std::runtime_error("max recursion level reached"));
302 return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
303 setflag(status_flags::dynallocated);
306 ex relational::simplify_ncmul(const exvector & v) const
308 return lh.simplify_ncmul(v);
313 int relational::compare_same_type(const basic & other) const
315 GINAC_ASSERT(is_exactly_of_type(other, relational));
316 const relational & oth=static_cast<const relational &>(const_cast<basic &>(other));
321 cmpval=lh.compare(oth.lh);
323 return rh.compare(oth.rh);
335 unsigned relational::return_type(void) const
337 GINAC_ASSERT(lh.return_type()==rh.return_type());
338 return lh.return_type();
341 unsigned relational::return_type_tinfo(void) const
343 GINAC_ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
344 return lh.return_type_tinfo();
348 // new virtual functions which can be overridden by derived classes
351 /** Left hand side of relational. */
352 ex relational::lhs(void) const
357 /** Right hand side of relational. */
358 ex relational::rhs(void) const
364 // non-virtual functions in this class
369 relational::operator bool() const
371 // please note that (a<b) == false does not imply (a>=b) == true
372 // a false result means the comparison is either false or undecidable
373 // (except for !=, where true means unequal or undecidable)
375 if (!is_ex_exactly_of_type(df,numeric)) {
376 return o==not_equal ? true : false; // cannot decide on non-numerical results
378 int cmpval=ex_to_numeric(df).compare(_num0());
395 case greater_or_equal:
399 throw(std::logic_error("invalid relational operator"));
405 // static member variables
410 unsigned relational::precedence=20;
416 const relational some_relational;
417 const type_info & typeid_relational=typeid(some_relational);
419 #ifndef NO_NAMESPACE_GINAC
421 #endif // ndef NO_NAMESPACE_GINAC