# Re: Matrix elements returned in wrong order

92 views
Skip to first unread message

### Ralph Heinkel

unread,
Jun 19, 2012, 6:04:07 AM6/19/12
to pyrs...@googlegroups.com
I'm not completely sure about this, it looks like that R has fortran-style arrays (left-most index changes fastest), while in Python we have c-style arrays ...
A solution would be to reshape the array in 'F'ortran order:

>>> arr = conn.r("matrix(1:12, nrow=4, ncol=3)")
>>> arr.reshape((4, 3), order='F')
array([[ 1, 5, 9],
[ 2, 6, 10],
[3, 7, 11],
[4, 8, 12]])

Not sure yet whether this should be a default transformation in pyRserve ... any comments about this?

Ralph

Am Montag, 18. Juni 2012 22:14:58 UTC+2 schrieb Vebjorn Ljosa:
I think matrix elements are being returned in the wrong order.

In R, I get the following:

> matrix(1:12, nrow=4, ncol=3)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12

When transfering the matrix to Python and pyRserve, the values are filled in row-wise instead of column-wise:

\$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyRserve
>>> pyRserve.__version__
'0.5.2'
>>> conn = pyRserve.connect()
>>> print conn.r("matrix(1:12, nrow=4, ncol=3)")
[[ 1  2  3]
[ 4  5  6]
[ 7  8  9]
[10 11 12]]
>>>

Thanks,
Vebjorn

### Ralph Heinkel

unread,
Jun 20, 2012, 9:56:07 AM6/20/12
to pyrs...@googlegroups.com
sorry, I just figured out that - for some strange reasons - the array must be 1-d before the reshape() method with the fortran order works. So the correct code is:
>>> arr = conn.r("matrix(1:12, nrow=4, ncol=3)")
>>> shape = arr.shape
>>> arr.shape = arr.size  # this makes it 1-d

>>> arr.reshape((4, 3), order='F')
array([[ 1, 5, 9],
[ 2, 6, 10],
[3, 7, 11],
[4, 8, 12]])

cheers,

Ralph

### Ralph Heinkel

unread,
Jan 5, 2013, 10:16:28 AM1/5/13
to pyrs...@googlegroups.com
Hi Kent,

thanks for your explanations of your problem. I'll look into this to see how a real clean solution would look like.

Ralph

Am Freitag, 4. Januar 2013 20:35:35 UTC+1 schrieb Kent Johnson:
A partial fix to the problem in my previous post seems to be to make RSerializer serialize arrays in Fortran order. This makes sense as the Rserve documentation at http://www.rforge.net/Rserve/faq.html#matrix says, "In general all arrays and matrices are one-dimensional vectors in R stored in column-major format with dimensions as attributes."

I modified rserializer.py at lines 193 and 212, adding the parameter "order='F'" to the calls to tostring(). This fixes my particular problem. It is only a partial fix because the array now is garbled when read back into Python.

Kent

### Ralph Heinkel

unread,
Jan 9, 2013, 2:41:10 AM1/9/13
to pyrs...@googlegroups.com
Hi Kent,

please check my latest post about pyRserve V 0.7.0 and give it a try - it should fix your problems!

### Kent Johnson

unread,
Jan 9, 2013, 10:19:39 AM1/9/13
to pyrs...@googlegroups.com
On Wednesday, January 9, 2013 2:41:10 AM UTC-5, Ralph Heinkel wrote:
Hi Kent,

please check my latest post about pyRserve V 0.7.0 and give it a try - it should fix your problems!

Yes, that is better, thanks!
Kent

### Ralph Heinkel

unread,
Jan 9, 2013, 11:42:16 AM1/9/13
to pyrs...@googlegroups.com
Hi Kent,

is "better" good enough? Or correct enough? For me it would be helpful if you could confirm that the arrays definitely behave as you would expect.

Thanks a lot,
Ralph

### Kent Johnson

unread,
Jan 9, 2013, 12:21:59 PM1/9/13
to pyrs...@googlegroups.com
Sorry, I didn't mean to be vague. It now behaves the way I expect both on the case I posted to the list and for my real application.

Kent

### Ralph Heinkel

unread,
Jan 10, 2013, 4:29:58 AM1/10/13
to pyrs...@googlegroups.com
Thanks Kent, that's great to hear.
If other users also confirm that their problems have been solved I'll
make an official package ready for download.

ciao ciao

Ralph
Reply all
Reply to author
Forward
0 new messages