Re: Do -like iteration construct with parallel steps?
- To: mathgroup at smc.vnet.net
- Subject: [mg84927] Re: [mg84895] Do -like iteration construct with parallel steps?
- From: Carl Woll <carlw at wolfram.com>
- Date: Sun, 20 Jan 2008 03:32:17 -0500 (EST)
- References: <200801181048.FAA10998@smc.vnet.net>
Louis Theran wrote: >Is there an iteration construct similar to Do, except that it steps >all the variables in parallel (as opposed to the ``nested'' way Do >does it)? I didn't see anything in the standard library, and I >couldn't figure out how to make one myself, so I figured I'd ask if >anybody has written this. (Even a sample implementation of Do as it >is would be helpful.) > >Thanks. > >^L > > > MapThread should do it: If you want a Do/Table like interface to MapThread, then you could try the function ThreadDo, defined below. For example: In[165]:= ThreadDo[x + y^2, {x, 3}, {y, {2, 3, 4}}] Out[165]= {5,11,19} As you can see, support for both the {x,start,end} and {x,list}iterators is provided. Here is the code: ThreadDo::incdim = "Incompatible iterator dimensions"; ThreadDo[function_, iterators : {_Symbol, __} ..] := Module[ { vars = {iterators}[[All, 1]], iteratorlists = ExpandIterator /@ {iterators} }, ( MapThread[Function[Evaluate[vars], function], iteratorlists] ) /; Which[ ! FreeQ[iteratorlists, ExpandIterator], pos = Position[iteratorlists, _ExpandIterator, 1, 1][[1, 1]]; Message[ThreadDo::itform, {iterators}[[pos]], pos + 1]; False, MatrixQ[iteratorlists, True &], True, True, Message[ThreadDo::incdim]; False ] ] ExpandIterator[{x_, i_List}] = i; ExpandIterator[{x_, start__Integer}] := Range[start] /; Length[{start}] < 4 Carl Woll Wolfram Research
- References:
- Do -like iteration construct with parallel steps?
- From: Louis Theran <theran@gmail.com>
- Do -like iteration construct with parallel steps?