Linear Algebra using NumPy
For beginner NumPy users with a linear algebra background, it’s easy to understand that to define a matrix, $A=\begin{bmatrix}1&2\\3&4\end{bmatrix}$, we should use
A = np.array([[1,2],[3,4]])
since
A
Out[2]:
array([[1, 2],
[3, 4]])
A.shape
Out[3]: (2, 2)
However, how should $x=\begin{bmatrix}1\\2\end{bmatrix}$ be defined? Should we use a 1-D array
x1 = np.array([1,2])
x1
Out[4]: array([1, 2])
x1.shape
Out[5]: (2,)
or a 2-D array with 1 column?
x2=np.array([[1],[2]])
x2
Out[6]:
array([[1],
[2]])
x2.shape
Out[7]: (2, 1)
If we were to decide based on the output of the console, x2
would seem to be the most appropriate since it looks like a column vector in the console, while x1
looks like a row vector. It turns out though, that the correct way is to use x1
to represent $x$. A vector (in the linear algebra sense) should be represented by a 1-D array in NumPy. A matrix (in the linear algebra sense) should be represented by a 2-D array in NumPy.
In fact, for NumPy, there’s no difference between a row or a column vector when it is represented by a 1-D array. The @
operator overloads matrix-vector and vector-matrix multiplication so the practitioner does not need to worry about transposing vectors. For example, to compute $x^TA=\begin{bmatrix}7&10\end{bmatrix}$, use
x1 @ A
Out[8]: array([ 7, 10])
and to compute $Ax=\begin{bmatrix}5\\11\end{bmatrix}$, use
A @ x1
Out[9]: array([ 5, 11])
Also, the weighted scalar product $x^TAx=27$ can be computed by
x1 @ A @ x1
Out[10]: 27
How about the outer product, $xx^T=\begin{bmatrix}1&2\\2&4\end{bmatrix}$? This computation does require something beyond the @
operator, namely, using NumPy’s outer
function
np.outer(x1,x1)
Out[11]:
array([[1, 2],
[2, 4]])