Newbie : innerproduct function from Numarray
Diez B. Roggisch
deets_noospaam at web.de
Mon Jan 26 17:07:14 EST 2004
> 1)
> from Numarray import innerproduct
> output = []
> temp = vector1 + vector1 # temp is twice the length of vector1
> for i in range(2000):
> output.append(innerproduct(temp[i:(i+2000)],vector2)
> 2)
> output = []
> temp = vector1 + vector1
> for i in range(2000):
> sum = 0
> for j in range(2000):
> sum += temp[i+j] * vector2[j]
> output.append(sum)
>
> I thought the first method using Numarray should be faster.
> But it looks like the second method is faster.
> Am I doing anything wrong?
> Do you guys know any faster way to do this?
First of all, I assume that both results are equal :)
>From the numarray-docs:
----
innerproduct(a, b)
innerproduct produces the inner product of arrays a and b. It is equivalent
to matrixmultiply(a, transpose(b)).
----
I'm not sure what this means mathematically (I understand the operation
made, but I'm not sure what an inner product _means_).
However, what you do with your hand-written code doesn't look like what I
would write if I would come up with my own implementation of the
aforementioned definition of innerproduct. Matrix-multiplication is
O(n**3), while your code is in O(n**2). So it seems that your special-case
with vectors, not arrays, produces an easier to compute variant of
innerproduct - and the differenes of n-times is of course important.
BTW: Its better to use xrange instead of range, it won't create the actual
list of numbers, but an iterable object instead - saves memory and time :)
Diez
More information about the Python-list
mailing list