Untitled
unknown
plain_text
5 months ago
2.2 kB
2
Indexable
import numpy as np def determinant(matrix): """Calculates the determinant of a 3x3 matrix.""" a = matrix[0][0] b = matrix[0][1] c = matrix[0][2] d = matrix[1][0] e = matrix[1][1] f = matrix[1][2] g = matrix[2][0] h = matrix[2][1] i = matrix[2][2] return a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g) def matrix_subtraction(matrix1, matrix2): """Subtracts two matrices.""" result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] for i in range(len(matrix1)): for j in range(len(matrix1[0])): result[i][j] = matrix1[i][j] - matrix2[i][j] return result def matrix_multiplication(matrix1, matrix2): """Multiplies two matrices.""" result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] for i in range(len(matrix1)): for j in range(len(matrix2[0])): for k in range(len(matrix2)): result[i][j] += matrix1[i][k] * matrix2[k][j] return result def find_eigenvalues_and_eigenvectors(matrix): """Finds eigenvalues and eigenvectors of a 3x3 matrix.""" identity_matrix = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] eigenvalues = [] eigenvectors = [] for lambda_val in range(-10, 11): # Search for eigenvalues within a range new_matrix = matrix_subtraction(matrix, [[lambda_val, 0, 0], [0, lambda_val, 0], [0, 0, lambda_val]]) if abs(determinant(new_matrix)) < 1e-6: # Check for near-zero determinant eigenvalues.append(lambda_val) # Find eigenvector corresponding to the eigenvalue # Solve the system of linear equations (A - λI)v = 0 # Example: using Gaussian elimination or other methods # Here we are just finding a non-zero solution eigenvector = [0, 0, 0] for i in range(3): if abs(new_matrix[0][i])> 1e-6: eigenvector = [1, 0, 0] break if abs(new_matrix[1][i])> 1e-6: eigenvector = [0, 1, 0] break if abs(new_matrix[2][i])> 1e-6: eigenvector = [0, 0, 1] break eigenvectors.append(eigenvector) return eigenvalues, eigenvectors # Example usage: matrix = [[2, 1, 0], [0, 2, 0], [0, 0, 3]] eigenvalues, eigenvectors = find_eigenvalues_and_eigenvectors(matrix) print("Eigenvalues:", eigenvalues) print("Eigenvectors:", eigenvectors)
Editor is loading...
Leave a Comment