# simplify_full() for sage matrices

My matrix is $$solution = \left(\begin{array}{rrr|r} 1 & 0 & 0 & \frac{2 \, {\left(\frac{2}{k - 2} - 3\right)}}{k - 1} - \frac{4}{k - 2} + 4 \\ 0 & 1 & 0 & \frac{2}{k - 2} - \frac{2}{{\left(k - 1\right)} {\left(k - 2\right)}} \\ 0 & 0 & 1 & \frac{2}{k - 1} \end{array}\right)$$

If I use the simplify command for an entry I get

sage: solution[0][3].simplify_full()

2*(2*k - 7)/(k - 1)

Is there a similar command for a full matrix?

• Unless you are using a very old version of Sage, the similar command solution.simplify_full() should work. See fuller answer below. Commented Apr 27, 2018 at 12:15
• @Ion Sme: Please unaccept my answer (so I can delete it). Samuel Lelièvre's answer is the better answer. Commented Jul 18, 2018 at 17:47

There is a built-in method for that, which is .simplify_full().

It can be applied to any matrix with entries in the symbolic ring.

To illustrate it, we define k as a symbolic variable, and build the matrix in the question.

sage: k = SR.var('k')
sage: a = identity_matrix(SR, 3)
sage: b = vector([
....:         (2 * (2 / (k - 2) - 3)) / (k - 1),
....:         2 / (k - 2) - 2 / ((k - 1) * (k - 2)),
....:         2 / (k - 1)
....:         ])
....:
sage: m = a.augment(b, subdivide=True)


Then m is as follows:

sage: m
[                              1                               0                               0|      2*(2/(k - 2) - 3)/(k - 1)]
[                              0                               1                               0|2/(k - 2) - 2/((k - 1)*(k - 2))]
[                              0                               0                               1|                      2/(k - 1)]
sage: show(m)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr|r}
1 & 0 & 0 & \frac{2 \, {\left(\frac{2}{k - 2} - 3\right)}}{k - 1} \\
0 & 1 & 0 & \frac{2}{k - 2} - \frac{2}{{\left(k - 1\right)} {\left(k - 2\right)}} \\
0 & 0 & 1 & \frac{2}{k - 1}
\end{array}\right)


Applying the .simplify_full() method applies simplify_full to each entry, giving the expected result.

sage: mm = m.simplify_full()
sage: mm
[                           1                            0                            0 -2*(3*k - 8)/(k^2 - 3*k + 2)]
[                           0                            1                            0                    2/(k - 1)]
[                           0                            0                            1                    2/(k - 1)]

sage: show(mm)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr}
1 & 0 & 0 & -\frac{2 \, {\left(3 \, k - 8\right)}}{k^{2} - 3 \, k + 2} \\
0 & 1 & 0 & \frac{2}{k - 1} \\
0 & 0 & 1 & \frac{2}{k - 1}
\end{array}\right)


Providing this method for matrices with entries in the symbolic ring was the object of

which was merged in the development release Sage 6.9.rc0 and in the stable release Sage 6.9.

Here's one way . . .

First define a function simp by

$\qquad$def simp(u): try: v=u.simplify_full() except: v=u return v

Then, for your matrix $A$, do

$\qquad$A = matrix(map(simp,A))

• Is there no built in command though? Commented Apr 21, 2018 at 17:26
• Not that I'm aware of. But once you define simp, the task is then just a one-liner. Commented Apr 21, 2018 at 22:47
• Did you try applying .simplify_full() to the matrix itself? What version of Sage are you using? Commented Jul 18, 2018 at 10:42
• @Samuel Lelièvre: I don't know why I didn't try that. I only tried applying .simplify_full to each matrix element, and of course, that fails for matrix elements which are integers. I didn't see your answer until now, but now that I see it, it's obviously the right answer (+1). If the OP unaccepts my answer, I'll delete it, in favor of yours. Commented Jul 18, 2018 at 16:45