Created
July 31, 2017 15:09
-
-
Save anilsathyan7/0f9e3682132471790738f258021e4524 to your computer and use it in GitHub Desktop.
Python numba matrix multiplication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| import numba | |
| from numba import jit | |
| import numpy as np | |
| #input matrices | |
| matrix1 = np.random.rand(30,30) | |
| matrix2 = np.random.rand(30,30) | |
| rmatrix = np.zeros(shape=(30,30)) | |
| #multiplication function | |
| @jit('void(float64[:,:],float64[:,:],float64[:,:])') | |
| def matmul(matrix1,matrix2,rmatrix): | |
| for i in range(len(matrix1)): | |
| for j in range(len(matrix2[0])): | |
| for k in range(len(matrix2)): | |
| rmatrix[i][j] += matrix1[i][k] * matrix2[k][j] | |
| #Calculate running time start=time.clock() | |
| matmul(matrix1,matrix2,rmatrix) | |
| end=time.clock() | |
| #print results | |
| print end-start | |
| for r in rmatrix: | |
| print(r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment