Matrix operations

Apart from element-wise calculations using the np.dot() function, you can also apply multiplications to your two-dimensional arrays based on matrix calculations, such as vector-matrix and matrix-matrix multiplications:

In: import numpy as np
M = np.arange(5*5, dtype=float).reshape(5,5)
M

Out:
array([[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[ 10., 11., 12., 13., 14.],
[ 15., 16., 17., 18., 19.],
[ 20., 21., 22., 23., 24.]])

As an example, we will create a 5 x 5 two-dimensional array of ordinal numbers from 0 to 24:

  1. We will define a vector of coefficients and an array column stacking the vector and its reverse:
In: coefs = np.array([1., 0.5, 0.5, 0.5, 0.5])
coefs_matrix = np.column_stack((coefs,coefs[::-1]))
print (coefs_matrix)

Out: [[ 1. 0.5]
[ 0.5 0.5]
[ 0.5 0.5]
[ 0.5 0.5]
[ 0.5 1. ]]
  1. We can now multiply the array with the vector by using the np.dot function:
In: np.dot(M,coefs)

Out: array([ 5., 20., 35., 50., 65.])
  1. Or the vector by the array:
In: np.dot(coefs,M)

Out: array([ 25., 28., 31., 34., 37.])
  1. Or the array by the stacked coefficient vectors (which is a 5 x 2 matrix):
In: np.dot(M,coefs_matrix)

Out: array([[ 5., 7.],
[ 20., 22.],
[ 35., 37.],
[ 50., 52.],
[ 65., 67.]])

NumPy also offers an object class, matrix, which is actually a subclass of ndarray, inheriting all its attributes and methods. NumPy matrices are exclusively two-dimensional (as arrays are actually multi-dimensional) by default. When multiplied, they apply matrix products, not element-wise ones (the same happens when raising powers), and they have some special matrix methods (.H for the conjugate transpose and .I for the inverse).

Apart from the convenience of operating in a fashion that is similar to that of MATLAB, they do not offer any other advantage. You may risk confusion in your scripts since you'll have to handle different product notations for matrix objects and arrays.

Since Python 3.5, a new operator, the @ (at) operator, dedicated to matrix multiplication, has been introduced in Python (the change is for all the packages in Python, not just NumPy). The introduction of this new operator brings a couple of advantages.
First, there won't be any more cases where the * operator will be meant to be used for matrix multiplication. The * operator will be used exclusively for element-wise operations (those operations where, having two matrices (or vectors) of the same dimension, you apply the operation between the elements having the same position in the two matrices).
Then, code that is representing formulas will gain in readability, thus becoming much easier to read and interpret. You won't have to evaluate operators (+ - / *) and methods (.dot) together anymore, only operators (+ - / * @).
You can learn more about this introduction (which is just formal – everything you could apply before using the .dot method works with the @ operator) and look at some examples of applications by reading the Python Enhancement Proposal (PEP) 465 at the Python foundation website: https://www.python.org/dev/peps/pep-0465/.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset