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