bug?

1 view
Skip to first unread message

jtaylor74

unread,
Feb 9, 2009, 9:43:18 PM2/9/09
to sympy
I don't know if this is a bug or not, but what I want is to evaluate a
lambdify'ed expression at a numpy.ndarray of points.

Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> from sympy import Symbol, Piecewise, lambdify
>>>
>>> t=Symbol('t')
>>> f=Piecewise((0, t<3), (t,(t<4)), (1,t>=4))
>>> fl = lambdify(t, f)
>>>
>>> print [fl(float(_t)) for _t in np.linspace(0,10,11)]
[0, 0, 0, 3.00000000000000, 1, 1, 1, 1, 1, 1, 1]
>>> print [fl(_t) for _t in np.linspace(0,10,11)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "/home/jtaylo/code/virtualenv/nipy/lib/python2.5/site-packages/
sympy-0.6.3-py2.5.egg/sympy/functions/elementary/piecewise.py", line
69, in __new__
" Relational or Number" % (ec[1], cond_type)
TypeError: Cond True is of type <type 'numpy.bool_'>, but must be a
bool, Relational or Number
>>>
>>> import numpy.version
>>> print numpy.version.version
1.2.0

>>> import sympy
>>> print sympy.__version__
0.6.3
>>>

Ondrej Certik

unread,
Feb 9, 2009, 10:33:59 PM2/9/09
to sy...@googlegroups.com
Hi jtaylor74,


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

Robert Kern

unread,
Feb 10, 2009, 1:48:53 AM2/10/09
to sy...@googlegroups.com
On Mon, Feb 9, 2009 at 21:33, Ondrej Certik <ond...@certik.cz> wrote:

> 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

Ondrej Certik

unread,
Feb 10, 2009, 12:14:29 PM2/10/09
to sy...@googlegroups.com
On Mon, Feb 9, 2009 at 10:48 PM, Robert Kern <rober...@gmail.com> wrote:
>
> On Mon, Feb 9, 2009 at 21:33, Ondrej Certik <ond...@certik.cz> wrote:
>
>> 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

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

Reply all
Reply to author
Forward
0 new messages