Ask Your Question
1

Members of a Polynomial Ring don't respond to solve

asked 2012-04-19 13:35:55 +0100

d3banjan gravatar image
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?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2012-04-20 10:04:43 +0100

Volker Braun gravatar image

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.

edit flag offensive delete link more

Comments

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.

kcrisman gravatar imagekcrisman ( 2012-04-20 12:02:38 +0100 )edit
1

answered 2012-04-19 16:12:26 +0100

kcrisman gravatar image

updated 2012-04-19 23:08:45 +0100

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

edit flag offensive delete link more

Comments

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?

d3banjan gravatar imaged3banjan ( 2012-04-19 16:25:00 +0100 )edit

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

d3banjan gravatar imaged3banjan ( 2012-04-19 16:29:29 +0100 )edit

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.

kcrisman gravatar imagekcrisman ( 2012-04-19 23:06:56 +0100 )edit

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.

kcrisman gravatar imagekcrisman ( 2012-04-19 23:08:17 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-04-19 13:35:55 +0100

Seen: 1,814 times

Last updated: Apr 20 '12