# Members of a Polynomial Ring don't respond to solve

R = PolynomialRing(RR,['a','b','c','d'])
S = matrix(R,2,R.gens())
S


yields $$\newcommand{\Bold}{\mathbf{#1}}\left(\begin{array}{rr} a & b \ c & d \end{array}\right)$$ as expected, but then solve does not respond -

solve([S==S,S==S],[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?

edit retag close merge delete

Sort by » oldest newest most voted

First, 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.

more

Nice, 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.

As 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) 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==S,S==S],[a,b])
---------------------------------------------------------------------------
TypeError: a is not a valid variable.


This is a workaround:

sage: solve([SR(S==S),SR(S==S)],[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==S,S==S]
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...

more

when you have an expression like a==0, where a is a declared variable, does it become an equation object instead of a binary digit?

@kcrisman - also could you please cite the docpage that discusses this bit? I have been facing problems with this for quite a while now!

I'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.

As 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.