1 /** @file operators.cpp
3 * Implementation of GiNaC's overloaded operators. */
6 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
26 #include "operators.h"
31 #include "relational.h"
34 #ifndef NO_GINAC_NAMESPACE
36 #endif // ndef NO_GINAC_NAMESPACE
38 // binary arithmetic operators ex with ex
40 ex operator+(ex const & lh, ex const & rh)
42 debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
46 ex operator-(ex const & lh, ex const & rh)
48 debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
49 return lh.exadd(rh.exmul(exMINUSONE()));
52 ex operator*(ex const & lh, ex const & rh)
54 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
58 ex operator/(ex const & lh, ex const & rh)
60 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
61 return lh.exmul(power(rh,exMINUSONE()));
64 ex operator%(ex const & lh, ex const & rh)
66 debugmsg("operator%(ex,ex)",LOGLEVEL_OPERATOR);
67 return lh.exncmul(rh);
72 // binary arithmetic operators ex with numeric
74 ex operator+(ex const & lh, numeric const & rh)
76 debugmsg("operator+(ex,numeric)",LOGLEVEL_OPERATOR);
80 ex operator-(ex const & lh, numeric const & rh)
82 debugmsg("operator-(ex,numeric)",LOGLEVEL_OPERATOR);
86 ex operator*(ex const & lh, numeric const & rh)
88 debugmsg("operator*(ex,numeric)",LOGLEVEL_OPERATOR);
92 ex operator/(ex const & lh, numeric const & rh)
94 debugmsg("operator/(ex,numeric)",LOGLEVEL_OPERATOR);
98 ex operator%(ex const & lh, numeric const & rh)
100 debugmsg("operator%(ex,numeric)",LOGLEVEL_OPERATOR);
104 // binary arithmetic operators numeric with ex
106 ex operator+(numeric const & lh, ex const & rh)
108 debugmsg("operator+(numeric,ex)",LOGLEVEL_OPERATOR);
112 ex operator-(numeric const & lh, ex const & rh)
114 debugmsg("operator-(numeric,ex)",LOGLEVEL_OPERATOR);
118 ex operator*(numeric const & lh, ex const & rh)
120 debugmsg("operator*(numeric,ex)",LOGLEVEL_OPERATOR);
124 ex operator/(numeric const & lh, ex const & rh)
126 debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
130 ex operator%(numeric const & lh, ex const & rh)
132 debugmsg("operator%(numeric,ex)",LOGLEVEL_OPERATOR);
138 // binary arithmetic operators numeric with numeric
140 numeric operator+(numeric const & lh, numeric const & rh)
142 debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
146 numeric operator-(numeric const & lh, numeric const & rh)
148 debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
152 numeric operator*(numeric const & lh, numeric const & rh)
154 debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
158 numeric operator/(numeric const & lh, numeric const & rh)
160 debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
164 // binary arithmetic assignment operators with ex
166 ex const & operator+=(ex & lh, ex const & rh)
168 debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
172 ex const & operator-=(ex & lh, ex const & rh)
174 debugmsg("operator-=(ex,ex)",LOGLEVEL_OPERATOR);
178 ex const & operator*=(ex & lh, ex const & rh)
180 debugmsg("operator*=(ex,ex)",LOGLEVEL_OPERATOR);
184 ex const & operator/=(ex & lh, ex const & rh)
186 debugmsg("operator/=(ex,ex)",LOGLEVEL_OPERATOR);
190 ex const & operator%=(ex & lh, ex const & rh)
192 debugmsg("operator%=(ex,ex)",LOGLEVEL_OPERATOR);
198 // binary arithmetic assignment operators with numeric
200 ex const & operator+=(ex & lh, numeric const & rh)
202 debugmsg("operator+=(ex,numeric)",LOGLEVEL_OPERATOR);
203 return (lh=lh+ex(rh));
206 ex const & operator-=(ex & lh, numeric const & rh)
208 debugmsg("operator-=(ex,numeric)",LOGLEVEL_OPERATOR);
209 return (lh=lh-ex(rh));
212 ex const & operator*=(ex & lh, numeric const & rh)
214 debugmsg("operator*=(ex,numeric)",LOGLEVEL_OPERATOR);
215 return (lh=lh*ex(rh));
218 ex const & operator/=(ex & lh, numeric const & rh)
220 debugmsg("operator/=(ex,numeric)",LOGLEVEL_OPERATOR);
221 return (lh=lh/ex(rh));
224 ex const & operator%=(ex & lh, numeric const & rh)
226 debugmsg("operator%=(ex,numeric)",LOGLEVEL_OPERATOR);
227 return (lh=lh%ex(rh));
232 // binary arithmetic assignment operators with numeric
234 numeric const & operator+=(numeric & lh, numeric const & rh)
236 debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
237 return (lh=lh.add(rh));
240 numeric const & operator-=(numeric & lh, numeric const & rh)
242 debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
243 return (lh=lh.sub(rh));
246 numeric const & operator*=(numeric & lh, numeric const & rh)
248 debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
249 return (lh=lh.mul(rh));
252 numeric const & operator/=(numeric & lh, numeric const & rh)
254 debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
255 return (lh=lh.div(rh));
260 ex operator+(ex const & lh)
265 ex operator-(ex const & lh)
267 return exMINUSONE()*lh;
270 numeric operator+(numeric const & lh)
275 numeric operator-(numeric const & lh)
277 return numMINUSONE()*lh;
280 /** Numeric prefix increment. Adds 1 and returns incremented number. */
281 numeric& operator++(numeric & rh)
287 /** Numeric prefix decrement. Subtracts 1 and returns decremented number. */
288 numeric& operator--(numeric & rh)
294 /** Numeric postfix increment. Returns the number and leaves the original
295 * incremented by 1. */
296 numeric operator++(numeric & lh, int)
303 /** Numeric Postfix decrement. Returns the number and leaves the original
304 * decremented by 1. */
305 numeric operator--(numeric & lh, int)
312 // binary relational operators ex with ex
314 relational operator==(ex const & lh, ex const & rh)
316 debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
317 return relational(lh,rh,relational::equal);
320 relational operator!=(ex const & lh, ex const & rh)
322 debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
323 return relational(lh,rh,relational::not_equal);
326 relational operator<(ex const & lh, ex const & rh)
328 debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
329 return relational(lh,rh,relational::less);
332 relational operator<=(ex const & lh, ex const & rh)
334 debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
335 return relational(lh,rh,relational::less_or_equal);
338 relational operator>(ex const & lh, ex const & rh)
340 debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
341 return relational(lh,rh,relational::greater);
344 relational operator>=(ex const & lh, ex const & rh)
346 debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
347 return relational(lh,rh,relational::greater_or_equal);
352 // binary relational operators ex with numeric
354 relational operator==(ex const & lh, numeric const & rh)
356 debugmsg("operator==(ex,numeric)",LOGLEVEL_OPERATOR);
357 return relational(lh,rh,relational::equal);
360 relational operator!=(ex const & lh, numeric const & rh)
362 debugmsg("operator!=(ex,numeric)",LOGLEVEL_OPERATOR);
363 return relational(lh,rh,relational::not_equal);
366 relational operator<(ex const & lh, numeric const & rh)
368 debugmsg("operator<(ex,numeric)",LOGLEVEL_OPERATOR);
369 return relational(lh,rh,relational::less);
372 relational operator<=(ex const & lh, numeric const & rh)
374 debugmsg("operator<=(ex,numeric)",LOGLEVEL_OPERATOR);
375 return relational(lh,rh,relational::less_or_equal);
378 relational operator>(ex const & lh, numeric const & rh)
380 debugmsg("operator>(ex,numeric)",LOGLEVEL_OPERATOR);
381 return relational(lh,rh,relational::greater);
384 relational operator>=(ex const & lh, numeric const & rh)
386 debugmsg("operator>=(ex,numeric)",LOGLEVEL_OPERATOR);
387 return relational(lh,rh,relational::greater_or_equal);
390 // binary relational operators numeric with ex
392 relational operator==(numeric const & lh, ex const & rh)
394 debugmsg("operator==(numeric,ex)",LOGLEVEL_OPERATOR);
395 return relational(lh,rh,relational::equal);
398 relational operator!=(numeric const & lh, ex const & rh)
400 debugmsg("operator!=(numeric,ex)",LOGLEVEL_OPERATOR);
401 return relational(lh,rh,relational::not_equal);
404 relational operator<(numeric const & lh, ex const & rh)
406 debugmsg("operator<(numeric,ex)",LOGLEVEL_OPERATOR);
407 return relational(lh,rh,relational::less);
410 relational operator<=(numeric const & lh, ex const & rh)
412 debugmsg("operator<=(numeric,ex)",LOGLEVEL_OPERATOR);
413 return relational(lh,rh,relational::less_or_equal);
416 relational operator>(numeric const & lh, ex const & rh)
418 debugmsg("operator>(numeric,ex)",LOGLEVEL_OPERATOR);
419 return relational(lh,rh,relational::greater);
422 relational operator>=(numeric const & lh, ex const & rh)
424 debugmsg("operator>=(numeric,ex)",LOGLEVEL_OPERATOR);
425 return relational(lh,rh,relational::greater_or_equal);
430 // input/output stream operators
432 ostream & operator<<(ostream & os, ex const & e)
438 istream & operator>>(istream & is, ex & e)
440 throw(std::logic_error("input from streams not yet implemented"));
443 #ifndef NO_GINAC_NAMESPACE
445 #endif // ndef NO_GINAC_NAMESPACE