Position

Position is used to determine the where a certain pattern can be found in an

expression. In the next cell Position returns a list of all the position of

all parts of demo that match the pattern (_Plus). The empty list at the end

of the output list indicates that (demo) itself matches the pattern Position

is looking for.

In the next cell we get the subexpression at three of the positions listed in

the previous result. Notice each of the sub-expressions is a sum. The last

element of the list returned by Position is {}, which indicates that demo

itself matches the pattern (_Plus).

In the next cell we pass the list Position returned and get a list of all the

parts of (demo) that match the pattern. However, if you have no other need

for the positions, it would be more direct to get the same result using

Cases[demo,_Plus,{0,∞}]. The results returned by Position can also

be used as arguments of MapAt, and ReplacePart.

In the next cell only the the whole expression matches the pattern and

Position indicates this by returning {{}}.

When Position finds no parts that match the pattern an empty list is returned

as in the next cell.

I don't provide further examples, but any combination of pattern matching constructs can be used with Position. Nuances of pattern matching are discussed in another section.

Position[ expr, pattn, levspec ]

Position will take a level specification and all the variations I discuss under Level Specification are supported. The next cell repeats an earlier example and returns all positions of demo with the head Plus.

The next cell uses level specification 4 and we get all sub-expressions at

levels 1 through 4 with the head Plus.

The next cell uses level specification {4} which means to only look at level

4.

The next cell uses level specification {0,4} which means to look at levels 0

through 4. Level 0 is the entire (demo) expression.

The default level specification for Position is {0,∞} as in the

next cell.

Position[ expr, pattn, levspec, n]

In the next cell we get the position of all integers in (data).

Next we can use Extract to get the integers at the above positions.

We can give position a positive integer as a argument to get only the first (n) positions. In the next cell we get the position of the first 4 integers. Here I am specifying {0,∞} as the level specification.

Next we ask for the position of the first 50 integers. There are only 10

integers to be found, so we are given all 10 positions.

Heads option

In the next cell Position tells us where the head Plus can be found. Plus is at position 0 (the head of demo). The head Plus is also at positions , and which are the heads of sub-expressions at positions , , .

Position has an option Heads with the default setting (Heads→True). If

Position is used with the option setting (Heads→False) it doesn't look

at the heads of an expression or its sub-expressions. Hence when the last

example is repeated with the setting (Heads→False) an empty list is

returned.

In the next cell we get the only position of (2*f[x]+3*f'[x]) where the integer 1 can be found. The only place in this example where the integer 1 can be found is in the order of the derivative.

The next cell repeats the previous example with the setting (Heads→False) and an empty list is returned. An empty list is returned because the pattern matcher would have to look in the head of f'[x] to find the integer 1.

Also read "Further Examples" at the end of the Position documentation in the Help Browser.

Created by Mathematica (May 16, 2004)