Basics of Matrix and Vector Algebra¶

Literature¶

PPNM Ch. 14.1 (not all of this sub-chapter is covered in this lab)

Brilliant overview of matrices and matrix algebra

Brilliant overview of Linear Algebra

Optional: EMEA Ch. 12.1-12.7

Learning goals¶

  • Understand what a matrix and vector are

  • Understand basic matrix operations: equality, addition and multiplication, and transpose

  • Understand how to write a system of equations in matrix form

  • Know the form and definition of the identity matrix

  • Matrix multiplication

  • Transpose of a matrix

  • Basics of working with vectors and matrices in Python

Systems of equations¶

In previous economics courses, you have met systems of equations: For example, price and quantity variables in both supply and demand equations that must be solved simultaneously. You likely have solved these in a somewhat ad-hoc way - manipulating one variable to be written as a function of another, and then substituting in order to eliminate a variable from an equation:

(ps. As it turns out, with the coefficients I chose for this example, you are not able to get a unique solution.)

Try it:¶

Write down the system of equations for the case with (n=4) variables and (m=3) equations. and $a_{ij} = i+2j+(-1)^i$ for i=1,2,3 and j=1,2,3,4, whereas $b_i = 2^i$ for i=1,2,3.

(EMEA exercise 12.2.3)

Matrices and vectors (video 1.2)¶

One of our main goals in this lab and the next will be to systemize the solving of systems of equations, starting by writing the equations in terms of matrices and vectors. But first, we need to understand what matrices and vectors are:

Here is the Khan Academy introduction to matrices.

Try it:¶

Write the coefficient matrix of the system of equations in the above exercise (12.2.3)

Matrix equality, matrix addition, and scalar multiplication¶

If we have 1 or more numbers, we can do a host of operations on these numbers: basically standard actions that manipulate these numbers with consistent results. For example, adding: we know that 2+2=4, and adding any other two numbers follow the same rules.

We have standard operations for matrices as well - sometimes that act similar to operations on numbers (called scalars in linear algebra world), but which sometimes are quite a bit different.

We start easy: Mattrix addition and scalar multiplication

Here is the Khan Academy video on matrix addition and subtraction.

Try it¶

Given a matrix $\mathbf{A} = \begin{bmatrix} 2,4 \\ -4, -2 \end{bmatrix}$ and $\mathbf{B} = \begin{bmatrix} -1,-2 \\ 1, 2 \end{bmatrix}$

  • Find $\mathbf{2A}$
  • Find $\mathbf{A+B}$

Matrix multiplication¶

Matrix multiplication is an enormously useful operation, but is a bit more complex than scalar multiplication. We walk through matrix multiplication here:

Here is the Khan Academy introduction to matrix multiplication.

Try it¶

Again, given a matrix $\mathbf{A} = \begin{bmatrix} 2,4 \\ -4, -2 \end{bmatrix}$ and $\mathbf{B} = \begin{bmatrix} -1,-2 \\ 1, 2 \end{bmatrix}$

  • Matrix multiply $\mathbf{AB}$

  • Matrix multiply $\mathbf{BA}$

Are the two the same?

Systems of equations in matrix form (video 1.5)¶

As we stated in the beginning, one of the main goals of this lecture was to be able to write our system of equations in matrix and vector form. Now we are ready to do that:

Try it¶

Write the system of equations from the exercise above (12.2.3) in matrix form by creating matrix $\mathbf{A}$ and vectors $B$ and $x$.

Rules for matrix multiplication (video 1.6)¶

As we saw in Video 1.4, matrix multiplication is not entirely analgous to multiplication of scalars. In this video, we talk a bit more about what we can and can not assume when we do matrix multiplication.

Try it¶

Verify the distributive law $\mathbf{A(B+C)} = \mathbf{AB + AC}$ where,

$$A=\begin{bmatrix} 1,2\\3,4\end{bmatrix}$$$$B=\begin{bmatrix} 2,-1, 1,0 \\3,-1,2,1\end{bmatrix}$$$$C=\begin{bmatrix} -1,1, 1,2 \\-2,2,0,-1\end{bmatrix}$$

(EMEA Exercise 12.6.1)

The identity matrix (video 1.7)¶

If you multiply a scalar by one, you get the same number back again ($5*1=5$). We call 1 for the identity. We have a similar concept in matrix algebra called the identity matrix:

Here is the Khan Academy video on the identity matrix.

Try it¶

