Talk:Eigenvalues and eigenvectors

Latest comment: 5 months ago by Guy vandegrift in topic Deriving the formula for eigenvalue k

Python code

edit

I had a bunch of linear algebra and machine learning demos I wrote at one point. I couldn't find them but I wrote up something similar just for kicks and thought I'd share it. It plots the columns of T's matrix as well, so you can see how it transforms the vector space along with the eigenvectors. I see you have a bunch of good examples already, but it's a neat tool to play around with. AP295 (discusscontribs) 05:47, 24 January 2024 (UTC)Reply

actual code

edit
Extended content
import matplotlib.pyplot as plt
import numpy as np
from numpy import linalg

fig, ax = plt.subplots(figsize = (12,12))

M = np.random.uniform(0.05,1,size=(2,2))
v1, v2 = M[:,0]
u1, u2 = M[:,1]

plot_vector = lambda v1,v2,color="black": ax.quiver(0.,0.,v1,v2, angles='xy', scale_units='xy', scale=1, width=0.005, color=color)
plot_vector(v1,v2)
plot_vector(u1,u2)

eigen_values,eigen_vectors = linalg.eig(M)
idx = eigen_values.argsort()[::-1]
eigen_values = eigen_values[idx]
eigen_vectors = eigen_vectors[:,idx]

for eigvec,eigval in zip(np.transpose(eigen_vectors),eigen_values):
    if np.all( M >= 0.) and np.all(eigvec < 0): # It prints more nicely when vectors are limited to the first quadrant
        eigvec *= -1    
    print(eigvec,eigval)
    eigvec = eigvec*eigval
    plot_vector(eigvec[0],eigvec[1],color="blue")

ax.grid(True)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
plt.axis([-1.2,1.2,-1.2,1.2])
plt.show()

I once actively programmed python an have a version on my computer. Your code performed nicely. Thanks 23:19, 24 January 2024 (UTC)

Deriving the formula for eigenvalue k

edit

The formula for k in terms of   is obtained from Bard. Here is a double check:

Extended content

 

  This is the determinant

  Standard form for quadratic equation

  Substitute from (3) and push   inside the previous polynomial (which is a perfect square.)

  As a double check, rewrite this in a more convenient form:

  Well known solution to quadratic equation with a=1.

  Substitute (3) and (4) into (6):

  Rewrite (6):

  Substitute (3) and (5) into (8)

  I find (9) more convenient than (8)

Guy vandegrift (discusscontribs) 03:30, 28 January 2024 (UTC)Reply

Return to "Eigenvalues and eigenvectors" page.