Linear Algebra with numpy – Part 1

Numpy is a package for scientific computing in Python.

Vector arithmetic

Declaration

a = np.array([1,2,3,4])
[1 2 3 4]

Addition / Subtraction

a = np.array([1,2,3,4])
b = np.array([4,3,2,1])
a + b
array([5, 5, 5, 5])
a - b
array([-3, -1,  1,  3])

Scalar Multiplication

a = np.array([1,2,3,4])
a * 3
array([ 3,  6,  9, 12])

To see why it is charming to use numpy’s array for this operation You have to consider the alternative:

c = [1,2,3,4]
d = [x * 3 for x in c]

Dot Product

a = np.array([1,2,3,4]) 
b = np.array([4,3,2,1])

a.dot(b)

20 # 1*3 + 2*3 + 3*2 + 4*1

Stay tuned for more algebraic stuff with numpy!

Leave a Reply

Your email address will not be published. Required fields are marked *