【问题标题】:Convert Sage List of list of list to R list of list of list将列表列表的 Sage 列表转换为列表列表的 R 列表
【发布时间】:2016-07-04 15:32:00
【问题描述】:

我有一个 sage 列表列表,它是对列表列表的 R 列表应用某些函数的结果(转换为 sage 格式)

代码如下:

%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])

我想将 output_my_sage_list 转换回 R 列表,以便将其保存到 .RData 文件中

关于 Sage 与 R 接口的文档主要是关于从 R 到 Sage,但不是相反。This question 涉及使用 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[ 将 sagemath 矩阵转换为 R 矩阵0][0]` 没有给出最后一级的列表,而是直接给出一些值。

可重现的代码(绕过 R 到 Sage 部分)将是:

    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

我想将output_d 转换为 R 列表

【问题讨论】:

    标签: r list sage rdata


    【解决方案1】:

    这里的关键是你的陈述

    我还尝试了简单的output_my_r_list = r(output_my_sage_list),它没有给出错误,但输出不正确,因为output_my_r_list[0][0] 没有给出最后一级的列表,而是直接给出了一些值。

    Sage 实际上记录了它将使列表只是一个连接列表,或者更准确地说是一个 R 向量

        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
    

    这直接是modeled upon R usage,并且可能(通过一些嵌套程序)甚至来自它:

    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.
    

    所以你的牛肉不完全是 Sage,而是 R,因为(不知何故)Sage 在这里得到了“正确”的东西。 Sage 这样做是因为 here 它说

    def _left_list_delim(self):
        return "c("
    

    然后 R 行为随之而来,因为这是递归完成的,它生成了一个向量向量。

    解决这个问题的方法是在那里返回"list("。另一个技巧是尝试使用r.eval("list(something)"),但我并没有成功。

    当然,如果能找到一种更轻松地处理这个问题的方法,并且在返回方向 Sage attempts to take nested R stuff and keep it nested 上(取得什么成功,我不知道),那就太好了。

    【讨论】:

      猜你喜欢
      • 2016-11-30
      • 1970-01-01
      • 2022-09-27
      • 2018-12-19
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多