Mathematica 7 compares to other languages

Roberto Bonvallet rbonvall at gmail.com
Thu Dec 11 04:28:04 CET 2008


Arnaud Delobelle wrote:

> def unit(v):
>     return map((sum(map(lambda x:x*x, v))**0.5).__rdiv__, v)
>
> The hard bit was to make it less readable than the Ruby version ;)

I loved the use of __rdiv__ :)
I would have proposed the more straightforward:

    def u(v):
        return [x/sum(x**2 for x in v)**0.5 for x in v]

or, in order to avoid computing the norm for each element:

    def u(v):
        return (lambda norm: [x/norm for x in v])(sum(x**2 for x in v)
**0.5)
--
Roberto Bonvallet



More information about the Python-list mailing list