Vectorization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RonnyM

    #1

    Vectorization

    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[i]
    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

  • Paul McGuire

    #2
    Re: Vectorization

    "RonnyM" <ronnyma@math.u io.no> wrote in message
    news:1149595753 .611358.189070@ i39g2000cwa.goo glegroups.com.. .[color=blue]
    > 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[i]
    > 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
    >[/color]

    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[i][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]) ]




    Comment

    • Robert Kern

      #3
      Re: Vectorization

      Paul McGuire wrote:[color=blue]
      > "RonnyM" <ronnyma@math.u io.no> wrote in message
      > news:1149595753 .611358.189070@ i39g2000cwa.goo glegroups.com.. .
      >[color=green]
      >>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[i]
      >> 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[/color]
      >
      > 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.[/color]

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



      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:



      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

      Comment

      Working...