Numeric and the new division operator
Chris Liechti
cliechti at gmx.net
Thu Jan 3 10:19:43 EST 2002
[posted and mailed]
mjbarber at ascc.artsci.wustl.edu (Michael James Barber) wrote in
news:a11rau$h3h at ascc.artsci.wustl.edu:
>
> I run into a problem when using the new "true division" operator in
> conjunction with Numeric. Do others see this too?
>
> I can't imagine what I could be doing wrong with what is below, but I
> hope someone can enlighten me if it is a usage problem. The problem is
> only in Numeric, btw - it works fine with floats and integers.
>
>
> Python 2.2 (#124, Dec 22 2001, 17:36:41) [CW CARBON GUSI2 THREADS GC]
> on mac
> Type "copyright", "credits" or "license" for more information.
>>>> from Numeric import arange
>>>> x = arange(10)
>>>> x
> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>>> x / 10
> array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>>> x / 10.
> array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
>>>> # classic division works as expected ...
>>>> # now let's try true division ...
>>>> from __future__ import division x/10
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: unsupported operand type(s) for /: 'array' and 'int'
>>>> x / 10.
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: unsupported operand type(s) for /: 'array' and 'float'
>>>> # oh, dear
from the docs:
---------
__div__(self, other)
__truediv__(self, other)
The division operator (/) is implemented by these methods. The
__truediv__() method is used when __future__.division is in effect,
otherwise __div__() is used. If only one of these two methods is defined,
the object will not support division in the alternate context; TypeError
will be raised instead.
---------
so i think they have not implemented __truediv__.
maybe it would suffuce to say "__truediv__ = __div__" in the Nummeric
classes, but i don't know them.
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list