Mathematica 7 compares to other languages
Tom McGlynn
tam at milkyway.gsfc.nasa.gov
Fri Dec 12 16:39:44 CET 2008
On Dec 11, 6:46 am, "William James" <w_a_x_... at yahoo.com> 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;
> > }
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
More information about the Python-list
mailing list