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()));
59 // binary arithmetic operators numeric with numeric
61 numeric operator+(const numeric & lh, const numeric & rh)
63 debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
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,ex)",LOGLEVEL_OPERATOR);
86 // binary arithmetic assignment operators with ex
88 const ex & operator+=(ex & lh, const ex & rh)
90 debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
91 return (lh=lh.exadd(rh));
94 const ex & operator-=(ex & lh, const ex & rh)
96 debugmsg("operator-=(ex,ex)",LOGLEVEL_OPERATOR);
97 return (lh=lh.exadd(rh.exmul(_ex_1())));
100 const ex & operator*=(ex & lh, const ex & rh)
102 debugmsg("operator*=(ex,ex)",LOGLEVEL_OPERATOR);
103 return (lh=lh.exmul(rh));
106 const ex & operator/=(ex & lh, const ex & rh)
108 debugmsg("operator/=(ex,ex)",LOGLEVEL_OPERATOR);
109 return (lh=lh.exmul(power(rh,_ex_1())));
113 // binary arithmetic assignment operators with numeric
115 const numeric & operator+=(numeric & lh, const numeric & rh)
117 debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
118 return (lh=lh.add(rh));
121 const numeric & operator-=(numeric & lh, const numeric & rh)
123 debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
124 return (lh=lh.sub(rh));
127 const numeric & operator*=(numeric & lh, const numeric & rh)
129 debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
130 return (lh=lh.mul(rh));
133 const numeric & operator/=(numeric & lh, const numeric & rh)
135 debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
136 return (lh=lh.div(rh));
141 ex operator+(const ex & lh)
143 debugmsg("operator+(ex)",LOGLEVEL_OPERATOR);
147 ex operator-(const ex & lh)
149 debugmsg("operator-(ex)",LOGLEVEL_OPERATOR);
150 return lh.exmul(_ex_1());
153 numeric operator+(const numeric & lh)
155 debugmsg("operator+(numeric)",LOGLEVEL_OPERATOR);
159 numeric operator-(const numeric & lh)
161 debugmsg("operator-(numeric)",LOGLEVEL_OPERATOR);
162 return _num_1().mul(lh);
165 /** Numeric prefix increment. Adds 1 and returns incremented number. */
166 numeric& operator++(numeric & rh)
168 debugmsg("operator++(numeric)",LOGLEVEL_OPERATOR);
169 rh = rh.add(_num1());
173 /** Numeric prefix decrement. Subtracts 1 and returns decremented number. */
174 numeric& operator--(numeric & rh)
176 debugmsg("operator--(numeric)",LOGLEVEL_OPERATOR);
177 rh = rh.add(_num_1());
181 /** Numeric postfix increment. Returns the number and leaves the original
182 * incremented by 1. */
183 numeric operator++(numeric & lh, int)
185 debugmsg("operator++(numeric,int)",LOGLEVEL_OPERATOR);
187 lh = lh.add(_num1());
191 /** Numeric Postfix decrement. Returns the number and leaves the original
192 * decremented by 1. */
193 numeric operator--(numeric & lh, int)
195 debugmsg("operator--(numeric,int)",LOGLEVEL_OPERATOR);
197 lh = lh.add(_num_1());
201 // binary relational operators ex with ex
203 relational operator==(const ex & lh, const ex & rh)
205 debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
206 return relational(lh,rh,relational::equal);
209 relational operator!=(const ex & lh, const ex & rh)
211 debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
212 return relational(lh,rh,relational::not_equal);
215 relational operator<(const ex & lh, const ex & rh)
217 debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
218 return relational(lh,rh,relational::less);
221 relational operator<=(const ex & lh, const ex & rh)
223 debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
224 return relational(lh,rh,relational::less_or_equal);
227 relational operator>(const ex & lh, const ex & rh)
229 debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
230 return relational(lh,rh,relational::greater);
233 relational operator>=(const ex & lh, const ex & rh)
235 debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
236 return relational(lh,rh,relational::greater_or_equal);
239 // input/output stream operators
241 std::ostream & operator<<(std::ostream & os, const ex & e)
247 std::istream & operator>>(std::istream & is, ex & e)
249 throw (std::logic_error("expression input from streams not implemented"));