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"
36 // binary arithmetic operators ex with ex
38 ex operator+(ex const & lh, ex const & rh)
40 debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
44 ex operator-(ex const & lh, ex const & rh)
46 debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
47 return lh.exadd(rh.exmul(exMINUSONE()));
50 ex operator*(ex const & lh, ex const & rh)
52 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
56 ex operator/(ex const & lh, ex const & rh)
58 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
59 return lh.exmul(power(rh,exMINUSONE()));
62 ex operator%(ex const & lh, ex const & rh)
64 debugmsg("operator%(ex,ex)",LOGLEVEL_OPERATOR);
65 return lh.exncmul(rh);
70 // binary arithmetic operators ex with numeric
72 ex operator+(ex const & lh, numeric const & rh)
74 debugmsg("operator+(ex,numeric)",LOGLEVEL_OPERATOR);
78 ex operator-(ex const & lh, numeric const & rh)
80 debugmsg("operator-(ex,numeric)",LOGLEVEL_OPERATOR);
84 ex operator*(ex const & lh, numeric const & rh)
86 debugmsg("operator*(ex,numeric)",LOGLEVEL_OPERATOR);
90 ex operator/(ex const & lh, numeric const & rh)
92 debugmsg("operator/(ex,numeric)",LOGLEVEL_OPERATOR);
96 ex operator%(ex const & lh, numeric const & rh)
98 debugmsg("operator%(ex,numeric)",LOGLEVEL_OPERATOR);
102 // binary arithmetic operators numeric with ex
104 ex operator+(numeric const & lh, ex const & rh)
106 debugmsg("operator+(numeric,ex)",LOGLEVEL_OPERATOR);
110 ex operator-(numeric const & lh, ex const & rh)
112 debugmsg("operator-(numeric,ex)",LOGLEVEL_OPERATOR);
116 ex operator*(numeric const & lh, ex const & rh)
118 debugmsg("operator*(numeric,ex)",LOGLEVEL_OPERATOR);
122 ex operator/(numeric const & lh, ex const & rh)
124 debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
128 ex operator%(numeric const & lh, ex const & rh)
130 debugmsg("operator%(numeric,ex)",LOGLEVEL_OPERATOR);
136 // binary arithmetic operators numeric with numeric
138 numeric operator+(numeric const & lh, numeric const & rh)
140 debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
144 numeric operator-(numeric const & lh, numeric const & rh)
146 debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
150 numeric operator*(numeric const & lh, numeric const & rh)
152 debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
156 numeric operator/(numeric const & lh, numeric const & rh)
158 debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
162 // binary arithmetic assignment operators with ex
164 ex const & operator+=(ex & lh, ex const & rh)
166 debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
170 ex const & operator-=(ex & lh, ex const & rh)
172 debugmsg("operator-=(ex,ex)",LOGLEVEL_OPERATOR);
176 ex const & operator*=(ex & lh, ex const & rh)
178 debugmsg("operator*=(ex,ex)",LOGLEVEL_OPERATOR);
182 ex const & operator/=(ex & lh, ex const & rh)
184 debugmsg("operator/=(ex,ex)",LOGLEVEL_OPERATOR);
188 ex const & operator%=(ex & lh, ex const & rh)
190 debugmsg("operator%=(ex,ex)",LOGLEVEL_OPERATOR);
196 // binary arithmetic assignment operators with numeric
198 ex const & operator+=(ex & lh, numeric const & rh)
200 debugmsg("operator+=(ex,numeric)",LOGLEVEL_OPERATOR);
201 return (lh=lh+ex(rh));
204 ex const & operator-=(ex & lh, numeric const & rh)
206 debugmsg("operator-=(ex,numeric)",LOGLEVEL_OPERATOR);
207 return (lh=lh-ex(rh));
210 ex const & operator*=(ex & lh, numeric const & rh)
212 debugmsg("operator*=(ex,numeric)",LOGLEVEL_OPERATOR);
213 return (lh=lh*ex(rh));
216 ex const & operator/=(ex & lh, numeric const & rh)
218 debugmsg("operator/=(ex,numeric)",LOGLEVEL_OPERATOR);
219 return (lh=lh/ex(rh));
222 ex const & operator%=(ex & lh, numeric const & rh)
224 debugmsg("operator%=(ex,numeric)",LOGLEVEL_OPERATOR);
225 return (lh=lh%ex(rh));
230 // binary arithmetic assignment operators with numeric
232 numeric const & operator+=(numeric & lh, numeric const & rh)
234 debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
235 return (lh=lh.add(rh));
238 numeric const & operator-=(numeric & lh, numeric const & rh)
240 debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
241 return (lh=lh.sub(rh));
244 numeric const & operator*=(numeric & lh, numeric const & rh)
246 debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
247 return (lh=lh.mul(rh));
250 numeric const & operator/=(numeric & lh, numeric const & rh)
252 debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
253 return (lh=lh.div(rh));
258 ex operator+(ex const & lh)
263 ex operator-(ex const & lh)
265 return exMINUSONE()*lh;
268 numeric operator+(numeric const & lh)
273 numeric operator-(numeric const & lh)
275 return (numeric(-1)*lh);
278 // binary relational operators ex with ex
280 relational operator==(ex const & lh, ex const & rh)
282 debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
283 return relational(lh,rh,relational::equal);
286 relational operator!=(ex const & lh, ex const & rh)
288 debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
289 return relational(lh,rh,relational::not_equal);
292 relational operator<(ex const & lh, ex const & rh)
294 debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
295 return relational(lh,rh,relational::less);
298 relational operator<=(ex const & lh, ex const & rh)
300 debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
301 return relational(lh,rh,relational::less_or_equal);
304 relational operator>(ex const & lh, ex const & rh)
306 debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
307 return relational(lh,rh,relational::greater);
310 relational operator>=(ex const & lh, ex const & rh)
312 debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
313 return relational(lh,rh,relational::greater_or_equal);
318 // binary relational operators ex with numeric
320 relational operator==(ex const & lh, numeric const & rh)
322 debugmsg("operator==(ex,numeric)",LOGLEVEL_OPERATOR);
323 return relational(lh,rh,relational::equal);
326 relational operator!=(ex const & lh, numeric const & rh)
328 debugmsg("operator!=(ex,numeric)",LOGLEVEL_OPERATOR);
329 return relational(lh,rh,relational::not_equal);
332 relational operator<(ex const & lh, numeric const & rh)
334 debugmsg("operator<(ex,numeric)",LOGLEVEL_OPERATOR);
335 return relational(lh,rh,relational::less);
338 relational operator<=(ex const & lh, numeric const & rh)
340 debugmsg("operator<=(ex,numeric)",LOGLEVEL_OPERATOR);
341 return relational(lh,rh,relational::less_or_equal);
344 relational operator>(ex const & lh, numeric const & rh)
346 debugmsg("operator>(ex,numeric)",LOGLEVEL_OPERATOR);
347 return relational(lh,rh,relational::greater);
350 relational operator>=(ex const & lh, numeric const & rh)
352 debugmsg("operator>=(ex,numeric)",LOGLEVEL_OPERATOR);
353 return relational(lh,rh,relational::greater_or_equal);
356 // binary relational operators numeric with ex
358 relational operator==(numeric const & lh, ex const & rh)
360 debugmsg("operator==(numeric,ex)",LOGLEVEL_OPERATOR);
361 return relational(lh,rh,relational::equal);
364 relational operator!=(numeric const & lh, ex const & rh)
366 debugmsg("operator!=(numeric,ex)",LOGLEVEL_OPERATOR);
367 return relational(lh,rh,relational::not_equal);
370 relational operator<(numeric const & lh, ex const & rh)
372 debugmsg("operator<(numeric,ex)",LOGLEVEL_OPERATOR);
373 return relational(lh,rh,relational::less);
376 relational operator<=(numeric const & lh, ex const & rh)
378 debugmsg("operator<=(numeric,ex)",LOGLEVEL_OPERATOR);
379 return relational(lh,rh,relational::less_or_equal);
382 relational operator>(numeric const & lh, ex const & rh)
384 debugmsg("operator>(numeric,ex)",LOGLEVEL_OPERATOR);
385 return relational(lh,rh,relational::greater);
388 relational operator>=(numeric const & lh, ex const & rh)
390 debugmsg("operator>=(numeric,ex)",LOGLEVEL_OPERATOR);
391 return relational(lh,rh,relational::greater_or_equal);
396 // input/output stream operators
398 ostream & operator<<(ostream & os, ex const & e)
404 istream & operator>>(istream & is, ex & e)
406 throw(std::logic_error("input from streams not yet implemented"));