ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 05 Feb 2016 15:51:01 +0100subs() function gives KeyError when keyword is a list memberhttps://ask.sagemath.org/question/32426/subs-function-gives-keyerror-when-keyword-is-a-list-member/ 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?Wed, 03 Feb 2016 10:25:06 +0100https://ask.sagemath.org/question/32426/subs-function-gives-keyerror-when-keyword-is-a-list-member/Answer by tmonteil for <p>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?</p>
https://ask.sagemath.org/question/32426/subs-function-gives-keyerror-when-keyword-is-a-list-member/?answer=32427#post-id-32427You 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
Wed, 03 Feb 2016 11:39:39 +0100https://ask.sagemath.org/question/32426/subs-function-gives-keyerror-when-keyword-is-a-list-member/?answer=32427#post-id-32427Comment by adnanbaysal for <p>You can use Python dictionaries in the substitute method, for example:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/32426/subs-function-gives-keyerror-when-keyword-is-a-list-member/?comment=32464#post-id-32464Thank you. This solved my problem.Fri, 05 Feb 2016 15:51:01 +0100https://ask.sagemath.org/question/32426/subs-function-gives-keyerror-when-keyword-is-a-list-member/?comment=32464#post-id-32464