image
image
image

Matrices

image

A two-dimensional set of numbers is considered a matrix. The matrices will be represented as lists of lists. Each of the inner lists will have the same size and will represent a matrices row. If K is a matrix, then K[c] [d] would be the element in the c row and d column. Mathematical convention dictates that matrices are represented by capital letters. You can see this here:

“ K = [[1, 2, 3],

[4, 5, 6]]

L = [[1, 2],

[3, 4],

[5, 6]] “

Note: When it comes to mathematics, the first row of a matrix would be labeled “row 1” and the first column would be names “column 1.” Since we are writing our matrices as Python lists, which are indexed at zero, our first matrix row will be labeled “row 0” the first column will be labeled “column 0.”

Since we are using list-of-lists representation, our matrix K will have len(K) rows and len(K[0]) columns. Here we will consider the shape.

“ def shap(K) ;

Num_rows = len(K)

Num_cols = len(K[0]) if K else 0

Return num_rows, num_cols “

If you have a matrix with c rows and d columns, it is called c X d matrix. You are able to view the rows of a c X d matrix as length c’s vector, and every column is the vector length d.

“ def get_row(K, c) :

Return K[c]

Def get_column (K, d):

Return [K_c [d]

For K_c in K] “

You will also want to make a matrix based on the shape and a function to create the elements. The can be done through a nested list comprehension.

“ def make_matrix(num_rows, num_cols, entry_fn) :

Return [[entry_fn (c, d)

For d in range (num_cols)]

For c in range (num_rows)] “

By using this function, you can create a five by five identity matrix that has a 1s on the diagonal and elsewhere would be a 0s.

“ def is_diagonal (c, d) :

Return 1 if c == d else 0

Identity_matrix = make_matrix (5, 5, is_diagonal)

These matrices end up being important for many different reasons.

A matrix can be used to represent a set of data that consists of several vectors by simply looking at each of the vectors as row for your matrix. An example would be if you have the ages, heights, and weights for 1,000 people, you are able to place them in a 1,000 X 3 matrix.

“ data = [[70, 170, 40],

[65, 120, 26],

[77, 250, 19],

# ...

] “

You are also able to use c X d matrix to show a linear function that will map your c-dimensional vectors to your d-dimensional vectors. There are a lot of concepts and techniques that will involve these types of functions.

The third thing you can do with matrices is to use them to represent binary relationships. One representation of an edge of a network is to show them as a collection pair (c, d). But another way you could do this is make a matrix K like K[c] [d] is one of the nodes c and d are connected and if not they are zero.

In the former representation you would have:

“ relationships = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4),

(4, 5), (5, 6), (5, 7), (6, 8), (7, 8), (8, 9)] “

This could also be shown as:

“ relationships = [[0, 1, 1, 0, 0, 0, 0, 0, 0, 0 ],

[1, 0, 1, 1, 0, 0, 0, 0, 0, 0],

[1, 1, 0, 1, 0, 0, 0, 0, 0, 0],

[0, 1, 1, 0, 1, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 1, 0, 1, 1, 0, 0],

[0, 0, 0, 0, 0, 1, 0, 0, 1, 0],

[0, 0, 0, 0, 0, 1, 0, 0, 1, 0],

[0, 0, 0, 0, 0, 0, 1, 1, 0, 1],

[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]] “

If you don’t have many connections, then this wouldn’t be a very efficient representation because you will more than likely have a lot of stored zeros. However, when you use a matrix representation it will be a lot quicker to check if your two nodes are connected. To do this you would only to do a matrix lookup instead of having to inspect every edge.

“ relationships [0] [2] == 1

relationships [0] [8] == 1 “

If you are looking to find connections that a node has, you would have to inspect column or row that corresponds with the node.

“ friends_of_five = [c

For c, is_friend in enumerate(relationships[5])

If is_friend] “

Previously you may have added a connections list to all of the node objects to speed up the process, but when it comes to evolving a large graph that would end up being a bit too expensive, and it would be hard to maintain.