[Date Index]
[Thread Index]
[Author Index]
Re: local variables - Module, For loop
*To*: mathgroup at smc.vnet.net
*Subject*: [mg113072] Re: local variables - Module, For loop
*From*: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
*Date*: Tue, 12 Oct 2010 04:30:05 -0400 (EDT)
Hi,
usually you want to create/process a list inside a loop and need the return=
ed list of Table.
If you don't, just put a semicolon at the end. But when you need the values=
, then you need to store it explicitely
inside a for-loop. For instance
Block[{i, result == {}},
For[i == 1, i <== 100, i++,
AppendTo[result, {i, PrimeQ[i]}]
];
result
]
to check the first 100 numbers to be prime. Table syntax:
Table[{i, PrimeQ[i]}, {i, 100}]
Check the timing of a For loop with the first 30000 numbers gives
First@AbsoluteTiming@Block[{i, result == {}},
For[i == 1, i <== 30000, i++,
AppendTo[result, {i, PrimeQ[i]}]
];
result
]
22 Seconds here. Similar Table syntax for the first 1000000 (!!) numbers gi=
ves
First@AbsoluteTiming@Table[{i, PrimeQ[i]}, {i, 1000000}]
0.65 Seconds.
Or you want to iterate some calculation:
AbsoluteTiming@Block[{i, c == 0.4 + 0.3 I},
For[i == 0, i < 1000000, i++,
c == c^2 + 0.1 - 0.2 I
];
c
]
gives 3.19 seconds here and the equivalent Nest syntax
AbsoluteTiming@Nest[#^2 + 0.1 - 0.2 I &, 0.4 + 0.3 I, 1000000]
needs 0.13 seconds.. And so on, and so on..
Just try to avoid the usual loops and use the rich set of list operations a=
vailable.
Cheers
Patrick
On Oct 11, 2010, at 1:05 PM, Sebastian Schmitt wrote:
> Hi Patrick!
>
> Thanks for the suggestion. Does this scale to nested or very "long" For
> loops? The Table will return a List in any case:
>
> In[7]:== Table[, {i, 0, 10}]
>
> Out[7]== {Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, \
> Null}
>
> Cheers,
>
> Sebastian
>
> Patrick Scheibe wrote:
>> Hi,
>>
>> then use Table, which is "more natural" and doesn't set the iterations v=
ariable.
>>
>> x == 10;
>> Table[Print[i], {i, 0, x}];
>>
>> Cheers
>> Patrick
>>
>> On Oct 11, 2010, at 11:15 AM, Sebastian Schmitt wrote:
>>
>>> Dear all!
>>>
>>> (I recycle my disclaimer.)
>>>
>>> I'm new to Mathematica with a background mostly in C++. Many times I
>>> have the impression that my style is not natural-Mathematica
>>> (Mathematicaesque so to say).
>>>
>>> If I have a For loop in a function like this:
>>>
>>> In[39]:== f[x_] :== Module[{},
>>> For[i == 0, i !== x, i++,
>>> Print[i]
>>> ]
>>> ]
>>>
>>> In[41]:== f[2]
>>>
>>> During evaluation of In[41]:== 0
>>>
>>> During evaluation of In[41]:== 1
>>>
>>> In[42]:== i
>>>
>>> Out[42]== 2
>>>
>>> I was surprised to find "i" being not local to the For loop. Do I have
>>> to keep track of all my throw-away-variables and put them in the list o=
f
>>> local variables of the Module? I find it pretty tedious. Is there a
>>> better way?
>>>
>>> Thanks in advance,
>>>
>>> Sebastian
>>>
>>
>>
>
Prev by Date:
**Re: C-pointers from Mathematica**
Next by Date:
**Re: Efficient Histogram Algorithm?**
Previous by thread:
**Re: local variables - Module, For loop**
Next by thread:
**Re: local variables - Module, For loop**
| |