Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6. def vectors_uniform(k):
  7. """Uniformly generates k vectors."""
  8. vectors = []
  9. for a in np.linspace(0, 2 * np.pi, k, endpoint=False):
  10. vectors.append(2 * np.array([np.sin(a), np.cos(a)]))
  11. return vectors
  12.  
  13.  
  14. def visualize_transformation(A, vectors):
  15. EVD_decomposition (A)
  16. """Plots original and transformed vectors for a given 2x2 transformation matrix A and a list of 2D vectors."""
  17. for i, v in enumerate(vectors):
  18. # Plot original vector.
  19. plt.quiver(0.0, 0.0, v[0], v[1], width=0.008, color="blue", scale_units='xy', angles='xy', scale=1,
  20. zorder=4)
  21. plt.text(v[0]/2 + 0.25, v[1]/2, "v{0}".format(i), color="blue")
  22.  
  23. # Plot transformed vector.
  24. tv = A.dot(v)
  25. plt.quiver(0.0, 0.0, tv[0], tv[1], width=0.005, color="magenta", scale_units='xy', angles='xy', scale=1,
  26. zorder=4)
  27. plt.text(tv[0] / 2 + 0.25, tv[1] / 2, "v{0}'".format(i), color="magenta")
  28. plt.xlim([-6, 6])
  29. plt.ylim([-6, 6])
  30. plt.margins(0.05)
  31. # Plot eigenvectors
  32. plot_eigenvectors(A)
  33. plt.show()
  34.  
  35.  
  36. def visualize_vectors(vectors, color="green"):
  37. """Plots all vectors in the list."""
  38. for i, v in enumerate(vectors):
  39. plt.quiver(0.0, 0.0, v[0], v[1], width=0.006, color=color, scale_units='xy', angles='xy', scale=1,
  40. zorder=4)
  41. plt.text(v[0] / 2 + 0.25, v[1] / 2, "eigv{0}".format(i), color=color)
  42.  
  43.  
  44. def plot_eigenvectors(A):
  45. """Plots all eigenvectors of the given 2x2 matrix A."""
  46. # TODO: Zad. 4.1. Oblicz wektory własne A. Możesz wykorzystać funkcję np.linalg.eig
  47. w, v = np.linalg.eig(A)
  48. #eigvec = []
  49. # TODO: Zad. 4.1. Upewnij się poprzez analizę wykresów, że rysowane są poprawne wektory własne (łatwo tu o pomyłkę).
  50. visualize_vectors(v)
  51.  
  52.  
  53. def EVD_decomposition(A):
  54. # TODO: Zad. 4.2. Uzupełnij funkcję tak by obliczała rozkład EVD zgodnie z zadaniem.
  55. w, v = np.linalg.eig(A)
  56. K = v
  57. L = np.diag(w)
  58. inv_K = np.linalg.inv(K)
  59.  
  60. A2 = K*L*inv_K
  61. print(' ')
  62. print(A2)
  63. print('-------------------------------------------')
  64. print(A)
  65.  
  66. def plot_attractors(A):
  67. # TODO: Zad. 4.3. Uzupełnij funkcję tak by generowała wykres z atraktorami.
  68. pass
  69.  
  70.  
  71. def test_A1(vectors):
  72. """Standard scaling transformation."""
  73. A = np.array([[2, 0],
  74. [0, 2]])
  75. visualize_transformation(A, vectors)
  76.  
  77.  
  78. def test_A2(vectors):
  79. A = np.array([[-1, 2],
  80. [2, 1]])
  81. visualize_transformation(A, vectors)
  82.  
  83.  
  84. def test_A3(vectors):
  85. A = np.array([[3, 1],
  86. [0, 2]])
  87. visualize_transformation(A, vectors)
  88.  
  89.  
  90.  
  91. if __name__ == "__main__":
  92. vectors = vectors_uniform(k=8)
  93. test_A1(vectors)
  94. test_A2(vectors)
  95. test_A3(vectors)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement