MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: thanks for the help with list - extended syntax question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40668] Re: thanks for the help with list - extended syntax question
  • From: Bill Rowe <listuser at earthlink.net>
  • Date: Sat, 12 Apr 2003 03:13:31 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 4/11/03 at 2:02 AM, nmoore at physics.umn.edu (Nathan Moore) wrote:

>Why is it in Mathematica that I can't operate on lists as if they are
>long arrays(in the style of c)  It would be orders of magnitude easier
>or me to just type, data = {{x1,y1},{x2,y2}...{xn,yn}} and then find
>the desited sum like, for(i=0,sum=0;i<;i++) { sum +=
>data[[i,1]]*data[[i,2]]; }
>
>It seems idiotic that syntax like this (which most of the scientific
>world would understand) is not allowed! 

That syntax is allowed and you can solve the problems you've posed in this manner. But doing so will cost you significantly in performance

For example;

data = Table[{Random[], Random[], Random[]}, {10000}];

sum1[x_List] :=
  Module[{ans = 0, n = Length[x] + 1},
    For[k = 1, k < n, k++, ans = ans + x[[k, 3]]*x[[k, 1]]];
    Return[ans]]
    
  sum2[x_List] :=
  Tr[Times @@@ x[[All, {1, 3}]]]
  
  Timing[sum1[data]]
{0.26 Second,2517.97}

Timing[sum2[data]]
{0.07 Second,2517.97}

The C like control structures are supported by Mathematica. They just aren't very efficient compared to other methods possilbe


  • Prev by Date: Calculating Easter
  • Next by Date: Re: music with Mathematica
  • Previous by thread: Re: thanks for the help with list - extended syntax question
  • Next by thread: RE: thanks for the help with list - extended syntax question