Mathematica 7 compares to other languages

Xah Lee xahlee at gmail.com
Thu Dec 25 06:24:31 EST 2008


> >On Dec 10, 2:47 pm, John W Kennedy <jwke... at attglobal.net> wrote:
> >> C:
>
> >> #include <stdlib.h>
> >> #include <math.h>
>
> >> void normal(int dim, float* x, float* a) {
> >>     float sum = 0.0f;
> >>     int i;
> >>     float divisor;
> >>     for (i = 0; i < dim; ++i) sum += x[i] * x[i];
> >>     divisor = sqrt(sum);
> >>     for (i = 0; i < dim; ++i) a[i] = x[i]/divisor;
>
> >> }

Due to the low level of C, this C example should perhaps then accept a
sequence of numbers separated by space, and print the output. Having
dimension as part of input is not acceptable.

For examples in other langs that can take any input (in source code)
and print output, see:
http://xahlee.org/UnixResource_dir/writ/Mathematica_expressiveness.html

A new addition is in Scheme Lisp:

;; Scheme Lisp. By Bakul Shah
(define (normalize vec)
   (let ((d (sqrt (apply + (map (lambda (x) (* x x)) vec)))))
     (map (lambda (x) (/ x d)) vec)))

(normalize '(3 4))

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”)

  Xah
∑ http://xahlee.org/


More information about the Python-list mailing list