(expr)
Takes an expression containing a piecewise function and returns the
    expression in piecewise form.

    Examples
    ========

    >>> from sympy import Piecewise, piecewise_fold, sympify as S
    >>> from sympy.abc import x
    >>> p = Piecewise((x, x < 1), (1, S(1) <= x))
    >>> piecewise_fold(x*p)
    Piecewise((x**2, x < 1), (x, 1 <= x))

    See Also
    ========

    Piecewise
Takes an expression containing a piecewise function and returns the
    expression in piecewise form.
Takes an expression containing a piecewise function and returns the expression in piecewise form .
def piecewise_fold(expr):
    """
    Takes an expression containing a piecewise function and returns the
    expression in piecewise form.

    Examples
    ========

    >>> from sympy import Piecewise, piecewise_fold, sympify as S
    >>> from sympy.abc import x
    >>> p = Piecewise((x, x < 1), (1, S(1) <= x))
    >>> piecewise_fold(x*p)
    Piecewise((x**2, x < 1), (x, 1 <= x))

    See Also
    ========

    Piecewise
    """
    if not isinstance(expr, Basic) or not expr.has(Piecewise):
        return expr
    new_args = list(map(piecewise_fold, expr.args))
    if expr.func is ExprCondPair:
        return ExprCondPair(*new_args)
    piecewise_args = []
    for n, arg in enumerate(new_args):
        if isinstance(arg, Piecewise):
            piecewise_args.append(n)
    if len(piecewise_args) > 0:
        n = piecewise_args[0]
        new_args = [(expr.func(*(new_args[:n] + [e] + new_args[n + 1:])), c)
                    for e, c in new_args[n].args]
        if isinstance(expr, Boolean):
            # If expr is Boolean, we must return some kind of PiecewiseBoolean.
            # This is constructed by means of Or, And and Not.
            # piecewise_fold(0 < Piecewise( (sin(x), x<0), (cos(x), True)))
            # can't return Piecewise((0 < sin(x), x < 0), (0 < cos(x), True))
            # but instead Or(And(x < 0, 0 < sin(x)), And(0 < cos(x), Not(x<0)))
            other = True
            rtn = False
            for e, c in new_args:
                rtn = Or(rtn, And(other, c, e))
                other = And(other, Not(c))
            if len(piecewise_args) > 1:
                return piecewise_fold(rtn)
            return rtn
        if len(piecewise_args) > 1:
            return piecewise_fold(Piecewise(*new_args))
        return Piecewise(*new_args)
    else:
        return expr.func(*new_args)
def piecewise_fold ( expr ) : if not isinstance ( expr , Basic ) or not expr . has ( Piecewise ) : return expr new_args = list ( map ( piecewise_fold , expr . args ) ) if expr . func is ExprCondPair : return ExprCondPair ( * new_args ) piecewise_args = [ ] for n , arg in enumerate ( new_args ) : if isinstance ( arg , Piecewise ) : piecewise_args . append ( n ) if len ( piecewise_args ) > 0 : n = piecewise_args [ 0 ] new_args = [ ( expr . func ( * ( new_args [ : n ] + [ e ] + new_args [ n + 1 : ] ) ) , c ) for e , c in new_args [ n ] . args ] if isinstance ( expr , Boolean ) : # If expr is Boolean, we must return some kind of PiecewiseBoolean. # This is constructed by means of Or, And and Not. # piecewise_fold(0 < Piecewise( (sin(x), x<0), (cos(x), True))) # can't return Piecewise((0 < sin(x), x < 0), (0 < cos(x), True)) # but instead Or(And(x < 0, 0 < sin(x)), And(0 < cos(x), Not(x<0))) other = True rtn = False for e , c in new_args : rtn = Or ( rtn , And ( other , c , e ) ) other = And ( other , Not ( c ) ) if len ( piecewise_args ) > 1 : return piecewise_fold ( rtn ) return rtn if len ( piecewise_args ) > 1 : return piecewise_fold ( Piecewise ( * new_args ) ) return Piecewise ( * new_args ) else : return expr . func ( * new_args )