Mathematica 7 compares to other languages
John W Kennedy
jwkenne at attglobal.net
Wed Dec 10 17:47:49 EST 2008
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[i] * x[i];
divisor = sqrt(sum);
for (i = 0; i < dim; ++i) a[i] = x[i]/divisor;
}
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;
}
--
John W. Kennedy
"Never try to take over the international economy based on a radical
feminist agenda if you're not sure your leader isn't a transvestite."
-- David Misch: "She-Spies", "While You Were Out"
More information about the Python-list
mailing list