On 30 Jul, 13:46, 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])
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.
--
http://mail.python.org/mailman/listinfo/python
"Alain Ketterlin" wrote in message
news:87fwyxgvuv@dpt-info.u-strasbg.fr...
> "Bartc" 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 it slower.)
>
> You mean by
"Bartc" 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 vectors ar
"Alain Ketterlin" wrote in message
news:877hkdhyl5@dpt-info.u-strasbg.fr...
> Lawrence D'Oliveiro 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 somethi
In message <87k4oah1rp@dpt-info.u-strasbg.fr>, Alain Ketterlin wrote:
> Lawrence D'Oliveiro 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):
>
> def f():
> print "
On Mon, Aug 2, 2010 at 1:50 AM, Terry Reedy 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 like th
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])
Lawrence D'Oliveiro 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 08/01/2010 03:41 AM, Lawrence D'Oliveiro wrote:
> In message <87sk2zhpcj@dpt-info.u-strasbg.fr>, Alain Ketterlin wrote:
>
>> Lawrence D'Oliveiro writes:
>>
>>> V = tuple \
>>> (
>>> x
>>> /
>>> l
>>>for x in V
>>>for l in
>>>
In message <87sk2zhpcj@dpt-info.u-strasbg.fr>, Alain Ketterlin wrote:
> Lawrence D'Oliveiro 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),
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])
>V = (V[0] / L,
Lawrence D'Oliveiro 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
> language features:
>
>
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
In message <877hkdhyl5@dpt-info.u-strasbg.fr>, Alain Ketterlin wrote:
> Lawrence D'Oliveiro 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 s
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
On Fri, Jul 30, 2010 at 4: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] +
Lawrence D'Oliveiro 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] + V[2] * V[2])
> V = (V[0]
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 li
19 matches
Mail list logo