Serie 11 and 12
This commit is contained in:
48
Kuengjoe_S12/Kuengjoe_S12_Aufg4.py
Normal file
48
Kuengjoe_S12/Kuengjoe_S12_Aufg4.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import numpy as np
|
||||
|
||||
def Kuengjoe_S12_Aufg4(in_matrix: np.ndarray, iteration: int):
|
||||
current_iteration = np.array(in_matrix, dtype=float)
|
||||
dimension = current_iteration.shape[0]
|
||||
acc_orthogonal_matrix = np.eye(dimension, dtype=float)
|
||||
|
||||
for iteration in range(iteration):
|
||||
q_matrix, r_matrix = np.linalg.qr(current_iteration)
|
||||
current_iteration = r_matrix @ q_matrix
|
||||
acc_orthogonal_matrix = acc_orthogonal_matrix @ q_matrix
|
||||
return current_iteration, acc_orthogonal_matrix
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
#4a)
|
||||
test_matrix = np.array([
|
||||
[1, -2, 0],
|
||||
[2, 0, 1],
|
||||
[0, -2, 1]
|
||||
], dtype=float)
|
||||
|
||||
ak_1, pk_1 = Kuengjoe_S12_Aufg4(test_matrix, 1)
|
||||
print("A1 =\n", np.round(ak_1, 6))
|
||||
print("P1 =\n", np.round(pk_1, 6))
|
||||
|
||||
#4b)
|
||||
symmetric_matrix = np.array([
|
||||
[6, 1, 2, 1, 2],
|
||||
[1, 5, 0, 2, -1],
|
||||
[2, 0, 5, -1, 0],
|
||||
[1, 2, -1, 6, 1],
|
||||
[2, -1, 0, 1, 7]
|
||||
], dtype=float)
|
||||
|
||||
ak_100, pk_100 = Kuengjoe_S12_Aufg4(symmetric_matrix, 100)
|
||||
|
||||
orthogonality_residual = np.linalg.norm(pk_100.T @ pk_100 - np.eye(5))
|
||||
print("||P^T P - I|| =", orthogonality_residual)
|
||||
|
||||
approx_eigenvalues_from_qr = np.diag(ak_100)
|
||||
print("Eigenvalues approx (diag(Ak)) =", approx_eigenvalues_from_qr)
|
||||
|
||||
#4c)
|
||||
|
||||
eigenvalues_numpy, eigenvectors_numpy = np.linalg.eig(symmetric_matrix)
|
||||
print(eigenvalues_numpy)
|
||||
|
||||
Reference in New Issue
Block a user