# How could I work with polynomial fields?

I have the simple program:

a = var('a')
b = 1/a
for i in (1..5):
c = b+1
b = 1/c
print b,",", c


The result is:

1/(1/a + 1) , 1/a + 1
1/(1/(1/a + 1) + 1) , 1/(1/a + 1) + 1
1/(1/(1/(1/a + 1) + 1) + 1) , 1/(1/(1/a + 1) + 1) + 1
1/(1/(1/(1/(1/a + 1) + 1) + 1) + 1) , 1/(1/(1/(1/a + 1) + 1) + 1) + 1
1/(1/(1/(1/(1/(1/a + 1) + 1) + 1) + 1) + 1) , 1/(1/(1/(1/(1/a + 1) + 1) + 1) + 1) + 1


Is there a way to get simpler expressions?

edit retag close merge delete

You can "accept" one of the answers by clicking the tick mark on the top left of that answer. When you reach 15 karma points, you can also upvote answers.

Sort by » oldest newest most voted

First, you can simplify the symbolic expression (which is not an algebraic object, just a formula) you obtained:

sage: b.full_simplify()
(5*a + 3)/(8*a + 5)


Then you can indeed work on the genuine fraction field of a polynomial ring:

sage: R.<a> = QQ[]
sage: F = R.fraction_field()
sage: b = 1/F(a)
sage: for i in (1..5):
....:     c = b+1
....:     b = 1/c
....:     print b,",", c
....:
a/(a + 1) , (a + 1)/a
(a + 1)/(2*a + 1) , (2*a + 1)/(a + 1)
(2*a + 1)/(3*a + 2) , (3*a + 2)/(2*a + 1)
(3*a + 2)/(5*a + 3) , (5*a + 3)/(3*a + 2)
(5*a + 3)/(8*a + 5) , (8*a + 5)/(5*a + 3)


more
sage: for i in (1..5):
....:       c = b+1
....:       b = 1/c
....:       print b.simplify_full(),",",c.simplify_full()
....:
a/(a + 1) , (a + 1)/a
(a + 1)/(2*a + 1) , (2*a + 1)/(a + 1)
(2*a + 1)/(3*a + 2) , (3*a + 2)/(2*a + 1)
(3*a + 2)/(5*a + 3) , (5*a + 3)/(3*a + 2)
(5*a + 3)/(8*a + 5) , (8*a + 5)/(5*a + 3)


That said, I'd use something like this:

sage: L = []
sage: for i in (1..5):
c = b+1
b = 1/c
L.append([b.simplify_full(),c.simplify_full()])
....:
b                         c
+-------------------------+-------------------------+
(8*a + 5)/(13*a + 8)      (13*a + 8)/(8*a + 5)
(13*a + 8)/(21*a + 13)    (21*a + 13)/(13*a + 8)
(21*a + 13)/(34*a + 21)   (34*a + 21)/(21*a + 13)
(34*a + 21)/(55*a + 34)   (55*a + 34)/(34*a + 21)
(55*a + 34)/(89*a + 55)   (89*a + 55)/(55*a + 34)


In the notebook if you do html(table(...)) and make sure to have everything be in latex() like so

sage: for i in (1..5):
c = b+1
b = 1/c
L.append(['$'+latex(b.simplify_full())+'$','$'+latex(c.simplify_full())+'$'])
....:
<html>
<div class="notruncate">
<table  class="table_form">
<tbody>
<tr>
<th>b</th>
<th>c</th>
</tr>
<tr class ="row-a">
<td><script type="math/tex"> \frac{144 \, a + 89}{233 \, a + 144} </script></td>
<td><script type="math/tex"> \frac{233 \, a + 144}{144 \, a + 89} </script></td>
</tr>
<tr class ="row-b">
<td><script type="math/tex"> \frac{233 \, a + 144}{377 \, a + 233} </script></td>
<td><script type="math/tex"> \frac{377 \, a + 233}{233 \, a + 144} </script></td>
</tr>
<tr class ="row-a">
<td><script type="math/tex"> \frac{377 \, a + 233}{610 \, a + 377} </script></td>
<td><script type="math/tex"> \frac{610 \, a + 377}{377 \, a + 233} </script></td>
</tr>
<tr class ="row-b">
<td><script type="math/tex"> \frac{610 \, a + 377}{987 \, a + 610} </script></td>
<td><script type="math/tex"> \frac{987 \, a + 610}{610 \, a + 377} </script></td>
</tr>
<tr class ="row-a">
<td><script type="math/tex"> \frac{987 \, a + 610}{1597 \, a + 987} </script></td>
<td><script type="math/tex"> \frac{1597 \, a + 987}{987 \, a + 610} </script></td>
</tr>
</tbody>
</table>
</div>
</html>


could be even more awesome. Good luck!

PS naturally I didn't answer about polynomial fields, but this is also possible, I think. See e.g. here.

more