【问题标题】:sagemath: convert R Element to floatsagemath:将 R 元素转换为浮点数
【发布时间】:2018-03-29 06:36:16
【问题描述】:

如何提取 R 元素的数字部分?

我在 sagemath 中使用 R 接口。我想获得浮点或整数值。

我有一个 R 元素,它是 sage.interfaces.r.RElement 类的一个对象,它包含一个我想提取的值。

例如,给定以下元素:

 x = [1] 6.5

如何将 6.5 值提取为浮点数?

float(x) 

不工作。

float(str(x).split()[1])

看起来过于粗糙了。

对于任何对生成示例 R 元素感兴趣的人,我可以在 a sagemathcell 使用以下代码完成:

aList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
fences = r.quantile(aList)
x = fences[[3]]
print (type(x))
print (x)

【问题讨论】:

    标签: python r sage


    【解决方案1】:

    您可以使用x.sage()x 转换回 Sage 中的浮点数。

    如果x 被定义为问题中的:

    sage: s = x.sage()
    sage: s
    6.5
    sage: type(s)
    <type 'float'>
    

    【讨论】:

    • 谢谢。这样可行。它似乎在其他情况下可能有用。
    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多