[Numpy-discussion] fixed-point arithmetic

Neal Becker ndbecker2@gmail....
Mon Sep 21 12:02:36 CDT 2009


Robert Kern wrote:

> On Mon, Sep 21, 2009 at 10:57, Neal Becker <ndbecker2@gmail.com> wrote:
>> David Cournapeau wrote:
>>
>>> On Mon, Sep 21, 2009 at 9:00 PM, Neal Becker <ndbecker2@gmail.com>
>>> wrote:
>>>>
>>>> numpy arrays of fpi should support all numeric operations.  Also mixed
>>>> fpi/integer operations.
>>>>
>>>> I'm not sure how to go about implementing this.  At first, I was
>>>> thinking to just subclass numpy array.  But, I don't think this
>>>> provides fpi scalars, and their associated operations.
>>>
>>> Using dtype seems more straightforward. I would first try to see how
>>> far you could go using a pure python object as a dtype. For example
>>> (on python 2.6):
>>>
>>> from decimal import Decimal
>>> import numpy as np
>>> a = np.array([1, 2, 3], Decimal)
>>> b = np.array([2, 3, 4], Decimal)
>>> a + b
>>>
>>> works as expected. A lot of things won't work (e.g. most transcendent
>>> functions, which would require a specific implementation anyway), but
>>> arithmetic, etc... would work.
>>>
>>> Then, you could think about implementing the class in cython. If speed
>>> is an issue, then implementing your own dtype seems the way to go - I
>>> don't know exactly what kind of speed increase you could hope from
>>> going the object -> dtype, though.
>>>
>>
>> We don't want to create arrays of fixed-pt objects.  That would be very
>> wasteful.  What I have in mind is that integer_bits, frac_bits are
>> attributes of the entire arrays, not the individual elements.  The array
>> elements are just plain integers.
>>
>> At first I'm thinking that we could subclass numpy array, adding the
>> int_bits and frac_bits attributes.  The arithmetic operators would all
>> have to be overloaded.
>>
>> The other aspect is that accessing an element of the array would return a
>> fixed-pt object (not an integer).
> 
> Actually, what you would do is create a new dtype, not a subclass of
> ndarray. The new datetime dtypes are similar in that they too are
> "parameterized" dtypes.
> 

But doesn't this mean that each array element has it's own int_bits, 
frac_bits attributes?  I don't want that.



More information about the NumPy-Discussion mailing list