]> www.ginac.de Git - ginac.git/blob - ginac/operators.cpp
- enforced GiNaC coding standards :-)
[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 "ginac.h"
26
27 // binary arithmetic operators ex with ex
28
29 ex operator+(ex const & lh, ex const & rh)
30 {
31     debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
32     return lh.exadd(rh);
33 }
34
35 ex operator-(ex const & lh, ex const & rh)
36 {
37     debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
38     return lh.exadd(rh.exmul(exMINUSONE()));
39 }
40
41 ex operator*(ex const & lh, ex const & rh)
42 {
43     debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
44     return lh.exmul(rh);
45 }
46
47 ex operator/(ex const & lh, ex const & rh)
48 {
49     debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
50     return lh.exmul(power(rh,exMINUSONE()));
51 }
52
53 ex operator%(ex const & lh, ex const & rh)
54 {
55     debugmsg("operator%(ex,ex)",LOGLEVEL_OPERATOR);
56     return lh.exncmul(rh);
57 }
58
59 /*
60
61 // binary arithmetic operators ex with numeric
62
63 ex operator+(ex const & lh, numeric const & rh)
64 {
65     debugmsg("operator+(ex,numeric)",LOGLEVEL_OPERATOR);
66     return lh+ex(rh);
67 }
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 // binary arithmetic operators numeric with ex
94
95 ex operator+(numeric const & lh, ex const & rh)
96 {
97     debugmsg("operator+(numeric,ex)",LOGLEVEL_OPERATOR);
98     return ex(lh)+rh;
99 }
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 */
126
127 // binary arithmetic operators numeric with numeric
128
129 numeric operator+(numeric const & lh, numeric const & rh)
130 {
131     debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
132     return lh.add(rh);
133 }
134
135 numeric operator-(numeric const & lh, numeric const & rh)
136 {
137     debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
138     return lh.sub(rh);
139 }
140
141 numeric operator*(numeric const & lh, numeric const & rh)
142 {
143     debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
144     return lh.mul(rh);
145 }
146
147 numeric operator/(numeric const & lh, numeric const & rh)
148 {
149     debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
150     return lh.div(rh);
151 }
152
153 // binary arithmetic assignment operators with ex
154
155 ex const & operator+=(ex & lh, ex const & rh)
156 {
157     debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
158     return (lh=lh+rh);
159 }
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 /*
186
187 // binary arithmetic assignment operators with numeric
188
189 ex const & operator+=(ex & lh, numeric const & rh)
190 {
191     debugmsg("operator+=(ex,numeric)",LOGLEVEL_OPERATOR);
192     return (lh=lh+ex(rh));
193 }
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 */
220
221 // binary arithmetic assignment operators with numeric
222
223 numeric const & operator+=(numeric & lh, numeric const & rh)
224 {
225     debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
226     return (lh=lh.add(rh));
227 }
228
229 numeric const & operator-=(numeric & lh, numeric const & rh)
230 {
231     debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
232     return (lh=lh.sub(rh));
233 }
234
235 numeric const & operator*=(numeric & lh, numeric const & rh)
236 {
237     debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
238     return (lh=lh.mul(rh));
239 }
240
241 numeric const & operator/=(numeric & lh, numeric const & rh)
242 {
243     debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
244     return (lh=lh.div(rh));
245 }
246
247 // unary operators
248
249 ex operator+(ex const & lh)
250 {
251     return lh;
252 }
253
254 ex operator-(ex const & lh)
255 {
256     return exMINUSONE()*lh;
257 }
258
259 numeric operator+(numeric const & lh)
260 {
261     return lh;
262 }
263
264 numeric operator-(numeric const & lh)
265 {
266     return (numeric(-1)*lh);
267 }
268
269 // binary relational operators ex with ex
270
271 relational operator==(ex const & lh, ex const & rh)
272 {
273     debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
274     return relational(lh,rh,relational::equal);
275 }
276
277 relational operator!=(ex const & lh, ex const & rh)
278 {
279     debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
280     return relational(lh,rh,relational::not_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::less);
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_or_equal);
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::greater);
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_or_equal);
305 }
306
307 /*
308
309 // binary relational operators ex with numeric
310
311 relational operator==(ex const & lh, numeric const & rh)
312 {
313     debugmsg("operator==(ex,numeric)",LOGLEVEL_OPERATOR);
314     return relational(lh,rh,relational::equal);
315 }
316
317 relational operator!=(ex const & lh, numeric const & rh)
318 {
319     debugmsg("operator!=(ex,numeric)",LOGLEVEL_OPERATOR);
320     return relational(lh,rh,relational::not_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::less);
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_or_equal);
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::greater);
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_or_equal);
345 }
346
347 // binary relational operators numeric with ex
348
349 relational operator==(numeric const & lh, ex const & rh)
350 {
351     debugmsg("operator==(numeric,ex)",LOGLEVEL_OPERATOR);
352     return relational(lh,rh,relational::equal);
353 }
354
355 relational operator!=(numeric const & lh, ex const & rh)
356 {
357     debugmsg("operator!=(numeric,ex)",LOGLEVEL_OPERATOR);
358     return relational(lh,rh,relational::not_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::less);
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_or_equal);
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::greater);
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_or_equal);
383 }
384
385 */
386
387 // input/output stream operators
388
389 ostream & operator<<(ostream & os, ex const & e)
390 {
391     e.print(os);
392     return os;
393 }
394
395 istream & operator>>(istream & is, ex & e)
396 {
397     throw(std::logic_error("input from streams not yet implemented"));
398 }
399