Mathematica 7 compares to other languages

W

William James

John said:
Xah said:
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 * x;
final float divisor = (float) Math.sqrt(sum);
float[] a = new float[x.length];
for (int i = 0; i < x.length; ++i) a = x/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)
}
 
W

William James

William said:
John said:
Xah said:
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 * x;
final float divisor = (float) Math.sqrt(sum);
float[] a = new float[x.length];
for (int i = 0; i < x.length; ++i) a = x/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)
}
 
T

the.brown.dragon.blog

William said:
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 * x;
   final float divisor = (float) Math.sqrt(sum);
   float[] a = new float[x.length];
   for (int i = 0; i < x.length; ++i) a = x/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? ;-)
 
T

the.brown.dragon.blog

William said:
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 * x;
   final float divisor = (float) Math.sqrt(sum);
   float[] a = new float[x.length];
   for (int i = 0; i < x.length; ++i) a = x/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
 
W

William James

William said:
John said:
Xah said:
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 * x;
final float divisor = (float) Math.sqrt(sum);
float[] a = new float[x.length];
for (int i = 0; i < x.length; ++i) a = x/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)
}
 
A

Andreas Waldenburger

from numpy.linalg import norm

:)

This is one line of Python code alright, but it's not a "one-liner" in
the "computes the a normalized vector"-sense (which was the original
challenge, if there ever was one).

If anything, you are now ready to compute the *norm* of a vector in a
subseqent line. (Or, if you must, after a semicolon on the same line.)

:)
/W
 
S

Stef Mientki

Andreas said:
This is one line of Python code alright, but it's not a "one-liner" in
the "computes the a normalized vector"-sense (which was the original
challenge, if there ever was one).

If anything, you are now ready to compute the *norm* of a vector in a
subseqent line. (Or, if you must, after a semicolon on the same line.)
Couldn't we assume that when you doing these kinds of math, there's always
import numpy
or
from numpy import *

is always at the start of the program, so it doesn't belong to the
functional code ?

And for those who use VPython (and thus needs " from visual import *" )
can simply do
a = norm ( v )

cheers,
Stef
 
X

Xah Lee

Xah said:
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.
[...]

Thanks to various replies.
I've now gather code solutions in ruby, python, C, Java, here:
now lacking is perl, elisp, which i can do well in a condensed way.
It'd be interesting also to have javascript...

mmm, stone soup...

javascript:

var map = function(fn, a) {
     var b = new Array(a.length);
     for (i = 0; i < a.length; i++) {
         b = fn(a);
     }
     return b

};

var reduce = function(fn, a, init) {
     var s = init;
     for (i = 0; i < a.length; i++) {
         s = fn(s, a);
     }
     return s

};

var sum = function(a) {
     return reduce(function(x, y) { return x + y }, a, 0.0)

};

var norm = function(a) {
     var pow = Math.pow;
     return Math.sqrt(sum(map(function(x) { return pow(x, 2) }, a)))

};

var Unit = function(a) {
     var N = norm(a);
     return map(function(x) { return x/N }, a)

};


thats about 15 lines. I'm pretty sure JavaScript doesn't need that
many?

Xah
∑ http://xahlee.org/

☄
 
X

Xah Lee

Xah Lee wrote:

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

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)

}

I run your code in SpiderMonkey:

// SpiderMonkey javascript. By William James.
function normal(v) {
var div=Math.sqrt(v.map(function(x) x*x).reduce(function(a,b) a+b))
return v.map(function(x) x/div)
}

print normal [3,4];

i got:

/Users/xah/Documents/temp/xx.js:3: SyntaxError: missing { before
function body:
/Users/xah/Documents/temp/xx.js:3: var div=Math.sqrt(v.map(function(x)
x*x).reduce(function(a,b) a+b))
/Users/xah/Documents/temp/xx.js:
3: ....................................^

Xah
∑ http://xahlee.org/

☄
 
X

Xah Lee

Xah said:
In lisp, python, perl, etc, you'll have 10 or so lines. In C or Java,
you'll have 50 or hundreds lines.

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 * x;
    divisor = sqrt(sum);
    for (i = 0; i < dim; ++i) a = x/divisor;

}


i don't have experience coding C. The code above doesn't seems to
satisfy the spec. The input should be just a vector, array, list, or
whatever the lang supports.
The output is the same datatype of the same dimension.

Xah
∑ http://xahlee.org/

☄
 
X

Xah Lee

On Dec 11, 6:50 am, (e-mail address removed) wrote:
;; Chicken Scheme. By (e-mail address removed)
(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/

☄
 
J

Jim Gibson

Xah said:
Xah said:
In lisp, python, perl, etc, you'll have 10 or so lines. In C or Java,
you'll have 50 or hundreds lines.

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 * x;
    divisor = sqrt(sum);
    for (i = 0; i < dim; ++i) a = x/divisor;

}

Java:

static float[] normal(final float[] x) {
    float sum = 0.0f;
    for (int i = 0; i < x.length; ++i) sum += x * x;
    final float divisor = (float) Math.sqrt(sum);
    float[] a = new float[x.length];
    for (int i = 0; i < x.length; ++i) a = x/divisor;
    return a;

}


Thanks to various replies.

I've now gather code solutions in ruby, python, C, Java, here:

€ A Example of Mathematica's Expressiveness
http://xahlee.org/UnixResource_dir/writ/Mathematica_expressiveness.html

now lacking is perl, elisp, which i can do well in a condensed way.
It'd be interesting also to have javascript... and perhaps erlang,
OCaml/F#, Haskell too.


