Re: Newbie Question
- To: mathgroup at smc.vnet.net
- Subject: [mg32735] Re: [mg32686] Newbie Question
- From: Andrzej Kozlowski <andrzej at platon.c.u-tokyo.ac.jp>
- Date: Thu, 7 Feb 2002 05:11:11 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
I think you will find Mathematica rather slow compared with Fortran for this sort of tasks (unless you learn to use MathLink and program in C). In this sort of problem Mathematica's handling of very large lists may be a handicap, since the functional programming style, which you usually need to avoid copying large lists, is often very memory intensive. Of course if you find a suitable built-in function (possibly by making a clever use of the various available options) that does the job for you you may be home and dry. However, to my surprise, I have found it rather difficult to do in this case. Here is the best I have managed to come up with: diffs[l_List] := With[{u = ListConvolve[{-1, 1}, l], v = Table[1, {Length[l] - 1}], p = Length[l]}, Reverse[Table[ListConvolve[Drop[v, -i], u], {i, 0, p - 2}]]] Here is what it does: lm = {x, y, z, w} In[1]:= diffs[lm] Out[2]= {{x-y,y-z,-w+z},{x-z,-w+y},{-w+x}} Let's try it on a large random list: In[3]:= ls=Table[Random[],{10^4}]; In[4]:= a=diffs[ls];//Timing Out[4]= {162.34 Second,Null} About 2.7 minutes on a 400 mghz computer, which, I assume is not going to impress you. I am not sure I would wish to try this on a list with 10^8 elements. Of course the algorithm used seems terribly wasteful since, given a list {a,b,c,d,e} we are computing differences like a-e as (a-b)+(b-c)+(c-d)+(d-e), so perhaps there is a much faster way which I just can't see at the moment. Andrzej Kozlowski Toyama International University JAPAN http://platon.c.u-tokyo.ac.jp/andrzej/ On Wednesday, February 6, 2002, at 05:41 PM, Brunsman, Kenneth J wrote: > Hello Group, > > I can't believe that I am asking this question, but ... I'm tired of > Fortran > and find the symbolic part of Mathematica extremely useful. > > I have a list with Dimensions = {10,000, 6} and I need to find the > Percent > Difference between items in a column. > > I can parse out the column I need using Take [data [[All, > Column #]]] --- no > big deal. So far, so good. > > Now here's the problem --- How do I get % Differences between any of > 10,000 > items in that list? I need to take % Differences between adjacent > pairs, > i.e. n and n-1, as well as items n and n-m where m can range from 1 to > 10,000. > > Further, I need to make sure that this code runs fast because I'm about > to > run this on a data set of 10^8 data points (financial times series). I > could do this standing on my head in Fortran, but I'm bound and > determined > to learn Mathematica if it takes the rest of my unnatural life. > > This is my first time attempting list processing and all I'm doing is > screwing up royally. > > Help is greatly appreciated. > > Thanks in advance, > Best to all, > > Ken Brunsman > > University of Oklahoma > Michael F. Price College of Business > Division of Management > 307 W. Brooks, Room 205 > Norman, OK 73019-4006 > > 405.325-5689 > > >