Session 11 Lecture 2
Session 11 Lecture 2
Linear algebra is the branch of mathematics that deals with vector spaces.
Vectors
Abstractly, vectors are objects that can be added together (to form new vectors) and that can be multiplied by
scalars (i.e., numbers), also to form new vectors.
For example, if you have the heights, weights, and ages of a large number of people, you can treat your data as
three-dimensional vectors (height, weight, age) . If you’re teaching a class with four exams, you can treat
student grades as four- dimensional vectors (exam1, exam2, exam3, exam4) .
In [1]:
1 # Vector addition
2 def vector_add(v, w):
3
4 print("v = ", v)
5 print("w = ", w)
6
7 return print("v + w = ",[v_i + w_i
8 for v_i, w_i in zip(v, w)])
9
10 l1 = [1, 2, 1, 4, 5]
11 l2 = [6, 5, 7, 4, 9]
12 vector_add(l1,l2)
v = [1, 2, 1, 4, 5]
w = [6, 5, 7, 4, 9]
v + w = [7, 7, 8, 8, 14]
In [3]:
In [2]:
v = [1, 2]
w = [6, 5]
v1 * w2 + v2 *w2 = 16
In [4]:
v = [1, 2, 3]
c = 3
c * v = [3, 6, 9]
Matrices
A matrix is a two-dimensional collection of numbers. We will represent matrices as list s of list s, with each inner
list having the same size and representing a row of the matrix.
If A is a matrix, then A [i] [j] is the element in the ith row and the jth column. Per mathematical convention, we
will typically use capital letters to represent matrices.
For example:
In [5]:
In [6]:
1 print(A)
In [7]:
In [8]:
1 B
Out[8]:
In [9]:
1 c = []
In [10]:
1 def shape(A):
2 num_rows = len(A)
3 num_cols = len(A[0]) # number of elements in first row
4 return num_rows, num_cols
5
6
In [11]:
1 shape(A)
Out[11]:
(2, 3)
In [ ]:
In [ ]: