[SciPy-dev] Python/Fortran multi-dimensional array issues

Pearu Peterson pearu at cens.ioc.ee
Mon Jan 14 14:38:12 CST 2002

On Tue, 15 Jan 2002, Prabhu Ramachandran wrote:

> >>>>> "PP" == Pearu Peterson <pearu at cens.ioc.ee> writes:
>     PP> Hi!
>     PP> In this long posting I will describe a new idea how to
>     PP> overcome with the difficulties caused by different
>     PP> storage-orders in C and Fortran multi-dimensional arrays.
> Sounds great, but I couldnt understand the algorithms (pseudo code).
> I'll look carefully later on.

If it helps, then fortran|c_array_from_pyobj() functions will replace the
current arr_from_pyobj() function in f2py generated code.

I can guess that difficult parts of the pseudo code are the functions
transpose_data_inplace, etc. Let me add here few comments on those:

transpose_data_inplace(obj) - obj is PyArrayObject that holds various
attributes, eg data - that is pointer to actual data array, dimensions,
rank, etc. The purpose of transpose_data_inplace(obj) is best described
by the following Python statement:
  obj = transpose(obj)
but it will not consume extra memory for that. The transpose operation is
carried out only for data attribute. Other attributes are not changed.

set_transpose_strides(obj) - again, obj is PyArrayObject. This function
behaves like Numeric.transpose, it permutes the dimensions and strides of
the array object, but does not touch data attribute.

swap_datapointer_and_typeinfo(obj1,obj2) - will swap the data attributes
of Numeric arrays, dimensions and strides are untouched. Since now data
will store different elements, also obj1 and obj2 typeinfo attributes
needs to be swapped. (Travis O., can you give a hand here to get this
function right?)

copy_ND_array(obj1,obj2) - this is equivalent to python statement
  obj1[:] = obj2
where obj1,obj2 can be also noncontiguous arrays.

intent - this argument is used to check whether a given variable had
certain intent(..) attribute set or not. For example, in C/API code
we'll have
  if (intent & F2PY_INTENT_IN) { ...

The typecode,dims, and rank arguments in *_array_from_pyobj() functions
correspond to expected properties of arguments. For example, if
in Fortran A is defined as

    integer a(m,n)

then in C/API module,
    typecode = PyArray_INT;
    dims[0] = m;
    dims[1] = n;
    rank = 2;
will be set.

Please don't hesitate to ask more clarifications. I will be happy to
explain the algorithm as eventually it must become perfect;)


More information about the Scipy-dev mailing list