Re: Mathematica 7 compares to other languages
On Dec 11, 4:53 pm, "William James" <[EMAIL PROTECTED]> wrote: > William James wrote: > > John W Kennedy wrote: > > > > Xah Lee wrote: > > > > In lisp, python, perl, etc, you'll have 10 or so lines. In C or > > > > Java, you'll have 50 or hundreds lines. > > > > Java: > > > > static float[] normal(final float[] x) { > > > float sum = 0.0f; > > > for (int i = 0; i < x.length; ++i) sum += x[i] * x[i]; > > > final float divisor = (float) Math.sqrt(sum); > > > float[] a = new float[x.length]; > > > for (int i = 0; i < x.length; ++i) a[i] = x[i]/divisor; > > > return a; > > > } > > > "We don't need no stinkin' loops!" > > > SpiderMonkey Javascript: > > > function normal( ary ) > > { div=Math.sqrt(ary.map(function(x) x*x).reduce(function(a,b) a+b)) > > return ary.map(function(x) x/div) > > } > > The variable "div" shouldn't be global. > > function normal( ary ) > { var div = Math.sqrt( > ary.map(function(x) x*x).reduce(function(a,b) a+b) ) > return ary.map(function(x) x/div) > > } > > Chicken Scheme: (require 'srfi-1) (define (norm vec) (map (cute / <> (sqrt (reduce + 0 (map (cute expt <> 2) vec vec)) Cute huh? ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Mathematica 7 compares to other languages
On Dec 11, 7:50 pm, [EMAIL PROTECTED] wrote: > On Dec 11, 4:53 pm, "William James" <[EMAIL PROTECTED]> wrote: > > > > > William James wrote: > > > John W Kennedy wrote: > > > > > Xah Lee wrote: > > > > > In lisp, python, perl, etc, you'll have 10 or so lines. In C or > > > > > Java, you'll have 50 or hundreds lines. > > > > > Java: > > > > > static float[] normal(final float[] x) { > > > > float sum = 0.0f; > > > > for (int i = 0; i < x.length; ++i) sum += x[i] * x[i]; > > > > final float divisor = (float) Math.sqrt(sum); > > > > float[] a = new float[x.length]; > > > > for (int i = 0; i < x.length; ++i) a[i] = x[i]/divisor; > > > > return a; > > > > } > > > > "We don't need no stinkin' loops!" > > > > SpiderMonkey Javascript: > > > > function normal( ary ) > > > { div=Math.sqrt(ary.map(function(x) x*x).reduce(function(a,b) a+b)) > > > return ary.map(function(x) x/div) > > > } > > > The variable "div" shouldn't be global. > > > function normal( ary ) > > { var div = Math.sqrt( > > ary.map(function(x) x*x).reduce(function(a,b) a+b) ) > > return ary.map(function(x) x/div) > > > } > > Chicken Scheme: > > (require 'srfi-1) > (define (norm vec) > (map (cute / <> (sqrt (reduce + 0 (map (cute expt <> 2) vec > vec)) > > Cute huh? ;-) Haskell looks the best though: norm v = map (/ (sqrt (sum (map (^2) v v -- http://mail.python.org/mailman/listinfo/python-list
Re: Mathematica 7 compares to other languages
On Dec 12, 12:12 am, Xah Lee wrote: > On Dec 11, 6:50 am, the.brown.dragon.b...@gmail.com wrote: > ;; Chicken Scheme. By the.brown.dragon...@gmail.com > (require 'srfi-1) > (define (normalize vec) > (map (cute / <> (sqrt (reduce + 0 (map (cute expt <> 2) vec > vec)) > > Is it possible to make it work in scsh? (i'm running scsh 0.6.4, and > don't know Scheme lisp well) > > Xah > ∑http://xahlee.org/ > > ☄ I don't have scsh but yes - it should work fine. "cute" is an SRFI (http://srfi.schemers.org/srfi-26/) which should be available. cheers, BD -- http://mail.python.org/mailman/listinfo/python-list