What is a Matrix?

What is a Matrix?#

A matrix is defined as an array of numbers, such as:

\[\begin{split} A = \begin{pmatrix} 3 & 1 & 8 \\ 0 & -4 & 5 \end{pmatrix} \end{split}\]
  • Matrices come in different sizes. We write the size of the matrix as rows×columns. So the matrix above is 2×3 matrix.

  • We use capital letters such as \(A\) or \(B\) to denote matrices.

  • The numbers contained in a matrix are known as matrix elements. They are denoted \(x_{ij}\), where \(i\) represents the row and \(j\) the column in which the element is in the matrix \(X\). For example, in the above matrix \(a_{11} = 3\) and \(a_{23}=5\).

  • Matrices with an equal number of rows and columns are called square matrices. For example, the 2×2 matrix below:

    \[\begin{split} \begin{pmatrix} 1 & -1 \\ 0 & 1 \end{pmatrix} \end{split}\]

Example

For the matrices below, find the size of the matrix and the value of \(x_{21}\) and \(x_{33}\) (where \(X\) is the matrix label), if possible.

  1. \(A = \begin{pmatrix} 4 & 5 & 6 \\ 1 & 2 & 3 \\ 4 & 5 & 6 \\ 1 & 2 & 3 \end{pmatrix}\)

  2. \(B = \begin{pmatrix} 1 \\ -1 \\ 40 \end{pmatrix}\)

  3. \(C = \begin{pmatrix} a & b & c \\ d & e & f \\ g & h & i \end{pmatrix}\)

Solution:

  1. The matrix \(A\) has 4 rows and 3 columns, hence this is a \(4\times 3\) matrix. We have that \(a_{21} = 1\) and \(a_{33}=6\).

  2. The matrix \(B\) has 3 rows and 1 column, hence this is a \(3\times 1\) matrix. We have that \(b_{21}=-1\) and \(b_{33}\) does not exist, as this is a \(3\times 1\) matrix.

  3. The matrix \(C\) has 3 rows and 3 columns, hence it is a \(3\times 3\) matrix (also a square matrix). We then have at \(c_{21}=d\) and \(c_{33}=i\).

Matrices are an excellent way of expressing large amounts of information or data in a small space, as shown in the chemistry example below.


The NumPy array is a great way to describe a matrix in code. We achieve this by passing a list of lists to the NumPy array creation function.

import numpy as np 

A = np.array([[4, 5, 6], 
              [1, 2, 3], 
              [4, 5, 6], 
              [1, 2, 3]])
A
array([[4, 5, 6],
       [1, 2, 3],
       [4, 5, 6],
       [1, 2, 3]])

We can determine the number of rows and columns by using the .shape property of the NumPy array.

A.shape
(4, 3)

Additionally, the specific values of the matrix can be found by array indexing.

A[1, 0]
1
A[2, 2]
6
B = np.array([[1], [-1], [40]])
B.shape
(3, 1)
B[1, 0]
-1

To populate the matrix with symbols, we need to use sympy.

from sympy import symbols

a, b, c, d, e, f, g, h, i = symbols('a b c d e f g h i')

C = np.array([[a, b, c],
              [d, e, f],
              [g, h, i]])
C.shape
(3, 3)
C[1, 0], C[2, 2]
(d, i)

Connectivity Matrix

Ignoring the hydrogens, find the atom connectivity matrix of but-1-ene shown below:

A but-1-ene molecular strucutre.

Solution: An atom connectivity matrix describes how the carbons in but-1-ene area bonded with each other. First, we label the carbons 1 to 4, going from left to right. We use the matrix elements \(a_{mn}\) (in row \(m\) and column \(n\)) to describe how carbon \(m\) and carbon \(n\) are bonded, hence we need to find a 4×4 matrix.

  • The matrix element \(a_{12}\) describes how carbon \(1\) is bonded to carbon \(2\), we have a double bond between them hence \(a_{12}=2\). This also means that \(a_{21}=2\) as this matrix element also describes how carbon \(2\) is bonded to carbon \(1\).

  • The matrix element \(a_{13}\) describes how carbon \(1\) is bonded to carbon \(3\), we have no bond between them, hence \(a_{13}=0\) this also means that \(a_{31}=0\).

  • As there is a single bond between carbon \(2\) and carbon \(3\), we have \(a_{23}=1\) and \(a_{32}=1\).

  • The matrix element \(a{11}\) describes how carbon \(1\) is bonded to carbon \(1\), however it does not make sense for an atom to be bonded to itself. Hence we tend to write in the atomic number of the atom, which for carbon is \(6\), so \(a_{11}=6\). This will alos be true for the rest of the matrix diagonal.

So when we find the rest of the matrix elements in the same way, we have that the atom connectivity matrix of but-1-ene is:

\[\begin{split} \begin{pmatrix} 6 & 2 & 0 & 0 \\ 2 & 6 & 1 & 0 \\ 0 & 1 & 6 & 0 \\ 0 & 0 & 1 & 6 \end{pmatrix} \end{split}\]

There are a variety of ways that such a matrix could be created with NumPy. The most strraight forward would be, the following.

connectivity = np.array([[6, 2, 0, 0], 
                         [2, 6, 1, 0],
                         [0, 1, 6, 1], 
                         [0, 0, 1, 6]])
connectivity
array([[6, 2, 0, 0],
       [2, 6, 1, 0],
       [0, 1, 6, 1],
       [0, 0, 1, 6]])

However connectivity matrix is a symmetric matrix, which we can take advantage of in creation.

np.diag(np.ones(4) * 6) + np.diag([2, 1, 1], k=1) + np.diag([2, 1, 1], k=-1)
array([[6., 2., 0., 0.],
       [2., 6., 1., 0.],
       [0., 1., 6., 1.],
       [0., 0., 1., 6.]])

Above, the k keyword argument defines the diagonal where the input array should be placed.