Re: Normalizing A Vector

2010-08-02 Thread Bartc
Alain Ketterlin al...@dpt-info.u-strasbg.fr wrote in message news:877hkdhyl5@dpt-info.u-strasbg.fr... Lawrence D'Oliveiro l...@geek-central.gen.new_zealand writes: Say a vector V is a tuple of 3 numbers, not all zero. You want to normalize it (scale all components by the same factor) so

Re: Normalizing A Vector

2010-08-02 Thread Alain Ketterlin
Bartc ba...@freeuk.com writes: def norm(V): L = math.sqrt( sum( [x**2 for x in V] ) ) return [ x/L for x in V ] There's a cost involved in using those fancy constructions. Sure. The above has three loops that take some time. I found the following to be about twice as fast, when

Re: Normalizing A Vector

2010-08-02 Thread Bartc
Alain Ketterlin al...@dpt-info.u-strasbg.fr wrote in message news:87fwyxgvuv@dpt-info.u-strasbg.fr... Bartc ba...@freeuk.com writes: def norm3d(v): L = math.sqrt((v[0]*v[0]+v[1]*v[1]+v[2]*v[2])) return (v[0]/L,v[1]/L,v[2]/L) (Strangely, changing those divides to multiplies made

Re: Normalizing A Vector

2010-08-02 Thread Lawrence D'Oliveiro
In message 6dw5o.72330$ds3.63...@hurricane, Bartc wrote: There's a cost involved in using those fancy constructions. Sure. But at the point that starts to matter, you have to ask yourself why you’re not rewriting the CPU-intensive part in C. --

Re: Normalizing A Vector

2010-08-02 Thread sturlamolden
On 30 Jul, 13:46, Lawrence D'Oliveiro l...@geek- central.gen.new_zealand wrote: Say a vector V is a tuple of 3 numbers, not all zero. You want to normalize it (scale all components by the same factor) so its magnitude is 1. The usual way is something like this:     L = math.sqrt(V[0] * V[0]

Re: Normalizing A Vector

2010-08-01 Thread Thomas Jollans
On 08/01/2010 03:41 AM, Lawrence D'Oliveiro wrote: In message 87sk2zhpcj@dpt-info.u-strasbg.fr, Alain Ketterlin wrote: Lawrence D'Oliveiro l...@geek-central.gen.new_zealand writes: V = tuple \ ( x / l for x in V for l in

Re: Normalizing A Vector

2010-08-01 Thread Alain Ketterlin
Lawrence D'Oliveiro l...@geek-central.gen.new_zealand writes: V = tuple \ ( x / l for x in V for l in (math.sqrt(reduce(lambda a, b : a + b, (y * y for y in V), 0)),) ) You got the order wrong (it has

Re: Normalizing A Vector

2010-08-01 Thread Terry Reedy
On 7/30/2010 7:46 AM, Lawrence D'Oliveiro wrote: Say a vector V is a tuple of 3 numbers, not all zero. You want to normalize it (scale all components by the same factor) so its magnitude is 1. The usual way is something like this: L = math.sqrt(V[0] * V[0] + V[1] * V[1] + V[2] * V[2])

Re: Normalizing A Vector

2010-08-01 Thread Matteo Landi
On Mon, Aug 2, 2010 at 1:50 AM, Terry Reedy tjre...@udel.edu wrote: On 7/30/2010 7:46 AM, Lawrence D'Oliveiro wrote: Say a vector V is a tuple of 3 numbers, not all zero. You want to normalize it (scale all components by the same factor) so its magnitude is 1. The usual way is something

Re: Normalizing A Vector

2010-08-01 Thread Lawrence D'Oliveiro
In message 87k4oah1rp@dpt-info.u-strasbg.fr, Alain Ketterlin wrote: Lawrence D'Oliveiro l...@geek-central.gen.new_zealand writes: No, I deliberately put it in that order to ensure that the value for l can only ever be evaulated once. Try this (essentially equivalent to your code):

Re: Normalizing A Vector

2010-07-31 Thread John Nagle
On 7/30/2010 6:46 AM, Alain Ketterlin wrote: Does the compiler hoist the math.sqrt(...) out of the implicit loop? Global optimization in Python? Not in CPython. The program might redefine math.sqrt from another thread while the program is running, which would invalidate the hoisting of

Re: Normalizing A Vector

2010-07-31 Thread Lawrence D'Oliveiro
In message 877hkdhyl5@dpt-info.u-strasbg.fr, Alain Ketterlin wrote: Lawrence D'Oliveiro l...@geek-central.gen.new_zealand writes: What I don’t like is having that intermediate variable L leftover after the computation. Well, it also guarantees that the square root is computed once.

Re: Normalizing A Vector

2010-07-31 Thread Thomas Jollans
On 07/31/2010 12:15 PM, Lawrence D'Oliveiro wrote: reduce(lambda a, b : a + b, (y * y for y in V), 0)) This is a lot more verbose than it has to be, and probably slower too. firstly: (lambda a,b: a+b) is equivalent to operator.add. == reduce(operator.add, (y*y for y in V), 0) However -

Re: Normalizing A Vector

2010-07-31 Thread Alain Ketterlin
Lawrence D'Oliveiro l...@geek-central.gen.new_zealand writes: What I don’t like is having that intermediate variable L leftover after the computation. Well, it also guarantees that the square root is computed once. OK, this version should solve that problem, without requiring any new

Re: Normalizing A Vector

2010-07-31 Thread Tim Roberts
Lawrence D'Oliveiro l...@geek-central.gen.new_zealand wrote: Say a vector V is a tuple of 3 numbers, not all zero. You want to normalize it (scale all components by the same factor) so its magnitude is 1. The usual way is something like this: L = math.sqrt(V[0] * V[0] + V[1] * V[1] + V[2]

Re: Normalizing A Vector

2010-07-31 Thread Lawrence D'Oliveiro
In message 87sk2zhpcj@dpt-info.u-strasbg.fr, Alain Ketterlin wrote: Lawrence D'Oliveiro l...@geek-central.gen.new_zealand writes: V = tuple \ ( x / l for x in V for l in (math.sqrt(reduce(lambda a, b : a + b, (y * y

Normalizing A Vector

2010-07-30 Thread Lawrence D'Oliveiro
Say a vector V is a tuple of 3 numbers, not all zero. You want to normalize it (scale all components by the same factor) so its magnitude is 1. The usual way is something like this: L = math.sqrt(V[0] * V[0] + V[1] * V[1] + V[2] * V[2]) V = (V[0] / L, V[1] / L, V[2] / L) What I don’t

Re: Normalizing A Vector

2010-07-30 Thread Alain Ketterlin
Lawrence D'Oliveiro l...@geek-central.gen.new_zealand writes: Say a vector V is a tuple of 3 numbers, not all zero. You want to normalize it (scale all components by the same factor) so its magnitude is 1. The usual way is something like this: L = math.sqrt(V[0] * V[0] + V[1] * V[1] +

Re: Normalizing A Vector

2010-07-30 Thread Chris Rebert
On Fri, Jul 30, 2010 at 4:46 AM, Lawrence D'Oliveiro l...@geek-central.gen.new_zealand wrote: Say a vector V is a tuple of 3 numbers, not all zero. You want to normalize it (scale all components by the same factor) so its magnitude is 1. The usual way is something like this:    L =