[Numpy-discussion] Detect if array has been transposed

Mads Ipsen mads.ipsen at gmail.com
Sun Oct 12 13:19:13 EDT 2014


Hi,

In part of my C++ code, I often do

void foo(PyObject * matrix) {
   do stuff
}

where matrix is a numpy mxn matrix created on the Python side, where 
foo() eg. is invoked as

a = numpy.array([[1,2],[3,5]])
foo(a)

However, if you call transpose() on a, some care should be taken, since 
numpy's internal matrix data first gets transposed on demand. In that 
case I must do

a = numpy.array([[1,2],[3,5]])
a.transpose()
foo(a.copy())

to make sure the correct data of the array gets transferred to the C++ 
side.

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? For example

if a_has_transposed_data:
     foo(a.copy())
else:
     foo(a)

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