-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlin_transform.py
More file actions
146 lines (113 loc) · 4 KB
/
lin_transform.py
File metadata and controls
146 lines (113 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#Create X and Y vectors
X = np.linspace(-2, 2, 100)
Y = 2 * X + 1
print(X);print("-"*80);print(Y)
plt.figure()
plt.xlabel("X")
plt.ylabel("Y")
# Set x-axis rangeplt.xlim((-7,7))
# Set y-axis rangeplt.ylim((-7,7))
# Draw lines to split quadrants
plt.plot([-6,6],[0,0], linewidth=2, color='black')
plt.plot([0,0],[6,-6], linewidth=2, color='black' )
plt.plot(X, Y)
plt.show()
X = np.linspace(-2, 2, 100)
Y = 2 * X + 1
Y1 = 2 * X + 3
print(X);print("-"*80);print(Y);print("-"*80);print(Y1)
plt.figure()
plt.xlabel("X")
plt.ylabel("Y")
# Set x-axis rangeplt.xlim((-7,7))
# Set y-axis rangeplt.ylim((-7,7))
# Draw lines to split quadrants
plt.plot([-6,6],[0,0], linewidth=2, color='black')
plt.plot([0,0],[6,-6], linewidth=2, color='black' )
plt.title('Quadrant plot')
plt.plot(X,Y)
plt.plot(X,Y1)
x = np.arange(-10, 10, 1)
y = np.arange(-10, 10, 1)
xx,yy = np.meshgrid(x, y)
print(xx,yy)
plt.scatter(xx, yy, s=20, c=xx+yy)
plt.xlabel("XX")
plt.ylabel("YY")
# Set x-axis rangeplt.xlim((-10,10))
# Set y-axis rangeplt.ylim((-10,10))
# Draw lines to split quadrants
plt.plot([-9,9],[0,0], linewidth=2, color='black')
plt.plot([0,0],[9,-9], linewidth=2, color='black' )
T = np.array([[-1 0],
[0, -1]
])
xy = np.vstack([xx.flatten(), yy.flatten()])
xy.shape
T @ xy[:, 0]
trans = T @ xy
trans.shape
xx_transformed = trans[0].reshape(xx.shape)
yy_transformed = trans[1].reshape(yy.shape)
f, axes = plt.subplots(1, 2, figsize=(6, 3))
#[...] Add axis, x and y witht the same scale
axes[0].scatter(xx, yy, s=10, c=xx+yy)
axes[0].set_xlabel("XX");axes[0].set_ylabel("YY")
axes[0].plot([-9,9],[0,0],linewidth=2,color = "lightgray")
axes[0].plot([0,0],[9,-9],linewidth=2,color = "lightgray")
axes[1].scatter(xx_transformed, yy_transformed, s=10, c=xx+yy)
axes[1].set_xlabel("XX")
axes[1].plot([-9,9],[0,0],linewidth=2,color = "lightgray")
axes[1].plot([0,0],[9,-9],linewidth=2,color = "lightgray")
T = np.array([ [1.3,-2.4],
[0.1, 2]
])
trans = T @ xy
xx_transformed = trans[0].reshape(xx.shape)yy_transformed = trans[1].reshape(yy.shape)
f, axes = plt.subplots(1, 2, figsize=(6, 3))
axes[0].scatter(xx, yy, s=10, c=xx+yy) axes[0].set_xlabel("XX")
axes[0].set_ylabel("YY")
axes[0].plot([-9,9],[0,0],linewidth=2,color = "lightgray")
axes[0].plot([0,0],[9,-9],linewidth=2,color = "lightgray")
axes[1].scatter(xx_transformed, yy_transformed, s=10, c=xx+yy)
axes[1].set_xlabel("XX")
axes[1].plot([-40,40],[0,0],linewidth=1,color = "lightgray")
axes[1].plot([0,0],[40,-40],linewidth=1,color = "lightgray")
T= np.array
([[1.3, -2.4],
[0.1, 2]])
trans = T @ xy
T_inv = np.linalg.inv(T)
un_trans = T_inv @ T @ xy
f, axes = plt.subplots(1, 3, figsize=(9, 3),dpi = 100)
axes[0].scatter(xx, yy, s=10, c=xx+yy) axes[0].set_xlabel("XX")
axes[0].set_ylabel("YY")
axes[0].plot([-15,15],[0,0],linewidth=2,color = "lightgray")
axes[0].plot([0,0],[15,-15],linewidth=2,color = "lightgray")
axes[0].set_title("Initial")
axes[1].scatter(trans[0].reshape(xx.shape),trans[1].reshape(yy.shape),s=10,c=xx+yy)
axes[1].set_xlabel("XX");
axes[1].plot([-40,40],[0,0],linewidth=1,color = "lightgray")
axes[1].plot([0,0],[40,-40],linewidth=1,color = "lightgray")
axes[1].set_title("Transformed")
axes[2].scatter(un_trans[0].reshape(xx.shape),un_trans[1].reshape(yy.shape),s=10,c=xx+yy)
axes[2].set_xlabel("XX");
axes[2].plot([-15,15],[0,0],linewidth=2,color = "lightgray")
axes[2].plot([0,0],[15,-15],linewidth=2,color="lightgray") axes[2].set_title("Transformed back with inverse")
T = np.array([
[3, 6],
[2, 4],
])
trans = T @ xy
f, axes = plt.subplots(1, 2, figsize=(6, 3),dpi = 100)
axes[0].scatter(xx, yy, s=10, c=xx+yy) axes[0].set_xlabel("XX");
axes[0].set_ylabel("YY")
axes[0].plot([-15,15],[0,0],linewidth=2,color = "lightgray")
axes[0].plot([0,0],[15,-15],linewidth=2,color = "lightgray")
axes[0].scatter(trans[0].reshape(xx.shape),trans[1].reshape(yy.shape),s=10,c=xx+yy)
axes[1].axes[1].set_xlabel("XX");
axes[1].plot([-50,50],[0,0],linewidth=2,color = "lightgray")
axes[1].plot([0,0],[50,-50],linewidth=2,color = "lightgray")