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.Wed, 23 Feb 2022 17:44:47 +0100Evaluating a `BooleanPolynomial`https://ask.sagemath.org/question/61263/evaluating-a-booleanpolynomial/When we cannot determine which variable will be determined by a constant,
I use a list to store variables, but the `BooleanPolynomialRing` does not
support change the `BooleanPolynomial` by variables in list.
It seems that it can only be changed by using the variable name;
but when we encounter this situation, is there another way
to solve this problem?
sage: from sage.crypto.boolean_function import BooleanFunction
sage: R = BooleanPolynomialRing(4, "a, b, c, d")
sage: R.inject_variables()
Defining a, b, c, d
sage: S = R.gens()
sage: print(S)
(a, b, c, d)
sage: f = a*b + b*c
sage: print(f(a=1))
b*c + b
sage: print(f(S[0]=1))
File "<ipython-input-9-504c2572fec0>", line 1
print(f(S[Integer(0)]=Integer(1)))
^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?Wed, 23 Feb 2022 03:06:36 +0100https://ask.sagemath.org/question/61263/evaluating-a-booleanpolynomial/Answer by John Palmieri for <p>When we cannot determine which variable will be determined by a constant,
I use a list to store variables, but the <code>BooleanPolynomialRing</code> does not
support change the <code>BooleanPolynomial</code> by variables in list.</p>
<p>It seems that it can only be changed by using the variable name;
but when we encounter this situation, is there another way
to solve this problem?</p>
<pre><code>sage: from sage.crypto.boolean_function import BooleanFunction
sage: R = BooleanPolynomialRing(4, "a, b, c, d")
sage: R.inject_variables()
Defining a, b, c, d
sage: S = R.gens()
sage: print(S)
(a, b, c, d)
sage: f = a*b + b*c
sage: print(f(a=1))
b*c + b
sage: print(f(S[0]=1))
File "<ipython-input-9-504c2572fec0>", line 1
print(f(S[Integer(0)]=Integer(1)))
^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
</code></pre>
https://ask.sagemath.org/question/61263/evaluating-a-booleanpolynomial/?answer=61269#post-id-61269You can use the `substitute` (or `subs`) method. I like using a Python `dict` as its argument:
sage: R.<a,b,c,d> = BooleanPolynomialRing()
sage: S = R.gens()
sage: f = a*b + b*c
sage: f.subs({a:1})
b*c + b
sage: f.subs({S[0]:1})
b*c + b
sage: f.subs({S[0]:1, S[2]:a})
a*b + b
Wed, 23 Feb 2022 17:44:47 +0100https://ask.sagemath.org/question/61263/evaluating-a-booleanpolynomial/?answer=61269#post-id-61269Answer by minionsss for <p>When we cannot determine which variable will be determined by a constant,
I use a list to store variables, but the <code>BooleanPolynomialRing</code> does not
support change the <code>BooleanPolynomial</code> by variables in list.</p>
<p>It seems that it can only be changed by using the variable name;
but when we encounter this situation, is there another way
to solve this problem?</p>
<pre><code>sage: from sage.crypto.boolean_function import BooleanFunction
sage: R = BooleanPolynomialRing(4, "a, b, c, d")
sage: R.inject_variables()
Defining a, b, c, d
sage: S = R.gens()
sage: print(S)
(a, b, c, d)
sage: f = a*b + b*c
sage: print(f(a=1))
b*c + b
sage: print(f(S[0]=1))
File "<ipython-input-9-504c2572fec0>", line 1
print(f(S[Integer(0)]=Integer(1)))
^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
</code></pre>
https://ask.sagemath.org/question/61263/evaluating-a-booleanpolynomial/?answer=61264#post-id-61264use
f(*S)
it can be solvedWed, 23 Feb 2022 03:17:49 +0100https://ask.sagemath.org/question/61263/evaluating-a-booleanpolynomial/?answer=61264#post-id-61264