]> www.ginac.de Git - ginac.git/blob - ginac/operators.cpp
- New figure classhierarchy.fig, which we all know, included in...
[ginac.git] / ginac / operators.cpp
1 /** @file operators.cpp
2  *
3  *  Implementation of GiNaC's overloaded operators. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <iostream>
24 #include <stdexcept>
25
26 #include "operators.h"
27 #include "basic.h"
28 #include "ex.h"
29 #include "numeric.h"
30 #include "power.h"
31 #include "relational.h"
32 #include "debugmsg.h"
33 #include "utils.h"
34
35 #ifndef NO_NAMESPACE_GINAC
36 namespace GiNaC {
37 #endif // ndef NO_NAMESPACE_GINAC
38
39 // binary arithmetic operators ex with ex
40
41 ex operator+(const ex & lh, const ex & rh)
42 {
43     debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
44     return lh.exadd(rh);
45 }
46
47 ex operator-(const ex & lh, const ex & rh)
48 {
49     debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
50     return lh.exadd(rh.exmul(_ex_1()));
51 }
52
53 ex operator*(const ex & lh, const ex & rh)
54 {
55     debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
56     return lh.exmul(rh);
57 }
58
59 ex operator/(const ex & lh, const ex & rh)
60 {
61     debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
62     return lh.exmul(power(rh,_ex_1()));
63 }
64
65 ex operator%(const ex & lh, const ex & rh)
66 {
67     debugmsg("operator%(ex,ex)",LOGLEVEL_OPERATOR);
68     return lh.exncmul(rh);
69 }
70
71
72 // binary arithmetic operators numeric with numeric
73
74 numeric operator+(const numeric & lh, const numeric & rh)
75 {
76     debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
77     return lh.add(rh);
78 }
79
80 numeric operator-(const numeric & lh, const numeric & rh)
81 {
82     debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
83     return lh.sub(rh);
84 }
85
86 numeric operator*(const numeric & lh, const numeric & rh)
87 {
88     debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
89     return lh.mul(rh);
90 }
91
92 numeric operator/(const numeric & lh, const numeric & rh)
93 {
94     debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
95     return lh.div(rh);
96 }
97
98
99 // binary arithmetic assignment operators with ex
100
101 const ex & operator+=(ex & lh, const ex & rh)
102 {
103     debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
104     return (lh=lh+rh);
105 }
106
107 const ex & operator-=(ex & lh, const ex & rh)
108 {
109     debugmsg("operator-=(ex,ex)",LOGLEVEL_OPERATOR);
110     return (lh=lh-rh);
111 }
112
113 const ex & operator*=(ex & lh, const ex & rh)
114 {
115     debugmsg("operator*=(ex,ex)",LOGLEVEL_OPERATOR);
116     return (lh=lh*rh);
117 }
118
119 const ex & operator/=(ex & lh, const ex & rh)
120 {
121     debugmsg("operator/=(ex,ex)",LOGLEVEL_OPERATOR);
122     return (lh=lh/rh);
123 }
124
125 const ex & operator%=(ex & lh, const ex & rh)
126 {
127     debugmsg("operator%=(ex,ex)",LOGLEVEL_OPERATOR);
128     return (lh=lh%rh);
129 }
130
131
132 // binary arithmetic assignment operators with numeric
133
134 const numeric & operator+=(numeric & lh, const numeric & rh)
135 {
136     debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
137     return (lh=lh.add(rh));
138 }
139
140 const numeric & operator-=(numeric & lh, const numeric & rh)
141 {
142     debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
143     return (lh=lh.sub(rh));
144 }
145
146 const numeric & operator*=(numeric & lh, const numeric & rh)
147 {
148     debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
149     return (lh=lh.mul(rh));
150 }
151
152 const numeric & operator/=(numeric & lh, const numeric & rh)
153 {
154     debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
155     return (lh=lh.div(rh));
156 }
157
158 // unary operators
159
160 ex operator+(const ex & lh)
161 {
162     return lh;
163 }
164
165 ex operator-(const ex & lh)
166 {
167     return lh.exmul(_ex_1());
168 }
169
170 numeric operator+(const numeric & lh)
171 {
172     return lh;
173 }
174
175 numeric operator-(const numeric & lh)
176 {
177     return _num_1()*lh;
178 }
179
180 /** Numeric prefix increment.  Adds 1 and returns incremented number. */
181 numeric& operator++(numeric & rh)
182 {
183     rh = rh+_num1();
184     return rh;
185 }
186
187 /** Numeric prefix decrement.  Subtracts 1 and returns decremented number. */
188 numeric& operator--(numeric & rh)
189 {
190     rh = rh-_num1();
191     return rh;
192 }
193
194 /** Numeric postfix increment.  Returns the number and leaves the original
195  *  incremented by 1. */
196 numeric operator++(numeric & lh, int)
197 {
198     numeric tmp = lh;
199     lh = lh+_num1();
200     return tmp;
201 }
202
203 /** Numeric Postfix decrement.  Returns the number and leaves the original
204  *  decremented by 1. */
205 numeric operator--(numeric & lh, int)
206 {
207     numeric tmp = lh;
208     lh = lh-_num1();
209     return tmp;
210 }
211
212 // binary relational operators ex with ex
213
214 relational operator==(const ex & lh, const ex & rh)
215 {
216     debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
217     return relational(lh,rh,relational::equal);
218 }
219
220 relational operator!=(const ex & lh, const ex & rh)
221 {
222     debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
223     return relational(lh,rh,relational::not_equal);
224 }
225
226 relational operator<(const ex & lh, const ex & rh)
227 {
228     debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
229     return relational(lh,rh,relational::less);
230 }
231
232 relational operator<=(const ex & lh, const ex & rh)
233 {
234     debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
235     return relational(lh,rh,relational::less_or_equal);
236 }
237
238 relational operator>(const ex & lh, const ex & rh)
239 {
240     debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
241     return relational(lh,rh,relational::greater);
242 }
243
244 relational operator>=(const ex & lh, const ex & rh)
245 {
246     debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
247     return relational(lh,rh,relational::greater_or_equal);
248 }
249
250 /*
251
252 // binary relational operators ex with numeric
253
254 relational operator==(const ex & lh, const numeric & rh)
255 {
256     debugmsg("operator==(ex,numeric)",LOGLEVEL_OPERATOR);
257     return relational(lh,rh,relational::equal);
258 }
259
260 relational operator!=(const ex & lh, const numeric & rh)
261 {
262     debugmsg("operator!=(ex,numeric)",LOGLEVEL_OPERATOR);
263     return relational(lh,rh,relational::not_equal);
264 }
265
266 relational operator<(const ex & lh, const numeric & rh)
267 {
268     debugmsg("operator<(ex,numeric)",LOGLEVEL_OPERATOR);
269     return relational(lh,rh,relational::less);
270 }
271
272 relational operator<=(const ex & lh, const numeric & rh)
273 {
274     debugmsg("operator<=(ex,numeric)",LOGLEVEL_OPERATOR);
275     return relational(lh,rh,relational::less_or_equal);
276 }
277
278 relational operator>(const ex & lh, const numeric & rh)
279 {
280     debugmsg("operator>(ex,numeric)",LOGLEVEL_OPERATOR);
281     return relational(lh,rh,relational::greater);
282 }
283
284 relational operator>=(const ex & lh, const numeric & rh)
285 {
286     debugmsg("operator>=(ex,numeric)",LOGLEVEL_OPERATOR);
287     return relational(lh,rh,relational::greater_or_equal);
288 }
289
290 // binary relational operators numeric with ex
291
292 relational operator==(const numeric & lh, const ex & rh)
293 {
294     debugmsg("operator==(numeric,ex)",LOGLEVEL_OPERATOR);
295     return relational(lh,rh,relational::equal);
296 }
297
298 relational operator!=(const numeric & lh, const ex & rh)
299 {
300     debugmsg("operator!=(numeric,ex)",LOGLEVEL_OPERATOR);
301     return relational(lh,rh,relational::not_equal);
302 }
303
304 relational operator<(const numeric & lh, const ex & rh)
305 {
306     debugmsg("operator<(numeric,ex)",LOGLEVEL_OPERATOR);
307     return relational(lh,rh,relational::less);
308 }
309
310 relational operator<=(const numeric & lh, const ex & rh)
311 {
312     debugmsg("operator<=(numeric,ex)",LOGLEVEL_OPERATOR);
313     return relational(lh,rh,relational::less_or_equal);
314 }
315
316 relational operator>(const numeric & lh, const ex & rh)
317 {
318     debugmsg("operator>(numeric,ex)",LOGLEVEL_OPERATOR);
319     return relational(lh,rh,relational::greater);
320 }
321
322 relational operator>=(const numeric & lh, const ex & rh)
323 {
324     debugmsg("operator>=(numeric,ex)",LOGLEVEL_OPERATOR);
325     return relational(lh,rh,relational::greater_or_equal);
326 }
327
328 */
329
330 // input/output stream operators
331
332 ostream & operator<<(ostream & os, const ex & e)
333 {
334     e.print(os);
335     return os;
336 }
337
338 istream & operator>>(istream & is, ex & e)
339 {
340     throw (std::logic_error("expression input from streams not implemented"));
341 }
342
343 #ifndef NO_NAMESPACE_GINAC
344 } // namespace GiNaC
345 #endif // ndef NO_NAMESPACE_GINAC