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
27 // binary arithmetic operators ex with ex
29 ex operator+(ex const & lh, ex const & rh)
31 debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
35 ex operator-(ex const & lh, ex const & rh)
37 debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
38 return lh.exadd(rh.exmul(exMINUSONE()));
41 ex operator*(ex const & lh, ex const & rh)
43 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
47 ex operator/(ex const & lh, ex const & rh)
49 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
50 return lh.exmul(power(rh,exMINUSONE()));
53 ex operator%(ex const & lh, ex const & rh)
55 debugmsg("operator%(ex,ex)",LOGLEVEL_OPERATOR);
56 return lh.exncmul(rh);
61 // binary arithmetic operators ex with numeric
63 ex operator+(ex const & lh, numeric const & rh)
65 debugmsg("operator+(ex,numeric)",LOGLEVEL_OPERATOR);
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 // binary arithmetic operators numeric with ex
95 ex operator+(numeric const & lh, ex const & rh)
97 debugmsg("operator+(numeric,ex)",LOGLEVEL_OPERATOR);
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);
127 // binary arithmetic operators numeric with numeric
129 numeric operator+(numeric const & lh, numeric const & rh)
131 debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
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,ex)",LOGLEVEL_OPERATOR);
153 // binary arithmetic assignment operators with ex
155 ex const & operator+=(ex & lh, ex const & rh)
157 debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
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);
187 // binary arithmetic assignment operators with numeric
189 ex const & operator+=(ex & lh, numeric const & rh)
191 debugmsg("operator+=(ex,numeric)",LOGLEVEL_OPERATOR);
192 return (lh=lh+ex(rh));
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));
221 // binary arithmetic assignment operators with numeric
223 numeric const & operator+=(numeric & lh, numeric const & rh)
225 debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
226 return (lh=lh.add(rh));
229 numeric const & operator-=(numeric & lh, numeric const & rh)
231 debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
232 return (lh=lh.sub(rh));
235 numeric const & operator*=(numeric & lh, numeric const & rh)
237 debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
238 return (lh=lh.mul(rh));
241 numeric const & operator/=(numeric & lh, numeric const & rh)
243 debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
244 return (lh=lh.div(rh));
249 ex operator+(ex const & lh)
254 ex operator-(ex const & lh)
256 return exMINUSONE()*lh;
259 numeric operator+(numeric const & lh)
264 numeric operator-(numeric const & lh)
266 return (numeric(-1)*lh);
269 // binary relational operators ex with ex
271 relational operator==(ex const & lh, ex const & rh)
273 debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
274 return relational(lh,rh,relational::equal);
277 relational operator!=(ex const & lh, ex const & rh)
279 debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
280 return relational(lh,rh,relational::not_equal);
283 relational operator<(ex const & lh, ex const & rh)
285 debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
286 return relational(lh,rh,relational::less);
289 relational operator<=(ex const & lh, ex const & rh)
291 debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
292 return relational(lh,rh,relational::less_or_equal);
295 relational operator>(ex const & lh, ex const & rh)
297 debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
298 return relational(lh,rh,relational::greater);
301 relational operator>=(ex const & lh, ex const & rh)
303 debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
304 return relational(lh,rh,relational::greater_or_equal);
309 // binary relational operators ex with numeric
311 relational operator==(ex const & lh, numeric const & rh)
313 debugmsg("operator==(ex,numeric)",LOGLEVEL_OPERATOR);
314 return relational(lh,rh,relational::equal);
317 relational operator!=(ex const & lh, numeric const & rh)
319 debugmsg("operator!=(ex,numeric)",LOGLEVEL_OPERATOR);
320 return relational(lh,rh,relational::not_equal);
323 relational operator<(ex const & lh, numeric const & rh)
325 debugmsg("operator<(ex,numeric)",LOGLEVEL_OPERATOR);
326 return relational(lh,rh,relational::less);
329 relational operator<=(ex const & lh, numeric const & rh)
331 debugmsg("operator<=(ex,numeric)",LOGLEVEL_OPERATOR);
332 return relational(lh,rh,relational::less_or_equal);
335 relational operator>(ex const & lh, numeric const & rh)
337 debugmsg("operator>(ex,numeric)",LOGLEVEL_OPERATOR);
338 return relational(lh,rh,relational::greater);
341 relational operator>=(ex const & lh, numeric const & rh)
343 debugmsg("operator>=(ex,numeric)",LOGLEVEL_OPERATOR);
344 return relational(lh,rh,relational::greater_or_equal);
347 // binary relational operators numeric with ex
349 relational operator==(numeric const & lh, ex const & rh)
351 debugmsg("operator==(numeric,ex)",LOGLEVEL_OPERATOR);
352 return relational(lh,rh,relational::equal);
355 relational operator!=(numeric const & lh, ex const & rh)
357 debugmsg("operator!=(numeric,ex)",LOGLEVEL_OPERATOR);
358 return relational(lh,rh,relational::not_equal);
361 relational operator<(numeric const & lh, ex const & rh)
363 debugmsg("operator<(numeric,ex)",LOGLEVEL_OPERATOR);
364 return relational(lh,rh,relational::less);
367 relational operator<=(numeric const & lh, ex const & rh)
369 debugmsg("operator<=(numeric,ex)",LOGLEVEL_OPERATOR);
370 return relational(lh,rh,relational::less_or_equal);
373 relational operator>(numeric const & lh, ex const & rh)
375 debugmsg("operator>(numeric,ex)",LOGLEVEL_OPERATOR);
376 return relational(lh,rh,relational::greater);
379 relational operator>=(numeric const & lh, ex const & rh)
381 debugmsg("operator>=(numeric,ex)",LOGLEVEL_OPERATOR);
382 return relational(lh,rh,relational::greater_or_equal);
387 // input/output stream operators
389 ostream & operator<<(ostream & os, ex const & e)
395 istream & operator>>(istream & is, ex & e)
397 throw(std::logic_error("input from streams not yet implemented"));