Question

I am trying to write a sage function.

In the following, code block B uses variables such as x0,x1,x2,x3. Im trying to generalize code block B as code block A. res[1] in code block A is a list which contains the four variables. However, on executing, I get the following error:

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

Is there any way I can make the code block accept the list elements?

Note: degreeAndMonomialsCalculate() is just a function which returns degree of a function in res[0] and its unique monomials(variables) in res[1]

Code Block A

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

Code Block 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
Was it helpful?

Solution

Usually it's typical to have a "minimal working example", so that we don't need your extra functions.

There are two problems here. First, even if the error you got was avoided, you'd get this one.

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

This is because Sage preparses the invalid Python A.<res> ... into something else that is valid.

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

This also causes your issue:

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.

and I don't see an easy way around it with this syntax. However,

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

seems like it should do the job. See

sage: BooleanPolynomialRing?

for more info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top