Mathematica 7 compares to other languages

William James w_a_x_man at yahoo.com
Sat Jan 31 00:32:31 EST 2009


w_a_x_man at yahoo.com wrote:

> On Dec 25, 5:24 am, Xah Lee <xah... at gmail.com> wrote:
> 
> > The JavaScript example:
> > 
> > // Javascript. By William James
> > function normalize( vec ) {
> > var div=Math.sqrt(vec.map(function(x) x*x).reduce(function(a,b)
> > a+b))   return vec.map(function(x) x/div)
> > 
> > }
> > 
> > is also not qualified. (it is syntax error in SpiderMonkey engine
> > “JavaScript-C 1.7.0 2007-10-03”)
> 
> Since you are using the latest version of Mathematica, you should
> also use the latest version of SpiderMonkey.
> 
> The function works outside of a web browser with jslibs, and it
> works in Firefox 3.0.1.
> 
> <html>
> <body>
> 
> <script type="application/javascript;version=1.8"/>
> 
> // Tested with Firefox 3.0.1.
> // SpiderMonkey JavaScript 1.6 added map().
> // 1.8 added reduce() and function shorthand:
> // function(x) { return x * x }
> //   can now be:
> // function(x) x * x
> 
> // Javascript. By William James
> function normalize( vec ) {
> var div=Math.sqrt(vec.map(function(x) x*x).reduce(function(a,b) a+b))
>   return vec.map(function(x) x/div)
> }
> 
> window.alert( normalize( [2,3,4] ).toSource() )
> 
> </script>
> 
> </body>
> </html>

Reduce:

procedure normalize vec;
  begin scalar div;
    div := for each u in vec sum u^2;
    return map(~x/div, vec)
  end;




More information about the Python-list mailing list