Last active
February 23, 2022 15:26
-
-
Save codistwa/aadfd4d13fe215c7c5c24e8ec215f427 to your computer and use it in GitHub Desktop.
Course source code: https://codistwa.com/guides/linear-algebra. More courses on https://codistwa.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================================================ | |
# What is a vector? | |
# ============================================================ | |
scalar = [3] # scalar | |
row = np.array([2,1], dtype=object) # row matrice / vector | |
col = np.array([ [3], [-1] ], dtype=object) # column matrice / vector | |
display(Math(sym.latex(sym.sympify(scalar)))) | |
display(Math(sym.latex(sym.sympify(row)))) | |
display(Math(sym.latex(sym.sympify(col)))) | |
ax = plt.axes() | |
ax.arrow(0, 0, row[0], row[1], head_width=0.2, label='$row$', head_length=0.2, color='r', length_includes_head=True) | |
ax.arrow(0, 0, col[0][0], col[1][0], head_width=0.2, label='$column$', head_length=0.2, color='b', length_includes_head=True) | |
plt.axis('square') | |
plt.legend() | |
plt.grid() | |
plt.show() | |
plt.title('Row and columns vectors',fontsize=12) | |
plt.savefig('row_col_vectors.png', bbox_inches='tight') | |
# ============================================================ | |
# What is a 3D vector? | |
# ============================================================ | |
vector1_3D = np.array([ [2], [3], [-2] ], dtype=object) | |
vector2_3D = np.array([ [2], [-4], [1] ], dtype=object) | |
vector3_3D = np.array([ [2], [1], [4] ], dtype=object) | |
display(Math(sym.latex(sym.sympify(vector1_3D)))) | |
display(Math(sym.latex(sym.sympify(vector2_3D)))) | |
display(Math(sym.latex(sym.sympify(vector3_3D)))) | |
fig = plt.figure() | |
ax = fig.add_subplot(projection='3d') | |
# draw vectors | |
ax.plot([0,vector1_3D[0]],[0,vector1_3D[1]],[0,vector1_3D[2]],'b',linewidth=3) | |
ax.plot([0,vector2_3D[0]],[0,vector2_3D[1]],[0,vector2_3D[2]],'r',linewidth=3) | |
ax.plot([0,vector3_3D[0]],[0,vector3_3D[1]],[0,vector3_3D[2]],'g',linewidth=3) | |
# guidelines | |
ax.plot([-5,5],[0,0],[0,0],'--',color=[.7,.7,.7]) | |
ax.plot([0,0],[-5,5],[0,0],'--',color=[.7,.7,.7]) | |
ax.plot([0,0],[0,0],[-5,5],'--',color=[.7,.7,.7]) | |
ax.set_xlim3d(-5,5) | |
ax.set_ylim3d(-5,5) | |
ax.set_zlim3d(-5,5) | |
ax.set_xlabel('X') | |
ax.set_ylabel('Y') | |
ax.set_zlabel('Z') | |
fig.savefig('3d_vectors.png', bbox_inches='tight') | |
plt.show() | |
# ============================================================ | |
# Sum of two vectors | |
# ============================================================ | |
vector1 = np.array([ [3], [-2] ], dtype=object) | |
vector2 = np.array([ [2], [-4] ], dtype=object) | |
display(Math(sym.latex(sym.sympify(vector1)))) | |
display(Math(sym.latex(sym.sympify(vector2)))) | |
vector_sum = vector1 + vector2 | |
vector_sum = np.add(vector1, vector2) | |
vector1_show = sym.latex(sym.sympify(vector1)) | |
vector2_show = sym.latex(sym.sympify(vector2)) | |
vector3_show = sym.latex(sym.sympify(vector_sum)) | |
display(Math('%s + %s=%s' %(vector1_show, vector2_show, vector3_show))) | |
ax = plt.axes() | |
ax.arrow(0, 0, vector1[0][0], vector1[1][0], label='$vector1$', head_width=0.2, head_length=0.2, color='b', length_includes_head=True) | |
ax.arrow(vector1[0][0], vector1[1][0], vector2[0][0], vector2[1][0], label='$vector2$', head_width=0.2, head_length=0.2, color='r', length_includes_head=True) | |
ax.arrow(0, 0, vector_sum[0][0], vector_sum[1][0], label='$vector1 + vector2$', head_width=0.2, head_length=0.2, color='g', length_includes_head=True) | |
plt.axis('on') | |
plt.legend() | |
plt.grid() | |
plt.title('Sum of two vectors',fontsize=12) | |
plt.savefig('sum_two_vectors.png', bbox_inches='tight') | |
plt.show() | |
# ============================================================ | |
# Difference of two vectors | |
# ============================================================ | |
vector1 = np.array([ [3], [-2] ], dtype=object) | |
vector2 = np.array([ [2], [-4] ], dtype=object) | |
vector2_inverse = np.array([ [-2], [4] ], dtype=object) | |
vector_difference = vector1 - vector2 | |
vector4_show = sym.latex(sym.sympify(vector_difference)) | |
display(Math('%s - %s=%s' %(vector1_show, vector2_show, vector4_show))) | |
ax = plt.axes() | |
ax.arrow(0, 0, vector1[0][0], vector1[1][0], head_width=0.2, label='$vector1$', head_length=0.2, color='b', length_includes_head=True) | |
ax.arrow(vector1[0][0], vector1[1][0], vector2_inverse[0][0], vector2_inverse[1][0], label='$vector2$', head_width=0.2, head_length=0.2, color='r', length_includes_head=True) | |
ax.arrow(0, 0, vector_difference[0][0], vector_difference[1][0], label='$vector1-vector2$', head_width=0.2, head_length=0.2, color='g', length_includes_head=True) | |
plt.title('Difference of two vectors',fontsize=12) | |
plt.axis('on') | |
plt.legend() | |
plt.grid() | |
plt.show() | |
plt.savefig('difference_two_vectors.png', bbox_inches='tight') | |
# ============================================================ | |
# Multiplying a vector by a scalar | |
# ============================================================ | |
vector_scalar = vector1 * scalar | |
vector5_show = sym.latex(sym.sympify(vector_scalar)) | |
display(Math('%s X % s=%s' %(vector1_show, scalar[0], vector5_show))) | |
# ============================================================ | |
# Multiplying a vector by a vector - the dot product | |
# ============================================================ | |
vector_dot = vector1 * vector2 | |
v1 = np.array([1, 2, 3]) | |
v2 = np.array([-3, 1, -4]) | |
print(v1 @ v2) # python operator https://www.python.org/dev/peps/pep-0465/ | |
print(np.dot(v1, v2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment