Re: Cascaded (Multi-range) Iterators?
- To: mathgroup at smc.vnet.net
- Subject: [mg79071] Re: Cascaded (Multi-range) Iterators?
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Tue, 17 Jul 2007 03:30:16 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f7f2lh$o64$1@smc.vnet.net>
AES wrote:
> Seems like a very useful (and feasible?) (and not too difficult)
> extension to the Iterator concept for Tables, etc., would be a cascaded
> or multi-range Iterator, e.g. something like
>
> Table[ f[x], { {x, 0, 1, 0.1}, {x, 2, 10}, {x, 20, 100, 10} } ]
>
> I often have need for a "different step sizes over different ranges"
> capability like this, and have to resort to workarounds to achieve it.
What you are looking for is nothing more than mapping a Table construct
over a list of iterators.
In[1]:= (Table[f[x], #1] & ) /@ {{x, 0, 1, 0.1}, {x, 2, 10}, {x, 20,
100, 10}}
Out[1]= {{f[0.], f[0.1], f[0.2], f[0.3], f[0.4], f[0.5], f[0.6],
f[0.7], f[0.8], f[0.9], f[1.]}, {f[2], f[3], f[4], f[5], f[6], f[7],
f[8], f[9], f[10]}, {f[20], f[30], f[40], f[50], f[60], f[70],
f[80], f[90], f[100]}}
If you want to "automatize" the above construct, you can either add a
definition to the built in function *Table* (note that I have tested the
pattern for lst against your example only).
In[2]:= Unprotect[Table];
Table[f_, lst : {{__} ..}] := (Table[f, #1] & ) /@ lst
Protect[Table];
Table[f[x], {{x, 0, 1, 0.1}, {x, 2, 10}, {x, 20, 100, 10}}]
Out[5]= {{f[0.], f[0.1], f[0.2], f[0.3], f[0.4], f[0.5], f[0.6],
f[0.7], f[0.8], f[0.9], f[1.]}, {f[2], f[3], f[4], f[5], f[6], f[7],
f[8], f[9], f[10]}, {f[20], f[30], f[40], f[50], f[60], f[70],
f[80], f[90], f[100]}}
or define a new function (so you are sure not to mess up Mathematica
built in functions).
In[6]:= myTable[f_, lst_] := (Table[f, #1] & ) /@ lst
myTable[f[x], {{x, 0, 1, 0.1}, {x, 2, 10}, {x, 20, 100, 10}}]
Out[7]= {{f[0.], f[0.1], f[0.2], f[0.3], f[0.4], f[0.5], f[0.6],
f[0.7], f[0.8], f[0.9], f[1.]}, {f[2], f[3], f[4], f[5], f[6], f[7],
f[8], f[9], f[10]}, {f[20], f[30], f[40], f[50], f[60], f[70],
f[80], f[90], f[100]}}
Regards,
Jean-Marc
> [Apologies if my searches in the Help files have missed something that
> already does this.]
>