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