Perl:

sub normal
{
my $sum = 0;
$sum += $_ ** 2 for @_;
my $length = sqrt($sum);
return map { $_/$length } @_;
}
 
T

Tom McGlynn

John said:
Xah said:
In lisp, python, perl, etc, you'll have 10 or so lines. In C or
Java, you'll have 50 or hundreds lines.

static float[] normal(final float[] x) {
float sum = 0.0f;
for (int i = 0; i < x.length; ++i) sum += x * x;
final float divisor = (float) Math.sqrt(sum);
float[] a = new float[x.length];
for (int i = 0; i < x.length; ++i) a = x/divisor;
return a;
}


Calculating the norm of a vector is a numeric operation, so not
surprising it's pretty
easy to do in Fortran. While pre Fortran 90 would using something
similar to the above,
today you can do it as

norm = x/sqrt(sum(x*x))

for an arbitratry vector (or tensor for that matter). It would take a
couple more lines to wrap it up in a function

real function norm(x)
real x(*)
norm = x/sqrt(sum(x*x))
end

[Caveat: my Fortran is more than rusty ....]

So even Fortran can do this in only 1 line or 4 as a function.

Judging by the LOC and, at least to my eye, the clarity of the method,
Fortran is a real winner! Just to give a tiny bit of Java relevance
to the discussion: It's the ability to write functions that
straightforwardly express the mathematical intent of the user that
makes operator overloading (in this case over arrays) so useful in
many numerical contexts. This is cross-posted to Python as well. I
understand it has similar array arithmetic capabilities to Fortran. I
believe this may be one reason for Python's burgeoning popularity

Tom McGlynn
 
G

George Neuner

Xah said:
In lisp, python, perl, etc, you'll have 10 or so lines. In C or Java,
you'll have 50 or hundreds lines.

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 * x;
    divisor = sqrt(sum);
    for (i = 0; i < dim; ++i) a = x/divisor;

}


i don't have experience coding C.


Then why do you talk about it as if you know something?
The code above doesn't seems to satisfy the spec.

It does.
The input should be just a vector, array, list, or
whatever the lang supports. The output is the same
datatype of the same dimension.

C's native arrays are stored contiguously. Multidimensional arrays
can be accessed as a vector of length (dim1 * dim2 * ... * dimN).

This code handles arrays of any dimensionality. The poorly named
argument 'dim' specifies the total number of elements in the array.

George
 
G

George Neuner

Dear George Neuner,




it's amazing that every tech geekers (aka idiots) want to quote
“Turing Complete” in every chance. Even a simple cellular automata,
such as Conway's game of life or rule 110, are complete.

http://en.wikipedia.org/wiki/Conway's_Game_of_Life
http://en.wikipedia.org/wiki/Rule_110

in fact, according to Stephen Wolfram's controversial thesis by the
name of “Principle of computational equivalence”, every goddamn thing
in nature is just about turing complete. (just imagine, when you take
a piss, the stream of yellow fluid is actually doing turning complete
computations!)

Wolfram's thesis does not make the case that everything is somehow
doing computation.
for a change, it'd be far more interesting and effective knowledge
showoff to cite langs that are not so-called **** of the turing
complete.

We geek idiots cite Turing because it is an important measure of a
language. There are plenty of languages which are not complete. That
you completely disregard a fundamental truth of computing is
disturbing.
the rest of you message went on stupidly on the turing complete point
of view on language's power, mixed with lisp fanaticism, and personal
gribes about merits and applicability assembly vs higher level langs.

You don't seem to understand the difference between leverage and power
and that disturbs all the geeks here who do. We worry that newbies
might actually listen to your ridiculous ramblings and be led away
from the truth.
It's fine to go on with your gribes, but be careful in using me as a
stepping stone.

Xah, if I wanted to step on you I would do it with combat boots. You
should be thankful that you live 3000 miles away and I don't care
enough about your petty name calling to come looking for you. If you
insult people in person like you do on usenet then I'm amazed that
you've lived this long.

George
 
B

Bakul Shah

George said:
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.
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 * x;
divisor = sqrt(sum);
for (i = 0; i < dim; ++i) a = x/divisor;

}

i don't have experience coding C.


Then why do you talk about it as if you know something?
The code above doesn't seems to satisfy the spec.

It does.
The input should be just a vector, array, list, or
whatever the lang supports. The output is the same
datatype of the same dimension.

C's native arrays are stored contiguously. Multidimensional arrays
can be accessed as a vector of length (dim1 * dim2 * ... * dimN).

This code handles arrays of any dimensionality. The poorly named
argument 'dim' specifies the total number of elements in the array.

George


Only if the length in each dimension is known at compile time (or
in C99, if this is an automatic array). When this is not the case,
you may have to implement something like the following (not the only
way, just one way):

float** new_matrix(int rows, int cols) {
float** m = malloc(sizeof(float*)*rows);
int i;
for (i = 0; i < rows; i++)
m = malloc(sizeof(float)*cols);
return m;
}

In this case normal() fails since matrix m is not in a single
contiguous area.

But I suspect Xah is complaining because the function doesn't
*return* a value of the same type; instead you have to pass in
the result vector. But such is life if you code in C!
 
T

the.brown.dragon.blog

On Dec 11, 6:50 am, (e-mail address removed) wrote:
;; Chicken Scheme. By (e-mail address removed)
(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
 
X

Xah Lee

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 * x;
divisor = sqrt(sum);
for (i = 0; i < dim; ++i) a = x/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/

☄
 
W

w_a_x_man

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top