[Numpy-discussion] Fortran order arrays to and from numpy arrays
Charles R Harris
charlesr.harris@gmail....
Sat Feb 24 23:30:49 CST 2007
On 25 Feb 2007 01:44:01 +0000, Alexander Schmolck <a.schmolck@gmx.net>
wrote:
>
> "Charles R Harris" <charlesr.harris@gmail.com> writes:
>
> > > Unfortunately I don't see an easy way to use the same approach the
> other
> > > way
> > > (matlab doesn't seem to offer much on the C level to manipulate
> arrays),
> > > so
> > > I'd presumably need something like:
> > >
> > > stuff_into_matlab_array(a.T.reshape(a.shape).copy())
> > >
> > > the question is how to avoid doing two copies.
> > >
> > > Any comments appreciated,
> >
> >
> > The easiest way to deal with the ordering is to use the order keyword in
> > numpy:
> >
> > In [4]: a = array([0,1,2,3]).reshape((2,2), order='F')
> >
> > In [5]: a
> > Out[5]:
> > array([[0, 2],
> > [1, 3]])
> >
> > You would still need to get access to something to reshape, shared
> memory or
> > something, but the key is that you don't have to reorder the elements,
> you
> > just need the correct strides and offsets to address the elements in
> Fortran
> > order. I have no idea if this works in numeric.
>
> It doesn't work in Numeric, but that isn't much of any issue because I
> think
> it ought to be pretty much equivalent by transposing and reshaping.
> However
> the problem is that I *do* need to reorder the elements for numpy->matlab
> and
> I'm not sure how to best do this (without unnecessary copying and
> temporary
> numpy array creation but using numpy functionality if possible).
I don't see any way to get around a copy, but you can make numpy do the
work. For example:
In [12]: a = array([[0,1],[2,3]])
In [13]: b = array(a, order='f')
In [14]: a.flags
Out[14]:
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
In [15]: b.flags
Out[15]:
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
F_CONTIGUOUS is what you want. The trick is to somehow use memory in the
construction of the reordered array that is already designated for matlab. I
don't know how to do this, but I think it might be doable. Travis is your
best bet to answer that question.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070224/a79b75cc/attachment.html
More information about the Numpy-discussion
mailing list