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
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
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
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.
--
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]
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
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
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])
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
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):
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
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.
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 -
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
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]
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
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
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] +
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 =
19 matches
Mail list logo