[Date Index]
[Thread Index]
[Author Index]
Re: Reposted, Reformatted Re: "mapping" functions over lists, again!
*To*: mathgroup at smc.vnet.net
*Subject*: [mg96389] Re: Reposted, Reformatted Re: "mapping" functions over lists, again!
*From*: Albert Retey <awnl at gmx-topmail.de>
*Date*: Thu, 12 Feb 2009 06:42:21 -0500 (EST)
*References*: <gmu8ik$geb$1@smc.vnet.net>
Hi,
> 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.
it does work better :-)
> 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.
>
> 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]}?
If I understand correctly what you try to achieve I think the easiest
solution will be to use replacement only, it will preserve the structure
and can be set up to handle the pairs as you want. Basically I see two
approaches, which also can be combined. The first one looks simpler, but
only works if the structure of your data doesn't change (the sizes of
the list of list can of course be arbitrary):
Replace[shortTestdata, {r_, c_} :> {f1[r], f2[c]}, {2}]
the trick is to restrict the replacement to the correct level. If you
are working with data where the level at which the pairs appear can be
arbitrary, you can pick the parts to be treated with a more elaborated
rule, like this:
shortTestdata /. {r_ /; Element[r, Reals],c_ /; Element[c, Complexes]}:>
{f1[r], f2[c]}
(/. is short for ReplaceAll). This version should work with arbitrary
structured lists of arbitrary depths. I think using the Element-Function
to decide what kind of number you are looking at is in your case closer
to what you want to achieve than to look at the Head, which has some
implications, as you have learned from other posts.
Finally you could combine the two approaches:
Replace[shortTestdata, {r_ /; Element[r, Reals],c_ /; Element[c,
Complexes]}:> {f1[r], f2[c]}, {2}]
this could make sense if you have a well structured list, but the pairs
could be either of the form {reals,reals}, {real,complex},
{complex,complex} and you want to treat these cases differently.
You should be aware that other approaches might be faster, but these
will for sure be more elaborate and error prone than the code above.
hth,
albert
Prev by Date:
**Re: Shortest Path Problem**
Next by Date:
**Re: Custom label for a slider in Manipulate**
Previous by thread:
**Reposted, Reformatted Re: "mapping" functions over lists, again!**
Next by thread:
**Re: Reposted, Reformatted Re: "mapping" functions over lists, again!**
| |