ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 20 Apr 2012 05:02:38 -0500Members of a Polynomial Ring don't respond to solvehttps://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/ R = PolynomialRing(RR,['a','b','c','d'])
S = matrix(R,2,R.gens())
S
yields $$\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr}
a & b \\
c & d
\end{array}\right)$$ as expected, but then solve does not respond -
solve([S[0][0]==S[0][1],S[1][0]==S[1][1]],[a,b])
outputs
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_13.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("c29sdmUoW1NbMF1bMF09PVNbMF1bMV0sU1sxXVswXT09U1sxXVsxXV0sW2EsYl0p"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpBgHnJ8/___code___.py", line 3, in <module>
exec compile(u'solve([S[_sage_const_0 ][_sage_const_0 ]==S[_sage_const_0 ][_sage_const_1 ],S[_sage_const_1 ][_sage_const_0 ]==S[_sage_const_1 ][_sage_const_1 ]],[a,b])
File "", line 1, in <module>
NameError: name 'a' is not defined
But all the same, the `.subs()` command works - as the elements of the Symbolic Ring, produced by `var('x')`.
Is there a substitute to `solve` for the PolynomialRing? Thu, 19 Apr 2012 06:35:55 -0500https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/Answer by Volker Braun for <pre><code>R = PolynomialRing(RR,['a','b','c','d'])
S = matrix(R,2,R.gens())
S
</code></pre>
<p>yields $$\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr}
a & b \
c & d
\end{array}\right)$$ as expected, but then solve does not respond - </p>
<pre><code>solve([S[0][0]==S[0][1],S[1][0]==S[1][1]],[a,b])
</code></pre>
<p>outputs</p>
<pre><code>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_13.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("c29sdmUoW1NbMF1bMF09PVNbMF1bMV0sU1sxXVswXT09U1sxXVsxXV0sW2EsYl0p"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpBgHnJ8/___code___.py", line 3, in <module>
exec compile(u'solve([S[_sage_const_0 ][_sage_const_0 ]==S[_sage_const_0 ][_sage_const_1 ],S[_sage_const_1 ][_sage_const_0 ]==S[_sage_const_1 ][_sage_const_1 ]],[a,b])
File "", line 1, in <module>
NameError: name 'a' is not defined
</code></pre>
<p>But all the same, the <code>.subs()</code> command works - as the elements of the Symbolic Ring, produced by <code>var('x')</code>.
Is there a substitute to <code>solve</code> for the PolynomialRing? </p>
https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?answer=13485#post-id-13485First, polynomial rings don't automatically inject their variables in the global name space. You can do this
sage: R = PolynomialRing(RR,['a','b','c','d'])
sage: R.inject_variables()
Defining a, b, c, d
Or use the following short-hand notation to declare the polynomial ring **and** inject the variables at the same time:
sage: R.<a,b,c,d> = RR[]
Second, you can't mix the symbolic solver with polynomial algebra. To "solve" (what does that even mean since there is no formula for the roots of polynomials of degree > 5) polynomial equations you should rephrase your question in terms of ideals and term orders.Fri, 20 Apr 2012 03:04:43 -0500https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?answer=13485#post-id-13485Comment by kcrisman for <p>First, polynomial rings don't automatically inject their variables in the global name space. You can do this</p>
<pre><code>sage: R = PolynomialRing(RR,['a','b','c','d'])
sage: R.inject_variables()
Defining a, b, c, d
</code></pre>
<p>Or use the following short-hand notation to declare the polynomial ring <strong>and</strong> inject the variables at the same time:</p>
<pre><code>sage: R.<a,b,c,d> = RR[]
</code></pre>
<p>Second, you can't mix the symbolic solver with polynomial algebra. To "solve" (what does that even mean since there is no formula for the roots of polynomials of degree > 5) polynomial equations you should rephrase your question in terms of ideals and term orders.</p>
https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?comment=19880#post-id-19880Nice, I should have known about `inject_variables`. I didn't want to use the `[]` notation since the OP didn't use that, but you are right that this is the optimal solution.Fri, 20 Apr 2012 05:02:38 -0500https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?comment=19880#post-id-19880Answer by kcrisman for <pre><code>R = PolynomialRing(RR,['a','b','c','d'])
S = matrix(R,2,R.gens())
S
</code></pre>
<p>yields $$\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr}
a & b \
c & d
\end{array}\right)$$ as expected, but then solve does not respond - </p>
<pre><code>solve([S[0][0]==S[0][1],S[1][0]==S[1][1]],[a,b])
</code></pre>
<p>outputs</p>
<pre><code>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_13.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("c29sdmUoW1NbMF1bMF09PVNbMF1bMV0sU1sxXVswXT09U1sxXVsxXV0sW2EsYl0p"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpBgHnJ8/___code___.py", line 3, in <module>
exec compile(u'solve([S[_sage_const_0 ][_sage_const_0 ]==S[_sage_const_0 ][_sage_const_1 ],S[_sage_const_1 ][_sage_const_0 ]==S[_sage_const_1 ][_sage_const_1 ]],[a,b])
File "", line 1, in <module>
NameError: name 'a' is not defined
</code></pre>
<p>But all the same, the <code>.subs()</code> command works - as the elements of the Symbolic Ring, produced by <code>var('x')</code>.
Is there a substitute to <code>solve</code> for the PolynomialRing? </p>
https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?answer=13480#post-id-13480As far as I can tell, this error message has nothing to do with solve. (See below for that issue, though.)
sage: R = PolynomialRing(RR,['a','b','c','d'])
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: a
---------------------------------------------------------------------------
NameError: name 'a' is not defined
If you do `PolynomialRing?` (see also [the official doc](http://www.sagemath.org/doc/reference/sage/rings/polynomial/polynomial_ring_constructor.html#sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing)) then you'll see
Use the diamond brackets notation to make the variable ready for
use after you define the ring:
So it looks like instead that this is just unsupported behavior that you are trying to do. I'm not sure exactly *why* it's not supported, but it's not.
sage: PolynomialRing(QQ, 'w')
Univariate Polynomial Ring in w over Rational Field
sage: w
---------------------------------------------------------------------------
NameError: name 'w' is not defined
sage: sage: R.<w> = PolynomialRing(QQ)
sage: w
w
Now, we still run into problems even after we do the "right" thing.
sage: R.<a,b,c,d> = PolynomialRing(RR)
sage: R
Multivariate Polynomial Ring in a, b, c, d over Real Field with 53 bits of precision
sage: a
a
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: solve([S[0][0]==S[0][1],S[1][0]==S[1][1]],[a,b])
---------------------------------------------------------------------------
TypeError: a is not a valid variable.
This is a workaround:
sage: solve([SR(S[0][0]==S[0][1]),SR(S[1][0]==S[1][1])],[SR(a),SR(b)])
[[a == r2, b == r1]]
But the code in the error above pretty clearly shows that we need symbolic variables. To make this code more concise for more complicated situations, you could do
sage: list1 = [S[0][0]==S[0][1],S[1][0]==S[1][1]]
sage: list2 = [a,b]
sage: solve( [ SR(w) for w in list1], [ SR(z) for z in list2])
[[a == r4, b == r3]]
and that preserves the type of `a` and friends...Thu, 19 Apr 2012 09:12:26 -0500https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?answer=13480#post-id-13480Comment by d3banjan for <p>As far as I can tell, this error message has nothing to do with solve. (See below for that issue, though.)</p>
<pre><code>sage: R = PolynomialRing(RR,['a','b','c','d'])
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: a
---------------------------------------------------------------------------
NameError: name 'a' is not defined
</code></pre>
<p>If you do <code>PolynomialRing?</code> (see also <a href="http://www.sagemath.org/doc/reference/sage/rings/polynomial/polynomial_ring_constructor.html#sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing">the official doc</a>) then you'll see</p>
<pre><code> Use the diamond brackets notation to make the variable ready for
use after you define the ring:
</code></pre>
<p>So it looks like instead that this is just unsupported behavior that you are trying to do. I'm not sure exactly <em>why</em> it's not supported, but it's not.</p>
<pre><code>sage: PolynomialRing(QQ, 'w')
Univariate Polynomial Ring in w over Rational Field
sage: w
---------------------------------------------------------------------------
NameError: name 'w' is not defined
sage: sage: R.<w> = PolynomialRing(QQ)
sage: w
w
</code></pre>
<p>Now, we still run into problems even after we do the "right" thing.</p>
<pre><code>sage: R.<a,b,c,d> = PolynomialRing(RR)
sage: R
Multivariate Polynomial Ring in a, b, c, d over Real Field with 53 bits of precision
sage: a
a
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: solve([S[0][0]==S[0][1],S[1][0]==S[1][1]],[a,b])
---------------------------------------------------------------------------
TypeError: a is not a valid variable.
</code></pre>
<p>This is a workaround:</p>
<pre><code>sage: solve([SR(S[0][0]==S[0][1]),SR(S[1][0]==S[1][1])],[SR(a),SR(b)])
[[a == r2, b == r1]]
</code></pre>
<p>But the code in the error above pretty clearly shows that we need symbolic variables. To make this code more concise for more complicated situations, you could do</p>
<pre><code>sage: list1 = [S[0][0]==S[0][1],S[1][0]==S[1][1]]
sage: list2 = [a,b]
sage: solve( [ SR(w) for w in list1], [ SR(z) for z in list2])
[[a == r4, b == r3]]
</code></pre>
<p>and that preserves the type of <code>a</code> and friends...</p>
https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?comment=19887#post-id-19887@kcrisman - also could you please cite the docpage that discusses this bit? I have been facing problems with this for quite a while now!Thu, 19 Apr 2012 09:29:29 -0500https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?comment=19887#post-id-19887Comment by kcrisman for <p>As far as I can tell, this error message has nothing to do with solve. (See below for that issue, though.)</p>
<pre><code>sage: R = PolynomialRing(RR,['a','b','c','d'])
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: a
---------------------------------------------------------------------------
NameError: name 'a' is not defined
</code></pre>
<p>If you do <code>PolynomialRing?</code> (see also <a href="http://www.sagemath.org/doc/reference/sage/rings/polynomial/polynomial_ring_constructor.html#sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing">the official doc</a>) then you'll see</p>
<pre><code> Use the diamond brackets notation to make the variable ready for
use after you define the ring:
</code></pre>
<p>So it looks like instead that this is just unsupported behavior that you are trying to do. I'm not sure exactly <em>why</em> it's not supported, but it's not.</p>
<pre><code>sage: PolynomialRing(QQ, 'w')
Univariate Polynomial Ring in w over Rational Field
sage: w
---------------------------------------------------------------------------
NameError: name 'w' is not defined
sage: sage: R.<w> = PolynomialRing(QQ)
sage: w
w
</code></pre>
<p>Now, we still run into problems even after we do the "right" thing.</p>
<pre><code>sage: R.<a,b,c,d> = PolynomialRing(RR)
sage: R
Multivariate Polynomial Ring in a, b, c, d over Real Field with 53 bits of precision
sage: a
a
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: solve([S[0][0]==S[0][1],S[1][0]==S[1][1]],[a,b])
---------------------------------------------------------------------------
TypeError: a is not a valid variable.
</code></pre>
<p>This is a workaround:</p>
<pre><code>sage: solve([SR(S[0][0]==S[0][1]),SR(S[1][0]==S[1][1])],[SR(a),SR(b)])
[[a == r2, b == r1]]
</code></pre>
<p>But the code in the error above pretty clearly shows that we need symbolic variables. To make this code more concise for more complicated situations, you could do</p>
<pre><code>sage: list1 = [S[0][0]==S[0][1],S[1][0]==S[1][1]]
sage: list2 = [a,b]
sage: solve( [ SR(w) for w in list1], [ SR(z) for z in list2])
[[a == r4, b == r3]]
</code></pre>
<p>and that preserves the type of <code>a</code> and friends...</p>
https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?comment=19884#post-id-19884I'm not sure I understand your first question about "binary digits". It is true that if `a` is a symbolic variable (not a polynomial ring variable), then `a==0` is a symbolic expression that's treated like an equation in some ways.Thu, 19 Apr 2012 16:06:56 -0500https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?comment=19884#post-id-19884Comment by kcrisman for <p>As far as I can tell, this error message has nothing to do with solve. (See below for that issue, though.)</p>
<pre><code>sage: R = PolynomialRing(RR,['a','b','c','d'])
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: a
---------------------------------------------------------------------------
NameError: name 'a' is not defined
</code></pre>
<p>If you do <code>PolynomialRing?</code> (see also <a href="http://www.sagemath.org/doc/reference/sage/rings/polynomial/polynomial_ring_constructor.html#sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing">the official doc</a>) then you'll see</p>
<pre><code> Use the diamond brackets notation to make the variable ready for
use after you define the ring:
</code></pre>
<p>So it looks like instead that this is just unsupported behavior that you are trying to do. I'm not sure exactly <em>why</em> it's not supported, but it's not.</p>
<pre><code>sage: PolynomialRing(QQ, 'w')
Univariate Polynomial Ring in w over Rational Field
sage: w
---------------------------------------------------------------------------
NameError: name 'w' is not defined
sage: sage: R.<w> = PolynomialRing(QQ)
sage: w
w
</code></pre>
<p>Now, we still run into problems even after we do the "right" thing.</p>
<pre><code>sage: R.<a,b,c,d> = PolynomialRing(RR)
sage: R
Multivariate Polynomial Ring in a, b, c, d over Real Field with 53 bits of precision
sage: a
a
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: solve([S[0][0]==S[0][1],S[1][0]==S[1][1]],[a,b])
---------------------------------------------------------------------------
TypeError: a is not a valid variable.
</code></pre>
<p>This is a workaround:</p>
<pre><code>sage: solve([SR(S[0][0]==S[0][1]),SR(S[1][0]==S[1][1])],[SR(a),SR(b)])
[[a == r2, b == r1]]
</code></pre>
<p>But the code in the error above pretty clearly shows that we need symbolic variables. To make this code more concise for more complicated situations, you could do</p>
<pre><code>sage: list1 = [S[0][0]==S[0][1],S[1][0]==S[1][1]]
sage: list2 = [a,b]
sage: solve( [ SR(w) for w in list1], [ SR(z) for z in list2])
[[a == r4, b == r3]]
</code></pre>
<p>and that preserves the type of <code>a</code> and friends...</p>
https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?comment=19883#post-id-19883As to the second one, I just typed `PolynomialRing?` and read what came out. I've edited the answer to include a link in the reference manual.Thu, 19 Apr 2012 16:08:17 -0500https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?comment=19883#post-id-19883Comment by d3banjan for <p>As far as I can tell, this error message has nothing to do with solve. (See below for that issue, though.)</p>
<pre><code>sage: R = PolynomialRing(RR,['a','b','c','d'])
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: a
---------------------------------------------------------------------------
NameError: name 'a' is not defined
</code></pre>
<p>If you do <code>PolynomialRing?</code> (see also <a href="http://www.sagemath.org/doc/reference/sage/rings/polynomial/polynomial_ring_constructor.html#sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing">the official doc</a>) then you'll see</p>
<pre><code> Use the diamond brackets notation to make the variable ready for
use after you define the ring:
</code></pre>
<p>So it looks like instead that this is just unsupported behavior that you are trying to do. I'm not sure exactly <em>why</em> it's not supported, but it's not.</p>
<pre><code>sage: PolynomialRing(QQ, 'w')
Univariate Polynomial Ring in w over Rational Field
sage: w
---------------------------------------------------------------------------
NameError: name 'w' is not defined
sage: sage: R.<w> = PolynomialRing(QQ)
sage: w
w
</code></pre>
<p>Now, we still run into problems even after we do the "right" thing.</p>
<pre><code>sage: R.<a,b,c,d> = PolynomialRing(RR)
sage: R
Multivariate Polynomial Ring in a, b, c, d over Real Field with 53 bits of precision
sage: a
a
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: solve([S[0][0]==S[0][1],S[1][0]==S[1][1]],[a,b])
---------------------------------------------------------------------------
TypeError: a is not a valid variable.
</code></pre>
<p>This is a workaround:</p>
<pre><code>sage: solve([SR(S[0][0]==S[0][1]),SR(S[1][0]==S[1][1])],[SR(a),SR(b)])
[[a == r2, b == r1]]
</code></pre>
<p>But the code in the error above pretty clearly shows that we need symbolic variables. To make this code more concise for more complicated situations, you could do</p>
<pre><code>sage: list1 = [S[0][0]==S[0][1],S[1][0]==S[1][1]]
sage: list2 = [a,b]
sage: solve( [ SR(w) for w in list1], [ SR(z) for z in list2])
[[a == r4, b == r3]]
</code></pre>
<p>and that preserves the type of <code>a</code> and friends...</p>
https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?comment=19888#post-id-19888when you have an expression like `a==0`, where `a` is a declared variable, does it become an equation object instead of a binary digit?Thu, 19 Apr 2012 09:25:00 -0500https://ask.sagemath.org/question/8906/members-of-a-polynomial-ring-dont-respond-to-solve/?comment=19888#post-id-19888