You have probably not normalised the vectors correctly. It looks like
you have made a matrix in which the rows are the eigenvectors but then
normalised the columns.

In a symbolic context it is usually a bad idea to normalise the unit
vectors. Instead use inverse rather than transpose in the formula:

In [48]: T, D = A_1.diagonalize()

In [49]: simplify(T.inv() * A_1 * T - D).is_zero_matrix
Out[49]: True

On Mon, 30 Dec 2024 at 23:58, Indranil <[email protected]> wrote:
>
> Hi
>
> I've been facing an issue diagonalizing a particular matrix. Through Sympy 
> the resulting eigVecs do not form the diagonalizing transform. A further 
> 45deg rotation is required to get the matrix diagonal.
> What is the source of this error?
>
> numpy ver: 1.26.4
> sympy ver: 1.13.2
>
> I've included the snippet below.
>
> import sympy as sp
> import numpy as np
>
> from IPython.display import display, Math, HTML
>
> delta, J, g = sp.symbols('delta J g')
>
> A_1 = sp.Matrix([[J, 0, 0, g],
>                  [0, J, 0, g],
>                  [0, 0, J, g],
>                  [g, g, g, 0]])
>
> eigVals = A_1.eigenvals()
> eigVecs = A_1.eigenvects()
>
> P = sp.Matrix.hstack(*[vec for eig in eigVecs for vec in eig[2]])
> display(Math(sp.latex(P)))
>
> rows, cols = P.shape
>
> # normalized transformation
> normalized_columns = []
>
> for i in range(cols):
>     col_new = P[:, i] / sp.sqrt(P[:, i].dot(P[:, i]))
>     # print(col_new)
>     normalized_columns.append(col_new)
>
> P_norm = sp.Matrix.hstack(*normalized_columns)
>
> display(Math(sp.latex(P_norm)))
>
> A_1_val = A_1.subs({J: 66.14, g: 20 * np.sqrt(2)})
>
> U_chk = P_norm.subs({J: 66.14, g: 20 * np.sqrt(2)})
>
> res_mat = U_chk.transpose() @ A_1_val @ U_chk
>
> display(Math(sp.latex(res_mat)))
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion visit 
> https://groups.google.com/d/msgid/sympy/d92c066c-b7e5-41ed-81e1-d898f17ded69n%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxQeuRa3aCaae_u5unMKSk6ehMWGe0GTw30mBJujPMk5AQ%40mail.gmail.com.

Reply via email to