Ask Your Question
0

How to convert sagemath matrix to R matrix?

asked 2012-10-17 17:17:52 -0500

b_ron gravatar image

I have a 5 x 30 matrix, G, created in sagemath. The type is

type 'sage.matrix.matrix_generic_dense.Matrix_generic_dense'.

I want to convert this matrix to a R matrix to perform various manipulations using the R interface.

How can I use sagemath matrix, G, to create a matrix in the R interface?

I tried the following but to no avail.

r.matrix(G)

r.matrix(G,5,30)

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
4

answered 2012-10-17 17:51:23 -0500

calc314 gravatar image

It looks like you have to put the matrix into a list representing a column vector. Then, you can use the r.matrix command. For example:

m=matrix(3,4,[1,2,3,4,5,6,7,8,9,10,11,12])
r.matrix(m.transpose().list(),nrow=3)

gives the following matrix in R:

     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
edit flag offensive delete link more

Comments

Ah. I see. That makes sense. By converting the sagemath matrix into a list using its list method, r.matrix() accepts the list as input for the data argument. Also, for others who made read this comment, r.matrix(m.list(), nrow=3, byrow="False") or r.matrix(m.list(),ncol=4,byrow="False") yield the same R matrix as the one above. Thank you very much, calc314!

b_ron gravatar imageb_ron ( 2012-10-17 18:47:12 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-10-17 17:17:52 -0500

Seen: 416 times

Last updated: Oct 17 '12