[SciPy-User] fast small matrix multiplication with cython?

Keith Goodman kwgoodman@gmail....
Mon Dec 6 18:31:28 CST 2010

On Mon, Dec 6, 2010 at 2:34 PM, Skipper Seabold <jsseabold@gmail.com> wrote:

> @cython.boundscheck(False)
> @cython.wraparound(False)
> cdef inline object matmult(ndarray[DOUBLE, ndim=2, mode='c'] A,
>                    ndarray[DOUBLE, ndim=2, mode='c'] B):
>    cdef int lda = A.shape[0]
>    cdef int n = B.shape[1]
>    cdef npy_intp *dims = [lda, n]
>    cdef ndarray[DOUBLE, ndim=2] out = PyArray_SimpleNew(2, dims, NPY_DOUBLE)
>    cdef int i,j,k
>    cdef double s

Do the cdef's above take a sizeable fraction of the time given that
your input arrays are small? If so, then you could do those before you
enter the inner loop where the dot product is needed. You wouldn't end
up with a reusable matmult function, but you'd get rid of some

So in your inner loop, you'd only have:

>    for i in xrange(lda):
>        for j in xrange(n):
>            s = 0
>            for k in xrange(A.shape[1]):
>                s += A[i,k] * B[k,j]
>            out[i,j] = s
>    return out

More information about the SciPy-User mailing list