[Numpy-discussion] fixed-point arithmetic
Mon Sep 21 10:57:18 CDT 2009
David Cournapeau wrote:
> On Mon, Sep 21, 2009 at 9:00 PM, Neal Becker <firstname.lastname@example.org> 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).
More information about the NumPy-Discussion