I can reproduce it with the latest git version:
In [1]: import numpy as np
In [2]: t=Symbol('t')
In [3]: f=Piecewise((0, t<3), (t,(t<4)), (1,t>=4))
In [4]: fl = lambdify(t, f)
In [5]: print [fl(float(_t)) for _t in np.linspace(0,10,11)]
[0, 0, 0, 3.00000000000000, 1, 1, 1, 1, 1, 1, 1]
In [6]: print [fl(_t) for _t in np.linspace(0,10,11)]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/ondra/repos/sympy/<ipython console> in <module>()
/home/ondra/repos/sympy/<string> in <lambda>(t)
/home/ondra/repos/sympy/sympy/functions/elementary/piecewise.pyc in
__new__(cls, *args, **options)
69 raise TypeError, \
70 "Cond %s is of type %s, but must be a bool," \
---> 71 " Relational or Number" % (ec[1], cond_type)
72
73 # sympify args
TypeError: Cond True is of type <type 'numpy.bool_'>, but must be a
bool, Relational or Number
So I think it's a bug. Thanks a lot for reporting it. The problem is
with this code:
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)
It checks for bool, but not numpy.bool_. Does anyone know how to make
it, so that it works with numpy types, but doesn't use numpy.bool_
explicitely?
Ondrej
> So I think it's a bug. Thanks a lot for reporting it. The problem is
> with this code:
>
> 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)
>
>
> It checks for bool, but not numpy.bool_. Does anyone know how to make
> it, so that it works with numpy types, but doesn't use numpy.bool_
> explicitely?
Do you *need* to typecheck?
If so, then try this out:
http://www.enthought.com/~rkern/cgi-bin/hgwebdir.cgi/deferredisinstance
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
I think something like that, thanks!
I need to study this thoroughly, it seems to me quite complicated, but
maybe that's the best we can do.
Ondrej