Dag Sverre Seljebotn wrote:
> Hi Ilmar,
>
> Ilmar Wilbers wrote:
>   
>> Hi list,
>>
>> While trying to wrap an external C library, I have stumbled upon some
>> question. A lot of them were answered by going through the mailing list
>> archive, however a few remain.
>>
>> I have the following function with arguments:
>> def tridag(np.ndarray[DTYPE_t, ndim=1, negative_indices=False] a)
>>
>> I want to do some assertions on the array a:
>>     assert a.dtype == np.float32
>>     assert a.flags['C_CONTIGUOUS']
>> but keep getting an error  "Attempting to index non-array type 'int'".
>> Checking the type of a.flags indicates that it is in int. What am I
>> missing?
>>     
>
> You have encountered one of the less polished aspects of Cython I'm
> afraid. The flags field is typed as int to give efficient access for
> or-style flags checking, which precludes the object behaviour you use.
>
> To make a long story short, here's your current options:
> - assert (<object>a).flags[....]
> - The assertion for datatype is already made if DTYPE_t is float32_t. Use
> mode="c" as a flag to ndarray to do the other assertion automatically (on
> entering the function).
>
> This is definitely something that should be more done something about
> (i.e. this should Just Work), I'll try to file an enhancement ticket for
> it.
>
>   
OK, thank you, Dag Sverre.

I realize that the mode is not really important for 1D arrays, but was 
merely working my way up to matrices.

Another question I was having, which I realize probably belongs on the 
numpy list, is the following:
The C function I am calling takes in float arrays, hence I do this:
cdef float * p_a = <np.float32_t*>a.data
in order to get the correct precision. This requires the assertion of 
DTYPE_t being float32_t, as simply writing
a.dtype=np.float32
doesn't seem to have any effect on the actual data when creating the 
pointer (but no errors, though).

This requires the explicit use of a = zeros(n, numpy.float32) in the 
calling Python code, which I was hoping to avoid.
Alternatively, I can use a loop like this:
    # Compare arrays, copy values if not same type:
    old = [a, b, c, r]
    new = [a_, b_, c_, r_]
    for i in range(len(new)):
        if not old[i].dtype == np.float32:
            new[i][:] = old[i][:]
        else:
            new[i] = old[i]

which is ugly and requires more memory.

My question is, can I use an alternative to
cdef float * p_a = <np.float32_t*>a.data

in case the arrays have dtype float64, for instance, for simply writing
cdef float * p_a = <np.float64_t*>a.data

doesn't help. Aka: is it possible to not make assumptions on the array 
dtype? Using np.float without 32 or 64 works fine, but the data sent to 
the C function is wrong. I guess float in C has the same precision as 
float32 in numpy and double in C has the same precision as float64 in numpy?

Thanks for the awesome software, by the way.

ilmar


_______________________________________________
Cython-dev mailing list
Cython-dev@codespeak.net
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to