Re: Re: Can anybody help?
- To: mathgroup at smc.vnet.net
- Subject: [mg63635] Re: [mg63603] Re: Can anybody help?
- From: "David Park" <djmp at earthlink.net>
- Date: Sun, 8 Jan 2006 03:32:58 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
This is difficult for new users because it combines Mathematica's functional programming with shortcut notation. I haven't followed the entire thread so I'll make up an example and see if I can help to clarify the notation. v1 = {d, g, e, b, h, j, i, a, f, c}; v3 = {b, d, e, f, g, i, j}; All the elements of list v3 are in list v1 and we want to know the position of each element of v3 in v1. In other words, we want to perform an operation on each element of v3 that turns it into the corresponding position in v1. For a given element of v3 we want Position[v1, element] You have to know that Mathematica does have a Position command and probably have to look it up in Help to see how it works and what the order of the arguments is. We still don't know how to fill in 'element' since it will be different for each element in v3. We do it by turning our command into a Function. Again, you have to know that there is such a thing as Function in Mathematica and obtain some knowledge of how to use it. We could write our Function, where element is an unspecified argument as... Function[element, Position[v1, element]] Then Function[element, Position[v1, element]][b], for example, is Position[v1, b]; b gets filled in as the argument. Next, we have to map this Function onto all the elements in v3. We can do this, naturally enough, with the command Map, which you also have to know about. Map[Function[element, Position[v1, element]], v3] {{{4}}, {{1}}, {{3}}, {{9}}, {{2}}, {{7}}, {{6}}} Maybe we don't want all the brackets on the positions. They are there because in general there is more than one position and each position may require more than one integer. We could simplify the result by using Flatten that gets rid of all the extra curly brackets. Flatten[Map[Function[element, Position[v1, element]], v3]] {4, 1, 3, 9, 2, 7, 6} However, operations like Map and Function are so useful and so commonly used that Mathematica has shortcuts for them. 'function /@ list' can be used to map a function onto a list. So we could equivalently write (forgetting Flatten for a moment)... Function[element, Position[v1, element]] /@ v3 There is also a shortcut notation for Functions called pure functions. We just write the regular expression but use # for the argument and end the expression with &. So our Function can also be written in the shorter form Position[v1, #]&, and our expression now becomes... Position[v1, #] & /@ v3 Finally, if we want to put Flatten back in we can use the notation for postfix function application, '//'. (Sort of like putting it in as an afterthought.) Position[v1, #] & /@ v3 // Flatten {4, 1, 3, 9, 2, 7, 6} So what is the lesson in all this? One should become familiar with the basic functional commands in Mathematica and their shortcut notations. This takes a little time and practice. But they are so useful and powerful and natural that you will be well rewarded for learning them. Soon you will be manipulating data and expressions like an old pro. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: plizak [mailto:plizak at gmail.com] To: mathgroup at smc.vnet.net It boggles my mind that some people call that code elegant. Short, yes, elegant, no. It is far from intuitive to know what Position[v1, #1]&/@v3 does. Anyone care to enlighten me?