Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

This is a jupyter notebook for the examples in the linear algebra 2 course on applications of eigenvalues & eigenvectors

Project: LA2 2020
Views: 477
Visibility: Unlisted (only visible to those who know the link)
Image: default
Kernel: Python 3 (system-wide)

Matrices & Vectors

A=np.array([[1,2],[3,4]]) v=np.array([-1,2]) print(A) print("v =",v) print("Av = ",np.dot(A,v))
[[1 2] [3 4]] v = [-1 2] Av = [3 5]

Power iteration

import numpy as np np.set_printoptions(precision=3) # power iteration / von Mises iteration # gives an approximation for an eigenvector for the dominant eigenvalue of a matrix A # by doing the power iteration k times def power_iteration(A, k: int): v = np.random.rand(A.shape[1]) for _ in range(k): # calculates b = Av b = np.dot(A, v) # calculate the norm of b b_norm = np.linalg.norm(b) # define v to be the normalized version of b v = b / b_norm return v def give_eigenvalue(A,k): v=power_iteration(A,k) lam = np.dot(A,v)[0]/v[0] return lam
# Using power iteration example A=np.array([[1,2],[3,4]]) print(A) v=power_iteration(A,3) print(v) print(np.dot(A,v)) print(np.dot(A,v)[0]/v[0]) print(np.dot(A,v)[1]/v[1]) give_eigenvalue(A,10)
[[1 2] [3 4]] [0.416 0.909] [2.235 4.885] 5.371605514503671 5.37249346495099
5.372281323264108
print("Eigenvector from power-it:", power_iteration(A,1)) print("Eigenvalue from power-it:", give_eigenvalue(A,1)) print("Eigenvector from power-it:", power_iteration(A,5)) print("Eigenvalue from power-it:", give_eigenvalue(A,5)) print("Eigenvector from power-it:", power_iteration(A,30)) print("Eigenvalue from power-it:", give_eigenvalue(A,30))
Eigenvector from power-it: [0.35 0.93] Eigenvalue from power-it: 5.193779822316735 Eigenvector from power-it: [0.42 0.91] Eigenvalue from power-it: 5.372280232439236 Eigenvector from power-it: [0.42 0.91] Eigenvalue from power-it: 5.372281323269014
# Eigenvectors and eigenvalues are implemented in numpy as follows: print("Eigenvectors:") print(np.linalg.eig(A)) print("Eigenvalues: ") print(np.linalg.eigvals(A))
Eigenvectors: (array([-0.37, 5.37]), array([[-0.82, -0.42], [ 0.57, -0.91]])) Eigenvalues: [-0.37 5.37]

(1) Population behavior / Leslie matrices

A=np.array([[0,3,2],[4/5,0,0],[0,2/5,0]]) print(A) print("Dominant eigenvalye:", give_eigenvalue(A,100)) print("Eigenvector:", 100*power_iteration(A,100))
[[0. 3. 2. ] [0.8 0. 0. ] [0. 0.4 0. ]] Dominant eigenvalye: 1.6684119203677292 Eigenvector: [89.69 43.01 10.31]

(2) Google Page rank

A=np.array([[0,1/3,0,0,0,0],[1,0,1/2,0,0,0],[0,1/3,0,1/2,0,1/2],[0,1/3,1/2,0,1/2,1/2],[0,0,0,1/2,0,0],[0,0,0,0,1/2,0]]) print(A) print("Dominant eigenvalye:", give_eigenvalue(A,100)) print("Eigenvector:", 1/0.16*power_iteration(A,100))
[[0. 0.33 0. 0. 0. 0. ] [1. 0. 0.5 0. 0. 0. ] [0. 0.33 0. 0.5 0. 0.5 ] [0. 0.33 0.5 0. 0.5 0.5 ] [0. 0. 0. 0.5 0. 0. ] [0. 0. 0. 0. 0.5 0. ]] Dominant eigenvalye: 1.0 Eigenvector: [0.83 2.5 3.33 4. 2. 1. ]

(3) Decision making

A=np.array([[1,4,2,3/2],[1/4,1,1/2,1/3],[1/2,2,1,1],[2/3,3,1,1]]) print(A) print("Dominant eigenvalye:", give_eigenvalue(A,1000)) print("Eigenvector:", power_iteration(A,1000))
[[1. 4. 2. 1.5 ] [0.25 1. 0.5 0.33] [0.5 2. 1. 1. ] [0.67 3. 1. 1. ]] Dominant eigenvalye: 4.016358973921448 Eigenvector: [0.75 0.18 0.41 0.48]