1 /** @file operators.cpp
3 * Implementation of GiNaC's overloaded operators. */
6 * GiNaC Copyright (C) 1999-2001 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
23 #include "operators.h"
29 #include "relational.h"
36 /** Used internally by operator+() to add two ex objects together. */
37 static inline const ex exadd(const ex & lh, const ex & rh)
39 return (new add(lh,rh))->setflag(status_flags::dynallocated);
42 /** Used internally by operator*() to multiply two ex objects together. */
43 static inline const ex exmul(const ex & lh, const ex & rh)
45 // Check if we are constructing a mul object or a ncmul object. Due to
46 // ncmul::eval()'s rule to pull out commutative elements we need to check
47 // only one of the elements.
48 if (rh.return_type()==return_types::commutative ||
49 lh.return_type()==return_types::commutative)
50 return (new mul(lh,rh))->setflag(status_flags::dynallocated);
52 return (new ncmul(lh,rh))->setflag(status_flags::dynallocated);
55 /** Used internally by operator-() and friends to change the sign of an argument. */
56 static inline const ex exminus(const ex & lh)
58 return (new mul(lh,_ex_1()))->setflag(status_flags::dynallocated);
61 // binary arithmetic operators ex with ex
63 const ex operator+(const ex & lh, const ex & rh)
65 debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
69 const ex operator-(const ex & lh, const ex & rh)
71 debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
72 return exadd(lh, exminus(rh));
75 const ex operator*(const ex & lh, const ex & rh)
77 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
81 const ex operator/(const ex & lh, const ex & rh)
83 debugmsg("operator/(ex,ex)",LOGLEVEL_OPERATOR);
84 return exmul(lh, power(rh,_ex_1()));
88 // binary arithmetic operators numeric with numeric
90 const numeric operator+(const numeric & lh, const numeric & rh)
92 debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
96 const numeric operator-(const numeric & lh, const numeric & rh)
98 debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
102 const numeric operator*(const numeric & lh, const numeric & rh)
104 debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
108 const numeric operator/(const numeric & lh, const numeric & rh)
110 debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
115 // binary arithmetic assignment operators with ex
117 ex & operator+=(ex & lh, const ex & rh)
119 debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
120 return lh = exadd(lh, rh);
123 ex & operator-=(ex & lh, const ex & rh)
125 debugmsg("operator-=(ex,ex)",LOGLEVEL_OPERATOR);
126 return lh = exadd(lh, exminus(rh));
129 ex & operator*=(ex & lh, const ex & rh)
131 debugmsg("operator*=(ex,ex)",LOGLEVEL_OPERATOR);
132 return lh = exmul(lh, rh);
135 ex & operator/=(ex & lh, const ex & rh)
137 debugmsg("operator/=(ex,ex)",LOGLEVEL_OPERATOR);
138 return lh = exmul(lh, power(rh,_ex_1()));
142 // binary arithmetic assignment operators with numeric
144 numeric & operator+=(numeric & lh, const numeric & rh)
146 debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
151 numeric & operator-=(numeric & lh, const numeric & rh)
153 debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
158 numeric & operator*=(numeric & lh, const numeric & rh)
160 debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
165 numeric & operator/=(numeric & lh, const numeric & rh)
167 debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
175 const ex operator+(const ex & lh)
177 debugmsg("operator+(ex)",LOGLEVEL_OPERATOR);
181 const ex operator-(const ex & lh)
183 debugmsg("operator-(ex)",LOGLEVEL_OPERATOR);
187 const numeric operator+(const numeric & lh)
189 debugmsg("operator+(numeric)",LOGLEVEL_OPERATOR);
193 const numeric operator-(const numeric & lh)
195 debugmsg("operator-(numeric)",LOGLEVEL_OPERATOR);
196 return _num_1().mul(lh);
200 // increment / decrement operators
202 /** Expression prefix increment. Adds 1 and returns incremented ex. */
203 ex & operator++(ex & rh)
205 debugmsg("operator++(ex)",LOGLEVEL_OPERATOR);
206 return rh = exadd(rh, _ex1());
209 /** Expression prefix decrement. Subtracts 1 and returns decremented ex. */
210 ex & operator--(ex & rh)
212 debugmsg("operator--(ex)",LOGLEVEL_OPERATOR);
213 return rh = exadd(rh, _ex_1());
216 /** Expression postfix increment. Returns the ex and leaves the original
217 * incremented by 1. */
218 const ex operator++(ex & lh, int)
220 debugmsg("operator++(ex,int)",LOGLEVEL_OPERATOR);
222 lh = exadd(lh, _ex1());
226 /** Expression Postfix decrement. Returns the ex and leaves the original
227 * decremented by 1. */
228 const ex operator--(ex & lh, int)
230 debugmsg("operator--(ex,int)",LOGLEVEL_OPERATOR);
232 lh = exadd(lh, _ex_1());
236 /** Numeric prefix increment. Adds 1 and returns incremented number. */
237 numeric& operator++(numeric & rh)
239 debugmsg("operator++(numeric)",LOGLEVEL_OPERATOR);
240 rh = rh.add(_num1());
244 /** Numeric prefix decrement. Subtracts 1 and returns decremented number. */
245 numeric& operator--(numeric & rh)
247 debugmsg("operator--(numeric)",LOGLEVEL_OPERATOR);
248 rh = rh.add(_num_1());
252 /** Numeric postfix increment. Returns the number and leaves the original
253 * incremented by 1. */
254 const numeric operator++(numeric & lh, int)
256 debugmsg("operator++(numeric,int)",LOGLEVEL_OPERATOR);
258 lh = lh.add(_num1());
262 /** Numeric Postfix decrement. Returns the number and leaves the original
263 * decremented by 1. */
264 const numeric operator--(numeric & lh, int)
266 debugmsg("operator--(numeric,int)",LOGLEVEL_OPERATOR);
268 lh = lh.add(_num_1());
272 // binary relational operators ex with ex
274 const relational operator==(const ex & lh, const ex & rh)
276 debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
277 return relational(lh,rh,relational::equal);
280 const relational operator!=(const ex & lh, const ex & rh)
282 debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
283 return relational(lh,rh,relational::not_equal);
286 const relational operator<(const ex & lh, const ex & rh)
288 debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
289 return relational(lh,rh,relational::less);
292 const relational operator<=(const ex & lh, const ex & rh)
294 debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
295 return relational(lh,rh,relational::less_or_equal);
298 const relational operator>(const ex & lh, const ex & rh)
300 debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
301 return relational(lh,rh,relational::greater);
304 const relational operator>=(const ex & lh, const ex & rh)
306 debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
307 return relational(lh,rh,relational::greater_or_equal);
310 // input/output stream operators
312 std::ostream & operator<<(std::ostream & os, const ex & e)
314 e.print(print_context(os));
318 std::istream & operator>>(std::istream & is, ex & e)
320 throw (std::logic_error("expression input from streams not implemented"));