Okay I applied all your comments and updated my repo
http://bitbucket.org/aterrel/sympy-aterrel/

-- Andy

On Oct 21, 11:35 pm, "Ondrej Certik" <[EMAIL PROTECTED]> wrote:
> This is +1, thanks!
>
> On Tue, Oct 21, 2008 at 10:07 PM, Andy R Terrel <[EMAIL PROTECTED]> wrote:
>
>
>
> > # HG changeset patch
> > # User "Andy R Terrel <[EMAIL PROTECTED]>"
> > # Date 1224619309 18000
> > # Node ID a4acb162ad0d40b8d86ee64b4a8cfa4c5426bc3e
> > # Parent  077e4d22890139d23f838926f307a9ed9054509b
> > Adding ExprCondPair data structure to piecewise objects and cleaning code.
>
> > diff --git a/sympy/functions/elementary/piecewise.py 
> > b/sympy/functions/elementary/piecewise.py
> > --- a/sympy/functions/elementary/piecewise.py
> > +++ b/sympy/functions/elementary/piecewise.py
> > @@ -1,9 +1,30 @@
>
> >  from sympy.core.basic import Basic, S
> > -from sympy.core.function import Function, FunctionClass, diff
> > +from sympy.core.function import Function, diff
> >  from sympy.core.numbers import Number
> >  from sympy.core.relational import Relational
> >  from sympy.core.sympify import sympify
> > +
> > +class ExprCondPair(Basic):
> > +    """Represents an expression, condition pair."""
> > +
> > +    def __new__(cls, expr, cond, **assumptions):
> > +        expr = sympify(expr)
> > +        cond = sympify(cond)
> > +        return Basic.__new__(cls, expr, cond, **assumptions)
> > +
> > +   [EMAIL PROTECTED]
> > +    def expr(self):
> > +        return self.args[0]
> > +
> > +   [EMAIL PROTECTED]
> > +    def cond(self):
> > +        return self.args[1]
> > +
> > +    def __iter__(self):
> > +        yield self.expr
> > +        yield self.cond
> > +
>
> >  class Piecewise(Function):
> >     """
> > @@ -36,13 +57,21 @@ class Piecewise(Function):
> >     nargs=None
>
> >     def __new__(cls, *args, **options):
> > -        for opt in ["nargs", "dummy", "comparable", "noncommutative", 
> > "commutative"]:
> > -            if opt in options:
> > -                del options[opt]
> > -        r = cls.canonize(*args)
> > +        # Check types first
> > +        for ec in args:
> > +            if not isinstance(ec, tuple) or len(ec) != 2:
> > +                raise TypeError, "args may only include (expr, cond) pairs"
> > +            cond_type = type(ec[1])
> > +            if not (cond_type is bool or issubclass(cond_type, Relational) 
> > or \
> > +                    issubclass(cond_type, Number)):
> > +                raise TypeError, \
> > +                    "Cond %s is of type %s, but must be a bool," \
> > +                    " Relational or Number" % (ec[1], cond_type)
>
> >         # sympify args
> > -        args = map(lambda x:(sympify(x[0]), sympify(x[1])), args)
> > +        args = map(lambda x:ExprCondPair(*x), args)
> > +
> > +        r = cls.canonize(*args)
>
> >         if r is None:
> >             return Basic.__new__(cls, *args, **options)
> > @@ -51,18 +80,6 @@ class Piecewise(Function):
>
> >     @classmethod
> >     def canonize(cls, *args):
> > -        # Check types first
> > -        for ec in args:
> > -            if (not isinstance(ec, tuple)) or len(ec)!=2:
> > -                raise TypeError, "args may only include (expr, cond) pairs"
> > -        for expr, cond in args:
> > -            cond_type = type(ec[1])
> > -            if cond_type != bool and not issubclass(cond_type, Relational) 
> > and\
> > -                    not issubclass(cond_type, Number):
> > -                raise TypeError, \
> > -                    "Cond %s is of type %s, but must be a bool,"\
> > -                    " Relational or Number" % (cond, cond_type)
> > -
> >         # Check for situations where we can evaluate the Piecewise object.
> >         # 1) Hit an unevaluatable cond (e.g. x<1) -> keep object
> >         # 2) Hit a true condition -> return that expr
> > @@ -116,7 +133,7 @@ class Piecewise(Function):
> >         #    -  eg x < 1, x < 3 -> [oo,1],[1,3] instead of [oo,1],[oo,3]
> >         # 3) Sort the intervals to make it easier to find correct exprs
> >         for expr, cond in self.args:
> > -            if issubclass(type(cond),Number):
> > +            if cond.is_Number:
> >                 if cond:
> >                     default = expr
> >                     break
> > @@ -129,9 +146,9 @@ class Piecewise(Function):
> >                 curr[1] = S.Infinity
> >             else:
> >                 raise NotImplementedError, \
> > -                    "Currently only supporting evaluation with only "\
> > -                    "sym on one side fo the relation."
> > -            curr = [max(a,curr[0]),min(b,curr[1])]
> > +                    "Currently only supporting evaluation with only " \
> > +                    "sym on one side of the relation."
> > +            curr = [max(a, curr[0]), min(b, curr[1])]
> >             for n in xrange(len(int_expr)):
> >                 if self.__eval_cond(curr[0] < int_expr[n][1]) and \
> >                         self.__eval_cond(curr[0] >= int_expr[n][0]):
> > @@ -140,7 +157,7 @@ class Piecewise(Function):
> >                         self.__eval_cond(curr[1] <= int_expr[n][1]):
> >                     curr[1] = int_expr[n][0]
> >             if self.__eval_cond(curr[0] < curr[1]):
> > -                int_expr.append(curr+[expr])
> > +                int_expr.append(curr + [expr])
> >         int_expr.sort(key=lambda x:x[0])
>
> >         # Add holes to list of intervals if there is a default value,
> > @@ -149,16 +166,16 @@ class Piecewise(Function):
> >         curr_low = a
> >         for int_a, int_b, expr in int_expr:
> >             if curr_low < int_a:
> > -                holes.append([curr_low, min(b,int_a), default])
> > +                holes.append([curr_low, min(b, int_a), default])
> >             curr_low = int_b
> >             if curr_low > b:
> >                 break
> >         if holes and default != None:
> >             int_expr.extend(holes)
> >         elif holes and default == None:
> > -            raise ValueError, "Called interval evaluation over piecewise "\
> > -                              "function on undefined intervals %s" %\
> > -                              ", ".join([str((h[0],h[1])) for h in holes])
> > +            raise ValueError, "Called interval evaluation over piecewise " 
> > \
> > +                              "function on undefined intervals %s" % \
> > +                              ", ".join([str((h[0], h[1])) for h in holes])
>
> >         # Finally run through the intervals and sum the evaluation.
> >         ret_fun = 0
> > @@ -178,13 +195,8 @@ class Piecewise(Function):
> >     @classmethod
> >     def __eval_cond(cls, cond):
> >         """Returns S.One if True, S.Zero if False, or None if 
> > undecidable."""
> > -        if issubclass(type(cond), Number) or type(cond) == bool:
> > +        if type(cond) == bool or cond.is_Number:
> >             return sympify(bool(cond))
> > -        arg0 = cond.args[0]
> > -        arg1 = cond.args[1]
> > -        if isinstance(arg0, FunctionClass) or isinstance(arg1, 
> > FunctionClass):
> > -            return None
> > -        if not issubclass(type(arg0.evalf()), Number) or \
> > -                not issubclass(type(arg1.evalf()), Number):
> > -            return None
> > -        return sympify(bool(cond))
> > +        if cond.args[0].is_Number and cond.args[1].is_Number:
> > +            return sympify(bool(cond))
> > +        return None
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy-patches" group.
To post to this group, send email to sympy-patches@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sympy-patches?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to