Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Vectorization and Numeric (Newbie)

Reply
Thread Tools

Vectorization and Numeric (Newbie)

 
 
Ronny Mandal
Guest
Posts: n/a
 
      02-28-2006
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!
 
Reply With Quote
 
 
 
 
Juho Schultz
Guest
Posts: n/a
 
      02-28-2006
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))
 
Reply With Quote
 
 
 
 
johnzenger@gmail.com
Guest
Posts: n/a
 
      02-28-2006
"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]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
automatic vectorization Lynn McGuire C++ 5 07-08-2010 10:16 PM
Vectorization of template functions vectorizor C++ 2 06-22-2007 05:09 PM
Vectorization RonnyM Python 2 06-06-2006 03:31 PM
Numeric, vectorization RonnyM Python 1 05-01-2006 04:32 PM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57