]> www.ginac.de Git - ginac.git/blob - ginac/operators.cpp
- modified GiNaC headers to Alexander's liking
[ginac.git] / ginac / operators.cpp
1 /** @file operators.cpp
2  *
3  *  Implementation of GiNaC's overloaded operators.
4  *
5  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <iostream>
23 #include <stdexcept>
24
25 #include "operators.h"
26 #include "basic.h"
27 #include "ex.h"
28 #include "numeric.h"
29 #include "power.h"
30 #include "relational.h"
31
32 // binary arithmetic operators ex with ex
33
34 ex operator+(ex const & lh, ex const & rh)
35 {
36     debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
37     return lh.exadd(rh);
38 }
39
40 ex operator-(ex const & lh, ex const & rh)
41 {
42     debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
43     return lh.exadd(rh.exmul(exMINUSONE()));
44 }
45
46 ex operator*(ex const & lh, ex const & rh)
47 {
48     debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
49     return lh.exmul(rh);
50 }
51
52 ex operator/(ex const & lh, ex const & rh)
53 {
54     debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
55     return lh.exmul(power(rh,exMINUSONE()));
56 }
57
58 ex operator%(ex const & lh, ex const & rh)
59 {
60     debugmsg("operator%(ex,ex)",LOGLEVEL_OPERATOR);
61     return lh.exncmul(rh);
62 }
63
64 /*
65
66 // binary arithmetic operators ex with numeric
67
68 ex operator+(ex const & lh, numeric const & rh)
69 {
70     debugmsg("operator+(ex,numeric)",LOGLEVEL_OPERATOR);
71     return lh+ex(rh);
72 }
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 // binary arithmetic operators numeric with ex
99
100 ex operator+(numeric const & lh, ex const & rh)
101 {
102     debugmsg("operator+(numeric,ex)",LOGLEVEL_OPERATOR);
103     return ex(lh)+rh;
104 }
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 */
131
132 // binary arithmetic operators numeric with numeric
133
134 numeric operator+(numeric const & lh, numeric const & rh)
135 {
136     debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
137     return lh.add(rh);
138 }
139
140 numeric operator-(numeric const & lh, numeric const & rh)
141 {
142     debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
143     return lh.sub(rh);
144 }
145
146 numeric operator*(numeric const & lh, numeric const & rh)
147 {
148     debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
149     return lh.mul(rh);
150 }
151
152 numeric operator/(numeric const & lh, numeric const & rh)
153 {
154     debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
155     return lh.div(rh);
156 }
157
158 // binary arithmetic assignment operators with ex
159
160 ex const & operator+=(ex & lh, ex const & rh)
161 {
162     debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
163     return (lh=lh+rh);
164 }
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 /*
191
192 // binary arithmetic assignment operators with numeric
193
194 ex const & operator+=(ex & lh, numeric const & rh)
195 {
196     debugmsg("operator+=(ex,numeric)",LOGLEVEL_OPERATOR);
197     return (lh=lh+ex(rh));
198 }
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 */
225
226 // binary arithmetic assignment operators with numeric
227
228 numeric const & operator+=(numeric & lh, numeric const & rh)
229 {
230     debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
231     return (lh=lh.add(rh));
232 }
233
234 numeric const & operator-=(numeric & lh, numeric const & rh)
235 {
236     debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
237     return (lh=lh.sub(rh));
238 }
239
240 numeric const & operator*=(numeric & lh, numeric const & rh)
241 {
242     debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
243     return (lh=lh.mul(rh));
244 }
245
246 numeric const & operator/=(numeric & lh, numeric const & rh)
247 {
248     debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
249     return (lh=lh.div(rh));
250 }
251
252 // unary operators
253
254 ex operator+(ex const & lh)
255 {
256     return lh;
257 }
258
259 ex operator-(ex const & lh)
260 {
261     return exMINUSONE()*lh;
262 }
263
264 numeric operator+(numeric const & lh)
265 {
266     return lh;
267 }
268
269 numeric operator-(numeric const & lh)
270 {
271     return (numeric(-1)*lh);
272 }
273
274 // binary relational operators ex with ex
275
276 relational operator==(ex const & lh, ex const & rh)
277 {
278     debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
279     return relational(lh,rh,relational::equal);
280 }
281
282 relational operator!=(ex const & lh, ex const & rh)
283 {
284     debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
285     return relational(lh,rh,relational::not_equal);
286 }
287
288 relational operator<(ex const & lh, ex const & rh)
289 {
290     debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
291     return relational(lh,rh,relational::less);
292 }
293
294 relational operator<=(ex const & lh, ex const & rh)
295 {
296     debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
297     return relational(lh,rh,relational::less_or_equal);
298 }
299
300 relational operator>(ex const & lh, ex const & rh)
301 {
302     debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
303     return relational(lh,rh,relational::greater);
304 }
305
306 relational operator>=(ex const & lh, ex const & rh)
307 {
308     debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
309     return relational(lh,rh,relational::greater_or_equal);
310 }
311
312 /*
313
314 // binary relational operators ex with numeric
315
316 relational operator==(ex const & lh, numeric const & rh)
317 {
318     debugmsg("operator==(ex,numeric)",LOGLEVEL_OPERATOR);
319     return relational(lh,rh,relational::equal);
320 }
321
322 relational operator!=(ex const & lh, numeric const & rh)
323 {
324     debugmsg("operator!=(ex,numeric)",LOGLEVEL_OPERATOR);
325     return relational(lh,rh,relational::not_equal);
326 }
327
328 relational operator<(ex const & lh, numeric const & rh)
329 {
330     debugmsg("operator<(ex,numeric)",LOGLEVEL_OPERATOR);
331     return relational(lh,rh,relational::less);
332 }
333
334 relational operator<=(ex const & lh, numeric const & rh)
335 {
336     debugmsg("operator<=(ex,numeric)",LOGLEVEL_OPERATOR);
337     return relational(lh,rh,relational::less_or_equal);
338 }
339
340 relational operator>(ex const & lh, numeric const & rh)
341 {
342     debugmsg("operator>(ex,numeric)",LOGLEVEL_OPERATOR);
343     return relational(lh,rh,relational::greater);
344 }
345
346 relational operator>=(ex const & lh, numeric const & rh)
347 {
348     debugmsg("operator>=(ex,numeric)",LOGLEVEL_OPERATOR);
349     return relational(lh,rh,relational::greater_or_equal);
350 }
351
352 // binary relational operators numeric with ex
353
354 relational operator==(numeric const & lh, ex const & rh)
355 {
356     debugmsg("operator==(numeric,ex)",LOGLEVEL_OPERATOR);
357     return relational(lh,rh,relational::equal);
358 }
359
360 relational operator!=(numeric const & lh, ex const & rh)
361 {
362     debugmsg("operator!=(numeric,ex)",LOGLEVEL_OPERATOR);
363     return relational(lh,rh,relational::not_equal);
364 }
365
366 relational operator<(numeric const & lh, ex const & rh)
367 {
368     debugmsg("operator<(numeric,ex)",LOGLEVEL_OPERATOR);
369     return relational(lh,rh,relational::less);
370 }
371
372 relational operator<=(numeric const & lh, ex const & rh)
373 {
374     debugmsg("operator<=(numeric,ex)",LOGLEVEL_OPERATOR);
375     return relational(lh,rh,relational::less_or_equal);
376 }
377
378 relational operator>(numeric const & lh, ex const & rh)
379 {
380     debugmsg("operator>(numeric,ex)",LOGLEVEL_OPERATOR);
381     return relational(lh,rh,relational::greater);
382 }
383
384 relational operator>=(numeric const & lh, ex const & rh)
385 {
386     debugmsg("operator>=(numeric,ex)",LOGLEVEL_OPERATOR);
387     return relational(lh,rh,relational::greater_or_equal);
388 }
389
390 */
391
392 // input/output stream operators
393
394 ostream & operator<<(ostream & os, ex const & e)
395 {
396     e.print(os);
397     return os;
398 }
399
400 istream & operator>>(istream & is, ex & e)
401 {
402     throw(std::logic_error("input from streams not yet implemented"));
403 }
404