import numpy as np from itertools import permutations def
is_in_gershgorin_disks(matrix): # Check if all eigenvalues of the matrix are in the
Gershgorin disks eigenvalues = np.linalg.eigvals(matrix) n = matrix.shape[0] for i in range(n): center = matrix[i, i] radius = np.sum(np.abs(matrix[i, :])) - np.abs(center) if not np.all(np.abs(eigenvalues - center) <= radius): return False return True def
permute_rows(matrix, permutation): # Permute rows in the matrix based on the given
permutation return matrix[list(permutation), :] # Input matrix original_matrix = np.array([ [1, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 1, 1] ]) # Get all possible row
permutations n = original_matrix.shape[0] all_permutations = list(permutations(range(n))) best_permutation = None best_fitness = 0.0 # Initialize
with the worst fitness value # Check each permutation for
Gershgorin's Circle Theorem for idx,
permutation in enumerate(all_permutations, start=1): # Permute rows permuted_matrix = permute_rows(original_matrix, permutation) # Check if the permuted matrix satisfies Gershgorin's
Circle Theorem if
is_in_gershgorin_disks(permuted_matrix): # Calculate
fitness value based on the sum of eigenvalues fitness_value = np.sum(np.linalg.eigvals(permuted_matrix)) # Update the
best permutation if the current one is better if fitness_value > best_fitness: best_permutation = permutation best_fitness = fitness_value print(f"Permutation
#{idx} results in a matrix satisfying Gershgorin's Circle Theorem with
fitness value: {fitness_value}") else: print(f"Permutation
#{idx} did not result in a matrix satisfying Gershgorin's Circle
Theorem.") # Print the best permutation found print("\nBest Permutation:",
best_permutation) |
No hay comentarios:
Publicar un comentario