Numeric and the new division operator
Author Message
Numeric and the new division operator

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

Quote:
>>> from Numeric import arange
>>> x = arange(10)
>>> x

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Quote:
>>> x / 10

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
Quote:
>>> x / 10.

array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9])
Quote:
>>> # 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'
Quote:
>>> x / 10.

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for /: 'array' and 'float'

Quote:
>>> # oh, dear

--
Michael J. Barber

Mon, 21 Jun 2004 22:54:22 GMT
Numeric and the new division operator
[posted and mailed]

Quote:

> 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
>>>> 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
---------

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.

--

Mon, 21 Jun 2004 23:19:43 GMT
Numeric and the new division operator
[true division doesn't work with Numeric]

Quote:

>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
>---------

>so i think they have not implemented __truediv__.

Ah, that would explain it.   Somehow I missed that in the docs -- too
focused on finding something related to Numeric, I guess.  Thanks for
clarifying that.

Looking further on the Numeric site, I have also found this:

Version 20.3
Please note that the __future__ options wrt integer division in Python
2.2 are NOT available in Numeric at this time.

at <http://www.pfdubois.com/numpy/changes.txt>.

I guess it's time to wait a bit longer. :(
--
Michael J. Barber

Tue, 22 Jun 2004 00:25:25 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages