Newbie : innerproduct function from Numarray
Colin J. Williams
cjw at sympatico.ca
Mon Jan 26 17:56:41 EST 2004
Ohkyu Yoon wrote:
> I have two vectors that have about 2000 elements.
> I need to calculate the innerproducts of those vectors 2000 times where
> one of them cycles left(ie 1234, then 2341, then 3412, etc)
> Basically, I want to calculate A*x, where A is a left-circulant cyclic
> matrix,
> and x is a vector.
>
> I tried it two ways.
> vector1 & vector2 are lists.
>
> 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?
>
> Thank you.
>
>
You might consider something like the following:
# ohkyu.py a sliding cross-product
import numarray as N
_nc= N.numarraycore
_nt= N.numerictypes
import numarray.random_array.RandomArray2 as _ra
# The sliding array
a= _nc.zeros(shape= (2, 10), type= _nt.Float64)
b= _nc.arange(10, shape= (10,), type= _nt.Float64) # data for the
sliding array
a+= b # the data from b is broadcast to both rows
print a # to show the 'wraparound'
a.ravel() # the second row is appended to the first
c= _ra.random(shape= (10,)) # data for the fixed array
for i in range(10):
z= _nc.dot(a[i:i+10], c)
print i, z
I would be interested to know how the time compares.
Colin W.
More information about the Python-list
mailing list