Mathematica 7 compares to other languages

Xah Lee xahlee at gmail.com
Wed Dec 10 23:15:09 CET 2008


Xah Lee 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.
> >
> > Note, that the “norm” as defined above works for vectors of any
> > dimention, i.e. list of any length.


On Dec 10, 12:37 pm, w_a_x_... at yahoo.com wrote:
> Ruby:
>
> def norm a
>   s = Math.sqrt(a.map{|x|x*x}.inject{|x,y|x+y})
>   a.map{|x| x/s}
> end

I don't know ruby, but i tried to run it and it does not work.

#ruby
def norm a
  s = Math.sqrt(a.map{|x|x*x}.inject{|x,y|x+y})
  a.map{|x| x/s}
end

v = [3,4]

p norm(v) # returns [0.6, 0.8]

The correct result for that input would be 5.

Also note, i wrote: «Note, that the “norm” as defined above works for
vectors of any dimention, i.e. list of any length.».

For detail, see:
• A Example of Mathematica's Expressiveness
  http://xahlee.org/UnixResource_dir/writ/Mathematica_expressiveness.html

  Xah
∑ http://xahlee.org/


More information about the Python-list mailing list