1 /** @file operators.cpp
3 * Implementation of GiNaC's overloaded operators. */
6 * GiNaC Copyright (C) 1999-2002 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"
35 /** Used internally by operator+() to add two ex objects together. */
36 static inline const ex exadd(const ex & lh, const ex & rh)
38 return (new add(lh,rh))->setflag(status_flags::dynallocated);
41 /** Used internally by operator*() to multiply two ex objects together. */
42 static inline const ex exmul(const ex & lh, const ex & rh)
44 // Check if we are constructing a mul object or a ncmul object. Due to
45 // ncmul::eval()'s rule to pull out commutative elements we need to check
46 // only one of the elements.
47 if (rh.return_type()==return_types::commutative ||
48 lh.return_type()==return_types::commutative)
49 return (new mul(lh,rh))->setflag(status_flags::dynallocated);
51 return (new ncmul(lh,rh))->setflag(status_flags::dynallocated);
54 /** Used internally by operator-() and friends to change the sign of an argument. */
55 static inline const ex exminus(const ex & lh)
57 return (new mul(lh,_ex_1))->setflag(status_flags::dynallocated);
60 // binary arithmetic operators ex with ex
62 const ex operator+(const ex & lh, const ex & rh)
67 const ex operator-(const ex & lh, const ex & rh)
69 return exadd(lh, exminus(rh));
72 const ex operator*(const ex & lh, const ex & rh)
77 const ex operator/(const ex & lh, const ex & rh)
79 return exmul(lh, power(rh,_ex_1));
83 // binary arithmetic operators numeric with numeric
85 const numeric operator+(const numeric & lh, const numeric & rh)
90 const numeric operator-(const numeric & lh, const numeric & rh)
95 const numeric operator*(const numeric & lh, const numeric & rh)
100 const numeric operator/(const numeric & lh, const numeric & rh)
106 // binary arithmetic assignment operators with ex
108 ex & operator+=(ex & lh, const ex & rh)
110 return lh = exadd(lh, rh);
113 ex & operator-=(ex & lh, const ex & rh)
115 return lh = exadd(lh, exminus(rh));
118 ex & operator*=(ex & lh, const ex & rh)
120 return lh = exmul(lh, rh);
123 ex & operator/=(ex & lh, const ex & rh)
125 return lh = exmul(lh, power(rh,_ex_1));
129 // binary arithmetic assignment operators with numeric
131 numeric & operator+=(numeric & lh, const numeric & rh)
137 numeric & operator-=(numeric & lh, const numeric & rh)
143 numeric & operator*=(numeric & lh, const numeric & rh)
149 numeric & operator/=(numeric & lh, const numeric & rh)
158 const ex operator+(const ex & lh)
163 const ex operator-(const ex & lh)
168 const numeric operator+(const numeric & lh)
173 const numeric operator-(const numeric & lh)
175 return _num_1.mul(lh);
179 // increment / decrement operators
181 /** Expression prefix increment. Adds 1 and returns incremented ex. */
182 ex & operator++(ex & rh)
184 return rh = exadd(rh, _ex1);
187 /** Expression prefix decrement. Subtracts 1 and returns decremented ex. */
188 ex & operator--(ex & rh)
190 return rh = exadd(rh, _ex_1);
193 /** Expression postfix increment. Returns the ex and leaves the original
194 * incremented by 1. */
195 const ex operator++(ex & lh, int)
198 lh = exadd(lh, _ex1);
202 /** Expression postfix decrement. Returns the ex and leaves the original
203 * decremented by 1. */
204 const ex operator--(ex & lh, int)
207 lh = exadd(lh, _ex_1);
211 /** Numeric prefix increment. Adds 1 and returns incremented number. */
212 numeric& operator++(numeric & rh)
218 /** Numeric prefix decrement. Subtracts 1 and returns decremented number. */
219 numeric& operator--(numeric & rh)
225 /** Numeric postfix increment. Returns the number and leaves the original
226 * incremented by 1. */
227 const numeric operator++(numeric & lh, int)
234 /** Numeric postfix decrement. Returns the number and leaves the original
235 * decremented by 1. */
236 const numeric operator--(numeric & lh, int)
243 // binary relational operators ex with ex
245 const relational operator==(const ex & lh, const ex & rh)
247 return relational(lh,rh,relational::equal);
250 const relational operator!=(const ex & lh, const ex & rh)
252 return relational(lh,rh,relational::not_equal);
255 const relational operator<(const ex & lh, const ex & rh)
257 return relational(lh,rh,relational::less);
260 const relational operator<=(const ex & lh, const ex & rh)
262 return relational(lh,rh,relational::less_or_equal);
265 const relational operator>(const ex & lh, const ex & rh)
267 return relational(lh,rh,relational::greater);
270 const relational operator>=(const ex & lh, const ex & rh)
272 return relational(lh,rh,relational::greater_or_equal);
275 // input/output stream operators
277 std::ostream & operator<<(std::ostream & os, const ex & e)
279 e.print(print_context(os));
283 std::istream & operator>>(std::istream & is, ex & e)
285 throw (std::logic_error("expression input from streams not implemented"));