1 /** @file operators.cpp
3 * Implementation of GiNaC's overloaded operators. */
6 * GiNaC Copyright (C) 1999-2000 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"
32 #include "input_lexer.h"
36 #ifndef NO_NAMESPACE_GINAC
38 #endif // ndef NO_NAMESPACE_GINAC
40 // binary arithmetic operators ex with ex
42 ex operator+(const ex & lh, const ex & rh)
44 debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
48 ex operator-(const ex & lh, const ex & rh)
50 debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
51 return lh.exadd(rh.exmul(_ex_1()));
54 ex operator*(const ex & lh, const ex & rh)
56 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
60 ex operator/(const ex & lh, const ex & rh)
62 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
63 return lh.exmul(power(rh,_ex_1()));
66 ex operator%(const ex & lh, const ex & rh)
68 debugmsg("operator%(ex,ex)",LOGLEVEL_OPERATOR);
69 return lh.exncmul(rh);
73 // binary arithmetic operators numeric with numeric
75 numeric operator+(const numeric & lh, const numeric & rh)
77 debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
81 numeric operator-(const numeric & lh, const numeric & rh)
83 debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
87 numeric operator*(const numeric & lh, const numeric & rh)
89 debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
93 numeric operator/(const numeric & lh, const numeric & rh)
95 debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
100 // binary arithmetic assignment operators with ex
102 const ex & operator+=(ex & lh, const ex & rh)
104 debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
108 const ex & operator-=(ex & lh, const ex & rh)
110 debugmsg("operator-=(ex,ex)",LOGLEVEL_OPERATOR);
114 const ex & operator*=(ex & lh, const ex & rh)
116 debugmsg("operator*=(ex,ex)",LOGLEVEL_OPERATOR);
120 const ex & operator/=(ex & lh, const ex & rh)
122 debugmsg("operator/=(ex,ex)",LOGLEVEL_OPERATOR);
126 const ex & operator%=(ex & lh, const ex & rh)
128 debugmsg("operator%=(ex,ex)",LOGLEVEL_OPERATOR);
133 // binary arithmetic assignment operators with numeric
135 const numeric & operator+=(numeric & lh, const numeric & rh)
137 debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
138 return (lh=lh.add(rh));
141 const numeric & operator-=(numeric & lh, const numeric & rh)
143 debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
144 return (lh=lh.sub(rh));
147 const numeric & operator*=(numeric & lh, const numeric & rh)
149 debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
150 return (lh=lh.mul(rh));
153 const numeric & operator/=(numeric & lh, const numeric & rh)
155 debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
156 return (lh=lh.div(rh));
161 ex operator+(const ex & lh)
166 ex operator-(const ex & lh)
168 return lh.exmul(_ex_1());
171 numeric operator+(const numeric & lh)
176 numeric operator-(const numeric & lh)
181 /** Numeric prefix increment. Adds 1 and returns incremented number. */
182 numeric& operator++(numeric & rh)
188 /** Numeric prefix decrement. Subtracts 1 and returns decremented number. */
189 numeric& operator--(numeric & rh)
195 /** Numeric postfix increment. Returns the number and leaves the original
196 * incremented by 1. */
197 numeric operator++(numeric & lh, int)
204 /** Numeric Postfix decrement. Returns the number and leaves the original
205 * decremented by 1. */
206 numeric operator--(numeric & lh, int)
213 // binary relational operators ex with ex
215 relational operator==(const ex & lh, const ex & rh)
217 debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
218 return relational(lh,rh,relational::equal);
221 relational operator!=(const ex & lh, const ex & rh)
223 debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
224 return relational(lh,rh,relational::not_equal);
227 relational operator<(const ex & lh, const ex & rh)
229 debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
230 return relational(lh,rh,relational::less);
233 relational operator<=(const ex & lh, const ex & rh)
235 debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
236 return relational(lh,rh,relational::less_or_equal);
239 relational operator>(const ex & lh, const ex & rh)
241 debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
242 return relational(lh,rh,relational::greater);
245 relational operator>=(const ex & lh, const ex & rh)
247 debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
248 return relational(lh,rh,relational::greater_or_equal);
253 // binary relational operators ex with numeric
255 relational operator==(const ex & lh, const numeric & rh)
257 debugmsg("operator==(ex,numeric)",LOGLEVEL_OPERATOR);
258 return relational(lh,rh,relational::equal);
261 relational operator!=(const ex & lh, const numeric & rh)
263 debugmsg("operator!=(ex,numeric)",LOGLEVEL_OPERATOR);
264 return relational(lh,rh,relational::not_equal);
267 relational operator<(const ex & lh, const numeric & rh)
269 debugmsg("operator<(ex,numeric)",LOGLEVEL_OPERATOR);
270 return relational(lh,rh,relational::less);
273 relational operator<=(const ex & lh, const numeric & rh)
275 debugmsg("operator<=(ex,numeric)",LOGLEVEL_OPERATOR);
276 return relational(lh,rh,relational::less_or_equal);
279 relational operator>(const ex & lh, const numeric & rh)
281 debugmsg("operator>(ex,numeric)",LOGLEVEL_OPERATOR);
282 return relational(lh,rh,relational::greater);
285 relational operator>=(const ex & lh, const numeric & rh)
287 debugmsg("operator>=(ex,numeric)",LOGLEVEL_OPERATOR);
288 return relational(lh,rh,relational::greater_or_equal);
291 // binary relational operators numeric with ex
293 relational operator==(const numeric & lh, const ex & rh)
295 debugmsg("operator==(numeric,ex)",LOGLEVEL_OPERATOR);
296 return relational(lh,rh,relational::equal);
299 relational operator!=(const numeric & lh, const ex & rh)
301 debugmsg("operator!=(numeric,ex)",LOGLEVEL_OPERATOR);
302 return relational(lh,rh,relational::not_equal);
305 relational operator<(const numeric & lh, const ex & rh)
307 debugmsg("operator<(numeric,ex)",LOGLEVEL_OPERATOR);
308 return relational(lh,rh,relational::less);
311 relational operator<=(const numeric & lh, const ex & rh)
313 debugmsg("operator<=(numeric,ex)",LOGLEVEL_OPERATOR);
314 return relational(lh,rh,relational::less_or_equal);
317 relational operator>(const numeric & lh, const ex & rh)
319 debugmsg("operator>(numeric,ex)",LOGLEVEL_OPERATOR);
320 return relational(lh,rh,relational::greater);
323 relational operator>=(const numeric & lh, const ex & rh)
325 debugmsg("operator>=(numeric,ex)",LOGLEVEL_OPERATOR);
326 return relational(lh,rh,relational::greater_or_equal);
331 // input/output stream operators
333 ostream & operator<<(ostream & os, const ex & e)
339 /** Input (parse) expression from stream. The input grammar is similar to the
340 * GiNaC output format. If 'e' contains a list of symbols upon entry, these
341 * symbols are used in the parsed expression in all places where symbols with
342 * the same names appear. All other encountered symbols will be newly created. */
343 istream & operator>>(istream & is, ex & e)
348 set_lexer_symbols(e);
349 ginac_yyrestart(NULL);
351 throw (std::runtime_error(get_parser_error()));
356 #ifndef NO_NAMESPACE_GINAC
358 #endif // ndef NO_NAMESPACE_GINAC