Status: Accepted
Owner: asmeurer
CC: andy.ter...@gmail.com
Labels: Type-Defect Priority-High Milestone-Release0.7.2

New issue 2626 by asmeurer: Piecewise should use a different syntax for "otherwise"
http://code.google.com/p/sympy/issues/detail?id=2626

This is something that's been bothering me for a while. Piecewise has the following syntax (I quote the docstring):

Usage
=====
  Piecewise( (expr,cond), (expr,cond), ... )
    - Each argument is a 2-tuple defining a expression and condition
    - The conds are evaluated in turn returning the first that is True.
      If any of the evaluated conds are not determined explicitly False,
      e.g. x < 1, the function is returned in symbolic form.
- If the function is evaluated at a place where all conditions are False,
      a ValueError exception will be raised.
    - Pairs where the cond is explicitly False, will be removed.

Examples
========
  >>> from sympy import Piecewise, log
  >>> from sympy.abc import x
  >>> f = x**2
  >>> g = log(x)
  >>> p = Piecewise( (0, x<-1), (f, x<=1), (g, True))
  >>> p.subs(x,1)
  1
  >>> p.subs(x,5)
  log(5)

The problem is the use of (g, True) for the "otherwise" condition. The problem is that if a conditional reduces to True, then this will be interpreted as an "otherwise" condition rather than returning it.

In [7]: Piecewise((f(x), x < 0), (x, 1 > 0))
Out[7]:
⎧f(x)  for x < 0
⎨
⎩ x    otherwise

This is also inconsistant in that it treats it correctly if only one condition is given:

In [6]: Piecewise((x, 1 > 0))
Out[6]: x

But this is just because it looks like an "otherwise" condition with no other conditions.

By the way, slightly related is issue 2567.

Unfortunately, this means breaking the syntax of Piecewise, but I think it should be done, as I'm quite convinced that the current way is wrong.

What's a better syntax?  Here's what Maple does:

piecewise(cond_1, f_1, cond_2, f_2, ..., cond_n, f_n, f_otherwise)

Although their conditions and functions are backwards (I like very much our (expr, cond) syntax), I think that making the last term the otherwise condition might work.

Or another option would be to make the otherwise condition a single element tuple. This would have the advantage of potentially allowing Piecewise to return a Tuple without syntactical confusion.

Indeed, I like this second option better.  So let's:

- Allow Piecewise((expr, cond), ..., (otherwise_expr,)) syntax
- Make Piecewise use the above syntax internally (i.e., for .args)
- Deprecate Piecewise(expr, cond), ..., (otherwise_expr, True)) syntax

This should be done before the next release, so we can start a deprecation cycle.

--
You received this message because you are subscribed to the Google Groups 
"sympy-issues" group.
To post to this group, send email to sympy-issues@googlegroups.com.
To unsubscribe from this group, send email to 
sympy-issues+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy-issues?hl=en.

Reply via email to