[Date Index]
[Thread Index]
[Author Index]
Re: Reposted, Reformatted Re: "mapping" functions over lists, again!
*To*: mathgroup at smc.vnet.net
*Subject*: [mg96364] Re: Reposted, Reformatted Re: "mapping" functions over lists, again!
*From*: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
*Date*: Thu, 12 Feb 2009 06:37:46 -0500 (EST)
*References*: <gmu8ik$geb$1@smc.vnet.net>
Hi Paul,
This can be done pretty simple:
In[78]:= shortTestdata /. {r_Real, c_Complex} -> {r, f[c]}
Out[78]= {{{40., f[28.0678 + 1.20855 I]}, {43.094,
f[28.0572 + 1.29852 I]}, {46.428, f[28.0553 + 1.40023 I]}, {50.019,
f[28.0551 + 1.50876 I]}, {53.888,
f[28.0573 + 1.62452 I]}, {58.057, f[28.0565 + 1.75026 I]}}, {{40.,
f[7.42169 + 0.219859 I]}, {43.094,
f[7.4084 + 0.234353 I]}, {46.428,
f[7.40377 + 0.252135 I]}, {50.019,
f[7.40131 + 0.271599 I]}, {53.888,
f[7.40048 + 0.292062 I]}, {58.057,
f[7.39994 + 0.314501 I]}}, {{40.,
f[1685.53 + 0.0480998 I]}, {43.094,
f[1694.69 - 0.0913363 I]}, {46.428,
f[1698.27 - 0.0273182 I]}, {50.019,
f[1699.76 - 0.0491538 I]}, {53.888,
f[1700.52 - 0.217922 I]}, {58.057,
f[1701.16 - 0.242314 I]}}, {{40.,
f[1808.7 - 0.00628662 I]}, {43.094,
f[1808.52 - 0.114076 I]}, {46.428,
f[1808.53 - 0.0244589 I]}, {50.019,
f[1808.44 - 0.106166 I]}, {53.888,
f[1808.48 - 0.176297 I]}, {58.057, f[1808.63 - 0.289451 I]}}}
BTW, your Map would work if your level specification would be level 2
only, i.e. with the 2 in curly brackets. Without brackets it's all
levels upto and including level 2.
realpart = Map[{First[#], Re[Last[#]]} &, shortTestdata, {2}]
gives
{{{40., 28.0678}, {43.094, 28.0572}, {46.428, 28.0553}, {50.019,
28.0551}, {53.888, 28.0573}, {58.057, 28.0565}}, {{40.,
7.42169}, {43.094, 7.4084}, {46.428, 7.40377}, {50.019,
7.40131}, {53.888, 7.40048}, {58.057, 7.39994}}, {{40.,
1685.53}, {43.094, 1694.69}, {46.428, 1698.27}, {50.019,
1699.76}, {53.888, 1700.52}, {58.057, 1701.16}}, {{40.,
1808.7}, {43.094, 1808.52}, {46.428, 1808.53}, {50.019,
1808.44}, {53.888, 1808.48}, {58.057, 1808.63}}}
Cheers -- Sjoerd
On Feb 11, 12:16 pm, "Paul Ellsmore" <paul.ellsm... at nanion.co.uk>
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
Prev by Date:
**Re: weather blog and ListStreamPlot sampling**
Next by Date:
**Re: Shortest Path Problem**
Previous by thread:
**Re: Reposted, Reformatted Re: "mapping" functions over lists, again!**
Next by thread:
**Re: LabeledListPlot**
| |