anp-scp commited on
Commit
b8c9155
·
unverified ·
1 Parent(s): e09a721

added option for normal space

Browse files
Files changed (2) hide show
  1. app.py +29 -8
  2. utils.py +6 -4
app.py CHANGED
@@ -10,13 +10,12 @@ ylim = (-10,10)
10
 
11
  st.title("Eigen Values and Eigen Vectors")
12
  st.write(
13
- "This app shows the effect of linear transformation with respect to eigen values and eigen vectors"
14
- )
15
 
16
  with st.sidebar:
17
  data = st.selectbox('Select type of dataset', ['Square', 'Circle'])
18
  st.write("---")
19
- st.text("Enter transformation matrix elements")
20
  minv = -5.0
21
  maxv = 5.0
22
  step = 0.1
@@ -24,7 +23,14 @@ with st.sidebar:
24
  a_01 = st.slider(label = '$A_{0,1}$', min_value = minv, max_value=maxv, value=0.0, step=step)
25
  a_10 = st.slider(label = '$A_{1,0}$', min_value = minv, max_value=maxv, value=0.0, step=step)
26
  a_11 = st.slider(label = '$A_{1,1}$', min_value = minv, max_value=maxv, value=1.0, step=step)
27
- t = np.array([[a_00,a_01], [a_10, a_11]], dtype=np.float64)
 
 
 
 
 
 
 
28
 
29
  x = np.linspace(-1,1,1000)
30
  y = getSquareYVectorised(x) if data == 'Square' else getCircle(x)
@@ -33,18 +39,33 @@ x_dash_up, y_dash_up = transform(x,y,t)
33
  x_dash_down, y_dash_down = transform(x,-y,t)
34
 
35
  evl, evec = np.linalg.eig(t)
 
36
  fig, ax = plt.subplots()
 
 
 
 
 
 
 
 
 
37
  ax.plot(x_dash_up,y_dash_up,'r')
38
  ax.plot(x_dash_down,y_dash_down, 'g')
39
  if not (np.iscomplex(evl).any() or np.iscomplex(evec).any()):
40
- ax.quiver(0,0,evec[0,0]*evl[0],evec[1,0]*evl[0],scale=1,scale_units ='xy',angles='xy', facecolor='yellow', label='$eigen\ vector_{\lambda_0}$')
41
- ax.quiver(0,0,evec[0,1]*evl[1],evec[1,1]*evl[1],scale=1,scale_units ='xy',angles='xy', facecolor='blue',label='$eigen\ vector_{\lambda_1}$')
 
 
42
 
43
- plotGridLines(xlim,ylim,t)
44
  ax.set_xlim(*xlim)
45
  ax.set_ylim(*ylim)
46
  ax.set_aspect('equal', adjustable='box')
47
- fig.legend()
 
 
 
 
48
  st.pyplot(fig)
49
 
