Spaces:
Runtime error
Runtime error
from matplotlib import pyplot as plt | |
import numpy as np | |
import streamlit as st | |
import pandas as pd | |
from utils import getSquareYVectorised, getCircle, transform, plotGridLines | |
np.set_printoptions(precision=3) | |
xlim = (-10,10) | |
ylim = (-10,10) | |
st.title("Eigen Values and Eigen Vectors") | |
st.write( | |
"This app shows the effect of linear transformation with respect to eigen values and eigen vectors" | |
) | |
with st.sidebar: | |
data = st.selectbox('Select type of dataset', ['Square', 'Circle']) | |
st.write("---") | |
st.text("Enter transformation matrix elements") | |
minv = -5.0 | |
maxv = 5.0 | |
step = 0.1 | |
a_00 = st.slider(label = '$A_{0,0}$', min_value = minv, max_value=maxv, value=1.0, step=step) | |
a_01 = st.slider(label = '$A_{0,1}$', min_value = minv, max_value=maxv, value=0.0, step=step) | |
a_10 = st.slider(label = '$A_{1,0}$', min_value = minv, max_value=maxv, value=0.0, step=step) | |
a_11 = st.slider(label = '$A_{1,1}$', min_value = minv, max_value=maxv, value=1.0, step=step) | |
t = np.array([[a_00,a_01], [a_10, a_11]], dtype=np.float64) | |
x = np.linspace(-1,1,1000) | |
y = getSquareYVectorised(x) if data == 'Square' else getCircle(x) | |
x_dash_up, y_dash_up = transform(x,y,t) | |
x_dash_down, y_dash_down = transform(x,-y,t) | |
evl, evec = np.linalg.eig(t) | |
fig, ax = plt.subplots() | |
ax.plot(x_dash_up,y_dash_up,'r') | |
ax.plot(x_dash_down,y_dash_down, 'g') | |
if not (np.iscomplex(evl).any() or np.iscomplex(evec).any()): | |
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}$') | |
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}$') | |
plotGridLines(xlim,ylim,t) | |
ax.set_xlim(*xlim) | |
ax.set_ylim(*ylim) | |
ax.set_aspect('equal', adjustable='box') | |
fig.legend() | |
st.pyplot(fig) | |
df = pd.DataFrame({'Eigen Values': evl, 'Eigen Vectors': [str(evec[:,0]), str(evec[:,1])],\ | |
'Transformed Eigen Vectors': [str(evec[:,0]*evl[0]), str(evec[:,1]*evl[1])]}) | |
st.table(df) | |
if np.iscomplex(evl).any() or np.iscomplex(evec).any(): | |
st.write("Due to complex eigen vectors or eigne values, the transformed eigen vectors are not\ | |
displayed...") |