How it works...

The @guvectorize decorator works on array arguments, taking four arguments in order to specify the gufunc signature:

  • The first three arguments specify the types of data to be managed and arrays of integers: void(int64[:,:], int64[:,:], int64[:,:]).
  • The last argument of @guvectorize specifies how to manipulate the matrix dimensions: (m,n),(n,p)->(m,p).

Then, the matrix multiplication operation is defined, where A and B are the input matrices and C is the output matrix: A(m,n)* B(n,p) = C(m,p)where m, n, and p are the matrix dimensions.

The matrix product is performed through three for loops along with the matrix indices:

      for i in range(m): 
            for j in range(p): 
                C[i, j] = 0 
                for k in range(n): 
                      C[i, j] += A[i, k] * B[k, j] 

The randint NumPy function is used here to build the input matrices of 10 × 10 dimensions:

dim = 10
A = np.random.randint(dim,size=(dim, dim))
B = np.random.randint(dim,size=(dim, dim))

Finally, the matmul function is called with these matrices as arguments, and the resultant C matrix is printed out:

C = matmul(A, B)
print("RESULT MATRIX C = A*B")
print(": %s" % C)

To execute this example, type the following:

(base) C:>python matMulNumba.py

The result shows the two matrices given as input and the matrix resulting from their product:

INPUT MATRIX A
:
[[8 7 1 3 1 0 4 9 2 2]
[3 6 2 7 7 9 8 4 4 9]
[8 9 9 9 1 1 1 1 8 0]
[0 5 0 7 1 3 2 0 7 3]
[4 2 6 4 1 2 9 1 0 5]
[3 0 6 5 1 0 4 3 7 4]
[0 9 7 2 1 4 3 3 7 3]
[1 7 2 7 1 8 0 3 4 1]
[5 1 5 0 7 7 2 3 0 9]
[4 6 3 6 0 3 3 4 1 2]]
INPUT MATRIX B
:
[[2 1 4 6 6 4 9 9 5 2]
[8 6 7 6 5 9 2 1 0 9]
[4 1 2 4 8 2 9 5 1 4]
[9 9 1 5 0 5 1 1 7 1]
[8 7 8 3 9 1 4 3 1 5]
[7 2 5 8 3 5 8 5 6 2]
[5 3 1 4 3 7 2 9 9 5]
[8 7 9 3 4 1 7 8 0 4]
[3 0 4 2 3 8 8 8 6 2]
[8 6 7 1 8 3 0 8 8 9]]
RESULT MATRIX C = A*B
:
[[225 172 201 161 170 172 189 230 127 169]
[400 277 289 251 278 276 240 324 295 273]
[257 171 177 217 208 254 265 224 176 174]
[187 130 116 117 94 175 105 128 152 114]
[199 133 117 143 168 156 143 214 188 157]
[180 118 124 113 152 149 175 213 167 122]
[238 142 186 165 188 215 202 200 139 192]
[237 158 162 176 122 185 169 140 137 130]
[249 160 220 159 249 125 201 241 169 191]
[209 152 142 154 131 160 147 161 132 137]]
..................Content has been hidden....................

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