Mathematica 7 compares to other languages
Arne Vajhøj
arne at vajhoej.dk
Wed Dec 10 18:44:51 EST 2008
Jon Harrop wrote:
> Xah Lee wrote:
>> Kaz Kylheku wrote:
>>> Really? ``50 or hundreds'' of lines in C?
>>>
>>> #include <math.h> /* for sqrt */
>>>
>>> void normalize(double *out, double *in)
>>> {
>>> double denom = sqrt(in[0] * in[0] + in[1] * in[1] + in[2] *
>>> in[2]);
>>>
>>> out[0] = in[0]/denom;
>>> out[1] = in[1]/denom;
>>> out[2] = in[2]/denom;
>>> }
>>>
>>> Doh?
>> Kaz, pay attention:
>>
>> Xah wrote: «Note, that the “norm” as defined above works for vectors
>> of any dimention, i.e. list of any length.»
>
> That is still only 6 lines of C code and not 50 as you claimed:
>
> double il = 0.0;
> for (int i=0; i<n; ++i)
> il += in[i] * in[i];
> il = 1.0 / sqrt(il);
> for (int i=0; i<n; ++i)
> out[i] = il * in[i];
Not that it matters, but the above requires C99 (or C++).
Arne
More information about the Python-list
mailing list