Vectors
Contents
## This cell just imports necessary modules
import numpy as np
import plotly.graph_objs as go
# This cell shows a function that can plot vectors using the Scatter3d() in plotly.
#In the plots the vectors have a big point to mark the direction.
def vector_plot(tvects,is_vect=True,orig=[0,0,0]):
"""Plot vectors using plotly"""
if is_vect:
if not hasattr(orig[0],"__iter__"):
coords = [[orig,np.sum([orig,v],axis=0)] for v in tvects]
else:
coords = [[o,np.sum([o,v],axis=0)] for o,v in zip(orig,tvects)]
else:
coords = tvects
data = []
for i,c in enumerate(coords):
X1, Y1, Z1 = zip(c[0])
X2, Y2, Z2 = zip(c[1])
vector = go.Scatter3d(x = [X1[0],X2[0]],
y = [Y1[0],Y2[0]],
z = [Z1[0],Z2[0]],
marker = dict(size = [0,5],
color = ['blue'],
line=dict(width=5,
color='DarkSlateGrey')),
name = 'Vector'+str(i+1))
data.append(vector)
layout = go.Layout(
margin = dict(l = 4,
r = 4,
b = 4,
t = 4)
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Vectors#
This notebook will illustrate how to apply the maths discussed in the lecture using Python.
In these notebooks, we will adopt the following prefix convention when naming variables:
's' (e.g. sDotProduct) means the variable is a scalar
'v' (e.g. vCrossProduct) means the variable is a vector
'm' (e.g. mA) means the variable is a matrix
# Let's define two vectors, by listing their components
vA = [1, 2, 1]
vB = [-1, 1, 0]
# Convert vA and vB from a list to an array so we can use numpy
# to perform vector operations on them.
vA = np.array(vA)
vB = np.array(vB)
print("Plot of vA (blue) and vB (red)")
vector_plot([vA,vB])
Plot of vA (blue) and vB (red)