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.Tue, 28 Apr 2015 15:41:16 +0200Sage symbolic expression modulohttps://ask.sagemath.org/question/26666/sage-symbolic-expression-modulo/Hi Guys,
I got a symbolic expression in sage that looks like this:
2*s_0_0 + s_0_4 + 3
Can i somehow calculate this modulo 2? So the output would be:
s_0_4 + 1
Any ideas? :)
Cheers!
Tue, 28 Apr 2015 13:51:49 +0200https://ask.sagemath.org/question/26666/sage-symbolic-expression-modulo/Comment by kcrisman for <p>Hi Guys,</p>
<p>I got a symbolic expression in sage that looks like this: </p>
<pre><code>2*s_0_0 + s_0_4 + 3
</code></pre>
<p>Can i somehow calculate this modulo 2? So the output would be:</p>
<pre><code>s_0_4 + 1
</code></pre>
<p>Any ideas? :)
Cheers!</p>
https://ask.sagemath.org/question/26666/sage-symbolic-expression-modulo/?comment=26667#post-id-26667Instead of symbolic expressions, you may wish to use polynomials over the field with two elements.Tue, 28 Apr 2015 14:19:28 +0200https://ask.sagemath.org/question/26666/sage-symbolic-expression-modulo/?comment=26667#post-id-26667Answer by slelievre for <p>Hi Guys,</p>
<p>I got a symbolic expression in sage that looks like this: </p>
<pre><code>2*s_0_0 + s_0_4 + 3
</code></pre>
<p>Can i somehow calculate this modulo 2? So the output would be:</p>
<pre><code>s_0_4 + 1
</code></pre>
<p>Any ideas? :)
Cheers!</p>
https://ask.sagemath.org/question/26666/sage-symbolic-expression-modulo/?answer=26669#post-id-26669Here is a way to follow @kcrisman's advice to move to polynomials over the field with two elements.
First define the expression from your question:
sage: s_0_0, s_0_4 = var('s_0_0 s_0_4')
sage: 2*s_0_0 + s_0_4 + 3
2*s_0_0 + s_0_4 + 3
sage: a = 2*s_0_0 + s_0_4 + 3
sage: a
2*s_0_0 + s_0_4 + 3
This expression lives in Sage's symbolic ring:
sage: a.parent()
Symbolic Ring
Turn it into a polynomial over GF(2) (the finite field with two elements):
sage: a.polynomial(GF(2))
s_0_4 + 1
Note that we could also define a polynomial ring:
sage: R = PolynomialRing(GF(2), names='s_0_0, s_0_4')
sage: R
Multivariate Polynomial Ring in s_0_0, s_0_4 over Finite Field of size 2
and force `a` into `R`:
sage: R(a)
s_0_4 + 1
Tue, 28 Apr 2015 15:35:54 +0200https://ask.sagemath.org/question/26666/sage-symbolic-expression-modulo/?answer=26669#post-id-26669Comment by nablahero for <p>Here is a way to follow <a href="/users/41/kcrisman/">@kcrisman</a>'s advice to move to polynomials over the field with two elements.</p>
<p>First define the expression from your question:</p>
<pre><code>sage: s_0_0, s_0_4 = var('s_0_0 s_0_4')
sage: 2*s_0_0 + s_0_4 + 3
2*s_0_0 + s_0_4 + 3
sage: a = 2*s_0_0 + s_0_4 + 3
sage: a
2*s_0_0 + s_0_4 + 3
</code></pre>
<p>This expression lives in Sage's symbolic ring:</p>
<pre><code>sage: a.parent()
Symbolic Ring
</code></pre>
<p>Turn it into a polynomial over GF(2) (the finite field with two elements):</p>
<pre><code>sage: a.polynomial(GF(2))
s_0_4 + 1
</code></pre>
<p>Note that we could also define a polynomial ring:</p>
<pre><code>sage: R = PolynomialRing(GF(2), names='s_0_0, s_0_4')
sage: R
Multivariate Polynomial Ring in s_0_0, s_0_4 over Finite Field of size 2
</code></pre>
<p>and force <code>a</code> into <code>R</code>:</p>
<pre><code>sage: R(a)
s_0_4 + 1
</code></pre>
https://ask.sagemath.org/question/26666/sage-symbolic-expression-modulo/?comment=26670#post-id-26670Thanks. I used yours and @kcrisman's advice and went for a PolynomialRing!
Cheers!Tue, 28 Apr 2015 15:41:16 +0200https://ask.sagemath.org/question/26666/sage-symbolic-expression-modulo/?comment=26670#post-id-26670