]> www.ginac.de Git - ginac.git/blob - ginac/relational.cpp
252d8b350660360abfdaaa022019388c67aa53e8
[ginac.git] / ginac / relational.cpp
1 /** @file relational.cpp
2  *
3  *  Implementation of relations between expressions */
4
5 #include <stdexcept>
6
7 #include "ginac.h"
8
9 //////////
10 // default constructor, destructor, copy constructor assignment operator and helpers
11 //////////
12
13 // public
14
15 relational::relational() : basic(TINFO_RELATIONAL)
16 {
17     debugmsg("relational default constructor",LOGLEVEL_CONSTRUCT);
18 }
19
20 relational::~relational()
21 {
22     debugmsg("relational destructor",LOGLEVEL_DESTRUCT);
23     destroy(0);
24 }
25
26 relational::relational(relational const & other)
27 {
28     debugmsg("relational copy constructor",LOGLEVEL_CONSTRUCT);
29     copy(other);
30 }
31
32 relational const & relational::operator=(relational const & other)
33 {
34     debugmsg("relational operator=",LOGLEVEL_ASSIGNMENT);
35     if (this != &other) {
36         destroy(1);
37         copy(other);
38     }
39     return *this;
40 }
41
42 // protected
43
44 void relational::copy(relational const & other)
45 {
46     basic::copy(other);
47     lh=other.lh;
48     rh=other.rh;
49     o=other.o;
50 }
51
52 void relational::destroy(bool call_parent)
53 {
54     if (call_parent) basic::destroy(call_parent);
55 }
56
57 //////////
58 // other constructors
59 //////////
60
61 // public
62
63 relational::relational(ex const & lhs, ex const & rhs, operators oper) : basic(TINFO_RELATIONAL)
64 {
65     debugmsg("relational constructor ex,ex,operator",LOGLEVEL_CONSTRUCT);
66     lh=lhs;
67     rh=rhs;
68     o=oper;
69 }
70
71 //////////
72 // functions overriding virtual functions from bases classes
73 //////////
74
75 // public
76
77 basic * relational::duplicate() const
78 {
79     debugmsg("relational duplicate",LOGLEVEL_DUPLICATE);
80     return new relational(*this);
81 }
82
83 bool relational::info(unsigned inf) const
84 {
85     switch (inf) {
86     case info_flags::relation:
87         return 1;
88     case info_flags::relation_equal:
89         return o==equal;
90     case info_flags::relation_not_equal:
91         return o==not_equal;
92     case info_flags::relation_less:
93         return o==less;
94     case info_flags::relation_less_or_equal:
95         return o==less_or_equal;
96     case info_flags::relation_greater:
97         return o==greater;
98     case info_flags::relation_greater_or_equal:
99         return o==greater_or_equal;
100     }
101     return 0;
102 }
103
104 int relational::nops() const
105 {
106     return 2;
107 }
108
109 ex & relational::let_op(int const i)
110 {
111     ASSERT(i>=0);
112     ASSERT(i<2);
113
114     return i==0 ? lh : rh;
115 }
116     
117 ex relational::eval(int level) const
118 {
119     if (level==1) {
120         return this->hold();
121     }
122     if (level == -max_recursion_level) {
123         throw(std::runtime_error("max recursion level reached"));
124     }
125     return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
126             setflag(status_flags::dynallocated  |
127                     status_flags::evaluated );
128 }
129
130 ex relational::evalf(int level) const
131 {
132     if (level==1) {
133         return *this;
134     }
135     if (level == -max_recursion_level) {
136         throw(std::runtime_error("max recursion level reached"));
137     }
138     return (new relational(lh.eval(level-1),rh.eval(level-1),o))->
139             setflag(status_flags::dynallocated);
140 }
141
142 ex relational::simplify_ncmul(exvector const & v) const
143 {
144     return lh.simplify_ncmul(v);
145 }
146
147 // protected
148
149 int relational::compare_same_type(basic const & other) const
150 {
151     ASSERT(is_exactly_of_type(other, relational));
152     relational const & oth=static_cast<relational const &>(const_cast<basic &>(other));
153     
154     int cmpval;
155     
156     if (o == oth.o) {
157         cmpval=lh.compare(oth.lh);
158         if (cmpval==0) {
159             return rh.compare(oth.rh);
160         } else {
161             return cmpval;
162         }
163     }
164     if (o<oth.o) {
165         return -1;
166     } else {
167         return 1;
168     }
169 }
170
171 unsigned relational::return_type(void) const
172 {
173     ASSERT(lh.return_type()==rh.return_type());
174     return lh.return_type();
175 }
176    
177 unsigned relational::return_type_tinfo(void) const
178 {
179     ASSERT(lh.return_type_tinfo()==rh.return_type_tinfo());
180     return lh.return_type_tinfo();
181 }
182
183 //////////
184 // new virtual functions which can be overridden by derived classes
185 //////////
186
187 // none
188
189 //////////
190 // non-virtual functions in this class
191 //////////
192
193 #include <iostream>
194
195 relational::operator bool() const
196 {
197     // please note that (a<b) == false does not imply (a>=b) == true
198     // a false result means the comparison is either false or undecidable
199     // (except for !=, where true means unequal or undecidable)
200     ex df=lh-rh;
201     if (!is_ex_exactly_of_type(df,numeric)) {
202         return o==not_equal ? true : false; // cannot decide on non-numerical results
203     }
204     int cmpval=ex_to_numeric(df).compare(numZERO());
205     switch (o) {
206     case equal:
207         return cmpval==0;
208         break;
209     case not_equal:
210         return cmpval!=0;
211         break;
212     case less:
213         return cmpval<0;
214         break;
215     case less_or_equal:
216         return cmpval<=0;
217         break;
218     case greater:
219         return cmpval>0;
220         break;
221     case greater_or_equal:
222         return cmpval>=0;
223         break;
224     default:
225         throw(std::logic_error("invalid relational operator"));
226     }
227     return 0;
228 }
229
230 //////////
231 // static member variables
232 //////////
233
234 // protected
235
236 unsigned relational::precedence=20;
237
238 //////////
239 // global constants
240 //////////
241
242 const relational some_relational;
243 type_info const & typeid_relational=typeid(some_relational);
244