Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import numpy as np
- import matplotlib.pyplot as plt
- def vectors_uniform(k):
- """Uniformly generates k vectors."""
- vectors = []
- for a in np.linspace(0, 2 * np.pi, k, endpoint=False):
- vectors.append(2 * np.array([np.sin(a), np.cos(a)]))
- return vectors
- def visualize_transformation(A, vectors):
- EVD_decomposition (A)
- """Plots original and transformed vectors for a given 2x2 transformation matrix A and a list of 2D vectors."""
- for i, v in enumerate(vectors):
- # Plot original vector.
- plt.quiver(0.0, 0.0, v[0], v[1], width=0.008, color="blue", scale_units='xy', angles='xy', scale=1,
- zorder=4)
- plt.text(v[0]/2 + 0.25, v[1]/2, "v{0}".format(i), color="blue")
- # Plot transformed vector.
- tv = A.dot(v)
- plt.quiver(0.0, 0.0, tv[0], tv[1], width=0.005, color="magenta", scale_units='xy', angles='xy', scale=1,
- zorder=4)
- plt.text(tv[0] / 2 + 0.25, tv[1] / 2, "v{0}'".format(i), color="magenta")
- plt.xlim([-6, 6])
- plt.ylim([-6, 6])
- plt.margins(0.05)
- # Plot eigenvectors
- plot_eigenvectors(A)
- plt.show()
- def visualize_vectors(vectors, color="green"):
- """Plots all vectors in the list."""
- for i, v in enumerate(vectors):
- plt.quiver(0.0, 0.0, v[0], v[1], width=0.006, color=color, scale_units='xy', angles='xy', scale=1,
- zorder=4)
- plt.text(v[0] / 2 + 0.25, v[1] / 2, "eigv{0}".format(i), color=color)
- def plot_eigenvectors(A):
- """Plots all eigenvectors of the given 2x2 matrix A."""
- # TODO: Zad. 4.1. Oblicz wektory własne A. Możesz wykorzystać funkcję np.linalg.eig
- w, v = np.linalg.eig(A)
- #eigvec = []
- # TODO: Zad. 4.1. Upewnij się poprzez analizę wykresów, że rysowane są poprawne wektory własne (łatwo tu o pomyłkę).
- visualize_vectors(v)
- def EVD_decomposition(A):
- # TODO: Zad. 4.2. Uzupełnij funkcję tak by obliczała rozkład EVD zgodnie z zadaniem.
- w, v = np.linalg.eig(A)
- K = v
- L = np.diag(w)
- inv_K = np.linalg.inv(K)
- A2 = K*L*inv_K
- print(' ')
- print(A2)
- print('-------------------------------------------')
- print(A)
- def plot_attractors(A):
- # TODO: Zad. 4.3. Uzupełnij funkcję tak by generowała wykres z atraktorami.
- pass
- def test_A1(vectors):
- """Standard scaling transformation."""
- A = np.array([[2, 0],
- [0, 2]])
- visualize_transformation(A, vectors)
- def test_A2(vectors):
- A = np.array([[-1, 2],
- [2, 1]])
- visualize_transformation(A, vectors)
- def test_A3(vectors):
- A = np.array([[3, 1],
- [0, 2]])
- visualize_transformation(A, vectors)
- if __name__ == "__main__":
- vectors = vectors_uniform(k=8)
- test_A1(vectors)
- test_A2(vectors)
- test_A3(vectors)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement