-1

I have a sage list of list of list that is the result of some function applied over an R list of list of list (converted to sage format)

The code goes like this:

%r
my_r_list<-load("my_r_list.RData")

for d in range(D):
    for s in range(S):
        for c in range(C):
            my_sage_list[d][s][c] = ("my_r_list")[[d+1]][[s+1]][[c+1]]._sage_()
            output_my_sage_list[d][s][c] = some_function(my_sage_list[d][s][c])

I would like to convert output_my_sage_list back into an R list so as to save it into an .RData file

Documentation on interface Sage with R is mainly about going from R to Sage but not the other way around.This question concerns converting a sagemath matrix to R matrix using r.matrix()' so i tried using 'r.list()' but with no luck. I also tried the simpleoutput_my_r_list = r(output_my_sage_list)which gives no error but the output is not correct as doingoutput_my_r_list[0][0]` doesn't give the last level of list but directly some values.

A reproducible code (bypassing the R to Sage part) would be:

    output_d = [[] for _ in range(9)]
    for d in range(9):
        output_s = [[] for _ in range(4)]
        output_d[d] = output_s
        for s in range(4):
            output_c = [[] for _ in range(2)]
            output_s[s] = output_c 
            for k in range(2):
                res = random()
                output_c[k] = res

I would like to convert output_d to an R list

1 Answers1

0

The key here is your statement

I also tried the simple output_my_r_list = r(output_my_sage_list) which gives no error but the output is not correct as doing output_my_r_list[0][0] doesn't give the last level of list but directly some values.

Sage actually documents that it will make the list just one concatenated list, or more properly an R vector:

    sage: x = r([10.4,5.6,3.1,6.4,21.7]); x
    [1] 10.4  5.6  3.1  6.4 21.7

The following assignment creates a vector $y$ with 11 entries which
consists of two copies of $x$ with a 0 in between::

    sage: y = r([x,0,x]); y
    [1] 10.4  5.6  3.1  6.4 21.7  0.0 10.4  5.6  3.1  6.4 21.7

This is directly modeled upon R usage, and presumably (via some nesting procedure) even just comes from it:

The further assignment

> y <- c(x, 0, x)

would create a vector y with 11 entries consisting of two copies of 
with a zero in the middle place.

So your beef is not exactly with Sage, but R, as (somehow) Sage gets the 'right' thing here. Sage does this because here it says

def _left_list_delim(self):
    return "c("

and then the R behavior follows as this is done recursively and it makes a vector of vectors.

A hack around this would be to return "list(" there. Another hack would be to try to use r.eval("list(something)") but I didn't experience a lot of success with this.

It would be nice to find a way to deal with this more easily, of course, and in the return direction Sage attempts to take nested R stuff and keep it nested (with what success, I don't know).

kcrisman
  • 4,374
  • 20
  • 41