Skip to article frontmatterSkip to article content

1.3 Matrices and Vectors

The building blocks of linear algebra

Dept. of Electrical and Systems Engineering
University of Pennsylvania

Binder

Lecture notes

1Reading

Material related to this page, as well as additional exercises, can be found in ALA Ch. 1.2, LAA Ch 2.1, and ILA Ch. 2.4. These notes are mostly based on ALA Ch 1.2 and LAA Ch 2.1.

2Learning Objectives

By the end of this page, you should know:

  • what matrices and vectors are
  • what arithmetic operations are allowed when working with matrices and vectors
  • how to perform arithmetic operations on matrices and vectors
  • how to represent linear system of equations using matrices and vectors

3What’s New from Math 1410

A lot of this page is reviewing mechanics that you’ve arleady seen in Math 1410. If you feel like you remember all of this material well and just want to skip to the new stuff, here are some shortcuts:

4Matrices and Vectors

A matrix is a rectangular array of numbers. For example

[103241],[π0e1210.83547],[000]\begin{bmatrix} 1 & 0 & 3 \\ -2 & 4 & 1 \end{bmatrix}, \quad \begin{bmatrix} \pi & 0 \\ e & \frac{1}{2} \\ -1 & 0.83 \\ \sqrt{5} & -\frac{4}{7} \end{bmatrix}, \quad \begin{bmatrix} 0 & 0 & 0 \end{bmatrix}

are all examples of matrices. We use the notation

A=[a11a12a1na21a22a2nam1am2amn]A = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix}

for a generic matrix AA of size m×nm \times n (read “mm by nn”), where mm denotes the number of rows in AA and nn denotes the number of columns[1]. Therefore, the preceding examples of matrices have respective sizes 2×32 \times 3, 4×24 \times 2, and 1×31 \times 3. A matrix is square if m=nm=n, i.e., it has the same number of rows as columns. A column vector is an m×1m \times 1 matrix, while a row vector is a 1×n1 \times n matrix. While these might seem like they are the same thing, they very much are not! Column vectors end up playing a much more important role in our story, and so whenever we just say “vector” we will always mean a column vector. A 1×11 \times 1 matrix, which has a single entry, is both a column and row vector, and as we’ll see later, behaves like an ordinary scalar number.

The number that lies in the iith row and jjth column of AA is called the (i,j)(i,j) entry of AA, and is denoted by aija_{ij}. The row index always appears first and the column index second. Each column of AA is a m×1m \times 1 vector, which we denote by a1,an\vv a_1, \dots \vv a_n. It will often be convenient to write a matrix in terms of its columns:

A=[a1a2an]A = \begin{bmatrix} \vv a_1 & \vv a_2 & \cdots & \vv a_n \end{bmatrix}

Python Break! Constructing Matrices and Vectors in NumPY

NumPy is a standard scientific computing module in the Python programming language, and is widely used in the engineering, data science, and financial industries. Throughout these notes, we will provide you with NumPy code snippets that implement the mathematical concepts we introduce. The purpose of these are twofold. First, you will have coding exercises on the problem sets, and these snippets give you examples of the syntax needed to implement concepts from class. Second, they let you see the math “come to life.” While we are starting with some pretty basic ideas now, we’ll soon be able to do some really interesting things with a few lines of code. A great way to get intuition about mathematical objects is to paly with them in code. You can do this by clicking the button below[2], which will launch an interactive version of this notebook where you can modify code to see what changes in the output. You are strongly encouraged to do this!

NumPy uses data structures called NumPy Arrays to represent matrices and vectors. Accessing elements of a matrix is done using zero indexing: see this page for examples.

Binder

# Constructing matrices and vectors
import numpy as np

# Create a 2x3 matrix A
A = np.array([[1, 2, 3],
             [4, 5, 6]])
print(f'A= {A}')

# We can check the size of an array by accessing its shape field
print(A.shape)

# Print 2,3 element of A (note the zero indexing means we access [1,2] element of array!)
print(f'A23 = {A[1,2]}')

# Create a 1x4 row-vector b
b = np.array([[0, -1, 1, 3]])
print(f'b = {b}')

# Print first two elements of b
print(f'first two elements of b = {b[0,0:2]}')

# a Create a 3x1 column-vector c
c = np.array([[0],
              [1],
              [2]])

print(f'c = {c}')
A= [[1 2 3]
 [4 5 6]]
(2, 3)
A23 = 6
b = [[ 0 -1  1  3]]
first two elements of b = [ 0 -1]
c = [[0]
 [1]
 [2]]

5Matrix Arithmetic

Matrix arithmetic involves three basic operations: matrix addition, scalar multiplication, and matrix multiplication.

5.1Matrix Addition

