Mathematica 7 compares to other languages

Juan Pablo Romero Méndez jpablo.romero at gmail.com
Wed Dec 10 22:02:31 EST 2008


In R:

norm = function(v) v/sqrt(sum(v^2))

:)


   Juan Pablo




2008/12/10 Arnaud Delobelle <arnodel at googlemail.com>:
> "Dotan Cohen" <dotancohen at gmail.com> writes:
>
>> 2008/12/10  <w_a_x_man at yahoo.com>:
>>> On Dec 5, 9:51 am, Xah Lee <xah... at gmail.com> wrote:
>>>>
>>>> For those of you who don't know linear algebra but knows coding, this
>>>> means, we want a function whose input is a list of 3 elements say
>>>> {x,y,z}, and output is also a list of 3 elements, say {a,b,c}, with
>>>> the condition that
>>>>
>>>> a = x/Sqrt[x^2+y^2+z^2]
>>>> b = y/Sqrt[x^2+y^2+z^2]
>>>> c = z/Sqrt[x^2+y^2+z^2]
>>>
>>>>
>>>> In lisp, python, perl, etc, you'll have 10 or so lines. In C or Java,
>>>> you'll have 50 or hundreds lines.
>>>
>>> Ruby:
>>>
>>> def norm a
>>>  s = Math.sqrt(a.map{|x|x*x}.inject{|x,y|x+y})
>>>  a.map{|x| x/s}
>>> end
>>
>> If someone doesn't counter with a Python one-liner then I'm going to
>> port that to brainfuck.
>
> 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 ;)
>
> --
> Arnaud
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list