2.3 Numerical Python Arrays
53
Note how zeros must be called with double parentheses now. The accessing of
individual matrix elements should be according to intuition. With some experience
from matrix-vector algebra, it is clear that y is correctly computed here. Note that
most programmers would use the NumPy function eye here, to generate the identity
matrix directly. One would then call I = eye(3) and get I as a two dimensional
array with ones on the diagonal.
If you are experienced with matrices and vectors in Matlab, there is another way
to handle matrices and vectors with NumPy, which will appear more like you are
used to. For example, a matrix-vector product is then coded as A*x and not by use of
the dot function. To achieve this, we must use objects of another type, i.e., matrix
objects (note that a matrix object will have different properties than an ndarray
object!). If we do the same matrix-vector calculation as above, we can show how
ndarray objects may be converted into matrix objects and how the calculations
then can be fulfilled:
In [1]: import numpy as np
In [2]: I = np.eye(3)
# create identity matrix
In [3]: I
Out[3]:
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
In [4]: type(I)
# confirm that type is ndarray
Out[4]: numpy.ndarray
In [5]: I = np.matrix(I)
# convert to matrix object
In [6]: type(I)
# confirm that type is matrix
Out[6]: numpy.matrixlib.defmatrix.matrix
In [7]: x = np.array([1.0, 2.0, 3.0])
# create ndarray vector
In [8]: x = np.matrix(x)
# convert to matrix object (row vector)
In [9]: x = x.transpose()
# convert to column vector
In [10]: y = I*x
# computes matrix-vector product
In [11]: y
Out[11]:
matrix([[ 1.],
[ 2.],
[ 3.]])
Note that np.matrix(x) turns x, with type ndarray, into a row vector by default
(type matrix), so x must be transposed with x.transpose() before it can be
multiplied with the matrix I.
53
Note how zeros must be called with double parentheses now. The accessing of
individual matrix elements should be according to intuition. With some experience
from matrix-vector algebra, it is clear that y is correctly computed here. Note that
most programmers would use the NumPy function eye here, to generate the identity
matrix directly. One would then call I = eye(3) and get I as a two dimensional
array with ones on the diagonal.
If you are experienced with matrices and vectors in Matlab, there is another way
to handle matrices and vectors with NumPy, which will appear more like you are
used to. For example, a matrix-vector product is then coded as A*x and not by use of
the dot function. To achieve this, we must use objects of another type, i.e., matrix
objects (note that a matrix object will have different properties than an ndarray
object!). If we do the same matrix-vector calculation as above, we can show how
ndarray objects may be converted into matrix objects and how the calculations
then can be fulfilled:
In [1]: import numpy as np
In [2]: I = np.eye(3)
# create identity matrix
In [3]: I
Out[3]:
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
In [4]: type(I)
# confirm that type is ndarray
Out[4]: numpy.ndarray
In [5]: I = np.matrix(I)
# convert to matrix object
In [6]: type(I)
# confirm that type is matrix
Out[6]: numpy.matrixlib.defmatrix.matrix
In [7]: x = np.array([1.0, 2.0, 3.0])
# create ndarray vector
In [8]: x = np.matrix(x)
# convert to matrix object (row vector)
In [9]: x = x.transpose()
# convert to column vector
In [10]: y = I*x
# computes matrix-vector product
In [11]: y
Out[11]:
matrix([[ 1.],
[ 2.],
[ 3.]])
Note that np.matrix(x) turns x, with type ndarray, into a row vector by default
(type matrix), so x must be transposed with x.transpose() before it can be
multiplied with the matrix I.