First we define addition of matrices. You are allowed to add two matrices only if they are of the same size, and matrix addition is performed entry-wise. For example

[1210]+[3521]=[4311]. \bm 1 & 2 \\ -1 & 0\em + \bm 3 & -5 \\ 2 & 1 \em = \bm 4 & -3 \\ 1 & 1 \em.

More generally, if AA and BB are m×nm \times n matrices, then their sum C=A+BC = A+ B is the m×nm \times n matrix whose entries are given by cij=aij+bijc_{ij} = a_{ij} + b_{ij} for i=1,,mi=1,\dots,m and j=1,,nj=1,\dots,n. When defined, matrix addition behaves just like ordinary addition. It is

  1. commutative: A+B=B+AA + B = B + A, and
  2. associative: A+(B+C)=(A+B)+C=A+B+CA + (B + C) = (A+ B) + C = A + B + C.

Python Break!

## Matrix addition

# Create two 2x3 matrices A and B
A = np.array([[1, 2, 3],
             [4, 5, 6]])

B = np.array([[2, 4, 6],
             [8, 10, 12]])

#Add them together
add = A + B # adding two matrices
print(f'A + B = {add}')

# this is a 1-D array of size 3 in python, which is different than the column-vector c created above
vec = np.array([-1, -2, 3]) 
# notice the difference how 1-D arrays add to 2-D arrays in python. # Try A+c and observe what happens
add_vec = A + vec 
print(f'Adding incompatible matrices in python does something strainge! A + vec = {add_vec}')
A + B = [[ 3  6  9]
 [12 15 18]]
Adding incompatible matrices in python does something strainge! A + vec = [[0 0 6]
 [3 3 9]]

5.2Scalar Multiplication

A scalar is a fancy name for an ordinary number. For now, we restrict ourselves to scalars, vectors, and matrices with real entries, but we will eventually extend these ideas to complex numbers and matrices with complex entries. Although technically not the same thing, we will treat the 1×11 \times 1 matrix [c][c] and the scalar cRc \in \R as a scalar,[3] that is to say as an ordinary number, so we will drop the brackets. Scalar multiplication takes a scalar cc and an m×nm \times n matrix AA an computes the m×nm \times n matrix B=cAB = cA by multiplying each entry of AA by cc. For example:

c=3,A=[1210],cA=3[1210]=[3630]. c = 3, \quad A = \bm 1 & 2 \\ -1 & 0 \em, \quad cA = 3\bm 1 & 2 \\ -1 & 0 \em=\bm 3 & 6 \\ -3 & 0\em.

In general, if B=cAB = cA, then bij=caijb_{ij}=ca_{ij} for each entry i=1,,mi=1,\dots,m and j=1,,nj=1,\dots,n.

Python Break!

# scalar multiplication
scalar = 3
scalar_mult = scalar*A
print(f'A = {A},\n c={scalar},\n cA = {scalar_mult}')
A = [[1 2 3]
 [4 5 6]],
 c=3,
 cA = [[ 3  6  9]
 [12 15 18]]

6Matrix Multiplication Warmup

We first look at multiplying a row vector with a column vector and use this as the base to multiply two matrices. Let a\vv a be a 1×n1 \times n row vector and x\vv x be a n×1n \times 1 column vector. The product ax\vv a \vv x is defined as

ax=[a1a2an][x1x2xn]=a1x1+a2x2+anxn=k=1nakxk\vv a \vv x = \begin{bmatrix} a_1 & a_2 & \cdots & a_n\end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix} = a_1x_1 + a_2x_2 + \cdots a_nx_n = \sum_{k=1}^n a_kx_k

Python Break!

# vector products
b_c = np.array([[1], [2], [-1], [2]])
product = b @ b_c # multipying row-vector b with column-vector b_c
print(f'b = {b},\n b_c = {b_c},\n  b x b_c = {product}')
b = [[ 0 -1  1  3]],
 b_c = [[ 1]
 [ 2]
 [-1]
 [ 2]],
  b x b_c = [[3]]

7Matrix-Matrix Multiplication

We can multiply two matrices A,BA, B if and only if they have compatible sizes. For example, to compute the product C=ABC = AB, AA must have the same number of columns as the number of rows in BB. If AA is an m×nm \times n matrix and BB is an n×pn \times p matrix, then C=ABC = AB is an m×pm \times p matrix, where each element is defined by

cij=k=1naikbkj c_{ij} = \sum_{k=1}^na_{ik}b_{kj}

The (i,j)th(i, j)^{th} entry of CC is the vector product of the ithi^{th} row of AA and jthj^{th} column of BB.

7.1Matrix-Vector Multiplication

