1 /** @file operators.cpp
3 * Implementation of GiNaC's overloaded operators. */
6 * GiNaC Copyright (C) 1999-2008 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
26 #include "operators.h"
32 #include "relational.h"
38 /** Used internally by operator+() to add two ex objects together. */
39 static inline const ex exadd(const ex & lh, const ex & rh)
41 return (new add(lh,rh))->setflag(status_flags::dynallocated);
44 /** Used internally by operator*() to multiply two ex objects together. */
45 static inline const ex exmul(const ex & lh, const ex & rh)
47 // Check if we are constructing a mul object or a ncmul object. Due to
48 // ncmul::eval()'s rule to pull out commutative elements we need to check
49 // only one of the elements.
50 if (rh.return_type()==return_types::commutative ||
51 lh.return_type()==return_types::commutative) {
52 return (new mul(lh,rh))->setflag(status_flags::dynallocated);
54 return (new ncmul(lh,rh))->setflag(status_flags::dynallocated);
58 /** Used internally by operator-() and friends to change the sign of an argument. */
59 static inline const ex exminus(const ex & lh)
61 return (new mul(lh,_ex_1))->setflag(status_flags::dynallocated);
64 // binary arithmetic operators ex with ex
66 const ex operator+(const ex & lh, const ex & rh)
71 const ex operator-(const ex & lh, const ex & rh)
73 return exadd(lh, exminus(rh));
76 const ex operator*(const ex & lh, const ex & rh)
81 const ex operator/(const ex & lh, const ex & rh)
83 return exmul(lh, power(rh,_ex_1));
87 // binary arithmetic operators numeric with numeric
89 const numeric operator+(const numeric & lh, const numeric & rh)
94 const numeric operator-(const numeric & lh, const numeric & rh)
99 const numeric operator*(const numeric & lh, const numeric & rh)
104 const numeric operator/(const numeric & lh, const numeric & rh)
110 // binary arithmetic assignment operators with ex
112 ex & operator+=(ex & lh, const ex & rh)
114 return lh = exadd(lh, rh);
117 ex & operator-=(ex & lh, const ex & rh)
119 return lh = exadd(lh, exminus(rh));
122 ex & operator*=(ex & lh, const ex & rh)
124 return lh = exmul(lh, rh);
127 ex & operator/=(ex & lh, const ex & rh)
129 return lh = exmul(lh, power(rh,_ex_1));
133 // binary arithmetic assignment operators with numeric
135 numeric & operator+=(numeric & lh, const numeric & rh)
141 numeric & operator-=(numeric & lh, const numeric & rh)
147 numeric & operator*=(numeric & lh, const numeric & rh)
153 numeric & operator/=(numeric & lh, const numeric & rh)
162 const ex operator+(const ex & lh)
167 const ex operator-(const ex & lh)
172 const numeric operator+(const numeric & lh)
177 const numeric operator-(const numeric & lh)
179 return _num_1_p->mul(lh);
183 // increment / decrement operators
185 /** Expression prefix increment. Adds 1 and returns incremented ex. */
186 ex & operator++(ex & rh)
188 return rh = exadd(rh, _ex1);
191 /** Expression prefix decrement. Subtracts 1 and returns decremented ex. */
192 ex & operator--(ex & rh)
194 return rh = exadd(rh, _ex_1);
197 /** Expression postfix increment. Returns the ex and leaves the original
198 * incremented by 1. */
199 const ex operator++(ex & lh, int)
202 lh = exadd(lh, _ex1);
206 /** Expression postfix decrement. Returns the ex and leaves the original
207 * decremented by 1. */
208 const ex operator--(ex & lh, int)
211 lh = exadd(lh, _ex_1);
215 /** Numeric prefix increment. Adds 1 and returns incremented number. */
216 numeric& operator++(numeric & rh)
218 rh = rh.add(*_num1_p);
222 /** Numeric prefix decrement. Subtracts 1 and returns decremented number. */
223 numeric& operator--(numeric & rh)
225 rh = rh.add(*_num_1_p);
229 /** Numeric postfix increment. Returns the number and leaves the original
230 * incremented by 1. */
231 const numeric operator++(numeric & lh, int)
234 lh = lh.add(*_num1_p);
238 /** Numeric postfix decrement. Returns the number and leaves the original
239 * decremented by 1. */
240 const numeric operator--(numeric & lh, int)
243 lh = lh.add(*_num_1_p);
247 // binary relational operators ex with ex
249 const relational operator==(const ex & lh, const ex & rh)
251 return relational(lh,rh,relational::equal);
254 const relational operator!=(const ex & lh, const ex & rh)
256 return relational(lh,rh,relational::not_equal);
259 const relational operator<(const ex & lh, const ex & rh)
261 return relational(lh,rh,relational::less);
264 const relational operator<=(const ex & lh, const ex & rh)
266 return relational(lh,rh,relational::less_or_equal);
269 const relational operator>(const ex & lh, const ex & rh)
271 return relational(lh,rh,relational::greater);
274 const relational operator>=(const ex & lh, const ex & rh)
276 return relational(lh,rh,relational::greater_or_equal);
279 // input/output stream operators and manipulators
281 static int my_ios_index()
283 static int i = std::ios_base::xalloc();
287 // Stream format gets copied or destroyed
288 static void my_ios_callback(std::ios_base::event ev, std::ios_base & s, int i)
290 print_context *p = static_cast<print_context *>(s.pword(i));
291 if (ev == std::ios_base::erase_event) {
294 } else if (ev == std::ios_base::copyfmt_event && p != 0)
295 s.pword(i) = p->duplicate();
299 callback_registered = 1
302 // Get print_context associated with stream, may return 0 if no context has
303 // been associated yet
304 static inline print_context *get_print_context(std::ios_base & s)
306 return static_cast<print_context *>(s.pword(my_ios_index()));
309 // Set print_context associated with stream, retain options
310 static void set_print_context(std::ios_base & s, const print_context & c)
312 int i = my_ios_index();
313 long flags = s.iword(i);
314 if (!(flags & callback_registered)) {
315 s.register_callback(my_ios_callback, i);
316 s.iword(i) = flags | callback_registered;
318 print_context *p = static_cast<print_context *>(s.pword(i));
319 unsigned options = p ? p->options : c.options;
322 p->options = options;
326 // Get options for print_context associated with stream
327 static inline unsigned get_print_options(std::ios_base & s)
329 print_context *p = get_print_context(s);
330 return p ? p->options : 0;
333 // Set options for print_context associated with stream
334 static void set_print_options(std::ostream & s, unsigned options)
336 print_context *p = get_print_context(s);
338 set_print_context(s, print_dflt(s, options));
340 p->options = options;
343 std::ostream & operator<<(std::ostream & os, const ex & e)
345 print_context *p = get_print_context(os);
347 e.print(print_dflt(os));
353 std::ostream & operator<<(std::ostream & os, const exvector & e)
355 print_context *p = get_print_context(os);
356 exvector::const_iterator i = e.begin();
357 exvector::const_iterator vend = e.end();
367 i -> print(print_dflt(os));
380 std::ostream & operator<<(std::ostream & os, const exset & e)
382 print_context *p = get_print_context(os);
383 exset::const_iterator i = e.begin();
384 exset::const_iterator send = e.end();
394 i->print(print_dflt(os));
407 std::ostream & operator<<(std::ostream & os, const exmap & e)
409 print_context *p = get_print_context(os);
410 exmap::const_iterator i = e.begin();
411 exmap::const_iterator mend = e.end();
421 i->first.print(print_dflt(os));
426 i->second.print(print_dflt(os));
439 std::istream & operator>>(std::istream & is, ex & e)
441 throw (std::logic_error("expression input from streams not implemented"));
444 std::ostream & dflt(std::ostream & os)
446 set_print_context(os, print_dflt(os));
447 set_print_options(os, 0);
451 std::ostream & latex(std::ostream & os)
453 set_print_context(os, print_latex(os));
457 std::ostream & python(std::ostream & os)
459 set_print_context(os, print_python(os));
463 std::ostream & python_repr(std::ostream & os)
465 set_print_context(os, print_python_repr(os));
469 std::ostream & tree(std::ostream & os)
471 set_print_context(os, print_tree(os));
475 std::ostream & csrc(std::ostream & os)
477 set_print_context(os, print_csrc_double(os));
481 std::ostream & csrc_float(std::ostream & os)
483 set_print_context(os, print_csrc_float(os));
487 std::ostream & csrc_double(std::ostream & os)
489 set_print_context(os, print_csrc_double(os));
493 std::ostream & csrc_cl_N(std::ostream & os)
495 set_print_context(os, print_csrc_cl_N(os));
499 std::ostream & index_dimensions(std::ostream & os)
501 set_print_options(os, get_print_options(os) | print_options::print_index_dimensions);
505 std::ostream & no_index_dimensions(std::ostream & os)
507 set_print_options(os, get_print_options(os) & ~print_options::print_index_dimensions);