Matthew Bennett

Logo

I am a data scientist working on time series forecasting (using R and Python 3) at the London Ambulance Service NHS Trust. I earned my PhD in cognitive neuroscience at the University of Glasgow working with fmri data and neural networks. I favour linux machines, and working in the terminal with Vim as my editor of choice.

View my GitHub Profile

View my LinkedIn Profile

View my CV

Dot product, length and matrix multiplication

Dot product

The dot product is only valid for pairs of vectors (with the same number of entries). You simply match up each element between the vectors, multiply them and add the results:

$$ \begin{equation} u \cdot v =% \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix} \cdot \begin{bmatrix} 4 \\ 5 \\ 6 \end{bmatrix} = 32 \end{equation} $$

Code implementation

We make both vectors row vectors and then carry out the multiplication and sum operations.

def dot(self, new_mat):
    # make both vectors rows with transpose
    if self.size(0) != 1:
        self = self.tr()
    if new_mat.size(0) != 1:
        new_mat = new_mat.tr()
    dot_prod = []
    for cols in zip(self.data[0], new_mat.data[0]):
        dot_prod.append(cols[0]*cols[1])
    dot_prod = sum(dot_prod)
    return dot_prod

Demo

We create two vectors, call the dot method and print the result:

import linalg as la

u = la.Mat([[1, 2, 2]])

v = la.Mat([[1, 1, 3]])

dotted = u.dot(v)

print(dotted)

Outputs:

>>> print(dotted)
9

Length

The length of a vector $u$ is defined as the square root of the dot product of the vector with itself: \(\sqrt{u \cdot u}\).

Code implementation

def length(self):
    return sqrt(self.dot(self))

Demo

We create a vector, call the length method and print the result:

u = la.Mat([[1, 1, 3]])

u_length = u.length()

print(u_length)

Outputs:

>>> print(u_length)
3.3166247903554

Norm

To normalise a vector we just divide by the length:

Code implementation

def norm(self):
    if self.length() != 0:
        self = self.div_elwise(self.length())
    return self

Demo

We create a vector, call the norm method and print the result. We also confirm that the length of the normalised vector is 1 (ignoring rounding errors after 15 decimal places):

u = la.Mat([[1, 0, 1]])

u_normalised = u.norm()

print_mat(u_normalised)

print(round(u_normalised.length(),15))

Outputs:

>>> print(u_length)
>>> print_mat(u_normalised)
[0.7071067811865475, 0.0, 0.7071067811865475]

>>> print(round(u_normalised.length(),15))
1.0

Matrix multiplication

At a low level, matrix multiplication can be seen as a series of dot products between the rows of one matrix and the columns of another. For example, the (2,3) entry of a matrix $C$ produced through the multiplication of two other matrices $A$ and $B$, would be the dot product of the 2nd row of $A$ with the 3rd row of $B$

\[\begin{equation} AB =% \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} \begin{bmatrix} 1 & 2 \\ 1 & 1 \\ 3 & 2 \end{bmatrix} =% \begin{bmatrix} 12 & 10 \\ 27 & 25 \end{bmatrix} \end{equation}\]

Code implementation

To do this we can make use of the transpose and dot product methods we already defined: We transpose the second matrix, then for each row of the first matrix we take the dot product with every 'row' of the transposed second matrix (the rows in the transposed matrix were formerly columns):

def multiply(self, new_mat):
    # preallocate empty matrix
    multiplied = gen_mat([self.size(0), new_mat.size(1)])
    # transpose one matrix, take a bunch of dot products
    new_mat = new_mat.tr()
    for i, row in enumerate(self.data):
        tmp_row = Mat([row])
        for j, col in enumerate(new_mat.data):
            # enter the dot product into our final matrix
            multiplied.data[i][j] = tmp_row.dot(Mat([col]))
    return multiplied

Demo

We create two matrices, call the multiply method and print the result:

A = la.Mat([[1, 1, 3],
            [2, 5, 4]])

B = la.Mat([[1, 1],
            [1, 3],
            [-1, 3]])

C = A.multiply(B)

la.print_mat(C)

Outputs:

>>> la.print_mat(C)
[-1, 13]
[3, 29]

< Element-wise functions of one or more matrices

$EA = U$ >

back to project main page
back to home