Compute the following matrix product:

$$\begin{bmatrix}1,2,-3 \end{bmatrix} \begin{bmatrix} 1,0,0 \\ 0,1,0 \\ 0,0,1 \end{bmatrix}$$

The transpose (video 1.8)¶

In the previous videos, we have seen how the dimensions and ordering of matrices can be critically important in doing matrix calculations. Often we are interested in being able to transform the dimensions of our matrix, without changing the values in the matrix or changing the ordering of those values. One of our tools for doing this is the transpose:

Here is the Khan academy video on transpose.

Try it¶

Find the transposes of the three matrices:

$$\mathbf{A} = \begin{bmatrix}3,5,8,3 \\ -1,2,6,2 \end{bmatrix}$$$$\mathbf{B} = \begin{bmatrix}0\\1\\-1\\2 \end{bmatrix}$$$$\mathbf{C} = \begin{bmatrix} 1,5,0,-1 \end{bmatrix}$$

Lab 1: Numeric matrix operations in python¶

Basics of matrices and arrays using numpy (PPNM 14.1)¶

We will be relying heavily on the np.array() data type

In [ ]:
import numpy as np

Vectors¶

NP arrays are fairly general and can contain many different sizes and dimensions of numbers

We can define both a row vector and a column vector

In [ ]:
vector_row = np.array([[1, -5, 3, 2, 4]])
vector_column = np.array([[1], 
                          [2], 
                          [3], 
                          [4]])
print(vector_row.shape)
print(vector_column.shape)
(1, 5)
(4, 1)

Notice above that the inner brackets ([]) define the rows.

We could also define an array with 5 numbers:

In [ ]:
myArray = np.array([1,-5,3,2,4])
myArray.shape
Out[ ]:
(5,)

But this array is just a collection of numbers, not a row- or column vector. A vector is defined a collection of numbers with a defined shape.

We can also calculate linear combinations of vectors:

In [ ]:
x=np.array([[1,2,5]])
z=np.array([[2,3,7]])

y = 3*x + .5*z
print(y)
[[ 4.   7.5 18.5]]

Matrices¶

We can also use numpy to create matrices. Really, there is no difference between vectors and matrices other than dimension: A vector is simply a 1xN or Nx1 matrix, and so it is in Python as well.

Below we create two matrices: P (3x2) and Q (2x4)

In [ ]:
P = np.array([[1, 7], [2, 3], [5, 0]])
Q = np.array([[2, 6, 3, 1], [1, 2, 3, 4]])
print(P)
print(Q)
[[1 7]
 [2 3]
 [5 0]]
[[2 6 3 1]
 [1 2 3 4]]

Again, notice the use of inner brackets to define rows.

To do matrix multiplication, we use the dot function (matrix multiplication is sometimes called the dot production. P is 3x2 and Q is 2x4, so we can calculate PQ, which should become a 3x4 matrix

In [ ]:
np.dot(P, Q)
Out[ ]:
array([[ 9, 20, 24, 29],
       [ 7, 18, 15, 14],
       [10, 30, 15,  5]])

Increasingly, you will see the "@" sign used for matrix multiplication, this works exactly the same, and perhaps is even a bit easier to read:

In [ ]:
P @ Q
Out[ ]:
array([[ 9, 20, 24, 29],
       [ 7, 18, 15, 14],
       [10, 30, 15,  5]])

But if we try QP:

In [ ]:
np.dot(Q,P)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/var/folders/fs/kr83xdyx2jq_8gww_xfmtxfr0000gn/T/ipykernel_56884/4123817340.py in <module>
----> 1 np.dot(Q,P)

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (2,4) and (3,2) not aligned: 4 (dim 1) != 3 (dim 0)

Order matters!

Identity matrix¶

We can automatically generate an identity matrix in python with a pun-y command "eye"

In [ ]:
I = np.eye(3)
print(I)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

we can use the random functions in numpy to populate a matrix with random numbers. Here we generate random integers between 0 and 10 in a 3x4 matrix:

In [ ]:
M = np.random.randint(10, size=(3, 4))
M
Out[ ]:
array([[7, 3, 1, 7],
       [1, 2, 1, 7],
       [7, 8, 8, 2]])

(Your results will be different, of course)

When we compute the IM we get our expected result

In [ ]:
np.dot(I,M)
Out[ ]:
array([[7., 3., 1., 7.],
       [1., 2., 1., 7.],
       [7., 8., 8., 2.]])

Assignment¶

1. Matrix operations¶

Given the matrices:

$$\mathbf{A} = \begin{bmatrix} 2,0\\-1,1 \end{bmatrix}$$$$\mathbf{B} = \begin{bmatrix} -1,2\\1,-1 \end{bmatrix}$$$$\mathbf{C} = \begin{bmatrix}2,3\\1,4 \end{bmatrix}$$$$\mathbf{D} = \begin{bmatrix}1,1,1\\ 1,3,4\end{bmatrix}$$

Calculate (where possible). If not possible, explain why

a.) $\mathbf{A-B}$

