Re: Reposted, Reformatted Re: "mapping" functions over lists, again!
- To: mathgroup at smc.vnet.net
- Subject: [mg96343] Re: Reposted, Reformatted Re: "mapping" functions over lists, again!
- From: dh <dh at metrohm.com>
- Date: Thu, 12 Feb 2009 06:33:56 -0500 (EST)
- References: <gmu8ik$geb$1@smc.vnet.net>
Hi Paul, assume you have some function fun of 2 variables (e.g.fun[r_,c]=r+ Im[c] ) then we can map this by: Map[{#[[1]], fun @@ #} &, shortTestdata, {2}] hope this helps, Daniel Paul Ellsmore wrote: > This is a repost of an earlier post, as there were problems with the = > email > formatting for some people. I have composed this one in Notepad, and cut = > and > pasted to Outlook. Hope it works better. > > Thanks to all who have already given me some advice, but I still haven't > quite got it yet! > > My data is in the form: > > shortTestdata = {{{40., 28.06776 + 1.208548*I}, {43.094, 28.05721 + > 1.298519*I}, {46.428, 28.05527 + 1.400228*I}, {50.019, 28.05509 + > 1.508759*I}, > {53.888, 28.05729 + 1.624517*I}, {58.057, 28.05651 + 1.75026*I}}, = > {{40., > 7.42169 + 0.2198589*I}, {43.094, 7.408397 + 0.2343525*I}, > {46.428, 7.403769 + 0.2521353*I}, {50.019, 7.401313 + 0.2715986*I}, > {53.888, 7.400478 + 0.2920617*I}, {58.057, 7.39994 + 0.3145005*I}}, > {{40., 1685.526 + 0.04809976*I}, {43.094, 1694.691 - 0.09133625*I}, > {46.428, 1698.265 - 0.02731824*I}, {50.019, 1699.761 - 0.0491538*I}, > {53.888, 1700.523 - 0.2179222*I}, {58.057, 1701.162 - 0.2423136*I}}, > {{40., 1808.702 - 0.006286621*I}, {43.094, 1808.524 - 0.1140757*I}, > {46.428, 1808.534 - 0.02445889*I}, {50.019, 1808.443 - 0.1061664*I}, > {53.888, 1808.481 - 0.1762974*I}, {58.057, 1808.631 - 0.2894506*I}}} > > This is a list of lists, the lowest level lists containing pairs of = > {real, > complex}. The individual lists are not all the same length, and the = > total > number of lists can vary, and I need to preserve the list structure. > > I want to "map" functions across all the lists, to convert the data = > pairs to > {real, f(real,complex)}. > > One suggestion was to use a Rule in Cases: > > Cases[shortTestdata, {r_Real, c_Complex} :> {r, Re[c]}], but when = > applied to > the data above it Flattens my list structure: > > In: realpart = Cases[shortTestdata, {r_Real, c_Complex} :> {r, Re[c]}, = > 2] > > Out: {{40., 28.06776}, {43.094, 28.05721}, {46.428, 28.05527}, {50.019, > 28.05509}, {53.888, 28.05729}, {58.057, 28.05651}, {40., 7.42169}, = > {43.094, > 7.408397}, > {46.428, 7.403769}, {50.019, 7.401313}, {53.888, 7.400478}, {58.057, > 7.39994}, {40., 1685.526}, {43.094, 1694.691}, {46.428, 1698.265}, = > {50.019, > 1699.761}, > {53.888, 1700.523}, {58.057, 1701.162}, {40., 1808.702}, {43.094, > 1808.524}, {46.428, 1808.534}, {50.019, 1808.443}, {53.888, 1808.481}, > {58.057, 1808.631}} > > I can ressurect the list structure by checking the length of every list = > in > the data, and using these lengths in a Partition statement, but I'd = > rather > not lose the list structure in the first place. Is there a way to do = > that? > > I am sure I could use Map in some way: > > realpart=Map[fxn, shortTestdata,2] > > but I have no real idea how to set up fxn to do what I want. I have = > tried: > In: Map[Cases[#_,{r_Real,c_Complex}->{r,Re[c]}&,shortTestdata,2] but I = > get a > list of empty lists out: > Out:{{},{},{},{}} > > Another suggestion was to use First[#], Last[#], so: > > In: realpart=Map[{First[#],Re[Last[#]]}&,shortTestdata,2] but this = > takes > first and last of the level 2 list: > Out: {{{40., 28.06776}, {58.057, 28.05651}}, {{40., 7.42169}, {58.057, > 7.39994}}, {{40., 1685.526}, {58.057, 1701.162}}, {{40., 1808.702}, = > {58.057, > 1808.631}}} > > If I Map at level 3 I get an error message. Why doesn't this work? = > Surely at > Level 3, each element is a list of length 2, so First would be the real = > and > Last would be the complex? > > My basic problem is that I don't know how to structure a fxn to be = > Mapped > over my lists, so that it applies different transformations to each = > element > of my data pairs. > > So I think the most succinct way of expressing my problem is, what form = > does > fxn take if I want to Map it across my lists of {real,complex} so that = > it > returns {fxn1[real],fxn2[complex]} or even {real,fxn[complex]}? > > Apologies if this is either a trivial question, or a nonsense question. > Ultimately, I think I can make Cases and Partition work for me, but I = > feel > sure there is a more elegant way, if only I understood the Mathematica > syntax better. > > Thanks again, > > Cheers, > > Paul. > > > > Dr. Paul A. Ellsmore > > Nanion Limited > Oxford Centre for Innovation > Mill Street > Oxford > United Kingdom > OX2 0JX > > Tel: +44 (0) 1865 811175 > Fax: +44 (0) 1865 248594 > > >