50
  df = pd.DataFrame({'Eigen Values': evl, 'Eigen Vectors': [str(evec[:,0]), str(evec[:,1])],\
 
10
 
11
  st.title("Eigen Values and Eigen Vectors")
12
  st.write(
13
+ "This app shows the effect of linear transformation with respect to eigen values and eigen vectors")
 
14
 
15
  with st.sidebar:
16
  data = st.selectbox('Select type of dataset', ['Square', 'Circle'])
17
  st.write("---")
18
+ st.text("Select elements of transformation\nmatrix (A)")
19
  minv = -5.0
20
  maxv = 5.0
21
  step = 0.1
 
23
  a_01 = st.slider(label = '$A_{0,1}$', min_value = minv, max_value=maxv, value=0.0, step=step)
24
  a_10 = st.slider(label = '$A_{1,0}$', min_value = minv, max_value=maxv, value=0.0, step=step)
25
  a_11 = st.slider(label = '$A_{1,1}$', min_value = minv, max_value=maxv, value=1.0, step=step)
26
+ t = np.array([[a_00,a_01], [a_10, a_11]], dtype=np.float64)
27
+ st.write("---")
28
+ st.write("The transformation matrix A is:")
29
+ st.table(pd.DataFrame(t))
30
+ st.write("---")
31
+ showNormalSpace = st.checkbox(label= 'Show normal space (without transform)', value=False)
32
+
33
+
34
 
35
  x = np.linspace(-1,1,1000)
36
  y = getSquareYVectorised(x) if data == 'Square' else getCircle(x)
 
39
  x_dash_down, y_dash_down = transform(x,-y,t)
40
 
41
  evl, evec = np.linalg.eig(t)
42
+ det = np.linalg.det(t)
43
  fig, ax = plt.subplots()
44
+
45
+ if showNormalSpace:
46
+ ax.plot(x, y, 'r', alpha=0.5)
47
+ ax.plot(x, -y, 'g', alpha=0.5)
48
+ if not np.iscomplex(evec).any():
49
+ ax.quiver(0,0,evec[0,0],evec[1,0],scale=1,scale_units ='xy',angles='xy', facecolor='black', alpha=0.5)
50
+ ax.quiver(0,0,evec[0,1],evec[1,1],scale=1,scale_units ='xy',angles='xy', facecolor='black', alpha=0.5)
51
+ plotGridLines(xlim,ylim,np.array([[1,0], [0,1]]),'#9D9D9D','Normal Space',0.4)
52
+
53
  ax.plot(x_dash_up,y_dash_up,'r')
54
  ax.plot(x_dash_down,y_dash_down, 'g')
55
  if not (np.iscomplex(evl).any() or np.iscomplex(evec).any()):
56
+ ax.quiver(0,0,evec[0,0]*evl[0],evec[1,0]*evl[0],scale=1,scale_units ='xy',angles='xy', facecolor='cyan', label='$eigen\ vector_{\lambda_0}$')
57
+ ax.quiver(0,0,evec[0,1]*evl[1],evec[1,1]*evl[1],scale=1,scale_units ='xy',angles='xy', facecolor='blue', label='$eigen\ vector_{\lambda_1}$')
58
+ plotGridLines(xlim,ylim,t,'#403B3B','Transformed space',0.6)
59
+ ax.text(11,5,'|A|={}'.format(det))
60
 
 
61
  ax.set_xlim(*xlim)
62
  ax.set_ylim(*ylim)
63
  ax.set_aspect('equal', adjustable='box')
64
+ ax.xaxis.set_tick_params(labelbottom=False)
65
+ ax.yaxis.set_tick_params(labelleft=False)
66
+ ax.set_xticks([])
67
+ ax.set_yticks([])
68
+ fig.legend(bbox_to_anchor=(1.05, 0.86), loc=1, borderaxespad=0., fontsize=8)
69
  st.pyplot(fig)
70
 
71
  df = pd.DataFrame({'Eigen Values': evl, 'Eigen Vectors': [str(evec[:,0]), str(evec[:,1])],\
utils.py CHANGED
@@ -17,15 +17,17 @@ def transform(x,y,t):
17
  result = t @ points
18
  return result[0,:], result[1,:]
19
 
20
- def plotGridLines(xlim,ylim,t):
21
  for i in range(xlim[0]-20,xlim[1]+21):
22
  x = [i,i]
23
  y = [ylim[0]-20,ylim[1]+20]
24
  x,y = transform(x,y,t)
25
- plt.plot(x,y, color='#DAD5D4',linestyle='dashed',linewidth=0.5)
 
 
 
26
  for i in range(ylim[0]-20,ylim[1]+21):
27
  y = [i,i]
28
  x = [xlim[0]-20,xlim[1]+20]
29
  x,y = transform(x,y,t)
30
- plt.plot(x,y, color='#DAD5D4',linestyle='dashed',linewidth=0.5)
31
-
 
17
  result = t @ points
18
  return result[0,:], result[1,:]
19
 
20
+ def plotGridLines(xlim,ylim,t,color,label,linewidth):
21
  for i in range(xlim[0]-20,xlim[1]+21):
22
  x = [i,i]
23
  y = [ylim[0]-20,ylim[1]+20]
24
  x,y = transform(x,y,t)
25
+ if i == xlim[0]-20:
26
+ plt.plot(x,y, color=color,linestyle='dashed',linewidth=linewidth,label=label)
27
+ else:
28
+ plt.plot(x,y, color=color,linestyle='dashed',linewidth=linewidth)
29
  for i in range(ylim[0]-20,ylim[1]+21):
30
  y = [i,i]
31
  x = [xlim[0]-20,xlim[1]+20]
32
  x,y = transform(x,y,t)
33
+ plt.plot(x,y, color=color,linestyle='dashed',linewidth=linewidth)