Linear Algebra and NumPy
In the following discussion, the vector space is of dimension and has basis
Vector Space¶
a vector space over the real numbers is a set together with two binary operations (mapping to ) and (mapping to ) satisfying the following axioms for any and
- closure: and
- associativity of addition:
- transitivity of addition:
- negation: there exists a such that
- zero element: such that
- associativity of scalar multiplication:
- multiplication by one:
- distributivity: and
Vector¶
Any vector in the vector space can be written uniquely as the following for some real numbers
Therefore, to represent on a computer, it suffices to store , , , and . 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 from a vector space to a vector space is a linear map if for any and any ,
Note if we want to define a linear map, it suffices to just write out for all basis. Say , we can then represent a linear map with number: each needs numbers and there are
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 is the multilinear map corresponding to matrix :
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 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 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,:]