[Numpy-discussion] On my Cython/NumPy project
Dag Sverre Seljebotn
dagss@student.matnat.uio...
Sat Jun 21 14:03:45 CDT 2008
Joris De Ridder wrote:
> Hi Dag,
>
>> General feedback is welcome; in particular, I need more opinions about
>> what syntax people would like. We seem unable to find something that
>> we
>> really like; this is the current best candidate (cdef is the way you
>> declare types on variables in Cython):
>>
>> cdef int i = 4, j = 6
>> cdef np.ndarray[np.float64, 2] arr = np.zeros((10, 10),
>> dtype=np.float64)
>> arr[i, j] = 1
>> ...
>>
>> The code above the under the hood acquires a buffer and uses it for
>> efficient access. arr[5], arr[5,6,7], arr[2:4,...] and in general
>> anything
>> but two simple indices will be passed to Python, while arr[i, j]
>> will be
>> passed to the buffer.
>
> The syntax looks quite good, I think.
> Some questions though:
> - I guess there are some technical reasons why np.float64 in your
> example has to be repeated twice?
There are all sorts of reasons. The right hand side is disconnected from
the left hand side. It could just as well say
cdef np.ndarray[float64, 2] arr = my_func()
or
cdef np.ndarray[float64, 2] arr = x
or
cdef np.ndarray[float64, 2] arr = np.zeros((3, 3),
dtype=np.int).astype(np.float64)
(If you assign a Python object that is not an ndarray of right dimensions
and type, a runtime exception is raised.)
So: If you can figure out a nice rule for not having to repeat the type
I'm all ears, but, really, the repeating of the type was only incidental.
> - When you say "negative indices are not supported" you mean that they
> are passed to python, or
> won't they work at all?
In unsafe mode: If you are really lucky they will segfault your
application, however usually you will not be that fortunate and they will
corrupt your data instead.
In safe mode, they will raise an IndexError.
(Passing them to Python would require to know that they are negative in
the first place. And knowing that they are negative would require an extra
if-test in tight, tight inner loops.)
(Well, of course, if you actually do "arr[-3, -2]" I suppose we can raise
a compiler error, the point is if the negative values are stored in
variables -- how do you know if they are negative without checking?)
Dag Sverre
More information about the Numpy-discussion
mailing list