simplify_full() for sage matrices

1.2k Views Asked by At

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?

2

There are 2 best solutions below

4
On BEST ANSWER

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))

0
On

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.