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.Thu, 07 Jan 2021 18:05:19 +0100How to express boolean polynomials by special formhttps://ask.sagemath.org/question/55168/how-to-express-boolean-polynomials-by-special-form/If I have boolean polynomials like
x_1 * x_2 + x_1*x_3* x_5 + x_3 + x_7*x_2
then given a variable `x_1`, how can I represent it as
x_1 * (x_2 + x_3*x_5) + (x_3 + x_7*x_2)
using Sage?
In general, I want to find a way to represent a form of `a * x_1 + c`,
where `a` is the linear combination of multiples of `x_1`
and `c` is the rest polynomial.Thu, 07 Jan 2021 14:33:39 +0100https://ask.sagemath.org/question/55168/how-to-express-boolean-polynomials-by-special-form/Comment by slelievre for <p>If I have boolean polynomials like</p>
<pre><code>x_1 * x_2 + x_1*x_3* x_5 + x_3 + x_7*x_2
</code></pre>
<p>then given a variable <code>x_1</code>, how can I represent it as</p>
<pre><code>x_1 * (x_2 + x_3*x_5) + (x_3 + x_7*x_2)
</code></pre>
<p>using Sage?</p>
<p>In general, I want to find a way to represent a form of <code>a * x_1 + c</code>,
where <code>a</code> is the linear combination of multiples of <code>x_1</code>
and <code>c</code> is the rest polynomial.</p>
https://ask.sagemath.org/question/55168/how-to-express-boolean-polynomials-by-special-form/?comment=55174#post-id-55174Welcome to Ask Sage!
Thank you for your question!Thu, 07 Jan 2021 18:05:19 +0100https://ask.sagemath.org/question/55168/how-to-express-boolean-polynomials-by-special-form/?comment=55174#post-id-55174Answer by slelievre for <p>If I have boolean polynomials like</p>
<pre><code>x_1 * x_2 + x_1*x_3* x_5 + x_3 + x_7*x_2
</code></pre>
<p>then given a variable <code>x_1</code>, how can I represent it as</p>
<pre><code>x_1 * (x_2 + x_3*x_5) + (x_3 + x_7*x_2)
</code></pre>
<p>using Sage?</p>
<p>In general, I want to find a way to represent a form of <code>a * x_1 + c</code>,
where <code>a</code> is the linear combination of multiples of <code>x_1</code>
and <code>c</code> is the rest polynomial.</p>
https://ask.sagemath.org/question/55168/how-to-express-boolean-polynomials-by-special-form/?answer=55173#post-id-55173There is a dedicated method for that for polynomials other than boolean in Sage.
Sadly it is not available for boolean polynomials, for a silly reason:
they lack a `dict` method that would return a dictionary whose
keys would be monomials and values would be coefficients.
I opened [Sage Trac ticket 31198](https://trac.sagemath.org/ticket/31198) for that.
The workaround is to write a small function to achieve that.
def as_poly(p, z):
r"""
Print `p` as a polynomial in `z`
"""
a = p / z
c = p - a*z
print(f'({a})*{z} + ({c})')
Usage:
sage: P = BooleanPolynomialRing(8, 'x')
sage: x = P.gens()
sage: p = x[1] * x[2] + x[1] * x[3] * x[5] + x[3] + x[7] * x[2]
sage: p
x1*x2 + x1*x3*x5 + x2*x7 + x3
sage: as_poly(p, x[1])
(x2 + x3*x5)*x1 + (x2*x7 + x3)
Thu, 07 Jan 2021 18:04:55 +0100https://ask.sagemath.org/question/55168/how-to-express-boolean-polynomials-by-special-form/?answer=55173#post-id-55173