Re: For loops with mathematica....

*To*: mathgroup at smc.vnet.net*Subject*: [mg101118] Re: For loops with mathematica....*From*: Bill Rowe <readnews at sbcglobal.net>*Date*: Wed, 24 Jun 2009 06:34:44 -0400 (EDT)

On 6/23/09 at 7:05 AM, jderr at cgr.harvard.edu (Julien) wrote: >I am totally new to mathematica. I want to use a simple loop for but >with many arguments in the loop... the help says to do >Do[ thingstodo, {i,8}] >i will go from 1 to 8 to do thingstodo. >First question: what if I want i to go from 20 then 200 then 300 >then 400, (can we give a sequence as argument?) Usually, there are better methods to get a particular result in Mathematica than using explicit loops. For example consider the following: In[1]:= m = 10^6; In[2]:= Timing[sum = 0; n = 1; Do[sum += n; n++, {m}]; sum] Out[2]= {2.47955,500000500000} In[3]:= Timing[For[n = 1; sum = 0, n <= m, n++, sum += n]; sum] Out[3]= {2.62515,500000500000} In[4]:= Timing[Plus @@ Range[m]] Out[4]= {0.285061,500000500000} In[5]:= Timing[Total@Range[m]] Out[5]= {0.233266,500000500000} In[6]:= Timing[Sum[n, {n, m}]] Out[6]= {0.250216,500000500000} In[7]:= Timing[Sum[n, {n, k}] /. k -> m] Out[7]= {0.098365,500000500000} Each method give exactly the same result but there is a very significant difference in the amount of time required for each to return an answer. Notice, methods using explicit loops are the slowest. A very large number of the functions built into Mathematica have the attribute LIstable. In these cases, a list of values can be given and a list of results will be returned. For example, In[8]:= x = Range[5]; x^2 Out[9]= {1,4,9,16,25} So, it is likely you can do f[Join[Range[20,200],Range[300,400]]] to get f[x] for x running from 20 to 200 and then from 300 to 400. In cases where the function you want does not have the attribute Listable, you can use Map to map the function to each element of a list. The syntax to do this would be Map[f, Join[Range[20,200],Range[300,400]]] or f/@Join[Range[20,200],Range[300,400]] Other methods are also possible. >Second question: I have a lot of line to do in things to do. should >I separate everything with a ; (doesn t seem to work, or use a >procedure or something?) The syntax expr1;expr2 represents a compound expression. When Mathematica encounters this syntax, each of the expri are executed in turn but only the result from the last expression is returned. The syntax expr; is the compound expression expr;Null. Mathematica executes expr but returns nothing since the last expression Null returns nothing. My guess is when you say things didn't seem to work is you used the form expr; and on not seeing a result returned you assumed things didn't work. But to answer your question, use of a semicolon depends on the results you want. There is no general rule that will always do what you want. For questions like this, it is always better to post what you tried, what you got and what you wanted. That will result in an answer more specific to your particular problem

**Re: laptop recommendation to run mathematica fast?**

**Re: Undocumented functions for working with graphics**

**Re: For loops with mathematica....**

**Re: For loops with mathematica....**