# How to convert sagemath matrix to R matrix?

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 close merge delete

Sort by » oldest newest most voted

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

more

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!