# [Numpy-discussion] matrix multiplication

Robert Kern robert.kern@gmail....
Fri Jun 5 18:00:10 CDT 2009

```On Fri, Jun 5, 2009 at 17:54, Keith Goodman <kwgoodman@gmail.com> wrote:
> On Fri, Jun 5, 2009 at 3:02 PM, Alan G Isaac <aisaac@american.edu> wrote:
>> I think something close to this would be possible:
>> add dot as an array method.
>>        A .dot(B) .dot(C)
>> is not as pretty as
>>        A * B * C
>> but it is much better than
>>        np.dot(np.dot(A,B),C)
>
> I've noticed that x.sum() is faster than sum(x)
>
>>> x = np.array([1,2,3])
>>> timeit x.sum()
> 100000 loops, best of 3: 3.01 µs per loop
>>> from numpy import sum
>>> timeit sum(x)
> 100000 loops, best of 3: 4.84 µs per loop
>
> Would the same be true of dot? That is, would x.dot(y) be faster than
> dot(x,y)? Or is it just that np.sum() has to go through some extra
> python code before it hits the C code?

No and yes, respectively.

> In general, if I'm trying to speed up an inner loop, I try to replace
> func(x) with x.func(). But I don't really understand the general
> principle at work here.

Most of the functions that mirror methods, including numpy.sum(), are
Python functions that have a little of bit of code to convert the
input to an array if necessary and to dispatch to the method.
numpy.dot() is already implemented in C.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
```