# Simplify a matrix with symbolic variables

Hello,

I've tried the following code in SageMath, but I don't succeed in getting a simplified expression (even using the method .simplify_full()) :

var('w_2, w_3, w_4', domain='complex')
a_2=sqrt(1-norm(w_2))*sqrt(1-norm(w_3))
a_3=sqrt(1-norm(w_3))*sqrt(1-norm(w_4))
b_2=-conjugate(w_3)*sqrt(1-norm(w_2))*sqrt(1-norm(w_4))
C=Matrix([[0, w_2, a_2, b_2], [0,0,w_3,a_3], [0,0,0,w_4], [0,0,0,0]])
Id=matrix.identity(4)
T=Id-C*(C.H)
T.simplify_full()


I get the "awfull" outcome

[-sqrt(-w_2*conjugate(w_2) + 1)*sqrt(-w_4*conjugate(w_4) + 1)*w_3*conjugate(sqrt(-w_2*conjugate(w_2) + 1))*conjugate(sqrt(-w_4*conjugate(w_4) + 1))*conjugate(w_3) - sqrt(-w_2*conjugate(w_2) + 1)*sqrt(-w_3*conjugate(w_3) + 1)*conjugate(sqrt(-w_2*conjugate(w_2) + 1))*conjugate(sqrt(-w_3*conjugate(w_3) + 1)) - w_2*conjugate(w_2) + 1                                                                                                  sqrt(-w_2*conjugate(w_2) + 1)*sqrt(-w_4*conjugate(w_4) + 1)*conjugate(sqrt(-w_3*conjugate(w_3) + 1))*conjugate(sqrt(-w_4*conjugate(w_4) + 1))*conjugate(w_3) - sqrt(-w_2*conjugate(w_2) + 1)*sqrt(-w_3*conjugate(w_3) + 1)*conjugate(w_3)                                                                                                                                                                                                                                                  sqrt(-w_2*conjugate(w_2) + 1)*sqrt(-w_4*conjugate(w_4) + 1)*conjugate(w_3)*conjugate(w_4)                                                                                                                                                                                                                                                                                                                                          0]
[                                                                                                 sqrt(-w_3*conjugate(w_3) + 1)*sqrt(-w_4*conjugate(w_4) + 1)*w_3*conjugate(sqrt(-w_2*conjugate(w_2) + 1))*conjugate(sqrt(-w_4*conjugate(w_4) + 1)) - w_3*conjugate(sqrt(-w_2*conjugate(w_2) + 1))*conjugate(sqrt(-w_3*conjugate(w_3) + 1))                                                                                                                                                                    -sqrt(-w_3*conjugate(w_3) + 1)*sqrt(-w_4*conjugate(w_4) + 1)*conjugate(sqrt(-w_3*conjugate(w_3) + 1))*conjugate(sqrt(-w_4*conjugate(w_4) + 1)) - w_3*conjugate(w_3) + 1                                                                                                                                                                                                                                                                -sqrt(-w_3*conjugate(w_3) + 1)*sqrt(-w_4*conjugate(w_4) + 1)*conjugate(w_4)                                                                                                                                                                                                                                                                                                                                          0]

[                                                                                                                                                                                                                                                 w_3*w_4*conjugate(sqrt(-w_2*conjugate(w_2) + 1))*conjugate(sqrt(-w_4*conjugate(w_4) + 1))                                                                                                                                                                                                                                                     -w_4*conjugate(sqrt(-w_3*conjugate(w_3) + 1))*conjugate(sqrt(-w_4*conjugate(w_4) + 1))                                                                                                                                                                                                                                                                                                                    -w_4*conjugate(w_4) + 1                                                                                                                                                                                                                                                                                                                                          0]
[                                                                                                                                                                                                                                                                                                                                         0                                                                                                                                                                                                                                                                                                                                          0                                                                                                                                                                                                                                                                                                                                          0                                                                                                                                                                                                                                                                                                                                      1]


