# A list of numbers in a matrix is known as a vector, a row from a matrix is known as a row vector, and a column from a matrix is known as a column vector
# Visual intuition behind linear algebra using Vectors
import matplotlib.pyplot as plt
import numpy as np
# This code draws the x and y axis as lines
plt.axhline(0, c='black', lw=0.5)
plt.axvline(0, c='black', lw=0.5)
plt.xlim(-3,3)
plt.ylim(-4,4)
# Geometric intuition of Vectors
# Visualize Vectors in matplotlib using quiver()
plt.quiver(0,0,2,3, angles='xy', scale_units='xy', scale=1, color='blue')
plt.quiver(0,0,-2,-3, angles='xy', scale_units='xy', scale=1, color='blue')
plt.quiver(0,0,1,1, angles='xy', scale_units='xy', scale=1, color='gold')
plt.quiver(0,0,2,2, angles='xy', scale_units='xy', scale=1, color='gold')
plt.show()
# Vector Operations
# To visualize Vectors addition, we need to connect the Vectors together instead of having all of the Vectors originate at (0,0)
import matplotlib.pyplot as plt
import numpy as np
# This code draws the x and y axis as lines
plt.axhline(0, c='black', lw=0.5)
plt.axvline(0, c='black', lw=0.5)
plt.xlim(-4,4)
plt.ylim(-1,4)
# Plot the vector
plt.quiver(0,0,3, 0, angles='xy', scale_units='xy', scale=1, color='blue')
plt.quiver(3,0,0,3, angles='xy', scale_units='xy', scale=1, color='blue')
plt.quiver(0,0,3,3, angles='xy', scale_units='xy', scale=1, color='green')
plt.show()
# Scaling Vectors by multiplying or dividing by a scalar (a real number)
# This code draws the x and y axis as lines
plt.axhline(0, c='black', lw=0.5)
plt.axvline(0, c='black', lw=0.5)
plt.xlim(0,10)
plt.ylim(0,5)
# Remember that the vectors originate at (0,0)
plt.quiver(0, 0, 3, 1, angles='xy', scale_units='xy', scale=1, color='blue')
plt.quiver(0, 0, 6, 2, angles='xy', scale_units='xy', scale=1, color='green')
plt.quiver(0, 0, 9, 3, angles='xy', scale_units='xy', scale=1, color='orange')
# Vectors in NumPy
# numpy.asarray() function to represent Vectors
import numpy as np
vector_one = np.asarray([
[1],
[2],
[1]
], dtype=np.float32)
vector_two = np.asarray([
[3],
[0],
[1]
], dtype=np.float32)
vector_linear_combination = 2*vector_one + 5*vector_two
print(vector_linear_combination)
# Dot Product
# To compute the dot product, we need to sum the products of the 2 values in each position in each Vector
vector_one = np.asarray([
[1],
[2],
[1]
], dtype=np.float32)
print("vector_one:", vector_one)
vector_two = np.asarray([
[3],
[0],
[1]
], dtype=np.float32)
print("vector_two", vector_two)
# The main quirk is that one of the two vectors need to be represented as a row vector while the other a column vector
dot_product = np.dot(vector_one[:,0], vector_two)
print("dot_product:", dot_product) # the result of a dot product is a scalar value
print("row vector:", vector_one[:,0]) # represented as row vector
print("column vector:", vector_two) # represented as column vector
# Linear Combination
# Being able to scale vectors using scalar multiplication then adding or subtracting these scaled vectors is known as linear combination
# Recall that we use the * operator to multiply a vector by a scalar and the numpy.dot() function when computing the dot product between 2 vectors
# Assign the vector
w = np.asarray([
[1],
[2]
], dtype=np.float32)
v = np.asarray([
[3],
[1]
], dtype=np.float32)
# Multiply v by 2 and subtract 2 times w
end_point = 2*v - 2*w
print(end_point)