Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/notebooks/linear_algebra_visulization.py
Views: 449
import matplotlib.pyplot as plt1import numpy as np2import numpy3def linearCombo(a, b, c):4'''This function is for visualizing linear combination of standard basis in 3D.5Function syntax: linearCombo(a, b, c), where a, b, c are the scalar multiplier,6also the elements of the vector.7'''8fig = plt.figure(figsize = (10,10))9ax = fig.add_subplot(projection='3d')1011######################## Standard basis and Scalar Multiplid Vectors#########################12vec = np.array([[[0, 0, 0, 1, 0, 0]], # e113[[0, 0, 0, 0, 1, 0]], # e214[[0, 0, 0, 0, 0, 1]], # e315[[0, 0, 0, a, 0, 0]], # a* e116[[0, 0, 0, 0, b, 0]], # b* e217[[0, 0, 0, 0, 0, c]], # c* e318[[0, 0, 0, a, b, c]]]) # ae1 + be2 + ce319colors = ['b','b','b','r','r','r','g']20for i in range(vec.shape[0]):21X, Y, Z, U, V, W = zip(*vec[i,:,:])22ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False,23color = colors[i] ,arrow_length_ratio = .08, pivot = 'tail',24linestyles = 'solid',linewidths = 3, alpha =.6)2526#################################Plot Rectangle Boxes##############################27dlines = np.array([[[a, 0, 0],[a, b, 0]],28[[0, b, 0],[a, b, 0]],29[[0, 0, c],[a, b, c]],30[[0, 0, c],[a, 0, c]],31[[a, 0, c],[a, b, c]],32[[0, 0, c],[0, b, c]],33[[0, b, c],[a, b, c]],34[[a, 0, 0],[a, 0, c]],35[[0, b, 0],[0, b, c]],36[[a, b, 0],[a, b, c]]])37colors = ['k','k','g','k','k','k','k','k','k']38for i in range(dlines.shape[0]):39ax.plot(dlines[i,:,0], dlines[i,:,1], dlines[i,:,2], lw =3, ls = '--', color = 'black', alpha=0.5)4041#################################Annotation########################################42ax.text(x = a, y = b, z = c, s= ' $(%0.d, %0.d, %.0d)$'% (a, b, c), size = 18)43ax.text(x = a, y = 0, z = 0, s= ' $%0.d e_1 = (%0.d, 0, 0)$'% (a, a), size = 15)44ax.text(x = 0, y = b, z = 0, s= ' $%0.d e_2 = (0, %0.d, 0)$'% (b, b), size = 15)45ax.text(x = 0, y = 0, z = c, s= ' $%0.d e_3 = (0, 0, %0.d)$' %(c, c), size = 15)4647#################################Axis Setting######################################48ax.grid()49ax.set_xlim([0, a+1])50ax.set_ylim([0, b+1])51ax.set_zlim([0, c+1])5253ax.set_xlabel('x-axis', size = 18)54ax.set_ylabel('y-axis', size = 18)55ax.set_zlabel('z-axis', size = 18)5657ax.set_title('Vector $(%0.d, %0.d, %.0d)$ Visualization' %(a, b, c), size = 20)5859ax.view_init(elev=20., azim=15)6061if __name__ == '__main__':62a = 763b = 464c = 965linearCombo(a, b, c)6667def linearComboNonStd(a, b, c, vec1, vec2, vec3):68'''This function is for visualizing linear combination of non-standard basis in 3D.69Function syntax: linearCombo(a, b, c, vec1, vec2, vec3), where a, b, c are the scalar multiplier,70ve1, vec2 and vec3 are the basis.71'''72fig = plt.figure(figsize = (10,10))73ax = fig.add_subplot(projection='3d')74########################Plot basis##############################75vec1 = np.array([[0, 0, 0, vec1[0], vec1[1], vec1[2]]])76X, Y, Z, U, V, W = zip(*vec1)77ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'blue',arrow_length_ratio = .08, pivot = 'tail',78linestyles = 'solid',linewidths = 3)7980vec2 = np.array([[0, 0, 0, vec2[0], vec2[1], vec2[2]]])81X, Y, Z, U, V, W = zip(*vec2)82ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'blue',arrow_length_ratio = .08, pivot = 'tail',83linestyles = 'solid',linewidths = 3)8485vec3 = np.array([[0, 0, 0, vec3[0], vec3[1], vec3[2]]])86X, Y, Z, U, V, W = zip(*vec3)87ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'blue',arrow_length_ratio = .08, pivot = 'tail',88linestyles = 'solid',linewidths = 3)8990###########################Plot Scalar Muliplied Vectors####################91avec1 = a * vec192X, Y, Z, U, V, W = zip(*avec1)93ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'red', alpha = .6,arrow_length_ratio = a/100, pivot = 'tail',94linestyles = 'solid',linewidths = 3)9596bvec2 = b * vec297X, Y, Z, U, V, W = zip(*bvec2)98ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'red', alpha = .6,arrow_length_ratio = b/100, pivot = 'tail',99linestyles = 'solid',linewidths = 3)100101cvec3 = c * vec3102X, Y, Z, U, V, W = zip(*cvec3)103ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'red', alpha = .6,arrow_length_ratio = c/100, pivot = 'tail',104linestyles = 'solid',linewidths = 3)105106combo = avec1 + bvec2 + cvec3107X, Y, Z, U, V, W = zip(*combo)108ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'green', alpha = .7,arrow_length_ratio = np.linalg.norm(combo)/300, pivot = 'tail',109linestyles = 'solid',linewidths = 3)110111#################################Plot Rectangle Boxes##############################112point1 = [avec1[0, 3], avec1[0, 4], avec1[0, 5]]113point2 = [avec1[0, 3]+bvec2[0, 3], avec1[0, 4]+bvec2[0, 4], avec1[0, 5]+bvec2[0, 5]]114line1 = np.array([point1, point2])115ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)116117point1 = [bvec2[0, 3], bvec2[0, 4], bvec2[0, 5]]118point2 = [avec1[0, 3]+bvec2[0, 3], avec1[0, 4]+bvec2[0, 4], avec1[0, 5]+bvec2[0, 5]]119line1 = np.array([point1, point2])120ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)121122point1 = [bvec2[0, 3], bvec2[0, 4], bvec2[0, 5]]123point2 = [cvec3[0, 3]+bvec2[0, 3], cvec3[0, 4]+bvec2[0, 4], cvec3[0, 5]+bvec2[0, 5]]124line1 = np.array([point1, point2])125ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)126127point1 = [cvec3[0, 3], cvec3[0, 4], cvec3[0, 5]]128point2 = [cvec3[0, 3]+bvec2[0, 3], cvec3[0, 4]+bvec2[0, 4], cvec3[0, 5]+bvec2[0, 5]]129line1 = np.array([point1, point2])130ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)131132point1 = [cvec3[0, 3], cvec3[0, 4], cvec3[0, 5]]133point2 = [cvec3[0, 3]+avec1[0, 3], cvec3[0, 4]+avec1[0, 4], cvec3[0, 5]+avec1[0, 5]]134line1 = np.array([point1, point2])135ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)136137point1 = [avec1[0, 3], avec1[0, 4], avec1[0, 5]]138point2 = [cvec3[0, 3]+avec1[0, 3], cvec3[0, 4]+avec1[0, 4], cvec3[0, 5]+avec1[0, 5]]139line1 = np.array([point1, point2])140ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)141142##143point1 = [avec1[0, 3]+bvec2[0, 3]+cvec3[0, 3],144avec1[0, 4]+bvec2[0, 4]+cvec3[0, 4],145avec1[0, 5]+bvec2[0, 5]+cvec3[0, 5]]146point2 = [cvec3[0, 3]+avec1[0, 3],147cvec3[0, 4]+avec1[0, 4],148cvec3[0, 5]+avec1[0, 5]]149line1 = np.array([point1, point2])150ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)151152##153point1 = [avec1[0, 3]+bvec2[0, 3]+cvec3[0, 3],154avec1[0, 4]+bvec2[0, 4]+cvec3[0, 4],155avec1[0, 5]+bvec2[0, 5]+cvec3[0, 5]]156point2 = [cvec3[0, 3]+bvec2[0, 3],157cvec3[0, 4]+bvec2[0, 4],158cvec3[0, 5]+bvec2[0, 5]]159line1 = np.array([point1, point2])160ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)161162##163point1 = [avec1[0, 3]+bvec2[0, 3]+cvec3[0, 3],164avec1[0, 4]+bvec2[0, 4]+cvec3[0, 4],165avec1[0, 5]+bvec2[0, 5]+cvec3[0, 5]]166point2 = [bvec2[0, 3]+avec1[0, 3],167bvec2[0, 4]+avec1[0, 4],168bvec2[0, 5]+avec1[0, 5]]169line1 = np.array([point1, point2])170ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)171#################################Annotation########################################172ax.text(x = vec1[0,3], y = vec1[0,4], z = vec1[0,5], s= ' $v_1 =(%0.d, %0.d, %.0d)$'% (vec1[0,3], vec1[0,4], vec1[0,4]), size = 8)173ax.text(x = vec2[0,3], y = vec2[0,4], z = vec2[0,5], s= ' $v_2 =(%0.d, %0.d, %.0d)$'% (vec2[0,3], vec2[0,4], vec2[0,4]), size = 8)174ax.text(x = vec3[0,3], y = vec3[0,4], z = vec3[0,5], s= ' $v_3= (%0.d, %0.d, %.0d)$'% (vec3[0,3], vec3[0,4], vec3[0,4]), size = 8)175176ax.text(x = avec1[0,3], y = avec1[0,4], z = avec1[0,5], s= ' $%.0d v_1 =(%0.d, %0.d, %.0d)$'% (a, avec1[0,3], avec1[0,4], avec1[0,4]), size = 8)177ax.text(x = bvec2[0,3], y = bvec2[0,4], z = bvec2[0,5], s= ' $%.0d v_2 =(%0.d, %0.d, %.0d)$'% (b, bvec2[0,3], bvec2[0,4], bvec2[0,4]), size = 8)178ax.text(x = cvec3[0,3], y = cvec3[0,4], z = cvec3[0,5], s= ' $%.0d v_3= (%0.d, %0.d, %.0d)$'% (c, cvec3[0,3], cvec3[0,4], cvec3[0,4]), size = 8)179# ax.text(x = 0, y = b, z = 0, s= ' $%0.d e_2 = (0, %0.d, 0)$'% (b, b), size = 15)180# ax.text(x = 0, y = 0, z = c, s= ' $%0.d e_3 = (0, 0, %0.d)$' %(c, c), size = 15)181182#################################Axis Setting######################################183ax.grid()184ax.set_xlim([0, 15])185ax.set_ylim([0, 15])186ax.set_zlim([0, 15])187188ax.set_xlabel('x-axis', size = 18)189ax.set_ylabel('y-axis', size = 18)190ax.set_zlabel('z-axis', size = 18)191192#ax.set_title('Vector $(%0.d, %0.d, %.0d)$ Visualization' %(a, b, c), size = 20)193194ax.view_init(elev=20., azim=15)195196if __name__ == '__main__':197a = 2198b = 3199c = 4200vec1 = np.array([2,1,0])201vec2 = np.array([0,3,1])202vec3 = np.array([1,2,3])203linearComboNonStd(a, b, c, vec1,vec2,vec3)204205206207