whereas when we compute this "by hand", we can show that

T= [[norm(w_3*w_4)*(1-norm(w_2)), -conjugate(w_3)*sqrt(1-norm(w_2))*sqrt(1-norm(w_3))*norm(w_4), conjugate(w_3*w_4)*sqrt(1-norm(w_2))*sqrt(1-norm(w_4))],
[-w_3*sqrt(1-norm(w_2))*sqrt(1-norm(w_3))*norm(w_4), norm(w_4)*(1-norm(w_3)), -conjugate(w_4)*sqrt(1-norm(w_3))*sqrt(1-norm(w_4))],
[w_3*w_4*sqrt(1-norm(w_2))*sqrt(1-norm(w_4)), -w_4*sqrt(1-norm(w_3))*sqrt(1-norm(w_4)), 1-norm(w_4)]]


Thus, when I want to diagonalize T using the T.eigenvalues() and T.eigenvectors_right(), I get something really complicated... whereas we can show "by hand" that the eigenvalues are just 0 and 1-norm(w_2*w_3*w_4)...

How could we manage to get those simple expressions with Sage Math ?

Thanks in advance for your help !

edit retag close merge delete

I fail to understant how a $4\times 4$ matrix can be equal to a $3\times 3$ matrix...

( 2023-10-20 08:16:47 +0100 )edit

Of course... I've forgotten the last row (but this row is quite trivial...).

( 2023-10-25 14:59:47 +0100 )edit

Sort by ยป oldest newest most voted

This issue is not really about matrices but about how Sage not being able to figure out that sqrt() here are real numbers that are invariant with respect to conjugate(). It may be possible to help Sage understand this with a set of assumptions, but I do not know how to do so (I've tried assume(norm(w_2) < 1) and alike, but it did not help).

Below I present an alternative solution, using the following function that manually looks for patterns conjugate(sqrt(...)) in a given expression, replaces them with just sqrt(...), and simplifies the result:

def simplify_conjugate_of_sqrt(f):
x = SR.wild(0)
return f.subs({conjugate(sqrt(x)):sqrt(x)}).full_simplify()


It amounts only to replacing T.simplify_full() in OP's code with T.apply_map(simplify_conjugate_of_sqrt) to get a much simpler matrix:

[                               -(w_2*conjugate(w_2) - 1)*w_3*w_4*conjugate(w_3)*conjugate(w_4) -sqrt(-w_2*conjugate(w_2) + 1)*sqrt(-w_3*conjugate(w_3) + 1)*w_4*conjugate(w_3)*conjugate(w_4)      sqrt(-w_2*conjugate(w_2) + 1)*sqrt(-w_4*conjugate(w_4) + 1)*conjugate(w_3)*conjugate(w_4)                                                                                              0]
[           -sqrt(-w_2*conjugate(w_2) + 1)*sqrt(-w_3*conjugate(w_3) + 1)*w_3*w_4*conjugate(w_4)                                                   -(w_3*conjugate(w_3) - 1)*w_4*conjugate(w_4)                    -sqrt(-w_3*conjugate(w_3) + 1)*sqrt(-w_4*conjugate(w_4) + 1)*conjugate(w_4)                                                                                              0]
[                           sqrt(-w_2*conjugate(w_2) + 1)*sqrt(-w_4*conjugate(w_4) + 1)*w_3*w_4                               -sqrt(-w_3*conjugate(w_3) + 1)*sqrt(-w_4*conjugate(w_4) + 1)*w_4                                                                        -w_4*conjugate(w_4) + 1                                                                                              0]
[                                                                                             0                                                                                              0                                                                                              0                                                                                              1]


Basically it matches the OP's expected matrix, with just each norm(z) replaced with z * conjugate(z), but this is how Sage handles the norm(z) function.

more

Thank you !

( 2023-10-25 15:00:26 +0100 )edit