ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 02 Dec 2014 00:09:22 -0600Declaring variable to be in a particular field/ring/grouphttp://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/Is it possible to have Sage symbolically simplify expressions involving variables subject to the assumption that the variables take values in a defined domain (field/ring/group/etc)?
The closest I've gotten is to declare a dummy polynomial ring over my domain of interest so that its variable has some notion of the domain, e.g.:
<pre>Z3=Integers(3)
Dummy.<x> = PolynomialRing(Z3)
3*x</pre>
evaluates to "0" as I'd expect, but sage fails to simplify "x^3" to "x", which ISTM should be doable if it really understood that x is a variable in Z/3Z.
Related things I've found in my searches that haven't panned out:<br/>
1. var('x', domain=*foo*) -- apparently *foo* can only be one of real/complex/positive (where I'd like to be able to say 'Z3' in the example above)<br/>
2. assume('x is Z3') - doesn't seem to have any effect.Fri, 28 Nov 2014 10:44:44 -0600http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/Answer by rws for <p>Is it possible to have Sage symbolically simplify expressions involving variables subject to the assumption that the variables take values in a defined domain (field/ring/group/etc)?</p>
<p>The closest I've gotten is to declare a dummy polynomial ring over my domain of interest so that its variable has some notion of the domain, e.g.:</p>
<pre>Z3=Integers(3)
Dummy.<x> = PolynomialRing(Z3)
3*x</pre>
<p>evaluates to "0" as I'd expect, but sage fails to simplify "x^3" to "x", which ISTM should be doable if it really understood that x is a variable in Z/3Z.</p>
<p>Related things I've found in my searches that haven't panned out:<br/>
1. var('x', domain=<em>foo</em>) -- apparently <em>foo</em> can only be one of real/complex/positive (where I'd like to be able to say 'Z3' in the example above)<br/>
2. assume('x is Z3') - doesn't seem to have any effect.</p>
http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/?answer=25052#post-id-25052What you are telling Sage with the code snippet is a polynomial whose coefficients are in Z/3Z, but you want a symbolic expression where the variable `x` is integer and whose values are in Z/3Z, or in other words, modulo 3.
sage: assume(x, "integer")
sage: (x^3).mod(3)
x^3
That would be the most natural way to express it but, as you see, the result is wrong. The Pari-style alternative `Mod` function only accepts numeric input.
Note that with noninteger `x` the result wold not be `x` but `-3*floor(x^3/3)+x^3`.
EDIT:
There is an old ticket at [trac #9935](http://trac.sagemath.org/ticket/9935) that is a prerequisite for such simplifications.Sat, 29 Nov 2014 02:14:20 -0600http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/?answer=25052#post-id-25052Comment by a1846583 for <p>What you are telling Sage with the code snippet is a polynomial whose coefficients are in Z/3Z, but you want a symbolic expression where the variable <code>x</code> is integer and whose values are in Z/3Z, or in other words, modulo 3.</p>
<pre><code>sage: assume(x, "integer")
sage: (x^3).mod(3)
x^3
</code></pre>
<p>That would be the most natural way to express it but, as you see, the result is wrong. The Pari-style alternative <code>Mod</code> function only accepts numeric input.</p>
<p>Note that with noninteger <code>x</code> the result wold not be <code>x</code> but <code>-3*floor(x^3/3)+x^3</code>.</p>
<p>EDIT:
There is an old ticket at <a href="http://trac.sagemath.org/ticket/9935">trac #9935</a> that is a prerequisite for such simplifications.</p>
http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/?comment=25057#post-id-25057Thanks for your answer. I guess there are two issues here:
1) the feature request you point out as #9935 to make mod work with symbolic expressions; and
2) that sage should know that PolynomialRing(Integers(3)) doesn't need to track powers higher than 2 (and generally that PR(I(p)) doesn't need to track powers higher than p-1). Is that natural fallout from #9935 or do you think that deserves its own feature request?
At any rate, it seems the short answer to my original question is "it's not possible (yet)". Unless you can think of a workaround?Sat, 29 Nov 2014 17:32:24 -0600http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/?comment=25057#post-id-25057Comment by rws for <p>What you are telling Sage with the code snippet is a polynomial whose coefficients are in Z/3Z, but you want a symbolic expression where the variable <code>x</code> is integer and whose values are in Z/3Z, or in other words, modulo 3.</p>
<pre><code>sage: assume(x, "integer")
sage: (x^3).mod(3)
x^3
</code></pre>
<p>That would be the most natural way to express it but, as you see, the result is wrong. The Pari-style alternative <code>Mod</code> function only accepts numeric input.</p>
<p>Note that with noninteger <code>x</code> the result wold not be <code>x</code> but <code>-3*floor(x^3/3)+x^3</code>.</p>
<p>EDIT:
There is an old ticket at <a href="http://trac.sagemath.org/ticket/9935">trac #9935</a> that is a prerequisite for such simplifications.</p>
http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/?comment=25058#post-id-25058I think it would be natural to add this logic to #9935, just like special functions get immediately expanded for specific values, depending on said assumptions here.Sun, 30 Nov 2014 08:59:04 -0600http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/?comment=25058#post-id-25058Comment by a1846583 for <p>What you are telling Sage with the code snippet is a polynomial whose coefficients are in Z/3Z, but you want a symbolic expression where the variable <code>x</code> is integer and whose values are in Z/3Z, or in other words, modulo 3.</p>
<pre><code>sage: assume(x, "integer")
sage: (x^3).mod(3)
x^3
</code></pre>
<p>That would be the most natural way to express it but, as you see, the result is wrong. The Pari-style alternative <code>Mod</code> function only accepts numeric input.</p>
<p>Note that with noninteger <code>x</code> the result wold not be <code>x</code> but <code>-3*floor(x^3/3)+x^3</code>.</p>
<p>EDIT:
There is an old ticket at <a href="http://trac.sagemath.org/ticket/9935">trac #9935</a> that is a prerequisite for such simplifications.</p>
http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/?comment=25077#post-id-25077Some more head-smacking revealed I can take a quotient to get what I wanted; see my self-answer above.
Thanks again for your feedback!Tue, 02 Dec 2014 00:09:22 -0600http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/?comment=25077#post-id-25077Answer by a1846583 for <p>Is it possible to have Sage symbolically simplify expressions involving variables subject to the assumption that the variables take values in a defined domain (field/ring/group/etc)?</p>
<p>The closest I've gotten is to declare a dummy polynomial ring over my domain of interest so that its variable has some notion of the domain, e.g.:</p>
<pre>Z3=Integers(3)
Dummy.<x> = PolynomialRing(Z3)
3*x</pre>
<p>evaluates to "0" as I'd expect, but sage fails to simplify "x^3" to "x", which ISTM should be doable if it really understood that x is a variable in Z/3Z.</p>
<p>Related things I've found in my searches that haven't panned out:<br/>
1. var('x', domain=<em>foo</em>) -- apparently <em>foo</em> can only be one of real/complex/positive (where I'd like to be able to say 'Z3' in the example above)<br/>
2. assume('x is Z3') - doesn't seem to have any effect.</p>
http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/?answer=25076#post-id-25076I think I found a workaround: make x not be the variable of the polynomial ring but rather that of the quotient of that ring by the ideal generated by z^2-z. In other words:
<pre>
var('X'); x=PolynomialRing(Integers(3), 'X').quotient([X^2-1], 'x').gens()[0]
print x^3==x and x^2==1 and 3*x==0
</pre>
evals to TrueTue, 02 Dec 2014 00:07:43 -0600http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/?answer=25076#post-id-25076