]> www.ginac.de Git - ginac.git/blob - ginac/operators.cpp
* Eliminated overloaded operator% for noncommunistic objects for good.
[ginac.git] / ginac / operators.cpp
1 /** @file operators.cpp
2  *
3  *  Implementation of GiNaC's overloaded operators. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "operators.h"
24 #include "numeric.h"
25 #include "power.h"
26 #include "relational.h"
27 #include "debugmsg.h"
28 #include "utils.h"
29
30 namespace GiNaC {
31
32 // binary arithmetic operators ex with ex
33
34 ex operator+(const ex & lh, const ex & rh)
35 {
36         debugmsg("operator+(ex,ex)",LOGLEVEL_OPERATOR);
37         return lh.exadd(rh);
38 }
39
40 ex operator-(const ex & lh, const ex & rh)
41 {
42         debugmsg("operator-(ex,ex)",LOGLEVEL_OPERATOR);
43         return lh.exadd(rh.exmul(_ex_1()));
44 }
45
46 ex operator*(const ex & lh, const ex & rh)
47 {
48         debugmsg("operator*(ex,ex)",LOGLEVEL_OPERATOR);
49         return lh.exmul(rh);
50 }
51
52 ex operator/(const ex & lh, const ex & rh)
53 {
54         debugmsg("operator/(ex,ex)",LOGLEVEL_OPERATOR);
55         return lh.exmul(power(rh,_ex_1()));
56 }
57
58
59 // binary arithmetic operators numeric with numeric
60
61 numeric operator+(const numeric & lh, const numeric & rh)
62 {
63         debugmsg("operator+(numeric,numeric)",LOGLEVEL_OPERATOR);
64         return lh.add(rh);
65 }
66
67 numeric operator-(const numeric & lh, const numeric & rh)
68 {
69         debugmsg("operator-(numeric,numeric)",LOGLEVEL_OPERATOR);
70         return lh.sub(rh);
71 }
72
73 numeric operator*(const numeric & lh, const numeric & rh)
74 {
75         debugmsg("operator*(numeric,numeric)",LOGLEVEL_OPERATOR);
76         return lh.mul(rh);
77 }
78
79 numeric operator/(const numeric & lh, const numeric & rh)
80 {
81         debugmsg("operator/(numeric,ex)",LOGLEVEL_OPERATOR);
82         return lh.div(rh);
83 }
84
85
86 // binary arithmetic assignment operators with ex
87
88 const ex & operator+=(ex & lh, const ex & rh)
89 {
90         debugmsg("operator+=(ex,ex)",LOGLEVEL_OPERATOR);
91         return (lh=lh.exadd(rh));
92 }
93
94 const ex & operator-=(ex & lh, const ex & rh)
95 {
96         debugmsg("operator-=(ex,ex)",LOGLEVEL_OPERATOR);
97         return (lh=lh.exadd(rh.exmul(_ex_1())));
98 }
99
100 const ex & operator*=(ex & lh, const ex & rh)
101 {
102         debugmsg("operator*=(ex,ex)",LOGLEVEL_OPERATOR);
103         return (lh=lh.exmul(rh));
104 }
105
106 const ex & operator/=(ex & lh, const ex & rh)
107 {
108         debugmsg("operator/=(ex,ex)",LOGLEVEL_OPERATOR);
109         return (lh=lh.exmul(power(rh,_ex_1())));
110 }
111
112
113 // binary arithmetic assignment operators with numeric
114
115 const numeric & operator+=(numeric & lh, const numeric & rh)
116 {
117         debugmsg("operator+=(numeric,numeric)",LOGLEVEL_OPERATOR);
118         return (lh=lh.add(rh));
119 }
120
121 const numeric & operator-=(numeric & lh, const numeric & rh)
122 {
123         debugmsg("operator-=(numeric,numeric)",LOGLEVEL_OPERATOR);
124         return (lh=lh.sub(rh));
125 }
126
127 const numeric & operator*=(numeric & lh, const numeric & rh)
128 {
129         debugmsg("operator*=(numeric,numeric)",LOGLEVEL_OPERATOR);
130         return (lh=lh.mul(rh));
131 }
132
133 const numeric & operator/=(numeric & lh, const numeric & rh)
134 {
135         debugmsg("operator/=(numeric,numeric)",LOGLEVEL_OPERATOR);
136         return (lh=lh.div(rh));
137 }
138
139 // unary operators
140
141 ex operator+(const ex & lh)
142 {
143         debugmsg("operator+(ex)",LOGLEVEL_OPERATOR);
144         return lh;
145 }
146
147 ex operator-(const ex & lh)
148 {
149         debugmsg("operator-(ex)",LOGLEVEL_OPERATOR);
150         return lh.exmul(_ex_1());
151 }
152
153 numeric operator+(const numeric & lh)
154 {
155         debugmsg("operator+(numeric)",LOGLEVEL_OPERATOR);
156         return lh;
157 }
158
159 numeric operator-(const numeric & lh)
160 {
161         debugmsg("operator-(numeric)",LOGLEVEL_OPERATOR);
162         return _num_1().mul(lh);
163 }
164
165 /** Numeric prefix increment.  Adds 1 and returns incremented number. */
166 numeric& operator++(numeric & rh)
167 {
168         debugmsg("operator++(numeric)",LOGLEVEL_OPERATOR);
169         rh = rh.add(_num1());
170         return rh;
171 }
172
173 /** Numeric prefix decrement.  Subtracts 1 and returns decremented number. */
174 numeric& operator--(numeric & rh)
175 {
176         debugmsg("operator--(numeric)",LOGLEVEL_OPERATOR);
177         rh = rh.add(_num_1());
178         return rh;
179 }
180
181 /** Numeric postfix increment.  Returns the number and leaves the original
182  *  incremented by 1. */
183 numeric operator++(numeric & lh, int)
184 {
185         debugmsg("operator++(numeric,int)",LOGLEVEL_OPERATOR);
186         numeric tmp(lh);
187         lh = lh.add(_num1());
188         return tmp;
189 }
190
191 /** Numeric Postfix decrement.  Returns the number and leaves the original
192  *  decremented by 1. */
193 numeric operator--(numeric & lh, int)
194 {
195         debugmsg("operator--(numeric,int)",LOGLEVEL_OPERATOR);
196         numeric tmp(lh);
197         lh = lh.add(_num_1());
198         return tmp;
199 }
200
201 // binary relational operators ex with ex
202
203 relational operator==(const ex & lh, const ex & rh)
204 {
205         debugmsg("operator==(ex,ex)",LOGLEVEL_OPERATOR);
206         return relational(lh,rh,relational::equal);
207 }
208
209 relational operator!=(const ex & lh, const ex & rh)
210 {
211         debugmsg("operator!=(ex,ex)",LOGLEVEL_OPERATOR);
212         return relational(lh,rh,relational::not_equal);
213 }
214
215 relational operator<(const ex & lh, const ex & rh)
216 {
217         debugmsg("operator<(ex,ex)",LOGLEVEL_OPERATOR);
218         return relational(lh,rh,relational::less);
219 }
220
221 relational operator<=(const ex & lh, const ex & rh)
222 {
223         debugmsg("operator<=(ex,ex)",LOGLEVEL_OPERATOR);
224         return relational(lh,rh,relational::less_or_equal);
225 }
226
227 relational operator>(const ex & lh, const ex & rh)
228 {
229         debugmsg("operator>(ex,ex)",LOGLEVEL_OPERATOR);
230         return relational(lh,rh,relational::greater);
231 }
232
233 relational operator>=(const ex & lh, const ex & rh)
234 {
235         debugmsg("operator>=(ex,ex)",LOGLEVEL_OPERATOR);
236         return relational(lh,rh,relational::greater_or_equal);
237 }
238
239 // input/output stream operators
240
241 std::ostream & operator<<(std::ostream & os, const ex & e)
242 {
243         e.print(os);
244         return os;
245 }
246
247 std::istream & operator>>(std::istream & is, ex & e)
248 {
249         throw (std::logic_error("expression input from streams not implemented"));
250 }
251
252 } // namespace GiNaC