Vectorization and Numeric (Newbie)

Discussion in 'Python' started by Ronny Mandal, Feb 28, 2006.

  1. Ronny Mandal

    Ronny Mandal Guest

    Assume you have a mathematical function, e.g. f(x) = x + 4

    To calculate all the values from 1 to n, a loop is one alternative.

    But to make this function work with vectors instead i.e
    f(x_vector) = result_vector,
    how should the function then be implemented?

    Thanks

    RM

    --

    Support bacteria - it's the only culture some people have!
     
    Ronny Mandal, Feb 28, 2006
    #1
    1. Advertising

  2. Ronny Mandal

    Juho Schultz Guest

    Ronny Mandal wrote:
    > Assume you have a mathematical function, e.g. f(x) = x + 4
    >
    > To calculate all the values from 1 to n, a loop is one alternative.
    >


    Numeric and friends (numarray,numpy) have something like numarray.arange
    - they return arrays similar to the lists returned by standard libs
    range function. I would recommend using the built-in array operations as
    much as possible - in most cases they are much faster than looping, and
    your code remains simpler.

    > But to make this function work with vectors instead i.e
    > f(x_vector) = result_vector,
    > how should the function then be implemented?
    >


    In most numeric libraries, vectors and scalars can be added etc.
    For the simple f(x) case you can use just the simplest approach:

    def f(x):
    return x+4

    and call this with scalar and vector args.
    f_pi = f(3.14159265)
    f_1to200 = f(numarray.arange(1,200))
     
    Juho Schultz, Feb 28, 2006
    #2
    1. Advertising

  3. Ronny Mandal

    Guest

    "map" takes a function and a list, applies the function to each item in
    a list, and returns the result list. For example:

    >>> def f(x): return x + 4


    >>> numbers = [4, 8, 15, 16, 23, 42]
    >>> map(f, numbers)

    [8, 12, 19, 20, 27, 46]

    So, rather that ask if there is a different way to write f, I'd just
    use f in a different way.

    Another way to accomplish the same result is with a list comprehension:

    >>> [f(x) for x in numbers]

    [8, 12, 19, 20, 27, 46]

    As you can see, if you wanted to "calculate all the values from 1 to
    n," you could also use these techniques instead of a loop.

    >>> n = 10
    >>> [f(x) for x in xrange(1, n+1)]

    [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
     
    , Feb 28, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RonnyM

    Numeric, vectorization

    RonnyM, May 1, 2006, in forum: Python
    Replies:
    1
    Views:
    261
    David Isaac
    May 1, 2006
  2. RonnyM

    Vectorization

    RonnyM, Jun 6, 2006, in forum: Python
    Replies:
    2
    Views:
    319
    Robert Kern
    Jun 6, 2006
  3. Replies:
    5
    Views:
    954
    X-Centric
    Jun 30, 2005
  4. vectorizor
    Replies:
    2
    Views:
    331
  5. Lynn McGuire

    automatic vectorization

    Lynn McGuire, Jul 8, 2010, in forum: C++
    Replies:
    5
    Views:
    417
    Ian Collins
    Jul 8, 2010
Loading...

Share This Page