library(Matrix)
library(ggplot2)
M <- matrix(c(1+2i, 3-4i, 5+6i, 2+3i, 4-5i, 6+7i, 1-2i, 3+4i, 5-6i), nrow = 3, byrow = TRUE)
eigen_result <- eigen(M)
eigenvectors <- eigen_result$vectors
D <- diag(eigen_result$values)
inv_eigenvectors <- solve(eigenvectors)
invariant_matrix <- eigenvectors %*% D %*% inv_eigenvectors
print(invariant_matrix)
## [,1] [,2] [,3]
## [1,] 1+2i 3-4i 5+6i
## [2,] 2+3i 4-5i 6+7i
## [3,] 1-2i 3+4i 5-6i
eigenvectors_df <- data.frame(Re1 = Re(eigenvectors[, 1]), Im1 = Im(eigenvectors[, 1]),
Re2 = Re(eigenvectors[, 2]), Im2 = Im(eigenvectors[, 2]),
Re3 = Re(eigenvectors[, 3]), Im3 = Im(eigenvectors[, 3]))
ggplot(eigenvectors_df) +
geom_segment(aes(x = 0, y = 0, xend = Re1, yend = Im1, color = "Eigenvector 1"), arrow = arrow(length = unit(0.2, "cm"))) +
geom_segment(aes(x = 0, y = 0, xend = Re2, yend = Im2, color = "Eigenvector 2"), arrow = arrow(length = unit(0.2, "cm"))) +
geom_segment(aes(x = 0, y = 0, xend = Re3, yend = Im3, color = "Eigenvector 3"), arrow = arrow(length = unit(0.2, "cm"))) +
geom_point(aes(x = Re1, y = Im1), color = "blue") +
geom_point(aes(x = Re2, y = Im2), color = "red") +
geom_point(aes(x = Re3, y = Im3), color = "green") +
labs(x = "Real", y = "Imaginary") +
theme_minimal() +
scale_color_manual(values = c("Eigenvector 1" = "blue", "Eigenvector 2" = "red", "Eigenvector 3" = "green"))

invariant_df <- data.frame(Re1 = Re(invariant_matrix[, 1]), Im1 = Im(invariant_matrix[, 1]),
Re2 = Re(invariant_matrix[, 2]), Im2 = Im(invariant_matrix[, 2]),
Re3 = Re(invariant_matrix[, 3]), Im3 = Im(invariant_matrix[, 3]))
ggplot(invariant_df) +
geom_segment(aes(x = 0, y = 0, xend = Re1, yend = Im1, color = "Invariant Matrix"), arrow = arrow(length = unit(0.2, "cm"))) +
geom_segment(aes(x = 0, y = 0, xend = Re2, yend = Im2, color = "Invariant Matrix"), arrow = arrow(length = unit(0.2, "cm"))) +
geom_segment(aes(x = 0, y = 0, xend = Re3, yend = Im3, color = "Invariant Matrix"), arrow = arrow(length = unit(0.2, "cm"))) +
geom_point(aes(x = Re1, y = Im1), color = "purple") +
geom_point(aes(x = Re2, y = Im2), color = "purple") +
geom_point(aes(x = Re3, y = Im3), color = "purple") +
labs(x = "Real", y = "Imaginary") +
theme_minimal()