An important special case of matrix-matrix multiplication is matrix-vector products. Let AA be an m×nm \times n matrix and x\vv x be an n×1n \times 1 column vector. Then, the matrix-vector product b=Ax\vv b = A \vv x is an m×1m\times 1 column vector, where the entries of b\vv b are

bi=k=1naikxi b_{i} = \sum_{k=1}^na_{ik}x_{i}

Python Break!

# Code implementing solutions to Exercise 4
A = np.array([[1, 1],
              [1, 0]])
B = np.array([[2, 4, 1],
              [0, 3, -1]])
x = np.array([13, 8, 2])
y = np.array([1, 2])

a_r = A@B
b_r = B@x
c_r = y@B
c_r1 = y@B@x
print("\nAB: \n", a_r, "\nBx: \n", b_r, "\nyB: \n", c_r, "\nyBx: \n", c_r1)

AB: 
 [[2 7 0]
 [2 4 1]] 
Bx: 
 [60 22] 
yB: 
 [ 2 10 -1] 
yBx: 
 104

8Special Matrices

Python Break! Built in NumPy functions

NumPy has built in functions for constructing special matrices such as the identity and all zeros matrix. Other special matrices include the all ones matrix and Vandermonde matrices (which you’ll see in your homework set).

# Identity matrices
I2 = np.eye(2)
I3 = np.eye(3)
print(f'I_2 = {I2}')
print(f'I_3 = {I3}')

# Zero matrix
O23 = np.zeros((2,3))
print(f'O_23 = {O23}')

# Ones matrix
ones_54 = np.ones((5,4))
print(f'ones_54 = {ones_54}')
I_2 = [[1. 0.]
 [0. 1.]]
I_3 = [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
O_23 = [[0. 0. 0.]
 [0. 0. 0.]]
ones_54 = [[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]

9Matrix Arithmetic Rules

Summarized below are the rules for how you can combine matrix addition, scalar multiplication, and matrix-matrix multiplication. These are properties you should have seen in Math 1410, so we will not go over them in detail, but it is important to keep these in mind as we progress through the semester. The big thing to remember is that matrix-matrix multiplication comes with more rules than traditional scalar multiplication.

Matrix arithmetic rules!

10Linear Systems in Matrix-Vector Notation

Let’s use our new matrix-vector tools to compactly write out systems of linear equations. Recall that a general linear system of mm equations in nn unknowns takes the form

a11x1+a12x2++a1nxn=b1,a21x1+a22x2++a2nxn=b2,am1x1+am2x2++amnxn=bm,\begin{array}{cccl} a_{11} x_1 + a_{12} x_2 +& \cdots &+ a_{1n} x_n = & b_1,\\ a_{21} x_1 + a_{22} x_2 +& \cdots &+ a_{2n} x_n = & b_2,\\ \vdots & \vdots && \vdots \\ a_{m1} x_1 + a_{m2} x_2 +& \cdots &+ a_{mn} x_n = & b_m,\\ \end{array}

which we rewrite compactly as

Ax=b.A \vv x = \vv b.

Equation (21) is composed of three basic ingredients: the m×nm \times n coefficient matrix AA, with entries aija_{ij} as in (2), the column vector x=[x1x2xn]\vv x = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n\end{bmatrix} containing the unknowns or variables and the column vector b=[b1b2bm]\vv b = \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_m \end{bmatrix} containing the right-hand sides. As you can see, it is a bit unwieldy to write column vectors inline, and so we will often equivalently write them as x=(x1,x2,,xn)\vv x = (x_1, x_2, \cdots, x_n) and b=(b1,b2,,bm)\vv b = (b_1, b_2, \cdots, b_m) instead.

Revisiting linear system (2), we see that the coefficient matrix AA, the unknown vector x\vv x, and the right hand side vector b\vv b can be read off as

A=[121261114],x=[x1x2x3],b=[273]. A = \begin{bmatrix} 1 & 2 & 1 \\ 2 & 6 & 1 \\ 1 & 1 & 4 \end{bmatrix}, \quad \vv x = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix}, \quad \vv b = \begin{bmatrix} 2 \\ 7 \\ 3 \end{bmatrix}.

An important observatoion is that if a variable doesn’t appear in an equation, then the corresponding matrix entry is 0. For example, the following (admittedly silly) system of 2 equations in 2 unknowns

x+y=2y=1\begin{align*} x + y &= 2\\ y & = 1 \end{align*}

becomes

[1101][xy]=[21].\begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}\begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 2 \\ 1\end{bmatrix}.

Binder

Footnotes
  1. It is not a coincidence that nn is also the letter that we used for the number of unknowns in a linear equation!

  2. Also found at the top and bottom of each page!

  3. Remember that we write xSx \in S to mean that the element xx lives in the set SS. In this example, cRc \in \R means that the element cc lives in the real line R\R, and hence cc is a scalar.