## 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#

Mathematics Methods 1

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)

Null vector#

Slide 9

# vNull is the null vector (a vector of all zeros,
# created using the zeros function)
vNull = np.zeros(3)
print("The null vector is: ", vNull)
The null vector is:  [0. 0. 0.]

Vector equality#

Slide 9

# Here we compare vectors vA and vB. This is done element-by-element.
# The .all() function allows us to check that the elements of vA.
# are ALL equal to those of vB. In other words, vA[i] == vB[i] for i = 1, 2, 3
if(np.equal(vA,vB).all()):
   print("Vectors vA and vB are equal")
else:
   print("Vectors vA and vB are NOT equal")
Vectors vA and vB are NOT equal

Multiplication of a vector by a scalar#

Slide 9

print("2*vA = ", 2*vA)
print("10*vB = ", 10*vB)

print("Plot of 2*vA (blue) and 10*vB (red)")
vector_plot([2*vA,10*vB])
2*vA =  [2 4 2]
10*vB =  [-10  10   0]
Plot of 2*vA (blue) and 10*vB (red)

Vector addition and subtraction#

Slide 10

print("vA + vB = ", vA + vB)
print("vA - vB = ", vA - vB)

print("Plot of vA + vB (blue) and vA - vB (red)")
vector_plot([vA + vB,vA - vB])
vA + vB =  [0 3 1]
vA - vB =  [2 1 1]
Plot of vA + vB (blue) and vA - vB (red)

Vector magnitude#

Slide 7

Compute the magnitude (i.e. Euclidean/L2 norm) of vectors vA and vB (to 3 significant figures). There are many ways to do this in Python. Try using the sqrt and dot functions (see below) to achieve the same result.

sMagnitude_vA = np.linalg.norm(vA, ord=2)
sMagnitude_vB = np.linalg.norm(vB, ord=2)
print("Magnitude of vector vA is %.3f" % sMagnitude_vA)
print("Magnitude of vector vB is %.3f" % sMagnitude_vB)
Magnitude of vector vA is 2.449
Magnitude of vector vB is 1.414

Dot product#

Slide 15

Remember: the dot product of two vectors always returns a scalar

sDotProduct_vAvB = np.dot(vA,vB)
print("The dot product of vA and vB is %.3f" % sDotProduct_vAvB)
The dot product of vA and vB is 1.000

Angle between two vectors#

Slide 18

# NOTE: arccos is inverse cos
sTheta = np.arccos(sDotProduct_vAvB/(sMagnitude_vA*sMagnitude_vB))
print("The angle between vA and vB is %.3f radians (or %.3f degrees)" 
      % (sTheta, sTheta*(180/np.pi)))
The angle between vA and vB is 1.278 radians (or 73.221 degrees)

Cross product#

Slide 19

Remember: the cross product of two vectors always returns another vector. We could use np.cross:

print("The cross product of vA and vB is", np.cross(vA,vB))
The cross product of vA and vB is [-1 -1  3]

Alternatively, we could compute determinants as in slide 22:

# Remember: in Python, array indices always start from zero,
# so the x, y and z components have an index of 0, 1 and 2 respectively.
vCrossProduct = [ np.linalg.det([[vA[1], vA[2]],
                                [vB[1], vB[2]]]), 
                         
              np.linalg.det([[vA[0], vA[2]],
                                [vB[0], vB[2]]]), 
              
              np.linalg.det([[vA[0], vA[1]],
                                [vB[0], vB[1]]]) ]

print("The cross product of vA and vB is", vCrossProduct)

print("Plot of vA (blue),vB (red) and the cross product of vA and vB (green)")
vector_plot([vA,vB,np.cross(vA,vB)])
The cross product of vA and vB is [-1.0, 1.0, 3.0000000000000004]
Plot of vA (blue),vB (red) and the cross product of vA and vB (green)