1 /** @file operators.cpp
3 * Implementation of GiNaC's overloaded operators. */
6 * GiNaC Copyright (C) 1999-2016 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "operators.h"
29 #include "relational.h"
37 /** Used internally by operator+() to add two ex objects. */
38 static inline const ex exadd(const ex & lh, const ex & rh)
40 return dynallocate<add>(lh, rh);
43 /** Used internally by operator*() to multiply two ex objects. */
44 static inline const ex exmul(const ex & lh, const ex & rh)
46 // Check if we are constructing a mul object or a ncmul object. Due to
47 // ncmul::eval()'s rule to pull out commutative elements we need to check
48 // only one of the elements.
49 if (rh.return_type()==return_types::commutative ||
50 lh.return_type()==return_types::commutative) {
51 return dynallocate<mul>(lh, rh);
53 return dynallocate<ncmul>(lh, rh);
57 /** Used internally by operator-() and friends to change the sign of an argument. */
58 static inline const ex exminus(const ex & lh)
60 return dynallocate<mul>(lh, _ex_1);
63 // binary arithmetic operators ex with ex
65 const ex operator+(const ex & lh, const ex & rh)
70 const ex operator-(const ex & lh, const ex & rh)
72 return exadd(lh, exminus(rh));
75 const ex operator*(const ex & lh, const ex & rh)
80 const ex operator/(const ex & lh, const ex & rh)
82 return exmul(lh, power(rh,_ex_1));
86 // binary arithmetic operators numeric with numeric
88 const numeric operator+(const numeric & lh, const numeric & rh)
93 const numeric operator-(const numeric & lh, const numeric & rh)
98 const numeric operator*(const numeric & lh, const numeric & rh)
103 const numeric operator/(const numeric & lh, const numeric & rh)
109 // binary arithmetic assignment operators with ex
111 ex & operator+=(ex & lh, const ex & rh)
113 return lh = exadd(lh, rh);
116 ex & operator-=(ex & lh, const ex & rh)
118 return lh = exadd(lh, exminus(rh));
121 ex & operator*=(ex & lh, const ex & rh)
123 return lh = exmul(lh, rh);
126 ex & operator/=(ex & lh, const ex & rh)
128 return lh = exmul(lh, power(rh,_ex_1));
132 // binary arithmetic assignment operators with numeric
134 numeric & operator+=(numeric & lh, const numeric & rh)
140 numeric & operator-=(numeric & lh, const numeric & rh)
146 numeric & operator*=(numeric & lh, const numeric & rh)
152 numeric & operator/=(numeric & lh, const numeric & rh)
161 const ex operator+(const ex & lh)
166 const ex operator-(const ex & lh)
171 const numeric operator+(const numeric & lh)
176 const numeric operator-(const numeric & lh)
178 return _num_1_p->mul(lh);
182 // increment / decrement operators
184 /** Expression prefix increment. Adds 1 and returns incremented ex. */
185 ex & operator++(ex & rh)
187 return rh = exadd(rh, _ex1);
190 /** Expression prefix decrement. Subtracts 1 and returns decremented ex. */
191 ex & operator--(ex & rh)
193 return rh = exadd(rh, _ex_1);
196 /** Expression postfix increment. Returns the ex and leaves the original
197 * incremented by 1. */
198 const ex operator++(ex & lh, int)
201 lh = exadd(lh, _ex1);
205 /** Expression postfix decrement. Returns the ex and leaves the original
206 * decremented by 1. */
207 const ex operator--(ex & lh, int)
210 lh = exadd(lh, _ex_1);
214 /** Numeric prefix increment. Adds 1 and returns incremented number. */
215 numeric& operator++(numeric & rh)
217 rh = rh.add(*_num1_p);
221 /** Numeric prefix decrement. Subtracts 1 and returns decremented number. */
222 numeric& operator--(numeric & rh)
224 rh = rh.add(*_num_1_p);
228 /** Numeric postfix increment. Returns the number and leaves the original
229 * incremented by 1. */
230 const numeric operator++(numeric & lh, int)
233 lh = lh.add(*_num1_p);
237 /** Numeric postfix decrement. Returns the number and leaves the original
238 * decremented by 1. */
239 const numeric operator--(numeric & lh, int)
242 lh = lh.add(*_num_1_p);
246 // binary relational operators ex with ex
248 const relational operator==(const ex & lh, const ex & rh)
250 return dynallocate<relational>(lh, rh, relational::equal);
253 const relational operator!=(const ex & lh, const ex & rh)
255 return dynallocate<relational>(lh, rh, relational::not_equal);
258 const relational operator<(const ex & lh, const ex & rh)
260 return dynallocate<relational>(lh, rh, relational::less);
263 const relational operator<=(const ex & lh, const ex & rh)
265 return dynallocate<relational>(lh, rh, relational::less_or_equal);
268 const relational operator>(const ex & lh, const ex & rh)
270 return dynallocate<relational>(lh, rh, relational::greater);
273 const relational operator>=(const ex & lh, const ex & rh)
275 return dynallocate<relational>(lh, rh, relational::greater_or_equal);
278 // input/output stream operators and manipulators
280 static int my_ios_index()
282 static int i = std::ios_base::xalloc();
286 // Stream format gets copied or destroyed
287 static void my_ios_callback(std::ios_base::event ev, std::ios_base & s, int i)
289 print_context *p = static_cast<print_context *>(s.pword(i));
290 if (ev == std::ios_base::erase_event) {
293 } else if (ev == std::ios_base::copyfmt_event && p != 0)
294 s.pword(i) = p->duplicate();
298 callback_registered = 1
301 // Get print_context associated with stream, may return 0 if no context has
302 // been associated yet
303 static inline print_context *get_print_context(std::ios_base & s)
305 return static_cast<print_context *>(s.pword(my_ios_index()));
308 // Set print_context associated with stream, retain options
309 static void set_print_context(std::ios_base & s, const print_context & c)
311 int i = my_ios_index();
312 long flags = s.iword(i);
313 if (!(flags & callback_registered)) {
314 s.register_callback(my_ios_callback, i);
315 s.iword(i) = flags | callback_registered;
317 print_context *p = static_cast<print_context *>(s.pword(i));
318 unsigned options = p ? p->options : c.options;
321 p->options = options;
325 // Get options for print_context associated with stream
326 static inline unsigned get_print_options(std::ios_base & s)
328 print_context *p = get_print_context(s);
329 return p ? p->options : 0;
332 // Set options for print_context associated with stream
333 static void set_print_options(std::ostream & s, unsigned options)
335 print_context *p = get_print_context(s);
337 set_print_context(s, print_dflt(s, options));
339 p->options = options;
342 std::ostream & operator<<(std::ostream & os, const ex & e)
344 print_context *p = get_print_context(os);
346 e.print(print_dflt(os));
352 std::ostream & operator<<(std::ostream & os, const exvector & e)
354 print_context *p = get_print_context(os);
355 exvector::const_iterator i = e.begin();
356 exvector::const_iterator vend = e.end();
366 i -> print(print_dflt(os));
379 std::ostream & operator<<(std::ostream & os, const exset & e)
381 print_context *p = get_print_context(os);
382 exset::const_iterator i = e.begin();
383 exset::const_iterator send = e.end();
393 i->print(print_dflt(os));
406 std::ostream & operator<<(std::ostream & os, const exmap & e)
408 print_context *p = get_print_context(os);
409 exmap::const_iterator i = e.begin();
410 exmap::const_iterator mend = e.end();
420 i->first.print(print_dflt(os));
425 i->second.print(print_dflt(os));
438 std::istream & operator>>(std::istream & is, ex & e)
440 throw (std::logic_error("expression input from streams not implemented"));
443 std::ostream & dflt(std::ostream & os)
445 set_print_context(os, print_dflt(os));
446 set_print_options(os, 0);
450 std::ostream & latex(std::ostream & os)
452 set_print_context(os, print_latex(os));
456 std::ostream & python(std::ostream & os)
458 set_print_context(os, print_python(os));
462 std::ostream & python_repr(std::ostream & os)
464 set_print_context(os, print_python_repr(os));
468 std::ostream & tree(std::ostream & os)
470 set_print_context(os, print_tree(os));
474 std::ostream & csrc(std::ostream & os)
476 set_print_context(os, print_csrc_double(os));
480 std::ostream & csrc_float(std::ostream & os)
482 set_print_context(os, print_csrc_float(os));
486 std::ostream & csrc_double(std::ostream & os)
488 set_print_context(os, print_csrc_double(os));
492 std::ostream & csrc_cl_N(std::ostream & os)
494 set_print_context(os, print_csrc_cl_N(os));
498 std::ostream & index_dimensions(std::ostream & os)
500 set_print_options(os, get_print_options(os) | print_options::print_index_dimensions);
504 std::ostream & no_index_dimensions(std::ostream & os)
506 set_print_options(os, get_print_options(os) & ~print_options::print_index_dimensions);