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.

Clear[x] ;  demo = x + 1/(x - 1) + Cos[(π (2 + x/3)^(1/2))/3] + Sin[(πf[x + y + 1]^(1/2))/6] ;  posn = Position[demo, _Plus]

{{1, 1}, {3, 1, 3, 1}, {4, 1, 3, 1, 1}, {}}


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).

Part[demo, 1, 1]  Part[demo, 3, 1, 3, 1]  Part[demo, 4, 1, 3, 1, 1]

x - 1

2 + x/3

1 + x + y


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.

Extract[demo, posn]

{x - 1, 2 + x/3, 1 + x + y, 1/(x - 1) + x + Cos[1/3 π (2 + x/3)^(1/2)] + Sin[1/6 π f[1 + x + y]^(1/2)]}


In the next cell only the the whole expression matches the pattern and
Position indicates this by returning {{}}.

Position[h[1, 2, 3], _h]

{{}}


When Position finds no parts that match the pattern an empty list is returned
as in the next cell.

Position[x + y, _h]

{}

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.

Position[demo, _Plus]

{{1, 1}, {3, 1, 3, 1}, {4, 1, 3, 1, 1}, {}}


The next cell uses level specification 4 and we get all sub-expressions at
levels 1 through 4 with the head Plus.

Position[demo, _Plus, 4]

{{1, 1}, {3, 1, 3, 1}}


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

Position[demo, _Plus, {4}]

{{3, 1, 3, 1}}


The next cell uses level specification {0,4} which means to look at levels 0
through 4. Level 0 is the entire (demo) expression.

Position[demo, _Plus, {0, 4}]

{{1, 1}, {3, 1, 3, 1}, {}}


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

Position[demo, _Plus, {0, ∞}]

{{1, 1}, {3, 1, 3, 1}, {4, 1, 3, 1, 1}, {}}

Position[ expr, pattn, levspec, n]


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

data = {1.3, {1.3, 10}, 1.3, 11, {{1.3, 1.3, 12, 13}}, 14, 15, 1.3, 16, 1.3, 17, 18, 19, 1.3} ;    posn = Position[data, _Integer]

{{2, 2}, {4}, {5, 1, 3}, {5, 1, 4}, {6}, {7}, {9}, {11}, {12}, {13}}


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

Extract[data, posn]

{10, 11, 12, 13, 14, 15, 16, 17, 18, 19}

We can give position a positive integer as a  4^th 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.

posn = Position[data, _Integer, {0, ∞}, 4]

{{2, 2}, {4}, {5, 1, 3}, {5, 1, 4}}


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.

posn = Position[data, _Integer, {0, ∞}, 50]

{{2, 2}, {4}, {5, 1, 3}, {5, 1, 4}, {6}, {7}, {9}, {11}, {12}, {13}}

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 {1, 1, 0}, {3, 1, 3, 1, 0} and {4, 1, 3, 1, 1, 0} which are the heads of sub-expressions at positions {1, 1}, {3, 1, 3, 1}, {4, 1, 3, 1, 1}.

Position[demo, Plus]

{{0}, {1, 1, 0}, {3, 1, 3, 1, 0}, {4, 1, 3, 1, 1, 0}}


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.

Position[demo, Plus, Heads->False]

{}

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.

ClearAll[f] ;  Position[2 * f[x] + 3 * f '[x], 1, Heads->True]

{{2, 2, 0, 0, 1}}

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.

Position[2 * f[x] + 3 * f '[x], 1, Heads->False]

{}

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


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page