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"
26 #include "relational.h"
32 // binary arithmetic operators ex with ex
34 ex operator+(const ex & lh, const ex & rh)
36 debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
40 ex operator-(const ex & lh, const ex & rh)
42 debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
43 return lh.exadd(rh.exmul(_ex_1()));
46 ex operator*(const ex & lh, const ex & rh)
48 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
52 ex operator/(const ex & lh, const ex & rh)
54 debugmsg("operator/(ex,ex)",LOGLEVEL_OPERATOR);
55 return lh.exmul(power(rh,_ex_1()));
58 ex operator%(const ex & lh, const ex & rh)
60 debugmsg("operator%(ex,ex)",LOGLEVEL_OPERATOR);
61 return lh.exncmul(rh);
65 // binary arithmetic operators numeric with numeric
67 numeric operator+(const numeric & lh, const numeric & rh)
69 debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
73 numeric operator-(const numeric & lh, const numeric & rh)
75 debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
79 numeric operator*(const numeric & lh, const numeric & rh)
81 debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
85 numeric operator/(const numeric & lh, const numeric & rh)
87 debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
92 // binary arithmetic assignment operators with ex
94 const ex & operator+=(ex & lh, const ex & rh)
96 debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
97 return (lh=lh.exadd(rh));
100 const ex & operator-=(ex & lh, const ex & rh)
102 debugmsg("operator-=(ex,ex)",LOGLEVEL_OPERATOR);
103 return (lh=lh.exadd(rh.exmul(_ex_1())));
106 const ex & operator*=(ex & lh, const ex & rh)
108 debugmsg("operator*=(ex,ex)",LOGLEVEL_OPERATOR);
109 return (lh=lh.exmul(rh));
112 const ex & operator/=(ex & lh, const ex & rh)
114 debugmsg("operator/=(ex,ex)",LOGLEVEL_OPERATOR);
115 return (lh=lh.exmul(power(rh,_ex_1())));
118 const ex & operator%=(ex & lh, const ex & rh)
120 debugmsg("operator%=(ex,ex)",LOGLEVEL_OPERATOR);
125 // binary arithmetic assignment operators with numeric
127 const numeric & operator+=(numeric & lh, const numeric & rh)
129 debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
130 return (lh=lh.add(rh));
133 const numeric & operator-=(numeric & lh, const numeric & rh)
135 debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
136 return (lh=lh.sub(rh));
139 const numeric & operator*=(numeric & lh, const numeric & rh)
141 debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
142 return (lh=lh.mul(rh));
145 const numeric & operator/=(numeric & lh, const numeric & rh)
147 debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
148 return (lh=lh.div(rh));
153 ex operator+(const ex & lh)
155 debugmsg("operator+(ex)",LOGLEVEL_OPERATOR);
159 ex operator-(const ex & lh)
161 debugmsg("operator-(ex)",LOGLEVEL_OPERATOR);
162 return lh.exmul(_ex_1());
165 numeric operator+(const numeric & lh)
167 debugmsg("operator+(numeric)",LOGLEVEL_OPERATOR);
171 numeric operator-(const numeric & lh)
173 debugmsg("operator-(numeric)",LOGLEVEL_OPERATOR);
174 return _num_1().mul(lh);
177 /** Numeric prefix increment. Adds 1 and returns incremented number. */
178 numeric& operator++(numeric & rh)
180 debugmsg("operator++(numeric)",LOGLEVEL_OPERATOR);
181 rh = rh.add(_num1());
185 /** Numeric prefix decrement. Subtracts 1 and returns decremented number. */
186 numeric& operator--(numeric & rh)
188 debugmsg("operator--(numeric)",LOGLEVEL_OPERATOR);
189 rh = rh.add(_num_1());
193 /** Numeric postfix increment. Returns the number and leaves the original
194 * incremented by 1. */
195 numeric operator++(numeric & lh, int)
197 debugmsg("operator++(numeric,int)",LOGLEVEL_OPERATOR);
199 lh = lh.add(_num1());
203 /** Numeric Postfix decrement. Returns the number and leaves the original
204 * decremented by 1. */
205 numeric operator--(numeric & lh, int)
207 debugmsg("operator--(numeric,int)",LOGLEVEL_OPERATOR);
209 lh = lh.add(_num_1());
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);
251 // input/output stream operators
253 std::ostream & operator<<(std::ostream & os, const ex & e)
259 std::istream & operator>>(std::istream & is, ex & e)
261 throw (std::logic_error("expression input from streams not implemented"));