]> www.ginac.de Git - ginac.git/blob - ginac/operators.cpp
- introduced info_flags::cinteger, info_flags::crational,
[ginac.git] / ginac / operators.cpp
1 /** @file operators.cpp
2  *
3  *  Implementation of GiNaC's overloaded operators. */
4
5 /*
6  *  GiNaC Copyright (C) 1999 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
34 #ifndef NO_GINAC_NAMESPACE
35 namespace GiNaC {
36 #endif // ndef NO_GINAC_NAMESPACE
37
38 // binary arithmetic operators ex with ex
39
40 ex operator+(ex const & lh, ex const & rh)
41 {
42     debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
43     return lh.exadd(rh);
44 }
45
46 ex operator-(ex const & lh, ex const & rh)
47 {
48     debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
49     return lh.exadd(rh.exmul(exMINUSONE()));
50 }
51
52 ex operator*(ex const & lh, ex const & rh)
53 {
54     debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
55     return lh.exmul(rh);
56 }
57
58 ex operator/(ex const & lh, ex const & rh)
59 {
60     debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
61     return lh.exmul(power(rh,exMINUSONE()));
62 }
63
64 ex operator%(ex const & lh, ex const & rh)
65 {
66     debugmsg("operator%(ex,ex)",LOGLEVEL_OPERATOR);
67     return lh.exncmul(rh);
68 }
69
70 /*
71
72 // binary arithmetic operators ex with numeric
73
74 ex operator+(ex const & lh, numeric const & rh)
75 {
76     debugmsg("operator+(ex,numeric)",LOGLEVEL_OPERATOR);
77     return lh+ex(rh);
78 }
79
80 ex operator-(ex const & lh, numeric const & rh)
81 {
82     debugmsg("operator-(ex,numeric)",LOGLEVEL_OPERATOR);
83     return lh-ex(rh);
84 }
85
86 ex operator*(ex const & lh, numeric const & rh)
87 {
88     debugmsg("operator*(ex,numeric)",LOGLEVEL_OPERATOR);
89     return lh*ex(rh);
90 }
91
92 ex operator/(ex const & lh, numeric const & rh)
93 {
94     debugmsg("operator/(ex,numeric)",LOGLEVEL_OPERATOR);
95     return lh/ex(rh);
96 }
97
98 ex operator%(ex const & lh, numeric const & rh)
99 {
100     debugmsg("operator%(ex,numeric)",LOGLEVEL_OPERATOR);
101     return lh%ex(rh);
102 }
103
104 // binary arithmetic operators numeric with ex
105
106 ex operator+(numeric const & lh, ex const & rh)
107 {
108     debugmsg("operator+(numeric,ex)",LOGLEVEL_OPERATOR);
109     return ex(lh)+rh;
110 }
111
112 ex operator-(numeric const & lh, ex const & rh)
113 {
114     debugmsg("operator-(numeric,ex)",LOGLEVEL_OPERATOR);
115     return ex(lh)-rh;
116 }
117
118 ex operator*(numeric const & lh, ex const & rh)
119 {
120     debugmsg("operator*(numeric,ex)",LOGLEVEL_OPERATOR);
121     return ex(lh)*rh;
122 }
123
124 ex operator/(numeric const & lh, ex const & rh)
125 {
126     debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
127     return ex(lh)/rh;
128 }
129
130 ex operator%(numeric const & lh, ex const & rh)
131 {
132     debugmsg("operator%(numeric,ex)",LOGLEVEL_OPERATOR);
133     return ex(lh)%rh;
134 }
135
136 */
137
138 // binary arithmetic operators numeric with numeric
139
140 numeric operator+(numeric const & lh, numeric const & rh)
141 {
142     debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
143     return lh.add(rh);
144 }
145
146 numeric operator-(numeric const & lh, numeric const & rh)
147 {
148     debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
149     return lh.sub(rh);
150 }
151
152 numeric operator*(numeric const & lh, numeric const & rh)
153 {
154     debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
155     return lh.mul(rh);
156 }
157
158 numeric operator/(numeric const & lh, numeric const & rh)
159 {
160     debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
161     return lh.div(rh);
162 }
163
164 // binary arithmetic assignment operators with ex
165
166 ex const & operator+=(ex & lh, ex const & rh)
167 {
168     debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
169     return (lh=lh+rh);
170 }
171
172 ex const & operator-=(ex & lh, ex const & rh)
173 {
174     debugmsg("operator-=(ex,ex)",LOGLEVEL_OPERATOR);
175     return (lh=lh-rh);
176 }
177
178 ex const & operator*=(ex & lh, ex const & rh)
179 {
180     debugmsg("operator*=(ex,ex)",LOGLEVEL_OPERATOR);
181     return (lh=lh*rh);
182 }
183
184 ex const & operator/=(ex & lh, ex const & rh)
185 {
186     debugmsg("operator/=(ex,ex)",LOGLEVEL_OPERATOR);
187     return (lh=lh/rh);
188 }
189
190 ex const & operator%=(ex & lh, ex const & rh)
191 {
192     debugmsg("operator%=(ex,ex)",LOGLEVEL_OPERATOR);
193     return (lh=lh%rh);
194 }
195
196 /*
197
198 // binary arithmetic assignment operators with numeric
199
200 ex const & operator+=(ex & lh, numeric const & rh)
201 {
202     debugmsg("operator+=(ex,numeric)",LOGLEVEL_OPERATOR);
203     return (lh=lh+ex(rh));
204 }
205
206 ex const & operator-=(ex & lh, numeric const & rh)
207 {
208     debugmsg("operator-=(ex,numeric)",LOGLEVEL_OPERATOR);
209     return (lh=lh-ex(rh));
210 }
211
212 ex const & operator*=(ex & lh, numeric const & rh)
213 {
214     debugmsg("operator*=(ex,numeric)",LOGLEVEL_OPERATOR);
215     return (lh=lh*ex(rh));
216 }
217
218 ex const & operator/=(ex & lh, numeric const & rh)
219 {
220     debugmsg("operator/=(ex,numeric)",LOGLEVEL_OPERATOR);
221     return (lh=lh/ex(rh));
222 }
223
224 ex const & operator%=(ex & lh, numeric const & rh)
225 {
226     debugmsg("operator%=(ex,numeric)",LOGLEVEL_OPERATOR);
227     return (lh=lh%ex(rh));
228 }
229
230 */
231
232 // binary arithmetic assignment operators with numeric
233
234 numeric const & operator+=(numeric & lh, numeric const & rh)
235 {
236     debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
237     return (lh=lh.add(rh));
238 }
239
240 numeric const & operator-=(numeric & lh, numeric const & rh)
241 {
242     debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
243     return (lh=lh.sub(rh));
244 }
245
246 numeric const & operator*=(numeric & lh, numeric const & rh)
247 {
248     debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
249     return (lh=lh.mul(rh));
250 }
251
252 numeric const & operator/=(numeric & lh, numeric const & rh)
253 {
254     debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
255     return (lh=lh.div(rh));
256 }
257
258 // unary operators
259
260 ex operator+(ex const & lh)
261 {
262     return lh;
263 }
264
265 ex operator-(ex const & lh)
266 {
267     return exMINUSONE()*lh;
268 }
269
270 numeric operator+(numeric const & lh)
271 {
272     return lh;
273 }
274
275 numeric operator-(numeric const & lh)
276 {
277     return numMINUSONE()*lh;
278 }
279
280 /** Numeric prefix increment.  Adds 1 and returns incremented number. */
281 numeric& operator++(numeric & rh)
282 {
283     rh = rh+numONE();
284     return rh;
285 }
286
287 /** Numeric prefix decrement.  Subtracts 1 and returns decremented number. */
288 numeric& operator--(numeric & rh)
289 {
290     rh = rh-numONE();
291     return rh;
292 }
293
294 /** Numeric postfix increment.  Returns the number and leaves the original
295  *  incremented by 1. */
296 numeric operator++(numeric & lh, int)
297 {
298     numeric tmp = lh;
299     lh = lh+numONE();
300     return tmp;
301 }
302
303 /** Numeric Postfix decrement.  Returns the number and leaves the original
304  *  decremented by 1. */
305 numeric operator--(numeric & lh, int)
306 {
307     numeric tmp = lh;
308     lh = lh-numONE();
309     return tmp;
310 }
311
312 // binary relational operators ex with ex
313
314 relational operator==(ex const & lh, ex const & rh)
315 {
316     debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
317     return relational(lh,rh,relational::equal);
318 }
319
320 relational operator!=(ex const & lh, ex const & rh)
321 {
322     debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
323     return relational(lh,rh,relational::not_equal);
324 }
325
326 relational operator<(ex const & lh, ex const & rh)
327 {
328     debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
329     return relational(lh,rh,relational::less);
330 }
331
332 relational operator<=(ex const & lh, ex const & rh)
333 {
334     debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
335     return relational(lh,rh,relational::less_or_equal);
336 }
337
338 relational operator>(ex const & lh, ex const & rh)
339 {
340     debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
341     return relational(lh,rh,relational::greater);
342 }
343
344 relational operator>=(ex const & lh, ex const & rh)
345 {
346     debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
347     return relational(lh,rh,relational::greater_or_equal);
348 }
349
350 /*
351
352 // binary relational operators ex with numeric
353
354 relational operator==(ex const & lh, numeric const & rh)
355 {
356     debugmsg("operator==(ex,numeric)",LOGLEVEL_OPERATOR);
357     return relational(lh,rh,relational::equal);
358 }
359
360 relational operator!=(ex const & lh, numeric const & rh)
361 {
362     debugmsg("operator!=(ex,numeric)",LOGLEVEL_OPERATOR);
363     return relational(lh,rh,relational::not_equal);
364 }
365
366 relational operator<(ex const & lh, numeric const & rh)
367 {
368     debugmsg("operator<(ex,numeric)",LOGLEVEL_OPERATOR);
369     return relational(lh,rh,relational::less);
370 }
371
372 relational operator<=(ex const & lh, numeric const & rh)
373 {
374     debugmsg("operator<=(ex,numeric)",LOGLEVEL_OPERATOR);
375     return relational(lh,rh,relational::less_or_equal);
376 }
377
378 relational operator>(ex const & lh, numeric const & rh)
379 {
380     debugmsg("operator>(ex,numeric)",LOGLEVEL_OPERATOR);
381     return relational(lh,rh,relational::greater);
382 }
383
384 relational operator>=(ex const & lh, numeric const & rh)
385 {
386     debugmsg("operator>=(ex,numeric)",LOGLEVEL_OPERATOR);
387     return relational(lh,rh,relational::greater_or_equal);
388 }
389
390 // binary relational operators numeric with ex
391
392 relational operator==(numeric const & lh, ex const & rh)
393 {
394     debugmsg("operator==(numeric,ex)",LOGLEVEL_OPERATOR);
395     return relational(lh,rh,relational::equal);
396 }
397
398 relational operator!=(numeric const & lh, ex const & rh)
399 {
400     debugmsg("operator!=(numeric,ex)",LOGLEVEL_OPERATOR);
401     return relational(lh,rh,relational::not_equal);
402 }
403
404 relational operator<(numeric const & lh, ex const & rh)
405 {
406     debugmsg("operator<(numeric,ex)",LOGLEVEL_OPERATOR);
407     return relational(lh,rh,relational::less);
408 }
409
410 relational operator<=(numeric const & lh, ex const & rh)
411 {
412     debugmsg("operator<=(numeric,ex)",LOGLEVEL_OPERATOR);
413     return relational(lh,rh,relational::less_or_equal);
414 }
415
416 relational operator>(numeric const & lh, ex const & rh)
417 {
418     debugmsg("operator>(numeric,ex)",LOGLEVEL_OPERATOR);
419     return relational(lh,rh,relational::greater);
420 }
421
422 relational operator>=(numeric const & lh, ex const & rh)
423 {
424     debugmsg("operator>=(numeric,ex)",LOGLEVEL_OPERATOR);
425     return relational(lh,rh,relational::greater_or_equal);
426 }
427
428 */
429
430 // input/output stream operators
431
432 ostream & operator<<(ostream & os, ex const & e)
433 {
434     e.print(os);
435     return os;
436 }
437
438 istream & operator>>(istream & is, ex & e)
439 {
440     throw(std::logic_error("input from streams not yet implemented"));
441 }
442
443 #ifndef NO_GINAC_NAMESPACE
444 } // namespace GiNaC
445 #endif // ndef NO_GINAC_NAMESPACE