b.) $\mathbf{A+B-2C}$

c.) $\mathbf{AB}$

d.) $\mathbf{C(AB)}$

e.) $\mathbf{AD}$

f.) $\mathbf{DC}$

g.) $\mathbf{2A-3B}$

h.) $\mathbf{(A-B)´}$

i.) $\mathbf{(C´A´)B´}$

j.) $\mathbf{C´(A´B´)}$

j.) $\mathbf{D´D´}$

k.) $\mathbf{D´D}$

(EMEA review exercise 12.2)

2.) Write the following system of equation in matrix notation:¶

$$x+y+z+t = a$$$$x+3y+2z + 4t = b$$$$x+4y+8z = c$$$$2x + z -t = d$$

(EMEA review exercise 12.3)

3.) Matrix operations on 3x3 matrices¶

Find the matrices $\mathbf{A+B}$, $\mathbf{AB}$, $\mathbf{BA}$ and $\mathbf{A(BC)}$ where

$$\mathbf{A} = \begin{bmatrix} 0,1,-2 \\ 3,4,5 \\ -6,7,15 \end{bmatrix}$$$$\mathbf{B} = \begin{bmatrix} 0,-5,3 \\ 5,2, -1 \\-4,2,0 \end{bmatrix}$$$$\mathbf{C} = \begin{bmatrix} 6,-2,-3 \\2,0,1 \\0,5,7 \end{bmatrix}$$

(EMEA review exercise 12.4)

4.) Computation: Do the following vector and matrix operations numerically¶

Defining vectors:

$$x=[3,5,7]$$$$y=[2,4,8]$$

a.) Create these vectors as np.arrays and compute the following linear combination: $.5x + 2y$

b.) Find the inner product of x and y, ($x \cdot y$)

Consider the two matrices

$$\mathbf{A_{3 \times 4}} = \begin{bmatrix} 2,4,6, 8 \\ 1,-2,4, -8 \\-3, 4, 5, -5 \end{bmatrix}$$$$\mathbf{B_{3 \times 4}} = \begin{bmatrix} 4,3,-1, 1 \\ -8,6,-4,2 \\1,2,2,1 \end{bmatrix}$$

c.) Create matrices $\mathbf{A}$ and $\mathbf{B}$ as np.arrays. Confirm the shape of the matrices are 3x4. Calculate $2\mathbf{A}$

d.) Calculate $2\mathbf{A}+.5\mathbf{B}$

e.) Is it possible to compute the matrix multiplication $\mathbf{AB}$. What happens if you try?

f.) Instead, matrix multiply $\mathbf{A^TB}$ where $T$ represents the transpose (Hint: if you have an np.array, x, you can create the transpose by x.T

g.) Fill a 5x3 matrix, $\mathbf{R}$, with random integers. What shape of an identity matrix, $\mathbf{I}$ do we need so that $\mathbf{IM=M}$. Demonstrate computationally.

5.) International flights (based on example 12.6.7 in EMEA)¶

Consider 3 countries A, B, and C. Country A has 3 airports, country B has 4 airport, and country C has 5 airports. There are international flights between country A and B and between B and C, but no direct flights between A and C. The matrices below represent the number of direct flights between airports in the different countries.

$$\mathbf{AB_{3 \times 4}} = \begin{bmatrix} 2&4&0& 1 \\ 1&0&4,& 0 \\0& 2& 7& 3 \end{bmatrix} $$$$\mathbf{BC_{4 \times 5}} = \begin{bmatrix} 4&3&0& 1& 3 \\ 0&6&0&2& 0 \\2&5&1&0&3 \\1&7&0&2&3 \end{bmatrix} $$

Do a calculation (in python) of these matrices to give the total number connections between countries A and C. (Hint, you wish to end up with a 3x5 matrix). Explain the logic behind this calculation.