Vectorization

R

RonnyM

Hi!

Need to vectorize this, but do not have a clue.

a = n*m matrix
x and y are n and m vectors

Suggestions?



def fill(a, x, y):
for i in range(1,a.shape[0]):
xp = x
for j in range(a.shape[1]):
yp = y[j]
a[i,j] = sin(xp*yp)*exp(-xp*yp) + a[i-1,j]
return a

Thanks in advance,

Ronny Mandal
 
P

Paul McGuire

RonnyM said:
Hi!

Need to vectorize this, but do not have a clue.

a = n*m matrix
x and y are n and m vectors

Suggestions?



def fill(a, x, y):
for i in range(1,a.shape[0]):
xp = x
for j in range(a.shape[1]):
yp = y[j]
a[i,j] = sin(xp*yp)*exp(-xp*yp) + a[i-1,j]
return a

Thanks in advance,

Ronny Mandal


Something like this, but the first row in a is never modified, is this
correct?

Note: this is a brute force Python attempt at a matrix, using a list of
lists. Look also at the array and numarray modules.

-- Paul


from math import sin,exp

def fill(a,x,y):
aRowCount = len(x)
aColCount = len(y)
#a = [[0]*aColCount for i in range(aRowCount)]
for ii,xp in enumerate(x[1:]):
i = ii+1
for j,yp in enumerate(y):
a[j] = sin(xp*yp)*exp(-xp*yp) + a[i-1][j]
return a

or more tersely (note - no side-effect of modifying a in place, makes a new
copy):

def fill2(a,x,y):
fn = lambda t,u,v:sin(t*u)*exp(-t*u) + v
return [a[0]] + [ [ fn(xp,yp,aa) for (yp,aa) in zip(y,arow) ] for
(xp,arow) in zip(x[1:],a[:-1]) ]
 
R

Robert Kern

Paul said:
Hi!

Need to vectorize this, but do not have a clue.

a = n*m matrix
x and y are n and m vectors

Suggestions?

def fill(a, x, y):
for i in range(1,a.shape[0]):
xp = x
for j in range(a.shape[1]):
yp = y[j]
a[i,j] = sin(xp*yp)*exp(-xp*yp) + a[i-1,j]
return a

Thanks in advance,

Ronny Mandal


Something like this, but the first row in a is never modified, is this
correct?

Note: this is a brute force Python attempt at a matrix, using a list of
lists. Look also at the array and numarray modules.


The array module doesn't do matrices, and numarray is deprecated. Please point
new users to numpy, instead.

http://numeric.scipy.org/

Since the OP seems to already be using one of Numeric/numarray/numpy, (given
"a.shape"), I would suggest that he ask the question on the appropriate mailing
list:

https://lists.sourceforge.net/lists/listinfo/numpy-discussion

I won't attempt an answer until the OP answers about the first row.

--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top