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"
33 // binary arithmetic operators ex with ex
35 ex operator+(ex const & lh, ex const & rh)
37 debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
41 ex operator-(ex const & lh, ex const & rh)
43 debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
44 return lh.exadd(rh.exmul(exMINUSONE()));
47 ex operator*(ex const & lh, ex const & rh)
49 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
53 ex operator/(ex const & lh, ex const & rh)
55 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
56 return lh.exmul(power(rh,exMINUSONE()));
59 ex operator%(ex const & lh, ex const & rh)
61 debugmsg("operator%(ex,ex)",LOGLEVEL_OPERATOR);
62 return lh.exncmul(rh);
67 // binary arithmetic operators ex with numeric
69 ex operator+(ex const & lh, numeric const & rh)
71 debugmsg("operator+(ex,numeric)",LOGLEVEL_OPERATOR);
75 ex operator-(ex const & lh, numeric const & rh)
77 debugmsg("operator-(ex,numeric)",LOGLEVEL_OPERATOR);
81 ex operator*(ex const & lh, numeric const & rh)
83 debugmsg("operator*(ex,numeric)",LOGLEVEL_OPERATOR);
87 ex operator/(ex const & lh, numeric const & rh)
89 debugmsg("operator/(ex,numeric)",LOGLEVEL_OPERATOR);
93 ex operator%(ex const & lh, numeric const & rh)
95 debugmsg("operator%(ex,numeric)",LOGLEVEL_OPERATOR);
99 // binary arithmetic operators numeric with ex
101 ex operator+(numeric const & lh, ex const & rh)
103 debugmsg("operator+(numeric,ex)",LOGLEVEL_OPERATOR);
107 ex operator-(numeric const & lh, ex const & rh)
109 debugmsg("operator-(numeric,ex)",LOGLEVEL_OPERATOR);
113 ex operator*(numeric const & lh, ex const & rh)
115 debugmsg("operator*(numeric,ex)",LOGLEVEL_OPERATOR);
119 ex operator/(numeric const & lh, ex const & rh)
121 debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
125 ex operator%(numeric const & lh, ex const & rh)
127 debugmsg("operator%(numeric,ex)",LOGLEVEL_OPERATOR);
133 // binary arithmetic operators numeric with numeric
135 numeric operator+(numeric const & lh, numeric const & rh)
137 debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
141 numeric operator-(numeric const & lh, numeric const & rh)
143 debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
147 numeric operator*(numeric const & lh, numeric const & rh)
149 debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
153 numeric operator/(numeric const & lh, numeric const & rh)
155 debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
159 // binary arithmetic assignment operators with ex
161 ex const & operator+=(ex & lh, ex const & rh)
163 debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
167 ex const & operator-=(ex & lh, ex const & rh)
169 debugmsg("operator-=(ex,ex)",LOGLEVEL_OPERATOR);
173 ex const & operator*=(ex & lh, ex const & rh)
175 debugmsg("operator*=(ex,ex)",LOGLEVEL_OPERATOR);
179 ex const & operator/=(ex & lh, ex const & rh)
181 debugmsg("operator/=(ex,ex)",LOGLEVEL_OPERATOR);
185 ex const & operator%=(ex & lh, ex const & rh)
187 debugmsg("operator%=(ex,ex)",LOGLEVEL_OPERATOR);
193 // binary arithmetic assignment operators with numeric
195 ex const & operator+=(ex & lh, numeric const & rh)
197 debugmsg("operator+=(ex,numeric)",LOGLEVEL_OPERATOR);
198 return (lh=lh+ex(rh));
201 ex const & operator-=(ex & lh, numeric const & rh)
203 debugmsg("operator-=(ex,numeric)",LOGLEVEL_OPERATOR);
204 return (lh=lh-ex(rh));
207 ex const & operator*=(ex & lh, numeric const & rh)
209 debugmsg("operator*=(ex,numeric)",LOGLEVEL_OPERATOR);
210 return (lh=lh*ex(rh));
213 ex const & operator/=(ex & lh, numeric const & rh)
215 debugmsg("operator/=(ex,numeric)",LOGLEVEL_OPERATOR);
216 return (lh=lh/ex(rh));
219 ex const & operator%=(ex & lh, numeric const & rh)
221 debugmsg("operator%=(ex,numeric)",LOGLEVEL_OPERATOR);
222 return (lh=lh%ex(rh));
227 // binary arithmetic assignment operators with numeric
229 numeric const & operator+=(numeric & lh, numeric const & rh)
231 debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
232 return (lh=lh.add(rh));
235 numeric const & operator-=(numeric & lh, numeric const & rh)
237 debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
238 return (lh=lh.sub(rh));
241 numeric const & operator*=(numeric & lh, numeric const & rh)
243 debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
244 return (lh=lh.mul(rh));
247 numeric const & operator/=(numeric & lh, numeric const & rh)
249 debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
250 return (lh=lh.div(rh));
255 ex operator+(ex const & lh)
260 ex operator-(ex const & lh)
262 return exMINUSONE()*lh;
265 numeric operator+(numeric const & lh)
270 numeric operator-(numeric const & lh)
272 return (numeric(-1)*lh);
275 // binary relational operators ex with ex
277 relational operator==(ex const & lh, ex const & rh)
279 debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
280 return relational(lh,rh,relational::equal);
283 relational operator!=(ex const & lh, ex const & rh)
285 debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
286 return relational(lh,rh,relational::not_equal);
289 relational operator<(ex const & lh, ex const & rh)
291 debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
292 return relational(lh,rh,relational::less);
295 relational operator<=(ex const & lh, ex const & rh)
297 debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
298 return relational(lh,rh,relational::less_or_equal);
301 relational operator>(ex const & lh, ex const & rh)
303 debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
304 return relational(lh,rh,relational::greater);
307 relational operator>=(ex const & lh, ex const & rh)
309 debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
310 return relational(lh,rh,relational::greater_or_equal);
315 // binary relational operators ex with numeric
317 relational operator==(ex const & lh, numeric const & rh)
319 debugmsg("operator==(ex,numeric)",LOGLEVEL_OPERATOR);
320 return relational(lh,rh,relational::equal);
323 relational operator!=(ex const & lh, numeric const & rh)
325 debugmsg("operator!=(ex,numeric)",LOGLEVEL_OPERATOR);
326 return relational(lh,rh,relational::not_equal);
329 relational operator<(ex const & lh, numeric const & rh)
331 debugmsg("operator<(ex,numeric)",LOGLEVEL_OPERATOR);
332 return relational(lh,rh,relational::less);
335 relational operator<=(ex const & lh, numeric const & rh)
337 debugmsg("operator<=(ex,numeric)",LOGLEVEL_OPERATOR);
338 return relational(lh,rh,relational::less_or_equal);
341 relational operator>(ex const & lh, numeric const & rh)
343 debugmsg("operator>(ex,numeric)",LOGLEVEL_OPERATOR);
344 return relational(lh,rh,relational::greater);
347 relational operator>=(ex const & lh, numeric const & rh)
349 debugmsg("operator>=(ex,numeric)",LOGLEVEL_OPERATOR);
350 return relational(lh,rh,relational::greater_or_equal);
353 // binary relational operators numeric with ex
355 relational operator==(numeric const & lh, ex const & rh)
357 debugmsg("operator==(numeric,ex)",LOGLEVEL_OPERATOR);
358 return relational(lh,rh,relational::equal);
361 relational operator!=(numeric const & lh, ex const & rh)
363 debugmsg("operator!=(numeric,ex)",LOGLEVEL_OPERATOR);
364 return relational(lh,rh,relational::not_equal);
367 relational operator<(numeric const & lh, ex const & rh)
369 debugmsg("operator<(numeric,ex)",LOGLEVEL_OPERATOR);
370 return relational(lh,rh,relational::less);
373 relational operator<=(numeric const & lh, ex const & rh)
375 debugmsg("operator<=(numeric,ex)",LOGLEVEL_OPERATOR);
376 return relational(lh,rh,relational::less_or_equal);
379 relational operator>(numeric const & lh, ex const & rh)
381 debugmsg("operator>(numeric,ex)",LOGLEVEL_OPERATOR);
382 return relational(lh,rh,relational::greater);
385 relational operator>=(numeric const & lh, ex const & rh)
387 debugmsg("operator>=(numeric,ex)",LOGLEVEL_OPERATOR);
388 return relational(lh,rh,relational::greater_or_equal);
393 // input/output stream operators
395 ostream & operator<<(ostream & os, ex const & e)
401 istream & operator>>(istream & is, ex & e)
403 throw(std::logic_error("input from streams not yet implemented"));