1 /** @file operators.cpp
3 * Implementation of GiNaC's overloaded operators.
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
25 #include "operators.h"
30 #include "relational.h"
32 // binary arithmetic operators ex with ex
34 ex operator+(ex const & lh, ex const & rh)
36 debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
40 ex operator-(ex const & lh, ex const & rh)
42 debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
43 return lh.exadd(rh.exmul(exMINUSONE()));
46 ex operator*(ex const & lh, ex const & rh)
48 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
52 ex operator/(ex const & lh, ex const & rh)
54 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
55 return lh.exmul(power(rh,exMINUSONE()));
58 ex operator%(ex const & lh, ex const & rh)
60 debugmsg("operator%(ex,ex)",LOGLEVEL_OPERATOR);
61 return lh.exncmul(rh);
66 // binary arithmetic operators ex with numeric
68 ex operator+(ex const & lh, numeric const & rh)
70 debugmsg("operator+(ex,numeric)",LOGLEVEL_OPERATOR);
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 // binary arithmetic operators numeric with ex
100 ex operator+(numeric const & lh, ex const & rh)
102 debugmsg("operator+(numeric,ex)",LOGLEVEL_OPERATOR);
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);
132 // binary arithmetic operators numeric with numeric
134 numeric operator+(numeric const & lh, numeric const & rh)
136 debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
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,ex)",LOGLEVEL_OPERATOR);
158 // binary arithmetic assignment operators with ex
160 ex const & operator+=(ex & lh, ex const & rh)
162 debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
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);
192 // binary arithmetic assignment operators with numeric
194 ex const & operator+=(ex & lh, numeric const & rh)
196 debugmsg("operator+=(ex,numeric)",LOGLEVEL_OPERATOR);
197 return (lh=lh+ex(rh));
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));
226 // binary arithmetic assignment operators with numeric
228 numeric const & operator+=(numeric & lh, numeric const & rh)
230 debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
231 return (lh=lh.add(rh));
234 numeric const & operator-=(numeric & lh, numeric const & rh)
236 debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
237 return (lh=lh.sub(rh));
240 numeric const & operator*=(numeric & lh, numeric const & rh)
242 debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
243 return (lh=lh.mul(rh));
246 numeric const & operator/=(numeric & lh, numeric const & rh)
248 debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
249 return (lh=lh.div(rh));
254 ex operator+(ex const & lh)
259 ex operator-(ex const & lh)
261 return exMINUSONE()*lh;
264 numeric operator+(numeric const & lh)
269 numeric operator-(numeric const & lh)
271 return (numeric(-1)*lh);
274 // binary relational operators ex with ex
276 relational operator==(ex const & lh, ex const & rh)
278 debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
279 return relational(lh,rh,relational::equal);
282 relational operator!=(ex const & lh, ex const & rh)
284 debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
285 return relational(lh,rh,relational::not_equal);
288 relational operator<(ex const & lh, ex const & rh)
290 debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
291 return relational(lh,rh,relational::less);
294 relational operator<=(ex const & lh, ex const & rh)
296 debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
297 return relational(lh,rh,relational::less_or_equal);
300 relational operator>(ex const & lh, ex const & rh)
302 debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
303 return relational(lh,rh,relational::greater);
306 relational operator>=(ex const & lh, ex const & rh)
308 debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
309 return relational(lh,rh,relational::greater_or_equal);
314 // binary relational operators ex with numeric
316 relational operator==(ex const & lh, numeric const & rh)
318 debugmsg("operator==(ex,numeric)",LOGLEVEL_OPERATOR);
319 return relational(lh,rh,relational::equal);
322 relational operator!=(ex const & lh, numeric const & rh)
324 debugmsg("operator!=(ex,numeric)",LOGLEVEL_OPERATOR);
325 return relational(lh,rh,relational::not_equal);
328 relational operator<(ex const & lh, numeric const & rh)
330 debugmsg("operator<(ex,numeric)",LOGLEVEL_OPERATOR);
331 return relational(lh,rh,relational::less);
334 relational operator<=(ex const & lh, numeric const & rh)
336 debugmsg("operator<=(ex,numeric)",LOGLEVEL_OPERATOR);
337 return relational(lh,rh,relational::less_or_equal);
340 relational operator>(ex const & lh, numeric const & rh)
342 debugmsg("operator>(ex,numeric)",LOGLEVEL_OPERATOR);
343 return relational(lh,rh,relational::greater);
346 relational operator>=(ex const & lh, numeric const & rh)
348 debugmsg("operator>=(ex,numeric)",LOGLEVEL_OPERATOR);
349 return relational(lh,rh,relational::greater_or_equal);
352 // binary relational operators numeric with ex
354 relational operator==(numeric const & lh, ex const & rh)
356 debugmsg("operator==(numeric,ex)",LOGLEVEL_OPERATOR);
357 return relational(lh,rh,relational::equal);
360 relational operator!=(numeric const & lh, ex const & rh)
362 debugmsg("operator!=(numeric,ex)",LOGLEVEL_OPERATOR);
363 return relational(lh,rh,relational::not_equal);
366 relational operator<(numeric const & lh, ex const & rh)
368 debugmsg("operator<(numeric,ex)",LOGLEVEL_OPERATOR);
369 return relational(lh,rh,relational::less);
372 relational operator<=(numeric const & lh, ex const & rh)
374 debugmsg("operator<=(numeric,ex)",LOGLEVEL_OPERATOR);
375 return relational(lh,rh,relational::less_or_equal);
378 relational operator>(numeric const & lh, ex const & rh)
380 debugmsg("operator>(numeric,ex)",LOGLEVEL_OPERATOR);
381 return relational(lh,rh,relational::greater);
384 relational operator>=(numeric const & lh, ex const & rh)
386 debugmsg("operator>=(numeric,ex)",LOGLEVEL_OPERATOR);
387 return relational(lh,rh,relational::greater_or_equal);
392 // input/output stream operators
394 ostream & operator<<(ostream & os, ex const & e)
400 istream & operator>>(istream & is, ex & e)
402 throw(std::logic_error("input from streams not yet implemented"));