【问题标题】:Accepting variables in sage BooleanPolynomialRing在 sage BooleanPolynomialRing 中接受变量
【发布时间】:2013-10-02 13:05:02
【问题描述】:

我正在尝试编写一个 sage 函数。

以下代码块 B 使用 x0,x1,x2,x3 等变量。我试图将代码块 B 概括为代码块 A。代码块 A 中的 res[1] 是一个包含四个变量的列表。但是,在执行时,我收到以下错误:

**ValueError: variable names must be alphanumeric, but one is 'res[_sage_const_1 ]' which is not.** 

有什么方法可以让代码块接受列表元素?

注意:degreeAndMonomialsCalculate() 只是一个函数,它在 res[0] 中返回函数的度数,在 res[1] 中返回其唯一的单项式(变量)

代码块 A

def annihilatorReturn(function):
    res=degreeAndMonomialsCalculate(function)
    A.<res[1]>=BooleanPolynomialRing(len(res[1]))
    X=BooleanFunction(function)
    B=X.annihilator(res[0])
    return B

代码块 B

def annihilatorReturn():
    A.<x0,x1,x2,x3>=BooleanPolynomialRing(4)
    Y=x0*x1*x2+x2*x1+x2*x3+x3*x1
    X=BooleanFunction(Y)
    B=X.annihilator(3)
    return B

【问题讨论】:

    标签: python sage


    【解决方案1】:

    通常有一个“最小的工作示例”,这样我们就不需要你的额外功能了。

    这里有两个问题。首先,即使你得到的错误被避免了,你也会得到这个。

    def test():
        res = ['x1','x2']
        A.<res> = BooleanPolynomialRing(len(res))
        return A
    ....: 
    sage: test()
    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
    <snip>
    IndexError: the number of names must equal the number of generators
    

    这是因为 Sage 预解析无效的 Python A.&lt;res&gt; ... 成其他有效的东西。

    sage: preparse("A.<res> = BooleanPolynomialRing(len(res))")
    "A = BooleanPolynomialRing(len(res), names=('res',)); (res,) = A._first_ngens(1)"
    

    这也会导致您的问题:

    sage: res = ['x','y']
    sage: A.<res> = BooleanPolynomialRing(len(res))
    <snip>
    ValueError: variable names must be alphanumeric, but one is 'res[Integer(1)]' which is not.
    

    而且我没有看到使用这种语法的简单方法。然而,

    sage: res = [2,['x','y']]
    sage: A = BooleanPolynomialRing(names=res[1])
    sage: A
    Boolean PolynomialRing in x, y
    

    似乎它应该完成这项工作。见

    sage: BooleanPolynomialRing?
    

    了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      相关资源
      最近更新 更多