Skip to article frontmatterSkip to article content

Linear Algebra and NumPy

Cornell University

In the following discussion, the vector space is of dimension dd and has basis x1,x2,,xdx_1, x_2, \dots, x_d

Vector Space

a vector space over the real numbers is a set VV together with two binary operations ++ (mapping V×VV \times V to VV) and \cdot (mapping R×V\R \times V to VV) satisfying the following axioms for any x,y,zVx, y, z \in V and a,bRa, b \in \R

Vector

Any vector vv in the vector space can be written uniquely as the following for some real numbers α1,α2,\alpha_1, \alpha_2, \ldots

v=α1x1+α2x2++αdxdv = \alpha_1 x_1 + \alpha_2 x_2 + \cdots + \alpha_d x_d

Therefore, to represent vv on a computer, it suffices to store α1\alpha_1, α2\alpha_2, \ldots, and αd\alpha_d. We store these alphas in an array and this gets us back to our CS-style notion of what a vector is.

Linear Map

We say a function FF from a vector space UU to a vector space VV is a linear map if for any x,yUx, y \in U and any aRa \in \R,

F(ax+y)=aF(x)+F(y)F(ax + y) = a F(x) + F(y)

Note if we want to define a linear map, it suffices to just write out F(xi)F(x_i) for all basis. Say xi=d=m,F(xi)=n|x_i| = d =m, |F(x_i)| = n, we can then represent a linear map with m×nm\times n number: each F(xi)F(x_i) needs nn numbers and there are mm F(xi)F(x_i)

Matrix

We have basically introduced the “matrix” in above. We call this two-dimensional-array representation of a linear map a matrix.

We use multiplication to denote the effect of a matrix operating on a vector (this is equivalent to applying a multilinear map as a function). E.g. if FF is the multilinear map corresponding to matrix AA:

y=F(x)    y=AxF:RmRn    ARn×my = F(x) \iff y = Ax \\ F:\R^m \to \R^n \iff A \in \R^{n \times m}

We can add two matrices, scale a matrix by a scalar, and these two operations follow the “axioms” described above for a vector space. This means that the set of matrices Rn×m\R^{n \times m} is itself a vector space. (This was covered in MATH3360)

Broadcasting

When operate on vectors / matrices, numpy broadcasts by acting as if there are infinite 1×1\times to the left of dimension:

if dimension of array not same:
    append newaxis to the lesser one until same
    if on a specific axis, one array has dim 1 and the other has > 1:
        broadcast that 1 axis to match dimension
    else:
        fail

However, it is good practice for you to always manually add dimension before broadcasting.

x = numpy.array([2,3])
x1 = x[numpy.newaxis,:]