# subs() function gives KeyError when keyword is a list member This post is a wiki. Anyone with karma >750 is welcome to improve it.

I have a multivariate Boolean polynomial my_poly in xi (x0,x1,...,etc) and want to substitute one of the variables (say e.g. x0=0). my_poly subs(x0=0) works but I need to determine the exact variable (left hand side of '=') and value (right hand side of '=') at run-time depending on some conditions. The problem is subs() function do not accept expression on the left hand side of '=' and I have many xi variables, so if else is not practical. How can I solve this issue?

edit retag close merge delete

Sort by » oldest newest most voted

You can use Python dictionaries in the substitute method, for example:

sage: R = BooleanPolynomialRing(5,'x') ; R
Boolean PolynomialRing in x0, x1, x2, x3, x4
sage: R.inject_variables()
Defining x0, x1, x2, x3, x4
sage: P = R.random_element() ; P
x0*x1 + x0*x4 + x0 + x1*x2 + x1*x4

sage: a = x0
sage: mydict = {a:0} ; mydict
{x0: 0}
sage: P.substitute(mydict)
x1*x2 + x1*x4

more