[Numpy-discussion] Detect if array has been transposed

Mads Ipsen mads.ipsen at gmail.com
Mon Oct 13 08:57:28 EDT 2014


On 13/10/14 01:18, Nathaniel Smith wrote:
> On Mon, Oct 13, 2014 at 12:07 AM, Pauli Virtanen <pav at iki.fi> wrote:
>> 12.10.2014, 22:16, Eric Firing kirjoitti:
>>> On 2014/10/12, 8:29 AM, Pauli Virtanen wrote:
>>>> 12.10.2014, 20:19, Mads Ipsen kirjoitti:
>>>>> Is there any way for me to detect (on the Python side) that transpose()
>>>>> has been invoked on the matrix, and thereby only do the copy operation
>>>>> when it really is needed?
>>>>
>>>> The correct way to do this is to, either:
>>>>
>>>> In your C code check PyArray_IS_C_CONTIGUOUS(obj) and raise an error if
>>>> it is not. In addition, on the Python side, check for
>>>> `a.flags.c_contiguous` and make a copy if it is not.
>>>>
>>>> OR
>>>>
>>>> In your C code, get an handle to the array using PyArray_FromANY (or
>>>> PyArray_FromOTF) with NPY_ARRAY_C_CONTIGUOUS requirement set so that it
>>>> makes a copy when necessary.
>>>
>>> or let numpy handle it on the python side:
>>>
>>> foo(numpy.ascontiguousarray(a))
>>
>> Yes, but the C code really should check that the input array is
>> C-contiguous, if it only works for C-contiguous inputs.
>
> I.e. your original instructions were correct, but instead of checking
> a.flags.c_contiguous by hand etc. the OP should just call
> ascontiguousarray which takes care of that part.
>
Hi,

To everybody that answered - your help is (as always) much appreciated.

Best regards,

Mads
-- 
+---------------------------------------------------------+
| Mads Ipsen                                              |
+----------------------+----------------------------------+
| Gåsebæksvej 7, 4. tv | phone:              +45-29716388 |
| DK-2500 Valby        | email:      mads.ipsen at gmail.com |
| Denmark              | map  :   www.tinyurl.com/ns52fpa |
+----------------------+----------------------------------+



More information about the NumPy-Discussion mailing list