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"
33 // binary arithmetic operators ex with ex
35 ex operator+(const ex & lh, const ex & rh)
37 debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
41 ex operator-(const ex & lh, const ex & rh)
43 debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
44 return lh.exadd(rh.exmul(_ex_1()));
47 ex operator*(const ex & lh, const ex & rh)
49 debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
53 ex operator/(const ex & lh, const ex & rh)
55 debugmsg("operator/(ex,ex)",LOGLEVEL_OPERATOR);
56 return lh.exmul(power(rh,_ex_1()));
60 // binary arithmetic operators numeric with numeric
62 numeric operator+(const numeric & lh, const numeric & rh)
64 debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
68 numeric operator-(const numeric & lh, const numeric & rh)
70 debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
74 numeric operator*(const numeric & lh, const numeric & rh)
76 debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
80 numeric operator/(const numeric & lh, const numeric & rh)
82 debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
87 // binary arithmetic assignment operators with ex
89 const ex & operator+=(ex & lh, const ex & rh)
91 debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
92 return (lh=lh.exadd(rh));
95 const ex & operator-=(ex & lh, const ex & rh)
97 debugmsg("operator-=(ex,ex)",LOGLEVEL_OPERATOR);
98 return (lh=lh.exadd(rh.exmul(_ex_1())));
101 const ex & operator*=(ex & lh, const ex & rh)
103 debugmsg("operator*=(ex,ex)",LOGLEVEL_OPERATOR);
104 return (lh=lh.exmul(rh));
107 const ex & operator/=(ex & lh, const ex & rh)
109 debugmsg("operator/=(ex,ex)",LOGLEVEL_OPERATOR);
110 return (lh=lh.exmul(power(rh,_ex_1())));
114 // binary arithmetic assignment operators with numeric
116 const numeric & operator+=(numeric & lh, const numeric & rh)
118 debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
119 return (lh=lh.add(rh));
122 const numeric & operator-=(numeric & lh, const numeric & rh)
124 debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
125 return (lh=lh.sub(rh));
128 const numeric & operator*=(numeric & lh, const numeric & rh)
130 debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
131 return (lh=lh.mul(rh));
134 const numeric & operator/=(numeric & lh, const numeric & rh)
136 debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
137 return (lh=lh.div(rh));
142 ex operator+(const ex & lh)
144 debugmsg("operator+(ex)",LOGLEVEL_OPERATOR);
148 ex operator-(const ex & lh)
150 debugmsg("operator-(ex)",LOGLEVEL_OPERATOR);
151 return lh.exmul(_ex_1());
154 numeric operator+(const numeric & lh)
156 debugmsg("operator+(numeric)",LOGLEVEL_OPERATOR);
160 numeric operator-(const numeric & lh)
162 debugmsg("operator-(numeric)",LOGLEVEL_OPERATOR);
163 return _num_1().mul(lh);
166 /** Numeric prefix increment. Adds 1 and returns incremented number. */
167 numeric& operator++(numeric & rh)
169 debugmsg("operator++(numeric)",LOGLEVEL_OPERATOR);
170 rh = rh.add(_num1());
174 /** Numeric prefix decrement. Subtracts 1 and returns decremented number. */
175 numeric& operator--(numeric & rh)
177 debugmsg("operator--(numeric)",LOGLEVEL_OPERATOR);
178 rh = rh.add(_num_1());
182 /** Numeric postfix increment. Returns the number and leaves the original
183 * incremented by 1. */
184 numeric operator++(numeric & lh, int)
186 debugmsg("operator++(numeric,int)",LOGLEVEL_OPERATOR);
188 lh = lh.add(_num1());
192 /** Numeric Postfix decrement. Returns the number and leaves the original
193 * decremented by 1. */
194 numeric operator--(numeric & lh, int)
196 debugmsg("operator--(numeric,int)",LOGLEVEL_OPERATOR);
198 lh = lh.add(_num_1());
202 // binary relational operators ex with ex
204 relational operator==(const ex & lh, const ex & rh)
206 debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
207 return relational(lh,rh,relational::equal);
210 relational operator!=(const ex & lh, const ex & rh)
212 debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
213 return relational(lh,rh,relational::not_equal);
216 relational operator<(const ex & lh, const ex & rh)
218 debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
219 return relational(lh,rh,relational::less);
222 relational operator<=(const ex & lh, const ex & rh)
224 debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
225 return relational(lh,rh,relational::less_or_equal);
228 relational operator>(const ex & lh, const ex & rh)
230 debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
231 return relational(lh,rh,relational::greater);
234 relational operator>=(const ex & lh, const ex & rh)
236 debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
237 return relational(lh,rh,relational::greater_or_equal);
240 // input/output stream operators
242 std::ostream & operator<<(std::ostream & os, const ex & e)
244 e.print(print_context(os));
248 std::istream & operator>>(std::istream & is, ex & e)
250 throw (std::logic_error("expression input from streams not implemented"));